Spring笔记:整合MyBtis
作者:互联网
Spring中通常有两种方式整合MyBtis,一种是通过在xml中配置SqlSession,另一种是定义一个继承SqlSessionDaoSupport的类。
会用到的maven依赖:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.12</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>
1. 整合方式一:在xml中配置SqlSession
直接贴示例,详细解释请看注释。
MyBtis部分代码
实体类:
package com.yun.pojo;
import lombok.Data;
@Data
public class User {
private int id;
private String name;
private String pwd;
}
mapper接口:
package com.yun.mapper;
import com.yun.pojo.User;
import java.util.List;
public interface UserMapper {
public List<User> selectUser();
}
mapper对应的xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yun.mapper.UserMapper">
<select id="selectUser" resultType="user">
select * from mybatis.user;
</select>
</mapper>
MyBtis的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>
<typeAliases>
<package name="com.yun.pojo"/>
</typeAliases>
<!-- 以下配置已经在Spring的xml中配置,就不用重复配置了 -->
<!--
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="com.yun.mapper.UserMapper"/>
</mappers>
-->
</configuration>
Spring整合MyBtis代码
Spring的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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 使用Spring的数据源替换MyBatis的配置,这里使用Spring提供的JDBC,
这样就不用在MyBatis的xml中来配置数据源了-->
<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&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- 使用Spring配置来创建sqlSessionFactory,不再单独写一个公共方法来创建sqlSessionFactory了 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 绑定MyBatis相关配置,这里需要注意classpath:后面没有空格 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/yun/mapper/*.xml"/>
</bean>
<!-- Spring的SqlSessionTemplate就相当于MyBatis中的SqlSession对象,
并且由于SqlSessionTemplate没有对应的set方法,所以只能使用构造器进行注入 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<bean id="userMapper" class="com.yun.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
Spring实现类:
package com.yun.mapper;
import com.yun.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
public class UserMapperImpl implements UserMapper{
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
// 将该类通过Spring注入之后,直接使用此方法即可获得数据库查询结果
public List<User> selectUser() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.selectUser();
}
}
测试:
import com.yun.mapper.UserMapper;
import com.yun.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
for (User user : userMapper.selectUser()) {
System.out.println(user);
}
}
}
输出:
User(id=1, name=ZhangSan, pwd=123456)
User(id=2, name=lisi, pwd=123456)
User(id=3, name=wangwu, pwd=123456)
User(id=4, name=zhaoliu, pwd=123456)
2. 整合方式二:自定义类,并继承SqlSessionDaoSupport类
这种方式其实就是第一种方式的简化版,下面只贴一些差异点。
实现类:
package com.yun.mapper;
import com.yun.pojo.User;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
// 需要继承SqlSessionDaoSupport类,并实现对应的mapper接口
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
public List<User> selectUser() {
// 通过getSqlSession()就可以直接拿到SqlSession对象,不需要再用set方法进行注入
return getSqlSession().getMapper(UserMapper.class).selectUser();
}
}
Spring中xml配置:
<!-- 与第一种方式相比,不需要再配置sqlSession,直接在实现类的bean中配置sqlSessionFactory即可 -->
<bean id="userMapper2" class="com.yun.mapper.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
标签:mapper,MyBtis,Spring,yun,笔记,User,import,org,public 来源: https://www.cnblogs.com/guyuyun/p/15640682.html