SSM(Spring + Spring MVC + MyBatis)整合

項目使用Maven架構,項目目錄結構以下:html

 

一、pom.xml :java

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 3   <modelVersion>4.0.0</modelVersion>
 4   <groupId>com.xxw</groupId>
 5   <artifactId>maven_ssm_redis</artifactId>
 6   <packaging>war</packaging>
 7   <version>2.0.1</version>  
 8   <!-- <name>maven_ssm_redis Maven Webapp</name>  9  <url>http://maven.apache.org</url> -->
 10   
 11   <!-- 聲明版本號 -->
 12   <properties>
 13       <!-- spring版本號 -->
 14       <spring.version>4.0.2.RELEASE</spring.version>
 15       <!-- mybatis版本號 -->
 16       <mybatis.version>3.2.6</mybatis.version>
 17       <!-- log4j日誌文件管理包版本 -->
 18       <slf4j.version>1.7.7</slf4j.version>
 19       <log4j.version>1.2.17</log4j.version>
 20   </properties>
 21   
 22   <!-- 依賴jar包 -->
 23   <dependencies>
 24       
 25       <dependency>
 26           <groupId>junit</groupId>
 27           <artifactId>junit</artifactId>
 28           <version>4.11</version>
 29           <!-- 表示開發時引入,發佈時不會加載此包 -->
 30           <scope>test</scope>
 31       </dependency>
 32       
 33       <dependency>
 34         <groupId>org.aspectj</groupId>
 35         <artifactId>aspectjweaver</artifactId>
 36         <version>1.7.4</version>
 37     </dependency>
 38       
 39       <!-- spring核心包 -->
 40       <dependency>  
 41          <groupId>org.springframework</groupId>  
 42          <artifactId>spring-core</artifactId>  
 43          <version>${spring.version}</version>  
 44      </dependency>  
 45 
 46      <dependency>  
 47          <groupId>org.springframework</groupId>  
 48          <artifactId>spring-web</artifactId>  
 49          <version>${spring.version}</version>  
 50      </dependency>  
 51      <dependency>  
 52          <groupId>org.springframework</groupId>  
 53          <artifactId>spring-oxm</artifactId>  
 54          <version>${spring.version}</version>  
 55      </dependency>  
 56      <dependency>  
 57          <groupId>org.springframework</groupId>  
 58          <artifactId>spring-tx</artifactId>  
 59          <version>${spring.version}</version>  
 60      </dependency>  
 61 
 62      <dependency>  
 63          <groupId>org.springframework</groupId>  
 64          <artifactId>spring-jdbc</artifactId>  
 65          <version>${spring.version}</version>  
 66      </dependency>  
 67 
 68      <dependency>  
 69          <groupId>org.springframework</groupId>  
 70          <artifactId>spring-webmvc</artifactId>  
 71          <version>${spring.version}</version>  
 72      </dependency>  
 73      <dependency>  
 74          <groupId>org.springframework</groupId>  
 75          <artifactId>spring-aop</artifactId>  
 76          <version>${spring.version}</version>  
 77      </dependency>  
 78 
 79      <dependency>  
 80          <groupId>org.springframework</groupId>  
 81          <artifactId>spring-context-support</artifactId>  
 82          <version>${spring.version}</version>  
 83      </dependency>  
 84 
 85      <dependency>  
 86          <groupId>org.springframework</groupId>  
 87          <artifactId>spring-test</artifactId>  
 88          <version>${spring.version}</version>  
 89      </dependency> 
 90      
 91      <!-- mybatis核心包 -->
 92      <dependency>  
 93          <groupId>org.mybatis</groupId>  
 94          <artifactId>mybatis</artifactId>  
 95          <version>${mybatis.version}</version>  
 96      </dependency>
 97      <!-- mybatis/spring包 -->
 98      <dependency>  
 99          <groupId>org.mybatis</groupId>  
100          <artifactId>mybatis-spring</artifactId>  
101          <version>1.2.2</version>  
102      </dependency>  
103      
104      <!-- 導入java ee jar包 -->
105      <dependency>  
106          <groupId>javax</groupId>  
107          <artifactId>javaee-api</artifactId>  
108          <version>7.0</version>  
109          <!-- 代表該包只在編譯和測試的時候使用 -->
110          <scope>provided</scope>
111      </dependency>  
112      
113      <!-- 導入MySQL數據庫鏈接jar包 -->
114      <dependency>  
115          <groupId>mysql</groupId>  
116          <artifactId>mysql-connector-java</artifactId>  
117          <version>5.1.30</version>  
118      </dependency>  
119      
120      <!-- 導入dbcp的jar包,用來在applicationContex.xml中配置數據庫 --> 
121      <dependency>  
122          <groupId>commons-dbcp</groupId>  
123          <artifactId>commons-dbcp</artifactId>  
124          <version>1.2.2</version>  
125      </dependency> 
126      
127      <!-- JSTL標籤類 -->
128      <dependency>  
129          <groupId>jstl</groupId>  
130          <artifactId>jstl</artifactId>  
131          <version>1.2</version>  
132      </dependency>
133      
134      <!-- 日誌文件管理包 start -->
135      <dependency>  
136           <groupId>log4j</groupId>  
137           <artifactId>log4j</artifactId>  
138           <version>${log4j.version}</version>  
139       </dependency>
140       <!-- 格式化對象,方便輸出日誌 -->
141       <dependency>  
142           <groupId>com.alibaba</groupId>  
143           <artifactId>fastjson</artifactId>  
144           <version>1.1.41</version>  
145       </dependency> 
146       
147       <dependency>  
148           <groupId>org.slf4j</groupId>  
149           <artifactId>slf4j-api</artifactId>  
150           <version>${slf4j.version}</version>  
151       </dependency> 
152       <dependency>  
153           <groupId>org.slf4j</groupId>  
154           <artifactId>slf4j-log4j12</artifactId>  
155           <version>${slf4j.version}</version>  
156       </dependency>
157       
158       <!-- 映入JSON -->
159       <dependency>  
160           <groupId>org.codehaus.jackson</groupId>  
161           <artifactId>jackson-mapper-asl</artifactId>  
162           <version>1.9.13</version>  
163       </dependency>
164       
165       <!-- 上傳組件包 -->
166       <dependency>  
167           <groupId>commons-fileupload</groupId>  
168           <artifactId>commons-fileupload</artifactId>  
169           <version>1.3.1</version>  
170       </dependency>  
171       <dependency>  
172           <groupId>commons-io</groupId>  
173           <artifactId>commons-io</artifactId>  
174           <version>2.4</version>  
175       </dependency>  
176       <dependency>  
177           <groupId>commons-codec</groupId>  
178           <artifactId>commons-codec</artifactId>  
179           <version>1.9</version>  
180       </dependency>  
181      
182          
183   </dependencies>
184   
185   <build>  
186         <plugins>  
187             <plugin>  
188                 <artifactId>maven-compiler-plugin</artifactId>  
189                 <version>2.3.2</version>  
190                 <configuration>  
191                     <source>1.7</source>  
192                     <target>1.7</target>  
193                 </configuration>  
194             </plugin>  
195             <plugin>  
196                 <artifactId>maven-war-plugin</artifactId>  
197                 <version>2.2</version>  
198                 <configuration>  
199                     <version>3.0</version>  
200                     <failOnMissingWebXml>false</failOnMissingWebXml>  
201                 </configuration>  
202             </plugin> 
203             <plugin>
204                 <groupId>org.codehaus.mojo</groupId>
205                 <artifactId>tomcat-maven-plugin</artifactId>
206                 <version>1.1</version>
207                 <configuration>
208                     <path>/wp</path>
209                     <port>8080</port>
210                     <uriEncoding>UTF-8</uriEncoding>
211                     <url>http://localhost:8080/manager/html</url>
212                     <server>tomcat6</server>
213                 </configuration>
214             </plugin> 
215         </plugins>  
216         <finalName>${project.artifactId}_${project.version}_${maven.build.timestamp}</finalName>  
217     </build>
218   
219 </project>

二、jdbc.propertiesmysql

 1 driver=com.mysql.jdbc.Driver  2 url=jdbc:mysql://127.0.0.1:3306/company  3 username=root  4 password=root  5 #定義初始鏈接數
 6 initialSize=0
 7 #定義最大鏈接數
 8 maxActive=20
 9 #定義最大空閒
10 maxIdle=20
11 #定義最小空閒
12 minIdle=1
13 #定義最長等待時間
14 maxWait=60000

三、log4j.propertiesweb

 

 1 #定義LOG輸出級別 
 2 log4j.rootLogger=INFO,Console,File  3 #定義日誌輸出目的地爲控制檯 
 4 log4j.appender.Console=org.apache.log4j.ConsoleAppender  5 log4j.appender.Console.Target=System.out  6 #能夠靈活地指定日誌輸出格式,下面一行是指定具體的格式 
 7 log4j.appender.Console.layout = org.apache.log4j.PatternLayout  8 log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n  9   
10 #文件大小到達指定尺寸的時候產生一個新的文件 
11 log4j.appender.File = org.apache.log4j.RollingFileAppender 12 #指定輸出目錄 
13 log4j.appender.File.File = logs/ssm.log 14 #定義文件最大大小 
15 log4j.appender.File.MaxFileSize = 10MB 16 # 輸出因此日誌,若是換成DEBUG表示輸出DEBUG以上級別日誌 
17 log4j.appender.File.Threshold = ALL 18 log4j.appender.File.layout = org.apache.log4j.PatternLayout 19 log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

 

四、mybatis_config.xmlredis

1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
3 <configuration>
4     <typeAliases>
5         <typeAlias type="com.xxw.pojo.User" alias="User"/>
6     </typeAliases>
7 </configuration>

五、ApplicationContext_DataSource.xmlspring

 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:p="http://www.springframework.org/schema/p"  
 4  xmlns:context="http://www.springframework.org/schema/context"  
 5  xmlns:aop="http://www.springframework.org/schema/aop" 
 6  xmlns:mvc="http://www.springframework.org/schema/mvc"  
 7  xmlns:tx="http://www.springframework.org/schema/tx"
 8  xsi:schemaLocation="http://www.springframework.org/schema/beans  9  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 10  http://www.springframework.org/schema/context 11  http://www.springframework.org/schema/context/spring-context-3.1.xsd 12  http://www.springframework.org/schema/aop 13  http://www.springframework.org/schema/aop/spring-aop.xsd 14  http://www.springframework.org/schema/mvc 15  http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 16  http://www.springframework.org/schema/tx 17  http://www.springframework.org/schema/tx/spring-tx.xsd">
18 
19     <!-- 引入JDBC配置文件 -->
20     <bean id="propertyConfigurer"
21  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
22         <property name="location" value="classpath:jdbc.properties" />
23     </bean>
24 
25     <!-- 配置數據源 -->
26     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
27         <property name="driverClassName" value="${driver}" />
28         <property name="url" value="${url}" />
29         <property name="username" value="${username}" />
30         <property name="password" value="${password}" />
31         <!-- 初始化鏈接大小 -->
32         <property name="initialSize" value="${initialSize}" />
33         <!-- 鏈接池最大數量 -->
34         <property name="maxActive" value="${maxActive}" />
35         <!-- 鏈接池最大空閒 -->
36         <property name="maxIdle" value="${maxIdle}" />
37         <!-- 鏈接池最小空閒 -->
38         <property name="minIdle" value="${minIdle}" />
39         <!-- 獲取鏈接最大等待時間 -->
40         <property name="maxWait" value="${maxWait}" />
41     </bean>
42     
43     <!-- 配置MyBatis -->
44     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
45         <property name="dataSource" ref="dataSource" />
46         <property name="configLocation" value="classpath:mybatis_config.xml"></property>
47         <!-- 自動掃描mapping.xml文件 -->
48         <property name="mapperLocations" value="classpath:com/xxw/mapper/*.xml" />
49     </bean>
50     
51     <!-- sql會話模板 -->
52     <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
53         <constructor-arg ref="sqlSessionFactory" />
54     </bean>
55     
56     <!-- 事物處理 -->
57     <aop:config>
58         <aop:pointcut id="pc" expression="execution(* com.xxw.service..*(..))" />
59         <aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
60     </aop:config>
61     <tx:advice id="txAdvice" transaction-manager="transactionManager">
62         <tx:attributes>
63             <tx:method name="delete*" propagation="REQUIRED" read-only="false" 
64  rollback-for="java.lang.Exception"/>
65             <tx:method name="insert*" propagation="REQUIRED" read-only="false" 
66  rollback-for="java.lang.Exception" />
67             <tx:method name="update*" propagation="REQUIRED" read-only="false" 
68  rollback-for="java.lang.Exception" />
69             <tx:method name="save*" propagation="REQUIRED" read-only="false" 
70  rollback-for="java.lang.Exception" />
71         </tx:attributes>
72     </tx:advice>
73     <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
74         <property name="dataSource" ref="dataSource"></property>
75      </bean>
76      
77 </beans>

六、ApplicationContext_mvc.xmlsql

 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:p="http://www.springframework.org/schema/p"  
 4  xmlns:context="http://www.springframework.org/schema/context"  
 5  xmlns:aop="http://www.springframework.org/schema/aop" 
 6  xmlns:mvc="http://www.springframework.org/schema/mvc"  
 7  xmlns:tx="http://www.springframework.org/schema/tx"
 8  xsi:schemaLocation="http://www.springframework.org/schema/beans  9  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 10  http://www.springframework.org/schema/context 11  http://www.springframework.org/schema/context/spring-context-3.1.xsd 12  http://www.springframework.org/schema/aop 13  http://www.springframework.org/schema/aop/spring-aop.xsd 14  http://www.springframework.org/schema/mvc 15  http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 16  http://www.springframework.org/schema/tx 17  http://www.springframework.org/schema/tx/spring-tx.xsd">
18 
19     <!-- 使用註解開發,自動掃描包 -->
20     <context:component-scan base-package="com.xxw.controller" />
21     <!-- Spring MVC爲@Controller分發請求所必須的 -->
22     <mvc:annotation-driven />
23     <!-- 靜態資源訪問 (交由DispatcherServlet來處理靜態資源訪問) -->
24     <mvc:default-servlet-handler />
25     <!-- 更智能的靜態資源訪問(js/image)的訪問(有SpringMVC本身來處理靜態資源訪問) -->
26     <mvc:resources location="/static/js/" mapping="/static/js/**"/>
27     <mvc:resources location="/static/image/" mapping="/static/image/**"/>
28     
29     <!-- 上傳攔截,配置文件上傳 -->  
30     <bean id="multipartResolver"    
31  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    
32         <!-- 默認編碼 -->  
33         <property name="defaultEncoding" value="utf-8" />    
34         <!-- 文件大小最大值 -->  
35         <property name="maxUploadSize" value="10485760000" />    
36         <!-- 內存中的最大值 -->  
37         <property name="maxInMemorySize" value="40960" />    
38         <!-- 啓用是爲了推遲文件解析,以便捕獲文件大小異常 -->
39         <property name="resolveLazily" value="true"/>
40     </bean>   
41     
42     <!-- 視圖解析器-->  
43     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
44         <!-- 跳轉的文件的前綴 -->  
45         <property name="prefix" value="/WEB-INF/jsp/" />  
46         <!-- 跳轉的文件的後綴 -->
47         <property name="suffix" value=".jsp" />  
48     </bean> 
49     
50 </beans>

七、ApplicationContext_main.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:p="http://www.springframework.org/schema/p"  
 4  xmlns:context="http://www.springframework.org/schema/context"  
 5  xmlns:aop="http://www.springframework.org/schema/aop" 
 6  xmlns:mvc="http://www.springframework.org/schema/mvc"  
 7  xmlns:tx="http://www.springframework.org/schema/tx"
 8  xsi:schemaLocation="http://www.springframework.org/schema/beans  9  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 10  http://www.springframework.org/schema/context 11  http://www.springframework.org/schema/context/spring-context-3.1.xsd 12  http://www.springframework.org/schema/aop 13  http://www.springframework.org/schema/aop/spring-aop.xsd 14  http://www.springframework.org/schema/mvc 15  http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 16  http://www.springframework.org/schema/tx 17  http://www.springframework.org/schema/tx/spring-tx.xsd">
18 
19     <!-- 啓用註解 -->
20     <context:annotation-config />
21     
22     <!-- 啓動組件掃描,排除@Controller組件,該組件由ApplicationContext_mvc.xml配置文件掃描 -->
23     <context:component-scan base-package="com.xxw">
24         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
25     </context:component-scan>
26     
27     <!-- 引入DataSource配置文件 -->
28     <import resource="classpath*:/ApplicationContext_DataSource.xml" />
29     
30 </beans>

 

八、web.xmlexpress

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
 3     <context-param>
 4         <param-name>contextConfigLocation</param-name>
 5         <param-value>classpath:ApplicationContext_main.xml</param-value>
 6     </context-param>
 7     
 8     <!-- Spring監聽 -->
12     <listener>
13           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
14     </listener>
15     <servlet>
16         <servlet-name>Spring</servlet-name>
17         <servlet-class>
18  org.springframework.web.servlet.DispatcherServlet 19         </servlet-class>
20         <init-param>
21           <param-name>contextConfigLocation</param-name>
22           <param-value>classpath:ApplicationContext_mvc.xml</param-value>
23         </init-param>
24         <load-on-startup>1</load-on-startup>
25     </servlet>
26     <servlet-mapping>
27         <servlet-name>Spring</servlet-name>
28         <url-pattern>/</url-pattern>
29     </servlet-mapping>
30     
31     <!-- 加載Log4J 配置文件 -->  
32     <context-param>  
33         <param-name>log4jConfigLocation</param-name>  
34         <param-value>classpath:log4j.properties</param-value>  
35     </context-param>  
36     <context-param>  
37         <param-name>log4jRefreshInterval</param-name>  
38         <param-value>3000</param-value>  
39      </context-param>  
40     <listener>  
41         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
42     </listener>
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         <init-param>  
53             <param-name>forceEncoding</param-name>  
54             <param-value>true</param-value>  
55         </init-param>  
56     </filter>  
57     <filter-mapping>  
58         <filter-name>characterEncodingFilter</filter-name>  
59         <url-pattern>/*</url-pattern>  
60     </filter-mapping>
61  
62      <!-- 初始默認訪問頁面 -->
63     <welcome-file-list>
64         <welcome-file>index.jsp</welcome-file>
65     </welcome-file-list>
66 </web-app>

 

總結:能湊合用apache

相關文章
相關標籤/搜索