ssm框架整合之Spring4+SpringMVC+Mybaties3之配置文件如何配置及内容解释--可直接拷贝使用--不定时更改之2017/4/29

经测试,需注意以下几点:

1,controller的自动扫描不能放在applicationContext.xml中,要放在spring-mvc.xml中。同样是<context:component-scan base-package="com.xxx.controller"></context:component-scan>。(不知道为啥必须这样,若相知,请相教!)

2,所有的classpath必须得小写"p",小写"p",小写"p"。下面代码中全部写成classPath,启动时不报错,但是运行时会报错。

以下配置可直接使用,只需更改包名。

关于内部标签的解释及用法,都以注解形式在代码内部说明。个人原创,转载需注明出处。

1,web.xml。添加jar包后首先需要配置WEB-INF下的web.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 5     id="WebApp_ID" version="3.0">
 6     <display-name>CRM</display-name>
 7     <welcome-file-list>
 8         <welcome-file>login.jsp</welcome-file>
 9     </welcome-file-list>
10 
11     <!-- 以下配置在spring文档中可通过搜索web-app获得(不包含中文编码器) -->
12 
13     <!-- spring的配置文件,默认放在WEB-INF下。这里将其放在src下,故需要如下配置 -->
14     <context-param>
15         <param-name>contextConfigLocation</param-name>
16         <param-value>classPath:appliationContext.xml</param-value>
17     </context-param>
18 
19     <!-- 启动web时加载spring的配置文件 -->
20     <listener>
21         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
22     </listener>
23 
24     <!-- 添加springMVC支持,单独springMVC时也需要在web.xml文件中配置 -->
25     <servlet>
26         <servlet-name>dispatcher</servlet-name>
27         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
28         <init-param>
29             <param-name>contextConfigLocation</param-name>
30             <param-value>classPath:spring-mvc.xml</param-value>
31         </init-param>
32         <!-- 启动web时就加载springmvc的servlet,即启动时就加载springmvc的配置文件 -->
33         <load-on-startup>1</load-on-startup>
34         <!-- 可异步执行,最好都配上,项目会比较流畅 -->
35         <!-- 配置异步时若报错,因为是3.0新特征,可将2.5改为3.0即可 -->
36         <async-supported>true</async-supported>
37     </servlet>
38     <servlet-mapping>
39         <servlet-name>dispatcher</servlet-name>
40         <!-- 拦截所有以.do结尾的请求,后续的业务代码请求后缀都将以.do结尾(个人习惯) -->
41         <url-pattern>*.do</url-pattern>
42     </servlet-mapping>
43 
44     <!-- 中文编码器,防止出现中文乱码 -->
45     <filter>
46         <filter-name>characterEncodingFilter</filter-name>
47         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
48         <init-param>
49             <param-name>encoding</param-name>
50             <param-value>UTF-8</param-value>
51         </init-param>
52     </filter>
53     <filter-mapping>
54         <filter-name>characterEncodingFilter</filter-name>
55         <!-- 所有请求都必须通过该校验 -->
56         <url-pattern>/*</url-pattern>
57     </filter-mapping>
58 
59 </web-app>
web.xml

2,applicationContext.xml。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xmlns:jee="http://www.springframework.org/schema/jee"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans
 7         http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context
 9         http://www.springframework.org/schema/context/spring-context.xsd
10         http://www.springframework.org/schema/aop
11         http://www.springframework.org/schema/aop/spring-aop.xsd
12         http://www.springframework.org/schema/tx
13         http://www.springframework.org/schema/tx/spring-tx.xsd
14         http://www.springframework.org/schema/jee
15         http://www.springframework.org/schema/jee/spring-jee.xsd">
16 
17     <!-- spring自动扫描base-package下及其子包下的所有Java文件,当扫描到含有@service或@controller注解时,自动将该类注册成bean -->
18     <!-- 不用再配置<context:annotation-config/>,因为已包含 -->
19     <context:component-scan base-package="com.xxx.service"></context:component-scan>
20     <context:component-scan base-package="com.xxx.controller"></context:component-scan>
21 
22     <!-- 配置数据源 -->
23     <bean id="dataSource"
24         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
25         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
26         <property name="url" value="jdbc:mysql://localhost:3306/db_Name"></property>
27         <property name="userName" value="root"></property>
28         <property name="password" value="123456"></property>
29     </bean>
30 
31     <!-- 配置mybatis的sqlSessionFactory -->
32     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
33         <property name="dataSource" ref="dataSource"></property>
34         <property name="configLocation" value="classPath:mybatis-config.xml"></property>
35         <property name="mapperLocations" value="classPath:com/xxx/mappers/*.xml"></property>
36         <property name="typeAliasesPackage" value="classPath:com.xxx.entity"></property>
37     </bean>
38     <!-- sqlSessionFactory的属性 -->
39     <!-- 1,dataSource:必须属性 -->
40     <!-- 2,configLocation:当mybatis-config.xml放在src下(个人习惯),配置该属性。 目前别名已经配置在SqlSessionFactoryBean里,可否省略mybatis-config.xml文件我也不清楚。若相知,请相告!谢谢! -->
41     <!-- 3,mapperLocations:自动扫描mapper.xml文件。 -->
42     <!-- 4,typeAliasesPackage:自动配置别名 -->
43 
44     <!-- 转换器MapperScannerConfig它可以将接口转换为Spring容器中的Bean,不需要注解(@service) -->
45     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
46         <property name="basePackage" value="com.xxx.dao"></property>
47         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
48     </bean>
49 
50     <!-- 配置事务 -->
51     <bean id="transactionManager"
52         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
53         <property name="dataSource" ref="dataSource" />
54     </bean>
55 
56     <!-- 配置事务通知 -->
57     <!-- 没有采用<tx:annotation-driven/>,即注解事务管理。是因为注解事务需要在很多public方法前加上@Transactional,很麻烦,用以下方法可以一劳永逸 -->
58     <tx:advice id="txAdvice" transaction-manager="transactionManager">
59         <tx:attributes>
60             <tx:method name="insert*" propagation="REQUIRED" />
61             <tx:method name="add*" propagation="REQUIRED" />
62             <tx:method name="save*" propagation="REQUIRED" />
63             <tx:method name="delete*" propagation="REQUIRED" />
64             <tx:method name="remove*" propagation="REQUIRED" />
65             <tx:method name="update*" propagation="REQUIRED" />
66             <tx:method name="edit*" propagation="REQUIRED" />
67             <tx:method name="get*" propagation="REQUIRED" read-only="true" />
68             <tx:method name="find*" propagation="REQUIRED" read-only="true" />
69             <tx:method name="*" propagation="REQUIRED" read-only="true" />
70         </tx:attributes>
71     </tx:advice>
72     <!-- 拦截以'insert','add'...'find'等开头的方法名方法。最后一句代表拦截所有方法 -->
73 
74     <!-- 配置事务切面,将事务通过切面的方式嵌套入代码逻辑,从而不侵入代码业务 -->
75     <!-- 若需单独用切面,在定义<aop:config>时需要加入切面类,即<aop:aspect> -->
76     <aop:config>
77         <aop:pointcut id="txPointCut" expression="execution(* com.xxx.service.*.*(..))" />
78         <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" />
79     </aop:config>
80     <!-- 切点方法表达式格式 -->
81     <!-- execution(<scope> <return-type> <fully-qualified-class-name>.<method-name>.(parameters)) 
82         例: execution(* com.spring.service.*.*(..)):匹配service包及其子包(service.impl)所有类的所有方法; 
83         execution(public List com.spring.service.impl.UserServiceImpl.getUserList(int,String)): 
84         匹配UserServiceImpl类中返回List且方法名为getUserList且参数为int和String类型的所有公共方法 -->
85 
86 </beans>
applicationContext.xml

3,spring-mvc.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="
 5         http://www.springframework.org/schema/beans
 6         http://www.springframework.org/schema/beans/spring-beans.xsd">
 7 
 8     <!-- 视图解析器 -->
 9     <bean id="viewResolver"
10         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
11         <!-- 前缀,WEB-INF下的jsp文件夹 -->
12         <property name="prefix" value="/WEB-INF/jsp/" />
13         <!-- 后缀,以.jsp结尾 -->
14         <property name="suffix" value=".jsp" />
15     </bean>
16 
17 </beans>
spring-mvc.xml

4,mybatis-config.xml

1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE configuration
3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
5 <!-- 只需要一个空的<configuration>(必须,不写会报错),数据源、别名、mapper的扫描都已经在applicationContext.xml中定义 -->
6 <configuration></configuration>
mybatis-config.xml

 

posted @ 2017-04-27 15:30  一个笨蛋杰  阅读(2129)  评论(0编辑  收藏  举报