Pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springStudy</artifactId>
<groupId>com.yyl</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-07-springAndMybatis</artifactId>
<dependencies>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--myBatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--spring-webmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--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.9.4</version>
</dependency>
<!--spring与mybatis整合的包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
</dependencies>
<!--在build中配置资源路径,防止自己写的配置文件无法导出的问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
注:这里面最重要的就是Mybatis与Spring的整合包
<!--spring与mybatis整合的包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
剩下的还需要之前提到过的Spring环境的搭建以及Mybatis环境的搭建,在本文中就不一一阐述了。
Mybatis环境搭建1
Mybatis环境搭建2
Spring环境搭建
在Spring的配置文件中配置Mybatis所需对象
这里,Spring的配置文件我将其命名为 spring-dao-servlet.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">
<!--在Spring管理的Bean容器中,需要配置一些与Mybatis有关的类-->
<!--DateSource:数据源,与数据库连接有关,这里使用Spring的JDBC来代替Mybatis-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3307/yyl?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--SqlFactory:它依赖数据源-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--绑定Mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/yyl/mapper/*.xml"/>
</bean>
<!--SqlSessionTemplate是Spring提供的用来代替Mybatis中的sqlSession的-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能用构造器注入,因为它没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!--注册自己写的实现类,需要别忘了添加set方法-->
<bean id="userMapperImpl" class="com.yyl.mapper.UserMapperImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean>
</beans>
【注】:将Spring中的数据源代替Mybatis的后,Mybatis中有关数据源配置的部分就可以去掉了。(即删除Mybatis配置文件中enviroments标签下的数据源配置)
编写一个Dao(Mapper)接口的实现类
这里以UserMapper及其实现为例:
package com.yyl.mapper;
import com.yyl.pojo.User;
import java.util.List;
/**
* @Author: LongLongA
* @Description:
* @Date: Created in 11:06 2020/12/16
*/
public interface UserMapper {
//查询所有用户
public List<User> selectUsers();
}
这里在UserMapperImpl类中注入了SqlSessionTemplate类,是Spring提供的用来代替SqlSession的
还要设置相应的Set方法,因为在配置文件中,SqlSessionTemplate是基于Set方法注入的
package com.yyl.mapper;
import com.yyl.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
/**
* @Author: LongLongA
* @Description:
* @Date: Created in 19:58 2020/12/16
*/
public class UserMapperImpl implements UserMapper {
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public List<User> selectUsers() {
UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
return mapper.selectUsers();
}
}
补充
上述实现类还有第二种方法:
package com.yyl.mapper;
import com.yyl.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
/**
* @Author: LongLongA
* @Description:
* @Date: Created in 10:28 2020/12/17
*/
/*获取SqlSession的第二种方法
与之前要注入SqlSessionTemplate不同,这次的实现类需要继承一个抽象类,
该抽象类提供了SqlSessionTemplate
只需要调用getSession()方法,就能得到SqlSessionTemplate
*/
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
@Override
public List<User> selectUsers() {
SqlSession sqlSession = getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.selectUsers();
}
}
接着在Spring配置文件中配置该实现类
需要注意的是,该实现类继承了SqlSessionDaoSupport父类,该父类中需要注入SqlSessionFactory
<!--注册用方式二实现的获取SqlSession的方法,要其继承的父类注入SqlSessionFactory-->
<bean id="userMapperImpl2" class="com.yyl.mapper.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
测试代码
@Test
public void test2(){
//获取Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//获取自己写的接口实现类
UserMapper userMapper = (UserMapper)context.getBean("userMapperImpl2");
List<User> users = userMapper.selectUsers();
for(User user: users){
System.out.println(user);
}
}
心得总结
在配置Spring接管Mybatis的配置文件中,大致顺序是:
- 配置数据源
- 配置SqlSessionFactory(它依赖于数据源)
- 配置SqlSessionTemplate(它依赖于SqlSessionFactory)
- 给接口加实现类,并在Spring容器中配置