spring06:mybatis-spring(Spring整合MyBatis)

spring06:mybatis-spring(Spring整合MyBatis)


文章目录

  • spring06:mybatis-spring(Spring整合MyBatis)
  • 前言:
    • [什么是 MyBatis-Spring?](#什么是 MyBatis-Spring?)
      • [MyBatis-Spring 会帮助你将 MyBatis 代码无缝地整合到 Spring 中。](#MyBatis-Spring 会帮助你将 MyBatis 代码无缝地整合到 Spring 中。)
  • 一、mybatis流程
    • [1. 编写实体类](#1. 编写实体类)
    • [2. 编写核心配置文件mybatis-config.xml](#2. 编写核心配置文件mybatis-config.xml)
    • [3. 编写接口UserMapper](#3. 编写接口UserMapper)
    • [4. 编写配置文件UserMapper.xml](#4. 编写配置文件UserMapper.xml)
    • [5. 编写测试类](#5. 编写测试类)
  • [二、spring整合mybatis 方式一](#二、spring整合mybatis 方式一)
  • [三、spring整合mybatis 方式二](#三、spring整合mybatis 方式二)
      • [1. 编写实现类UserDaoImpl 直接继承 SqlSessionDaoSupport](#1. 编写实现类UserDaoImpl 直接继承 SqlSessionDaoSupport)
      • [2. 编写spring配置文件spring-dao.xml(相当于beans.xml)](#2. 编写spring配置文件spring-dao.xml(相当于beans.xml))
        • [applicationContext.xml是beans.xml配置文件的 整合!!!](#applicationContext.xml是beans.xml配置文件的 整合!!!)
      • [3. 编写测试类](#3. 编写测试类)
  • 总结

前言:

什么是 MyBatis-Spring?

MyBatis-Spring 会帮助你将 MyBatis 代码无缝地整合到 Spring 中。


提示:以下是本篇文章正文内容:

一、mybatis流程

1.编写实体类

2.编写核心配置文件mybatis-config.xml

3.编写工具类MyBatisUtil

4.编写接口UserMapper

5.编写配置文件UserMapper.xml

6.编写测试类

1. 编写实体类

java 复制代码
// 使用了lombok注解简化:
@Data
public class User {
   private int id;
   private String name;
   private String pwd;
}

2. 编写核心配置文件mybatis-config.xml

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="nuc.ss.pojo"/>
   </typeAliases>

   <environments default="development">
       <environment id="development">
           <transactionManager type="JDBC"/>
           <dataSource type="POOLED">
               <property name="driver" value="com.mysql.jdbc.Driver"/>
               <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true;useUnicode=true;characterEncoding=utf8"/>
               <property name="username" value="root"/>
               <property name="password" value="admin"/>
           </dataSource>
       </environment>
   </environments>

   <mappers>
       <package name="nuc.ss.dao"/>
   </mappers>
</configuration>

3. 编写接口UserMapper

java 复制代码
public interface UserMapper {
   public List<User> selectUser();
}

4. 编写配置文件UserMapper.xml

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="nuc.ss.dao.UserMapper">
   <select id="selectUser" resultType="User">
    select * from user
   </select>

</mapper>

5. 编写测试类

MyBatisUtils工具类的作用就是创建sqlSession

我们在测试类直接创建,逻辑和 MyBatisUtils工具类是差不多的。

java 复制代码
@Test
public void selectUser() throws IOException {
	// MyBatisUtils工具类的作用就是创建sqlSession
	// 我们在测试类直接创建,逻辑和 MyBatisUtils工具类是差不多的。
   String resource = "mybatis-config.xml";
   InputStream inputStream = Resources.getResourceAsStream(resource);
   SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
   SqlSession sqlSession = sqlSessionFactory.openSession();

   UserMapper mapper = sqlSession.getMapper(UserMapper.class);

   List<User> userList = mapper.selectUser();
   for (User user: userList){
       System.out.println(user);
  }

   sqlSession.close();
}


二、spring整合mybatis 方式一

1.编写数据源配置

2.sqlSessionFactory

3.sqlSessionTemplate

4.需要给接口加实现类

5.将自己写的实现类,注入到spring当中

6.编写测试类

1. 在pom.xml导入maven依赖:mybatis-spring

xml 复制代码
<dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis-spring</artifactId>
   <version>2.0.2</version>
</dependency>

2. 写一个接口实现类:UserDaoImpl(对于UserMapper接口)

在后面须要把这个类注入到spring容器里面(beams.xml)

这个类的作用:相当于mybatis的测试类,

得到sqlSession对象,执行UserMapper的方法。(执行sql语句)

在mybatis-spring里面,我们需要写这样一个方法,然后注入到spring里面,交给spring来管理。

java 复制代码
public class UserDaoImpl implements UserMapper {
   //sqlSession不用我们自己创建了,Spring来管理
   private SqlSessionTemplate sqlSession;

   public void setSqlSession(SqlSessionTemplate sqlSession) {
       this.sqlSession = sqlSession;
  }
   public List<User> selectUser() {
       UserMapper mapper = sqlSession.getMapper(UserMapper.class);
       return mapper.selectUser();
  }
}

3. 编写spring配置文件spring-dao.xml(相当于beans.xml)

注意:上面图片第4步的将实现类注入到spring中,写错了(那个是第二种方式的)

下面这个才是:(私有化一个sqlSession)

xml 复制代码
<bean id="userDao" class="nuc.ss.dao.UserDaoImpl">
   <property name="sqlSession" ref="sqlSession"/>
</bean>

4. 编写测试类

java 复制代码
// 1.获取spring容器
// 2.获取对象
// 3.使用对象执行方法
@Test
public void test(){
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserMapper mapper = (UserMapper) context.getBean("userDao");
    List<User> user = mapper.selectUser();
    System.out.println(user);
}


三、spring整合mybatis 方式二

dao继承SqlSessionDaoSupport 类 , 直接利用 getSqlSession() 获得 , 然后直接注入SqlSessionFactory .

比起方式1 , 不需要管理SqlSessionTemplate , 而且对事务的支持更加友好

1. 编写实现类UserDaoImpl 直接继承 SqlSessionDaoSupport

java 复制代码
// 1.获取sqlSession对象
// 2.得到mapper对象
// 3.利用mapper对象执行方法
public class UserDaoImpl extends SqlSessionDaoSupport implements UserMapper {
   public List<User> selectUser() {
       UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
       return mapper.selectUser();
  }
}

2. 编写spring配置文件spring-dao.xml(相当于beans.xml)

applicationContext.xml是beans.xml配置文件的 整合!!!

3. 编写测试类

感觉 mybatis的测试类的步骤 和 mybatis-spring的测试类的步骤 好像!!!

java 复制代码
// 1.获取spring容器
// 2.拿到mapper对象
// 3.利用mapper对象执行方法
@Test
public void test2(){
   ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
   UserMapper mapper = (UserMapper) context.getBean("userDao");
   List<User> user = mapper.selectUser();
   System.out.println(user);
}

总结

提示:这里对文章进行总结:

😊😊😊

相关推荐
知产xiao_xin7 小时前
知识产权入门 —— 相关权(邻接权)
经验分享·笔记·知识产权
2601_962055977 小时前
跟据spring boot版本,查看对应的tomcat,并查看可支持的tomcat的版本范围
spring boot·后端·tomcat
EXI-小洲7 小时前
Java 操作 Word:字符串替换、图片插入、动态生成表格与API接口下载
java·开发语言·spring boot·word
自小吃多7 小时前
DXF文件导入与PCB板框定义笔记
笔记·嵌入式硬件
2601_961901708 小时前
SpringBoot使用Nacos进行application.yml配置管理
java·spring boot·spring
Lost of 程序猿8 小时前
ASP.NET Core API 幂等性设计深度实战:从一次重复申领事故说起
后端·asp.net
是隼人8 小时前
buuctf-pwn xdctf2015_pwn200题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
何以解忧,唯有..9 小时前
LangChain 工具调用(Tool Calling)实战指南
java·前端·langchain
jike_20269 小时前
iPhone采访录音同时拍现场照片的APP:图片音频同步记录实测
笔记·智能手机·音视频·语音识别
大衛說9 小时前
《Python 从入门到精通》系列总览与学习路线
开发语言·python·学习