Spring-MyBatis整合
Spring-MyBatis整合
步骤
一 导入相关的jar包
- junit
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
- mybatis
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
- mysql数据库
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
- spring相关的
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
- spring操控数据库spring-jdbc
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
- aop织入
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
- spring-mybatis
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
导入上面的jar之后,基本的mybatis-spring就已经可以开发了
=回忆mybatis====================
整理之前我们回忆一下mybatis
mybatis相关的jar我们上面已经导入了,这里就不强调了
- 编写实体类
- 编写工具类获取sqlSession 但是有了spring以后,就不再需要这里步了,直接交给spring 管理sqlSession
- 编写核心配置文件mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties">
</properties>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<package name="com.wcz.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/wcz/dao/UserMapper.xml"/>
</mappers>
</configuration>
- 编写接口
- 编写Mapper-xml
- 测试
==================================================================================================
接下来接着讲spring-mybatis
**二 编写配置文件 **
- 配置spring文件
配置spring俗称bean.xml文件要导入aop 下面是完整的bean.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
- spring之后要用spring的数据源替代MyBatis的位置,前面也导入了spring-jdbc jar包,我们要在bean.xml中使用spring提供的jdbc
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
- 通过数据源datasource获取sqlSessionFactory
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/wcz/mapper/*.xml"/>
</bean>
- 要执行sql,mybaits中要创建sqlSession对象,但是在spring,用SqlSessionTemplate即可,SqlSessionTemplate还是线程安全。
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能用构造器注入sqlSession 源码中没有set方法。具体请看源码-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
- 创建实现UserMapper接口类
public class UserMapperImpl implements UserMapper {
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public List<User> queryUser() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.queryUser();
return userList;
}
}
这时候就有小伙伴有疑问了,为了spring整合mybatis要多出一个实现类?再说了mybatis不也可以实现这个骚操作吗?答案是,因为spring在ioc中已经接管了seSession,或者说接管mybatis中很多对象,而你们别忘了,spring可以自动创建对象 (setter注入) 而mybatis不能,那怎么自动创建呢?只能我们手动给它加一个set方法
这时候你可能又惊讶心里又有疑问,卧槽!spring这么强大,ioc和aop 接管了所有的对象,都能替代mybatis了,那为什么还要用mybatis呢?直接所有的操作在bean.xml下完成就行了嘛。这个问题问得好!你为什么不问问神奇的海螺呢? 哈哈,你想,mybatis只用来单单写sql语句,spring用来处理和接管对象,那疑问不就解开了吗?哈哈哈哈哈哈哈哈!!~~
三 测试
public class MyTest {
@Test
public void Test() throws IOException {
ApplicationContext Context = new ClassPathXmlApplicationContext("spring-dao.xml");
UserMapper queryUser = Context.getBean("queryUser", UserMapper.class);
List<User> userList = queryUser.queryUser();
for (User user:userList){
System.out.println(user);
}
}
}
**配置spring-dao.xml文件 **
这里基本都是固定的写法,获取sql数据源,获取seSession等
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/wcz/mapper/*.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
</beans>