JAVA框架 Spring 和Mybatis整合
Posted evil_liu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA框架 Spring 和Mybatis整合相关的知识,希望对你有一定的参考价值。
一:我们使用spring处理service,mybaits处理dao层。
二:导入jar包
pom.xml文件内容:
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>jd.com</groupId> 8 <artifactId>AddSpringAndMy</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <packaging>war</packaging> 11 12 <name>AddSpringAndMy Maven Webapp</name> 13 <!-- FIXME change it to the project\'s website --> 14 <url>http://www.example.com</url> 15 16 <properties> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 <maven.compiler.source>1.8</maven.compiler.source> 19 <maven.compiler.target>1.8</maven.compiler.target> 20 </properties> 21 <dependencies> 22 <dependency> 23 <groupId>org.hamcrest</groupId> 24 <artifactId>hamcrest-library</artifactId> 25 <version>1.3</version> 26 </dependency> 27 <dependency> 28 <groupId>org.slf4j</groupId> 29 <artifactId>slf4j-log4j12</artifactId> 30 <version>1.7.5</version> 31 </dependency> 32 <dependency> 33 <groupId>org.slf4j</groupId> 34 <artifactId>slf4j-api</artifactId> 35 <version>1.7.5</version> 36 </dependency> 37 <dependency> 38 <groupId>org.apache.logging.log4j</groupId> 39 <artifactId>log4j-core</artifactId> 40 <version>2.0.2</version> 41 </dependency> 42 <dependency> 43 <groupId>org.apache.logging.log4j</groupId> 44 <artifactId>log4j-api</artifactId> 45 <version>2.0.2</version> 46 </dependency> 47 <dependency> 48 <groupId>spring-jms</groupId> 49 <artifactId>spring-jms</artifactId> 50 <version>4.2.4</version> 51 </dependency> 52 <dependency> 53 <groupId>org.springframework</groupId> 54 <artifactId>spring-web</artifactId> 55 <version>4.3.12.RELEASE</version> 56 </dependency> 57 <dependency> 58 <groupId>mysql</groupId> 59 <artifactId>mysql-connector-java</artifactId> 60 <version>5.1.34</version> 61 </dependency> 62 <dependency> 63 <groupId>org.springframework</groupId> 64 <artifactId>spring-oxm</artifactId> 65 <version>4.1.5.RELEASE</version> 66 </dependency> 67 <dependency> 68 <groupId>org.springframework</groupId> 69 <artifactId>spring-test</artifactId> 70 <version>4.1.5.RELEASE</version> 71 </dependency> 72 <dependency> 73 <groupId>org.springframework</groupId> 74 <artifactId>spring-expression</artifactId> 75 <version>4.3.12.RELEASE</version> 76 </dependency> 77 <dependency> 78 <groupId>org.springframework</groupId> 79 <artifactId>spring-core</artifactId> 80 <version>4.3.12.RELEASE</version> 81 </dependency> 82 <dependency> 83 <groupId>org.springframework</groupId> 84 <artifactId>spring-beans</artifactId> 85 <version>4.3.12.RELEASE</version> 86 </dependency> 87 <dependency> 88 <groupId>org.springframework</groupId> 89 <artifactId>spring-context</artifactId> 90 <version>4.3.12.RELEASE</version> 91 </dependency> 92 <dependency> 93 <groupId>org.springframework</groupId> 94 <artifactId>spring-aop</artifactId> 95 <version>4.3.12.RELEASE</version> 96 </dependency> 97 <dependency> 98 <groupId>aopalliance</groupId> 99 <artifactId>aopalliance</artifactId> 100 <version>1.0</version> 101 </dependency> 102 <dependency> 103 <groupId>spring-aspects</groupId> 104 <artifactId>spring-aspects</artifactId> 105 <version>4.2.4</version> 106 </dependency> 107 <dependency> 108 <groupId>com.springsource.org</groupId> 109 <artifactId>aspectj.weaver</artifactId> 110 <version>1.6.8</version> 111 </dependency> 112 113 <dependency> 114 <groupId>org.apache.commons</groupId> 115 <artifactId>commons-pool2</artifactId> 116 <version>2.4.1</version> 117 </dependency> 118 119 <dependency> 120 <groupId>org.apache.commons</groupId> 121 <artifactId>commons-dbcp2</artifactId> 122 <version>2.0.1</version> 123 </dependency> 124 <dependency> 125 <groupId>org.springframework</groupId> 126 <artifactId>spring-jdbc</artifactId> 127 <version>4.1.5.RELEASE</version> 128 </dependency> 129 <dependency> 130 <groupId>log4j</groupId> 131 <artifactId>log4j</artifactId> 132 <version>1.2.17</version> 133 </dependency> 134 <dependency> 135 <groupId>org.javassist</groupId> 136 <artifactId>javassist</artifactId> 137 <version>3.16.1-GA</version> 138 </dependency> 139 <dependency> 140 <groupId>org.mybatis</groupId> 141 <artifactId>mybatis-spring</artifactId> 142 <version>1.2.2</version> 143 </dependency> 144 <dependency> 145 <groupId>org.springframework</groupId> 146 <artifactId>spring-tx</artifactId> 147 <version>4.1.5.RELEASE</version> 148 </dependency> 149 <dependency> 150 <groupId>commons-logging</groupId> 151 <artifactId>commons-logging</artifactId> 152 <version>1.1.1</version> 153 </dependency> 154 <dependency> 155 <groupId>cglib</groupId> 156 <artifactId>cglib</artifactId> 157 <version>2.2.2</version> 158 </dependency> 159 <dependency> 160 <groupId>asm</groupId> 161 <artifactId>asm</artifactId> 162 <version>3.3.1</version> 163 </dependency> 164 <dependency> 165 <groupId>org.mybatis</groupId> 166 <artifactId>mybatis</artifactId> 167 <version>3.2.7</version> 168 </dependency> 169 <dependency> 170 <groupId>junit</groupId> 171 <artifactId>junit</artifactId> 172 <version>4.11</version> 173 <scope>test</scope> 174 </dependency> 175 <dependency> 176 <groupId>org.junit.jupiter</groupId> 177 <artifactId>junit-jupiter-api</artifactId> 178 <version>RELEASE</version> 179 </dependency> 180 <dependency> 181 <groupId>org.springframework</groupId> 182 <artifactId>spring-test</artifactId> 183 <version>4.3.12.RELEASE</version> 184 </dependency> 185 <dependency> 186 <groupId>junit</groupId> 187 <artifactId>junit</artifactId> 188 <version>RELEASE</version> 189 </dependency> 190 </dependencies> 191 192 <build> 193 <finalName>AddSpringAndMy</finalName> 194 <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> 195 <plugins> 196 <plugin> 197 <artifactId>maven-clean-plugin</artifactId> 198 <version>3.0.0</version> 199 </plugin> 200 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> 201 <plugin> 202 <artifactId>maven-resources-plugin</artifactId> 203 <version>3.0.2</version> 204 </plugin> 205 <plugin> 206 <artifactId>maven-compiler-plugin</artifactId> 207 <version>3.7.0</version> 208 </plugin> 209 <plugin> 210 <artifactId>maven-surefire-plugin</artifactId> 211 <version>2.20.1</version> 212 </plugin> 213 <plugin> 214 <artifactId>maven-war-plugin</artifactId> 215 <version>3.2.0</version> 216 </plugin> 217 <plugin> 218 <artifactId>maven-install-plugin</artifactId> 219 <version>2.5.2</version> 220 </plugin> 221 <plugin> 222 <artifactId>maven-deploy-plugin</artifactId> 223 <version>2.8.2</version> 224 </plugin> 225 </plugins> 226 </pluginManagement> 227 </build> 228 </project>
注意:
在引用jar包的话,会出现jar包冲突,注意版本的引用,最好版本号一致。
jar包清单:
三、配置文件整合
1)首先引用properites文件(注意引用外部文件使用classpath引用)
1 <context:property-placeholder location="classpath:db.properites" />
2)开启注解扫描和动态代理
1 <context:component-scan base-package="jd.com" /> 2 <aop:aspectj-autoproxy />
3)我们将DataSource和sqlSessionFactory叫给spring个管理
1 <!--定义数据库连接池--> 2 <bean id="basicDataSource" class="org.apache.commons.dbcp2.BasicDataSource" > 3 <!--支持el表达式--> 4 <property name="driverClassName" value="${jdbc.driver}"/> 5 <property name="url" value="${jdbc.url}" /> 6 <property name="username" value="${jdbc.username}" /> 7 <property name="password" value="${jdbc.password}" /> 8 <property name="maxTotal" value="10" /> 9 <property name="maxIdle" value="3" /> 10 </bean>
因为SqlSessionFactory是接口,可以使用mybatis-spring的整合的实现类:SqlSessionFactoryBean
其中需要在注入DataSource和配置文件的注入。注意需要是用classpath文件引用
1 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 2 <!--依赖DataSource--> 3 <property name="dataSource" ref="basicDataSource"/> 4 <!--加载mybaits的配置文件--> 5 <property name="configLocation" value="classpath:SqlMapConfig.xml" /> 6 </bean>
4)注入dao层和service层
1 <bean id="dao" class="jd.com.dao.trDaoImpl"> 2 <property name="SqlSessionFactory" ref="sqlSessionFactory"/> 3 </bean> 4 <bean class="jd.com.service.serFindByIdImpl" id="serv"> 5 <property name="dao" ref="dao" /> 6 </bean>
applicationContext.xml完整配置文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!--总的约束--> 3 <beans xmlns="http://www.springframework.org/schema/beans" 4 xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 9 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 10 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> 11 <!--加载配置文件,定义的properites文件 12 引入外部文件使用classpath关键字。 13 --> 14 15 <context:component-scan base-package="jd.com" /> 16 <aop:aspectj-autoproxy /> 17 18 <context:property-placeholder location="classpath:db.properites" /> 19 <!--定义数据库连接池--> 20 <bean id="basicDataSource" class="org.apache.commons.dbcp2.BasicDataSource" > 21 <!--支持el表达式--> 22 <property name="driverClassName" value="${jdbc.driver}"/> 23 <property name="url" value="${jdbc.url}" /> 24 <property name="username" value="${jdbc.username}" /> 25 <property name="password" value="${jdbc.password}" /> 26 <property name="maxTotal" value="10" /> 27 <property name="maxIdle" value="3" /> 28 </bean> 29 <!--配置mapper 30 其中:org.mybatis.spring.SqlSessionFactoryBean 是SqlSessionFactory的实现类,该类在mybatis-spring.1.2.2.jar里。 31 --> 32 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 33 <!--依赖DataSource--> 34 <property name="dataSource" ref="basicDataSource"/> 35 <!--加载mybaits的配置文件--> 36 <property name="configLocation" value="classpath:SqlMapConfig.xml" /> 37 </bean> 38 <bean id="dao" class="jd.com.dao.trDaoImpl"> 39 <property name="SqlSessionFactory" ref="sqlSessionFactory"/> 40 </bean> 41 <bean class="jd.com.service.serFindByIdImpl" id="serv"> 42 <property name="dao" ref="dao" /> 43 </bean> 44 45 46 </beans>
其中db.properits是配置数据库配置。
在看下mybatis的配置文件:只是一个子配置文件引入:
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 6 7 <configuration> 8 <mappers > 9 <mapper resource="user.xml" /> 10 11 <!--<mapper class="jd.com.proxydao.AccMapper" />--> 12 <!--<package name="jd.com.proxydao" />--> 13 <!--<package name="jd.com.vo" />--> 14 <!--<package name="jd.com.vodemo" />--> 15 <!--导入配置文件--> 16 <!--<package name="jd.com" />--> 17 </mappers> 18 </configuration>
子配置文件user.xml:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <!--和主配置文件约束是不一样的--> 6 <mapper namespace="findById"> 7 <select id="findId" parameterType="int" resultType="jd.com.dao.user" > 8 SELECT * FROM username WHERE id=#{id}; 9 </select> 10 </mapper>
注意:在不使用mybatis的动态代理的情况下,需要注意 namespace和id不是对应的接口的接口名和方法。
user.class实体类:
1 package jd.com.dao; 2 3 public class user { 4 private Integer id; 5 private String name; 6 private String sex; 7 8 public void setName(String name) { 9 this.name = name; 10 } 11 12 public void setSex(String sex) { 13 this.sex = sex; 14 } 15 16 public void setId(Integer id) { 17 this.id = id; 18 } 19 20 public Integer getId() { 21 return id; 22 } 23 24 public String getName() { 25 return name; 26 } 27 28 public String getSex() { 29 return sex; 30 } 31 32 @Override 33 public String toString() { 34 return this.getName()+" "+this.getId()+" "+this.getSex(); 35 } 36 }
dao层:
接口:
1 package jd.com.dao; 2 3 public interface trDao { 4 user findUserByName(Integer id); 5 }
实现类:注意这个里继承扫起来SqlSessionSupport类。在注入dao层的时候需要注意的引用sqlsessionfactory 所以这之前spring的自己daosupport一样需要引用相应的(DataSource)
1 package jd.com.dao; 2 3 import org.apache.ibatis.session.SqlSession; 4 import org.mybatis.spring.support.SqlSessionDaoSupport; 5 import org.springframework.stereotype.Repository; 6 7 8 //需要继承SqlSessionDaoSupport 9 10 public class trDaoImpl extends SqlSessionDaoSupport implements trDao { 11 12 13 @Override 14 public user findUserByName(Integer id) { 15 // SqlSessionDaoSupport该类有相应的set方法和get方法 应该类型spring的DaoSupport 16 SqlSession sqlSession=this.getSqlSession(); 17 18 user use=sqlSession.selectOne("findById.findId",id); 19 return use; 20 } 21 }
service层:
接口:
1 package jd.com.service; 2 3 import jd.com.dao.user; 4 5 6 public interface trService { 7 user serFindById(Integer id); 8 }
实现类:
1 package jd.com.service; 2 3 import jd.com.dao.trDao; 4 import jd.com.dao.user; 5 import org.springframework.stereotype.Service; 6 7 import javax.annotation.Resource; 8 9 10 @Service(value = "serv") 11 public class serFindByIdImpl implements trService { 12 @Resource(name="dao") 13 private trDao dao; 14 15 @Override 16 public user serFindById(Integer id) { 17 user use=this.dao.findUserByName(id); 18 return use; 《Java Spring框架》通过Idea 整合Spring和Mybatis[技术篇(java)] 教你整合最优雅SSM框架:SpringMVC + Spring + MyBatis