解密Mybatis,手写Mybatis框架(一)

Mybatis快速入门

步骤

  • 1.加入mybatis的依赖
  • 2.添加Mybatis的配置文件
  • 3.场景介绍
  • 4.编写实体类丶mapper接口及mapper.xml文件
  • 5.编写测试代码

demo:

public class TUser {
    private Integer id;

    private String userName;

    private String realName;

    private Byte sex;

    private String mobile;

    private String email;

    private String note;

    private Integer positionId;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public Byte getSex() {
        return sex;
    }

    public void setSex(Byte sex) {
        this.sex = sex;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public Integer getPositionId() {
        return positionId;
    }

    public void setPositionId(Integer positionId) {
        this.positionId = positionId;
    }
      
}

mapper接口:

public interface TUserMapper {
    
    TUser selectByPrimaryKey(Integer id);
    
}

TUserMapper.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.enjoylearning.mybatis.mapper.TUserMapper">

    <select id="selectByPrimaryKey" resultType="com.enjoylearning.mybatis.entity.TUser"    parameterType="java.lang.Integer">
        select
            id, 
            user_name , 
            real_name , 
            sex, 
            mobile, 
            email, 
            note,
            position_id positionId
        from t_user
        where id = #{id}
    </select>

</mapper>

mybatis-config.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <properties resource="db.properties" />

    <settings>
        <!-- 全局开关-->

        <!-- 设置自动驼峰转换-->
        <setting name="mapUnderscoreToCamelCase" value="true" />

        <!-- 开启懒加载-->

        <setting name="lazyLoadingEnabled" value="false"/>
        
    
    </settings>
    <!--别名定义 -->
    <typeAliases>
    <package name="com.enjoylearning.mybatis.entity"/>
</typeAliases>

    <!--配置environment环境 -->
    <environments default="development">
        <!-- 环境配置1,每个SqlSessionFactory对应一个环境 -->
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="UNPOOLED">
                <property name="driver" value="${jdbc_driver}" />
                <property name="url" value="${jdbc_url}" />
                <property name="username" value="${jdbc_username}" />
                <property name="password" value="${jdbc_password}" />
            </dataSource>
        </environment>

    </environments>

    <!-- 映射文件,mapper的配置文件 -->
<!--     <mappers>
        直接映射到相应的mapper文件
        <mapper resource="sqlmapper/TUserMapper.xml" />
    </mappers> -->

    <mappers>
        <mapper class="com.enjoylearning.mybatis.mapper.TUserMapper" />
    </mappers>


</configuration>

db.properties:

jdbc_driver=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
jdbc_username=root
jdbc_password=qq997921

测试类:

public class MybatisQuickStart {

private SqlSessionFactory sqlSessionFactory;



@Test
// 快速入门
public void quickStart() throws IOException {
    //-------------第一阶段-------------
    // 1.读取mybatis配置文件创SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    inputStream.close();
    //-------------第二阶段-------------
    // 2.获取sqlSession
    SqlSession sqlSession = sqlSessionFactory.openSession();
    // 3.获取对应mapper
    TUserMapper mapper = sqlSession.getMapper(TUserMapper.class);

    //-------------第三阶段-------------
    // 4.执行查询语句并返回结果
    TUser user = mapper.selectByPrimaryKey(1);
    System.out.println(user.toString());

}

}

Mybatis两种编程模型

1.封装了iBatis编程模型,使用sqlSession对外提供数据库的访问
比如使用sqlSession直接访问数据库,代码如:

User user1 = session.selectOne("com.XX.XX.XXMapper.selectOneByKey","1");

2.使用mapper接口编程,就可以访问数据库

UserMapper userMapper = session.getMapper(UserMapper.class);
User user = userMapper.selectByPrimaryKey(1);

对比两种模型
第一种缺点:代码里面会很多硬编码,硬编码不好维护,代码不优雅,并且selectOne方法名称没有业务含义.

优先选择mapper接口编程。

Mybatis核心流程三大阶段

解密Mybatis,手写Mybatis框架(一)

缕清思路也就是Mybatis核心流程之后,我们就开始写代码了,详见下节~~

相关推荐