首页 > 编程语言 > 详细

SSM(Spring + Spring MVC + MyBatis)整合

时间:2018-01-23 12:16:03      阅读:272      评论:0      收藏:0      [点我收藏+]

项目使用Maven架构,项目目录结构如下:

技术分享图片

 

1、pom.xml :

  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>

2、jdbc.properties

 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

3、log4j.properties

 

 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  

 

4、mybatis_config.xml

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>

5、ApplicationContext_DataSource.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     <!-- 引入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.fh.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>

6、ApplicationContext_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" 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>

7、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>

 

8、web.xml

 

 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监听 -->
 9     <listener>
10         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
11     </listener>
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>

 

总结:能凑合用

技术分享图片

 

SSM(Spring + Spring MVC + MyBatis)整合

原文:https://www.cnblogs.com/duniang/p/8334983.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!