01|简介

MyBatis是一款优秀的基于ORM的半自动轻量级持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。MyBatis可以使用简单的XML或注解来配置和映射原生类型、接口和Java的POJO为数据库中的记录。

02|构建

  • 引入maven依赖

    1. <dependency>
    2. <groupId>org.mybatis</groupId>
    3. <artifactId>mybatis</artifactId>
    4. <version>3.5.5</version>
    5. </dependency>
  • 创建配置文件

    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <!DOCTYPE configuration
    3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
    5. <configuration>
    6. <environments default="development">
    7. <environment id="development">
    8. <transactionManager type="JDBC"/>
    9. <dataSource type="POOLED">
    10. <property name="driver" value="${driver}"/>
    11. <property name="url" value="${url}"/>
    12. <property name="username" value="${username}"/>
    13. <property name="password" value="${password}"/>
    14. </dataSource>
    15. </environment>
    16. </environments>
    17. <mappers>
    18. <mapper resource="mapper/UserMapper.xml"/>
    19. </mappers>
    20. </configuration>
  • 创建Mapper.xml

    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <!DOCTYPE mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="white.colde.mapper.UserMapper">
    6. <select id="selectUser" resultType="white.colde.entity.User">
    7. select * from user where id = #{id}
    8. </select>
    9. </mapper>
  • 创建映射实体

    1. public class User{
    2. private String username;
    3. private String age;
    4. private String sex;
    5. // getter setter ....
    6. }
  • 创建映射器接口

    1. public interface UserMapper{
    2. User selectUser(Integer id)
    3. }
  • 构建使用

    1. public class MapperTest {
    2. @Test
    3. public void mapperTest() throws IOException {
    4. final SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
    5. final SqlSession sqlSession = factory.openSession();
    6. final UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    7. final User user = mapper.selectUser(1);
    8. }
    9. }

    03|Java API

    SqlSessionFactoryBuilder

    用于构建SqlSessionFactory实例,生命周期为构建完即销毁。

    SqlSessionFactory

    用于创建SqlSession实例,生命周期和应用一致。

    SqlSession

    包含了所有执行语句、提交或回滚事务以及获取映射器实例的方法,包含以下几类:

  • 语句执行方法

    1. <T> T selectOne(String statement, Object parameter)
    2. <E> List<E> selectList(String statement, Object parameter)
    3. <T> Cursor<T> selectCursor(String statement, Object parameter)
    4. <K,V> Map<K,V> selectMap(String statement, Object parameter, String mapKey)
    5. int insert(String statement, Object parameter)
    6. int update(String statement, Object parameter)
    7. int delete(String statement, Object parameter)
  • 事务控制方法

    1. void commit()
    2. void commit(boolean force)
    3. void rollback()
    4. void rollback(boolean force)
  • 获取映射器

    1. <T> T getMapper(Class<T> type)