先放目录结构,便于清晰了解整个项目




由简单的到复杂的进行排列:

SqlMapConfig.xml:


<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 配置别名 --> <
typeAliases> <!-- 批量扫描 别名--> <package name="com.lnp.model"/> </typeAliases> </
configuration> jdbc.properties:jdbc.driver=com.mysql.jdbc.Driver jdbc.url=
jdbc:mysql://localhost:3306/data?characterEncoding=utf-8jdbc.username=root
jdbc.password=123log4j.properties:# Global logging configuration
log4j.rootLogger=DEBUG, stdout # MyBatis logging configuration...
log4j.logger.com.how2java=TRACE # Console output... log4j.appender.stdout=
org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=
org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=
%5p [%t] - %m%n
springMvc.xml:


<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" xmlns:context
="http://www.springframework.org/schema/context" xmlns:mvc
="http://www.springframework.org/schema/mvc" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!--这里让扫描
controller,指定controller的包--> <context:component-scan base-package
="com.lnp.controller"/> <!-- 注解的映射器 --> <!-- <bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
--> <!-- 注解的适配器 --> <!-- <bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
--> <!-- 使用 mvc:annotation-driven可以代替上面注解映射器和注解适配器配置 mvc:annotation-driven
默认加载了很多的参数绑定方法,比如json转换解析器就默认加载了 实际开发中使用mvc:annotation-driven --> <!--
注解驱动:配置处理器映射器和适配器--> <mvc:annotation-driven/> <!-- 视图解析器 解析jsp解析,默认使用jstl标签,
classpath下得有jstl的包 --> <bean class
="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置jsp
路径的前缀--> <property name="prefix" value="/WEB-INF/jsp"/> <!-- 配置jsp路径的后缀 --> <
propertyname="suffix" value=".jsp"/> </bean> <!--静态资源映射--> <mvc:resources
mapping="/css/**" location="/WEB-INF/css/"/> <mvc:resources mapping="/js/**"
location="/WEB-INF/js"/> <!--<!– 转换器 –>--> <!--<property
name="converters">--> <!--<list>--> <!--<!– 日期类型转换 –>-->
<!--<bean class="com.lnp.controller.converter.CustomDateConverter"/>-->
<!--</list>--> <!--</property>--> <!--</bean>--> <!--<!– 全局拦截器
–>--> <!--<mvc:interceptors>--> <!--<!– 顺序执行 –>-->
<!--<mvc:interceptor>--> <!--<mvc:mapping path="/**"/>--> <!--<bean
class="com.lnp.interceptor.HandlerInterceptor1"></bean>-->
<!--</mvc:interceptor>--> <!--<mvc:interceptor>--> <!--<mvc:mapping
path="/**"/>--> <!--<bean
class="com.lnp.interceptor.HandlerInterceptor2"></bean>-->
<!--</mvc:interceptor>--> <!--<mvc:interceptor>--> <!--<mvc:mapping
path="/**"/>--> <!--<bean
class="com.lnp.interceptor.LoginInterceptor"></bean>-->
<!--</mvc:interceptor>--> <!--</mvc:interceptors>--> </beans>



applicationContext.xml:


<?xml version="1.0" encoding="UTF-8"?> <beans xmlns
="http://www.springframework.org/schema/beans" xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop
="http://www.springframework.org/schema/aop" xmlns:tx
="http://www.springframework.org/schema/tx"xmlns:jdbc
="http://www.springframework.org/schema/jdbc" xmlns:context
="http://www.springframework.org/schema/context" xmlns:mvc
="http://www.springframework.org/schema/mvc" xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--开启注解模式--> <
context:annotation-config /> <!-- 数据库连接池 --> <!-- 加载配置文件 --> <context
:property-placeholderlocation="classpath:properties/jdbc.properties"/> <!--
数据库连接池--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close"> <property name="url" value="${jdbc.url}"/> <property
name="username" value="${jdbc.username}"/> <property name="password" value
="${jdbc.password}"/> <property name="driverClassName" value="${jdbc.driver}"/>
<property name="maxActive" value="10"/> <property name="minIdle" value="5"/> </
bean> <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --> <bean id
="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"> <!--
数据库连接池--> <property name="dataSource" ref="dataSource"/> <!-- 加载mybatis的全局配置文件
--> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
</bean> <!--配置mapper扫描包--> <bean class
="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name
="basePackage"value="com.lnp.mapper"/> </bean> <!--==========================
以上内容为applicationContext-dao配置===================================--> <!--自动扫描
service--> <context:component-scan base-package="com.lnp.service" /> <!-- 事务管理器
--> <bean id="transactionManager" class
="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 数据源
--> <property name="dataSource" ref="dataSource"/> </bean> <!-- 通知 --> <tx
:adviceid="txAdvice" transaction-manager="transactionManager"> <tx:attributes>
<!--传播行为 --> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name
="insert*"propagation="REQUIRED" /> <tx:method name="add*" propagation
="REQUIRED"/> <tx:method name="create*" propagation="REQUIRED" /> <tx:method
name="delete*" propagation="REQUIRED" /> <tx:method name="update*" propagation
="REQUIRED"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" /> <tx:method
name="get*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx
:advice> <!-- 切面 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut
="execution(* com.lnp.service.*.*(..))"/> </aop:config>
<!--==========================以上内容为applicationContext-service配置
============================--> </beans>



web.xml:


<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application
2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>Archetype
Created Web Application</display-name> <welcome-file-list> <welcome-file>
index.jsp</welcome-file> </welcome-file-list> <!-- log4j配置文件地址 --> <
context-param> <param-name>log4jConfigLocation</param-name> <param-value>
classpath:properties/log4j.properties</param-value> </context-param> <!-- Log4j
的监听器要放在spring监听器前面 --> <listener> <listener-class>
org.springframework.web.util.Log4jConfigListener</listener-class> </listener>
<!--加载spring容器 --> <context-param> <param-name>contextConfigLocation</param-name
> <param-value>classpath:spring/applicationContext.xml</param-value> </
context-param> <listener> <listener-class>
org.springframework.web.context.ContextLoaderListener</listener-class> </
listener> <!--前端控制器--> <servlet> <servlet-name>taotao-manager</servlet-name> <
servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation配置springmvc记载的配置文件(配置处理器映射器、适配器等等) 如果不配置
contextConfigLocation,默认加载的是/WEB-INF/servlet名称-servlet.xml --> <init-param> <
param-name>contextConfigLocation</param-name> <param-value>
classpath:spring/springMvc.xml</param-value> </init-param> <!-- 配置服务器启动后立即加载
Spring MVC配置文件 --> <load-on-startup>1</load-on-startup> </servlet> <
servlet-mapping> <servlet-name>taotao-manager</servlet-name> <!-- 第一种:*.action
,访问以action结尾由DispatcherServlet进行解析 第二种:/,所有的访问的地址都由DispatcherServlet
进行解析,对于静态的文件我们希望不用DispatcherServlet解析, 需要在springMvc.xml中添加静态资源映射!!。 使用此种方法可以实现
RESTful风格的url 第三种:/*,这种配置不对,使用这种配置,最终要转发到一个jsp页面时,仍然会由DispatcherServlet解析jsp
,不能根据jsp页面找到handler,会报错 --> <url-pattern>/</url-pattern> </servlet-mapping>
<!-- post乱码过滤器 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <
filter-class>org.springframework.web.filter.CharacterEncodingFilter</
filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8
</param-value> </init-param> </filter> <filter-mapping> <filter-name>
CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </
filter-mapping> </web-app>

至此配置完成,可以开心的写业务逻辑了。

对了,建项目的时候,pom依赖根据自己的需要进行引入。

我的pom依赖如下:


<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</
maven.compiler.target> <java.version>1.8</java.version> <
project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <
spring.version>4.1.4.RELEASE</spring.version> <jackson.version>2.5.0</
jackson.version> <jstl.version>1.2</jstl.version> <servlet-api.version>2.5</
servlet-api.version> <jsp-api.version>2.0</jsp-api.version> </properties> <
dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</
artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--
spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>
spring-core</artifactId> <version>${spring.version}</version> </dependency> <
dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</
artifactId> <version>${spring.version}</version> </dependency> <dependency> <
groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <
version>${spring.version}</version> </dependency> <dependency> <groupId>
org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>
${spring.version}</version> </dependency> <dependency> <groupId>
org.springframework</groupId> <artifactId>spring-web</artifactId> <version>
${spring.version}</version> </dependency> <dependency> <groupId>
org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>
${spring.version}</version> </dependency> <dependency> <groupId>
org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>
${spring.version}</version> </dependency> <dependency> <groupId>
org.springframework</groupId> <artifactId>spring-test</artifactId> <version>
${spring.version}</version> <scope>test</scope> </dependency> <!-- mybatis 包 -->
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <
version>3.2.8</version> </dependency> <!--mybatis spring 插件 --> <dependency> <
groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>
1.2.2</version> </dependency> <!-- mysql连接 --> <dependency> <groupId>mysql</
groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version>
</dependency> <!-- 数据源 --> <dependency> <groupId>com.alibaba</groupId> <
artifactId>druid</artifactId> <version>1.0.12</version> </dependency> <
dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId
> <version>1.8.4</version> </dependency> <!-- log4j --> <dependency> <groupId>
log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </
dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12
</artifactId> <version>1.6.4</version> </dependency> <!-- servlet --> <
dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId
> <version>${servlet-api.version}</version> <scope>provided</scope> </dependency
> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</
artifactId> <version>2.3.23</version> </dependency> <!-- JSP相关 --> <dependency>
<groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</
version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId
>jsp-api</artifactId> <version>${jsp-api.version}</version> <scope>provided</
scope> </dependency> <!-- json --> <dependency> <groupId>org.codehaus.jackson</
groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version>
</dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</
artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>
com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId
> <version>${jackson.version}</version> </dependency> <dependency> <groupId>
com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <
version>${jackson.version}</version> </dependency> <dependency> <groupId>
com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <
version>${jackson.version}</version> </dependency> <!-- 文件上传 --> <dependency> <
groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</
version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <
artifactId>commons-fileupload</artifactId> <version>1.2.2</version> </dependency
> </dependencies> <build>。。。。。 <plugins> <!-- 配置Tomcat插件 --> <plugin> <groupId>
org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version> <configuration> <url>http://localhost:8080/manager/text</
url> <server>admin</server> </configuration> </plugin> </plugins> </build>

使用的是maven-tomcat插件进行的部署运行。








友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信