整合Mybatis

步骤:
1.导入相关jar包

  • junit
  • mybatis
  • mysql数据库
  • spring相关的
  • aop织入
  • mybatis-spring

1、新建一个maven项目


    
    
        junit
        junit
        4.12
    
    
    
    
        mysql
        mysql-connerctor-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
    
    


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

2.编写配置文件

3.测试

1.1、回忆Mybatis

1.编写实体类

package com.jialidun.pojo;

import lombok .Data;

@Data
public class User{
    private int id;
    private String name;
    private String pwd;
}

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





    
    
        
    
    
        
            
            
              
              
              
              
            
        
    


    
        
    

3.编写接口

package com.jialidun.mapper;

public interface UserMapper{
    public List selectUser();

}

4.编写Mapper.xml





    

5.测试

public class TestDemo{
    @Test
    public void test01{
        String resources="mybatis-config.xml";
        InputStream in = Resources.getResourceAsStream(resources);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sessionFactory.openSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List userList = mapper.selectUser();

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

}

你可能感兴趣的:(mybatis)