依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
整合方式一:
User类
@Data
public class User {
private int id;
private String name;
private String pwd;
}
接口UserMapper
public interface UserMapper {
List<User> selectUser();
}
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.zhy.mapper.UserMapper">
<select id="selectUser" resultType="user">
select *
from mybatis.user
</select>
</mapper>
1、配置数据源
<!--DataSource 使用Spring的数据源替换Mybatis配置
我们这里使用的是Spring提供的JDBC org.springframework.jdbc.datasource
-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2b8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
2、sqlSessionFactory
<!--配置sqlSessionFactory-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
<!--绑定mabatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:cn/zhy/mapper/*.xml"/>
</bean>
3、sqlSessionTemplate
<!--SqlSessionTemplate就是我们使用的sqlSession-->
<bean class="org.mybatis.spring.SqlSessionTemplate" id="sqlSession">
<!--SqlSessionTemplate中没有set方法,只能通过构造器注入-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
4、需要给接口加实现类
public class UserMapperImpl implements UserMapper {
//原来我们所有的操作通过sqlSession执行,
//现在使用SqlSessionTemplate,他们俩一样的
private SqlSessionTemplate sqlSession;
public SqlSessionTemplate getSqlSession() {
return sqlSession;
}
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<User> selectUser() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.selectUser();
}
}
5、将自己写的实现类注入到Spring容器中
<bean class="cn.zhy.mapper.UserMapperImpl" id="userMapper">
<property name="sqlSession" ref="sqlSession"/>
</bean>
完整xml代码
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--xmlns:context="http://www.springframework.org/schema/context" 注意别导错了-->
<!--开启组件扫描功能 扫描cn.zhy.pojo包下所有类是否添加了@Component注解-->
<!-- <context:component-scan base-package="cn.zhy"/>-->
<!-- <context:annotation-config/>-->
<!-- <!–开启Aop注解支持–>-->
<!-- <aop:aspectj-autoproxy/>-->
<!--DataSource 使用Spring的数据源替换Mybatis配置
我们这里使用的是Spring提供的JDBC org.springframework.jdbc.datasource
-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2b8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--配置sqlSessionFactory-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
<!--绑定mabatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:cn/zhy/mapper/*.xml"/>
</bean>
<!--SqlSessionTemplate就是我们使用的sqlSession-->
<bean class="org.mybatis.spring.SqlSessionTemplate" id="sqlSession">
<!--SqlSessionTemplate中没有set方法,只能通过构造器注入-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<bean class="cn.zhy.mapper.UserMapperImpl" id="userMapper">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
mybatis-config.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--xmlns:context="http://www.springframework.org/schema/context" 注意别导错了-->
<!--开启组件扫描功能 扫描cn.zhy.pojo包下所有类是否添加了@Component注解-->
<!-- <context:component-scan base-package="cn.zhy"/>-->
<!-- <context:annotation-config/>-->
<!-- <!–开启Aop注解支持–>-->
<!-- <aop:aspectj-autoproxy/>-->
<!--DataSource 使用Spring的数据源替换Mybatis配置
我们这里使用的是Spring提供的JDBC org.springframework.jdbc.datasource
-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2b8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--配置sqlSessionFactory-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="dataSource"/>
<!--绑定mabatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:cn/zhy/mapper/*.xml"/>
</bean>
<!--SqlSessionTemplate就是我们使用的sqlSession-->
<bean class="org.mybatis.spring.SqlSessionTemplate" id="sqlSession">
<!--SqlSessionTemplate中没有set方法,只能通过构造器注入-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<bean class="cn.zhy.mapper.UserMapperImpl" id="userMapper">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
测试
public class Mytest {
@Test
public void selectUser() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapperImpl userMapper = context.getBean("userMapper", UserMapperImpl.class);
List<User> users = userMapper.selectUser();
for (User user : users) {
System.out.println(user);
}
}
}
第二种方式整合
1、实现类继承 SqlSessionDaoSupport,方法里面获取getSqlSession
public class UserMapperImplTwo extends SqlSessionDaoSupport implements UserMapper {
@Override
public List<User> selectUser() {
SqlSession sqlSession = getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.selectUser();
}
}
2、注册bean
<bean class="cn.zhy.mapper.UserMapperImplTwo" id="userMapper2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>