12 整合MyBatis

步骤:

  1. 导入相关jar包
    • junit
    • mybatis
    • mysql 数据库
    • spring 相关
    • aop 织入
    • mybatis-spring
    
        
        
            org.springframework
            spring-webmvc
            5.3.14
        

        
        
            org.springframework
            spring-jdbc
            5.3.14
        

        
            junit
            junit
            4.12
        

        
        
            org.aspectj
            aspectjweaver
            1.9.7
            runtime
        

        
            mysql
            mysql-connector-java
            5.1.44
        

        
            org.mybatis
            mybatis
            3.5.2
        

        
            org.aspectj
            aspectjweaver
            1.9.7
        

        
            org.mybatis
            mybatis-spring
            2.0.2
        
    
  1. 编写配置文件
  2. 测试

12.1 回忆mybatis

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



    
        
    

    
    

    
        
            
            
                
                
                
                
            
        
    
    
    
        
    

  1. 编写接口
import com.hunter.pojo.User;
import java.util.List;
public interface UserMapper {
    public List selectUser();
}
  1. 编写Mapper.xml



    

  1. 测试
public class MyTest {
    @Test
    public void test() throws IOException {
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List userList = mapper.selectUser();
        for (User user : userList){
            System.out.println(user);
        }
    }
}

12.2 Mybatis-Spring

  1. 编写数据源/SqlSessionFactory/SqlSessionTemplate


    
    
        
        
        
        
    

    
    
        
        
        
        
    
    
    
        
        
    

  1. 需要给接口加实现类
import com.hunter.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;

public class UserMapperImpl implements UserMapper{
    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }

    // 我们的所有操作都使用SqlSessionTemplate
    private SqlSessionTemplate sqlSessionTemplate;

    @Override
    public List selectUser() {
        UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
        return mapper.selectUser();
    }
}
  1. 将自己写的实现类注入到Spring中,测试使用



    

    
        
    

import com.hunter.mapper.UserMapper;
import com.hunter.mapper.UserMapperImpl;
import com.hunter.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MyTest {
    @Test
    public void test() throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
        for (User user: userMapper.selectUser()){
            System.out.println(user);
        }
    }
}

你可能感兴趣的:(12 整合MyBatis)