重新学习MyBatis

本次实践参考官方文档:mybatis-3.4.1.pdf。

2.1 Getting started

public class MyBatisTest {
    /**
     * 1、接口式编程
     *  原生:     Dao     ====>  DaoImpl
     *  mybatis:    Mapper  ====>  xxMapper.xml
     * 
     * 2、SqlSession代表和数据库的一次会话;用完必须关闭;
     * 3、SqlSession和connection一样是非线程安全。每次使用都应该去获取新的对象。
     * 4、mapper接口没有实现类,但是mybatis会为这个接口生成一个代理对象。
     *      (将接口和xml进行绑定)
     *      EmployeeMapper empMapper =  sqlSession.getMapper(EmployeeMapper.class);
     * 5、两个重要的配置文件:
     *      mybatis的全局配置文件:包含数据库连接池信息,事务管理器信息等...系统运行环境信息
     *      sql映射文件:保存了每一个sql语句的映射信息:
     *                  将sql抽取出来。   
     */
    
    @Test
    public void test() throws IOException{
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession openSession = sqlSessionFactory.openSession();
        try{
            Employee employee = openSession.selectOne("com.it.mybatis.EmployeeMapper.selectEmp", 1);
            System.out.println(employee);
        }finally{
            openSession.close();
        }
    }
    
    /**
     * 1、根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象 有数据源一些运行环境信息
     * 2、sql映射文件;配置了每一个sql,以及sql的封装规则等。 
     * 3、将sql映射文件注册在全局配置文件中
     * 4、写代码:
     *      1)、根据全局配置文件得到SqlSessionFactory;
     *      2)、使用sqlSession工厂,获取到sqlSession对象使用他来执行增删改查
     *          一个sqlSession就是代表和数据库的一次会话,用完关闭
     *      3)、使用sql的唯一标志来告诉MyBatis执行哪个sql。sql都是保存在sql映射文件中的。
     */
    
    @Test
    public void test2() throws IOException{
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession openSession = sqlSessionFactory.openSession();
        try{
            EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
            Employee emp = mapper.getEmployeeById(1);
            System.out.println(emp);
        }finally{
            openSession.close();
        }
    }
}

相应dao

public interface EmployeeMapper {
   
    Employee getEmployeeById(Integer id);
}

3.1 Configuration