Spring MVC Spring MyBatis 整合 - 快速上手

Posted 风间影月の技术小站

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring MVC Spring MyBatis 整合 - 快速上手相关的知识,希望对你有一定的参考价值。

我个人比较喜欢写注释,在工作中对注释的重要性看的也比较高,所以大部分文字都在注释中,代码外的文字会写的偏少,关键能懂就行

先看一下整合后的工程目录(单工程,多工程以后会采用maven)

技术分享

5个package分别对应 action, entity, mapper(也就是DAO,采用动态代理), service, service 实现类

resources 下 相关资源文件:

db.properties: 数据库连接参数

log4j.properties: 日志

applicationContext-dao.xml: spring 和 mybatis 整合的一些配置(sqlSessionFactory,mapper 配置)
applicationContext-service.xml: 业务
applicationContext.xml: spring 核心配置文件(配置一些公用的内容,比如数据源,事务)
springmvc.xml: springmvc 全局配置文件

SqlMapConfig.xml: mybatis 全局配置文件

 

webapp结构简单就不多说了

下面先贴出maven的pom内容:

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  4     <modelVersion>4.0.0</modelVersion>
  5     <groupId>com.lee</groupId>
  6     <artifactId>ssm</artifactId>
  7     <name>maven-ssm-hello</name>
  8     <packaging>war</packaging>
  9     <version>1.0.0-BUILD-SNAPSHOT</version>
 10     <properties>
 11         <java-version>1.7</java-version>
 12         <org.springframework-version>3.2.3.RELEASE</org.springframework-version>
 13         <org.aspectj-version>1.6.10</org.aspectj-version>
 14         <org.slf4j-version>1.6.6</org.slf4j-version>
 15     </properties>
 16     
 17     <dependencies>
 18         <!-- Spring -->
 19         <dependency>
 20             <groupId>org.springframework</groupId>
 21             <artifactId>spring-context</artifactId>
 22             <version>${org.springframework-version}</version>
 23             <exclusions>
 24                 <!-- Exclude Commons Logging in favor of SLF4j -->
 25                 <exclusion>
 26                     <groupId>commons-logging</groupId>
 27                     <artifactId>commons-logging</artifactId>
 28                  </exclusion>
 29             </exclusions>
 30         </dependency>
 31         <dependency>
 32             <groupId>org.springframework</groupId>
 33             <artifactId>spring-webmvc</artifactId>
 34             <version>${org.springframework-version}</version>
 35         </dependency>
 36         <dependency>  
 37             <groupId>org.springframework</groupId>  
 38             <artifactId>spring-jdbc</artifactId>  
 39             <version>${org.springframework-version}</version>  
 40         </dependency>
 41 
 42         <!-- data source: dbcp -->
 43         <dependency>
 44             <groupId>commons-dbcp</groupId>
 45             <artifactId>commons-dbcp</artifactId>
 46             <version>1.4</version>
 47         </dependency>
 48 
 49         
 50         <!-- AspectJ -->
 51         <dependency>
 52             <groupId>org.aspectj</groupId>
 53             <artifactId>aspectjrt</artifactId>
 54             <version>${org.aspectj-version}</version>
 55         </dependency>    
 56         
 57         <!-- Logging -->
 58         <dependency>
 59             <groupId>org.slf4j</groupId>
 60             <artifactId>slf4j-api</artifactId>
 61             <version>${org.slf4j-version}</version>
 62         </dependency>
 63         <dependency>
 64             <groupId>org.slf4j</groupId>
 65             <artifactId>jcl-over-slf4j</artifactId>
 66             <version>${org.slf4j-version}</version>
 67             <scope>runtime</scope>
 68         </dependency>
 69         <dependency>
 70             <groupId>org.slf4j</groupId>
 71             <artifactId>slf4j-log4j12</artifactId>
 72             <version>${org.slf4j-version}</version>
 73             <scope>runtime</scope>
 74         </dependency>
 75         <dependency>
 76             <groupId>log4j</groupId>
 77             <artifactId>log4j</artifactId>
 78             <version>1.2.15</version>
 79             <exclusions>
 80                 <exclusion>
 81                     <groupId>javax.mail</groupId>
 82                     <artifactId>mail</artifactId>
 83                 </exclusion>
 84                 <exclusion>
 85                     <groupId>javax.jms</groupId>
 86                     <artifactId>jms</artifactId>
 87                 </exclusion>
 88                 <exclusion>
 89                     <groupId>com.sun.jdmk</groupId>
 90                     <artifactId>jmxtools</artifactId>
 91                 </exclusion>
 92                 <exclusion>
 93                     <groupId>com.sun.jmx</groupId>
 94                     <artifactId>jmxri</artifactId>
 95                 </exclusion>
 96             </exclusions>
 97             <scope>runtime</scope>
 98         </dependency>
 99 
100         <!-- @Inject -->
101         <dependency>
102             <groupId>javax.inject</groupId>
103             <artifactId>javax.inject</artifactId>
104             <version>1</version>
105         </dependency>
106                 
107         <!-- Servlet -->
108         <dependency>
109             <groupId>javax.servlet</groupId>
110             <artifactId>servlet-api</artifactId>
111             <version>2.5</version>
112             <scope>provided</scope>
113         </dependency>
114         <dependency>
115             <groupId>javax.servlet.jsp</groupId>
116             <artifactId>jsp-api</artifactId>
117             <version>2.1</version>
118             <scope>provided</scope>
119         </dependency>
120         <dependency>
121             <groupId>javax.servlet</groupId>
122             <artifactId>jstl</artifactId>
123             <version>1.2</version>
124         </dependency>
125     
126         <!-- mybatis -->  
127         <dependency>  
128             <groupId>org.mybatis</groupId>  
129             <artifactId>mybatis</artifactId>  
130             <version>3.2.3</version>  
131         </dependency>  
132         <dependency>  
133             <groupId>org.mybatis</groupId>  
134             <artifactId>mybatis-spring</artifactId>  
135             <version>1.2.3</version>  
136         </dependency>  
137         <dependency>
138             <groupId>mysql</groupId>
139             <artifactId>mysql-connector-java</artifactId>
140             <version>5.1.38</version>
141         </dependency>
142     
143         <!-- Test -->
144         <dependency>
145             <groupId>junit</groupId>
146             <artifactId>junit</artifactId>
147             <version>4.7</version>
148             <scope>test</scope>
149         </dependency>  
150         
151         <dependency>  
152             <groupId>com.alibaba</groupId>  
153             <artifactId>druid</artifactId>  
154             <version>1.0.18</version>  
155         </dependency>  
156   
157         <dependency>  
158             <groupId>org.aspectj</groupId>  
159             <artifactId>aspectjweaver</artifactId>  
160             <version>1.7.2</version>  
161         </dependency>  
162         <dependency>  
163             <groupId>com.alibaba</groupId>  
164             <artifactId>fastjson</artifactId>  
165             <version>1.1.34</version>  
166         </dependency>  
167         <dependency>  
168             <groupId>com.fasterxml.jackson.core</groupId>  
169             <artifactId>jackson-core</artifactId>  
170             <version>2.2.2</version>  
171         </dependency>  
172         <dependency>  
173             <groupId>com.fasterxml.jackson.core</groupId>  
174             <artifactId>jackson-databind</artifactId>  
175             <version>2.2.2</version>  
176         </dependency>  
177               
178     </dependencies>
179     
180     <build>
181         <plugins>
182             <plugin>
183                 <artifactId>maven-eclipse-plugin</artifactId>
184                 <version>2.9</version>
185                 <configuration>
186                     <additionalProjectnatures>
187                         <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
188                     </additionalProjectnatures>
189                     <additionalBuildcommands>
190                         <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
191                     </additionalBuildcommands>
192                     <downloadSources>true</downloadSources>
193                     <downloadJavadocs>true</downloadJavadocs>
194                 </configuration>
195             </plugin>
196             
197             <plugin>
198                 <groupId>org.apache.maven.plugins</groupId>
199                 <artifactId>maven-compiler-plugin</artifactId>
200                 <version>2.5.1</version>
201                 <configuration>
202                     <source>1.7</source>
203                     <target>1.7</target>
204                     <compilerArgument>-Xlint:all</compilerArgument>
205                     <showWarnings>true</showWarnings>
206                     <showDeprecation>true</showDeprecation>
207                 </configuration>
208             </plugin>
209             
210             <plugin>
211                 <groupId>org.codehaus.mojo</groupId>
212                 <artifactId>exec-maven-plugin</artifactId>
213                 <version>1.2.1</version>
214                 <configuration>
215                     <mainClass>org.test.int1.Main</mainClass>
216                 </configuration>
217             </plugin>
218         </plugins>
219     </build>
220 </project>

相关资源文件:

SqlMapConfig.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>
 6     
 7     <!-- 定义别名 -->
 8     <typeAliases>
 9         <!-- 批量别名定义
10         name:指定包名,将包下边的所有pojo定义别名 ,别名为类名(首字母大写或小写都行)
11          -->
12         <package name="com.mybatis.bean"/>
13     </typeAliases>
14     
15 </configuration>

 

applicationContext-dao.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="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5 
 6     <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
 7         <!-- 数据源 -->
 8         <property name="dataSource" ref="dataSource" />
 9         <!-- 配置SqlMapConfig.xml -->
10         <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
11     </bean>
12         
13     <!-- 
14         使用mapper 批量扫描器扫描mapper接口
15         不需要id,规则:mapper.xml 与 mapper.java 同名并且在同一个包下    
16         扫描出来的mapper会自动向spring容器中注册,bean的id就是类名(首字母小写)
17      -->
18     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
19         <!-- 配置会话工厂 -->
20         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
21         <!-- 
22             扫描包的路径
23             如果有多个mapper的包,那就用半角的 ‘,‘ 来隔开    
24          -->
25         <property name="basePackage" value="com.lee.ssm.dao.mapper"></property>
26     </bean>
27     
28 </beans>

 

applicationContext-service.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="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
5 
6     <!-- 用户管理 -->
7 <!--     <bean id="userService" class="com.lee.ssm.service.impl.UserServiceImpl" /> -->
8 </beans>

 

applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3 

以上是关于Spring MVC Spring MyBatis 整合 - 快速上手的主要内容,如果未能解决你的问题,请参考以下文章

spring mvc mybatis 整合 大体步骤

ssm(spring,spring mvc,mybatis)框架

Mybatis系列六:与Spring MVC的集成

[ Spring MVC ] 整合Mybatis开发

工具类中怎么注入service,spring mvc +mybatiss

Spring4 + Spring MVC + MyBatis 整合思路