Spring-12-spring整合Mybatis

12. 整合Mybatis

步骤:

  1. 导入相关jar包
    • junit
    • mybatis
    • mysql数据库
    • spring相关的
    • aop织入
    • mybatis-spring【新知识点】

    
        junit
        junit
        4.12
    
    
        mysql
        mysql-connector-java
        5.1.47
    
    
        org.mybatis
        mybatis
        3.5.2
    
    
        org.springframework
        spring-webmvc
        5.1.9.RELEASE
    
    
    
    
        org.springframework
        spring-jdbc
        5.1.9.RELEASE
    
    
        org.aspectj
        aspectjweaver
        1.8.13
    
    
    
        org.mybatis
        mybatis-spring
        2.0.2
    
    

    org.projectlombok
    lombok
    1.16.10
    provided

 
  1. 编写配置文件

  2. 测试

12.1 回忆Mybatis

  1. 编写实体类
@Data
public class User {
    private int id;
    private String name;
    private String pwd;
}
  1. 编写核心配置类




    

    
    
        
        
    
    
    
        
            
            
                
                
                
                
            
        
    

    
        
    




db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username=huba
password=123456
  1. 编写接口
public interface UserMapper {
    public List selectUser();
}
  1. 编写mapper.xml



    


  1. 测试
@Test
public void test1() throws IOException {
    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 userList = mapper.selectUser();

    for (User user : userList) {
        System.out.println(user);
    }
}

解决资源过滤问题:(pom.xml)


    
        
            src/main/java
            
                **/*.properties
                **/*.xml
            
            false
        
        
            src/main/resources
            
                **/*.properties
                **/*.xml
            
            false
        
    

12.2 Mybatis-Spring

整合方式一:SqlSessionTemplate

  1. 编写数据源配置

    
        
        
        
        
    
    
  2. sqlSessionFactory

      
    
        
        
        
        
    
    
  3. sqlSessionTemplate

    
    
        
        
    
    
  4. 需要给接口加实现类

    public class UserMapperImpl implements UserMapper{
    
        //以前,所有的操作都使用sqlSession来执行;现在,由sqlSessionTemplate执行
        private SqlSessionTemplate sqlSession;
    
        public void setSqlSession(SqlSessionTemplate sqlSession) {
            this.sqlSession = sqlSession;
        }
    
        public List selectUser() {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            return mapper.selectUser();
    
        }
    }
    
  5. 将实现类注入到spring中

    
        
    
    
  6. 测试使用即可

    @Test
    public void test2(){
        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);
        }
    }
    

整合方式二: SqlSessionDaoSupport

SqlSessionDaoSupport是一个抽象的支持类,可以调用getSqlSession()得到一个SqlSessionTemplate。

实现类(继承DaoSupport):

public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
    public List selectUser() {
        /*SqlSession sqlSession = getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.selectUser();*/
        return getSqlSession().getMapper(UserMapper.class).selectUser();
    }
}

配置bean(只需要注入sqlSessionFactory):


    

你可能感兴趣的:(Spring-12-spring整合Mybatis)