Spring (六):整合Mybatis

本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出一遍就懂!b站搜索狂神说或点击下面链接

https://space.bilibili.com/95256449?spm_id_from=333.788.b_765f7570696e666f.2

 

 

整合Mybatis

步骤

  1. 导入jar包

    • junit

    • Mybatis

    • MySQL

    • Spring

    • AOP

    • Mybatis-Spring

  2. 编写配置文件

  3. 测试

MyBatis-Spring

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

  • 它将允许 MyBatis 参与到 Spring 的事务管理之中,创建映射器 mapper 和 SqlSession 并注入到 bean 中,以及将 Mybatis 的异常转换为 Spring 的 DataAccessException

  • 最终,可以做到应用代码不依赖于 MyBatis,Spring 或 MyBatis-Spring。

方式一:

  1. 编写数据源配置

    • 其实就相当于原来的Mybatis主配置文件加上SqlSessionFactory的功能。

    • 可以导入Mybatis配置文件,启用Mybatis配置的内容,但是数据库链接和SqlSessionFactory必须在这里配置。

    • Mybatis配置的这里也都可以配置,比如别名、设置、Mapper等。

Spring (六):整合Mybatis_第1张图片

 

 



       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       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
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        
        
        
        
    
    
    class="org.mybatis.spring.SqlSessionFactoryBean">
        
        
        
        
        
    class="org.mybatis.spring.SqlSessionTemplate">
        
        
    

 

  • 注:在spring中注册的org.mybatis.spring.SqlSessionTemplate其实就是Mybatis和Spring整合以后的SqlSession,而且是线程安全的。

  • Mybatis.xml


DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

        <package name="com.rzp.pojo"/>
    
​
​

 

 

  1. Mapper接口和实现类、Mapper.xml

  • Mapper接口

package com.rzp.mapper;
​
import com.rzp.pojo.User;
​
import java.util.List;
​
public interface UserMapper {
    public List selectUser();
}
​

 

  • Mapper实现类

package com.rzp.mapper;
​
import com.rzp.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
​
import java.util.List;
​
public class UserMapperImpl implements UserMapper {
    //我们所有的操作,原来使用SqlSession执行,现在改成使用SqlSessionTemplate
    private SqlSessionTemplate sqlSessionT;
    public void setSqlSessionT(SqlSessionTemplate sqlSession){
        this.sqlSessionT = sqlSession;
    }
    @Override
    public List selectUser() {
        UserMapper mapper = sqlSessionT.getMapper(UserMapper.class);
        return mapper.selectUser();
    }
}
​

 

  1. 将实现类注入到Spring

    • 其实也可以放在spring-dao里面,但是现在spring-dao里只有Mybatis相关的,更规范,而且就是一个模板了。

Spring (六):整合Mybatis_第2张图片

 

 



       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       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
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
<import resource="spring-dao.xml"/>
    
    class="com.rzp.mapper.UserMapperImpl">
        
    

 

  • 测试

public class MyTest {
    @Test
    public void test() throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        UserMapper mapper = context.getBean(UserMapper.class);
        List users = mapper.selectUser();
        for (User user : users) {
            System.out.println(user);
        }
    }
}

 

方式二:在实现类中直接创建SqlSession

  • 方式二是一个简化的操作,实现类可以继承SqlSessionDaoSupport,通过父类方法getSqlSession直接生成sqlSession,那么以下两个部分可以省略:

  • xml文件中这部分

class="org.mybatis.spring.SqlSessionTemplate">
    
    

 

  • 实现类的这部分

    private SqlSessionTemplate sqlSessionT;
    public void setSqlSessionT(SqlSessionTemplate sqlSession){
        this.sqlSessionT = sqlSession;
    }

 

示例

  • 实现类

package com.rzp.mapper;
​
import com.rzp.pojo.User;
import org.mybatis.spring.support.SqlSessionDaoSupport;
​
import java.util.List;
​
public class UserMapper2 extends SqlSessionDaoSupport implements UserMapper {
    @Override
    public List selectUser() {
        return getSqlSession().getMapper(UserMapper.class).selectUser();
    }
}
​

 

  • spring注册

    class="com.rzp.mapper.UserMapper2">
        
    

 

Spring中事务管理

  • 在xml文件中导入tx,然后加入以下内容即可



       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd">
​
​
    
    
    
        
        
        
            
            
            
            
            
            
            
        
    
        
        
        
    

 

示例

  • xml



       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd">

    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        
        
        
        
    
    
    class="org.mybatis.spring.SqlSessionFactoryBean">
        
        
        
        
        
    class="org.mybatis.spring.SqlSessionTemplate">
        
        
    
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
    
    
    
        
        
        
            
            
            
            
            
            
            
        
    
        
        
    

 

  • 接口实现

package com.rzp.mapper;
​
import com.rzp.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
​
import java.util.List;
​
public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper {
​
    @Override
    public List selectUser() {
        User user = new User(4, "rzp222", "123123");
        UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
        mapper.insertUser(user);
        //执行删除,因为删除语句是错的,就可以根据上面的insert是否执行成功来查看事务是否有开启
        mapper.deleteUser(6);
​
        return  mapper.selectUser();
    }
​
    @Override
    public void insertUser(User user) {
        getSqlSession().getMapper(UserMapper.class).insertUser(user);
    }
​
    @Override
    public void deleteUser(int id) {
        getSqlSession().getMapper(UserMapper.class).deleteUser(id);
    }
}

 


  • Mapper.xml


DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
        insert into user (id,name,pwd) values (#{id},#{name},#{pwd})
    
    
    
    
        deletes from user where id = #{id}
    

 

 

 

  • 其他不变,测试:

import com.rzp.mapper.UserMapper;
import com.rzp.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
​
import java.util.List;
​
public class Mytest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
        List users = userMapper.selectUser();
        for (User user : users) {
            System.out.println(user);
        }
    }
}
​

 

  • 执行selectUser的时候,会先执行insert,再删除另一个记录。

  • 因为删除的语句是错的,只要看insert的记录是否存在就可以知道事务是否开启了

Spring (六):整合Mybatis_第3张图片

 

 

 

你可能感兴趣的:(Spring (六):整合Mybatis)