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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM