美文网首页
mybatis源码学习整理

mybatis源码学习整理

作者: 瑜骐 | 来源:发表于2018-05-13 20:46 被阅读0次

    1. 整体类图

    mybatis整体关键类图

    2. 整体流程

    2.1 代码

    2.1.1 main类

    public static void main(String[] args) {
            String resource = "mybatis-config.xml";
            InputStream inputStream =  Resources.getResourceAsStream(resource);
            SqlSession sqlSession  = new SqlSessionFactoryBuilder().build(inputStream);
            RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
            Role role = new Role();
            role.setRoleName("testName");
            role.setNote("testNote");
            roleMapper.insertRole(role);
            roleMapper.deleteRole(1L);
            sqlSession.commit();
        }
    

    2.1.2 RoleMapper接口

    public interface RoleMapper {
        public Role getRole(Long id);
        public int deleteRole(Long id);
        public int insertRole(Role role);
    }
    

    2.1.3 Role类(省去getter和setter方法)

    public class Role {
        private Long id;
        private String roleName;
        private String note;
      // 省去getter和setter方法
    }
    

    2.1.4 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>
        <typeAliases>
            <typeAlias alias="role" type="com.yjk.mybatis.Role"/>
        </typeAliases>
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC">
                    <property name="autoCommit" value="false"/>
                </transactionManager>
                <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://localhost:3306/myfirstdb?characterEncoding=utf8&amp;useSSL=false"/>
                    <property name="username" value="root"/>
                    <property name="password" value="12345678"/>
                </dataSource>
            </environment>
        </environments>
        <mappers>
            <mapper resource="roleMapper.xml"/>
        </mappers>
    </configuration>
    

    2.1.5 roleMapper.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.yjk.mybatis.RoleMapper">
        <select id="getRole" parameterType="long" resultType="role">
            select id,role_name as roleName,note from t_role where id = #{id}
        </select>
        <insert id="insertRole" parameterType="role">
            insert into t_role(role_name,note) values (#{roleName},#{note})
        </insert>
        <delete id="deleteRole" parameterType="long">
            delete from t_role where id = #{id}
        </delete>
    </mapper>
    

    2.2 构建sqlSession和getMapper方法对应的流程

    构建sqlSession和getMapper方法对应的流程

    2.3 对应Mapper接口中的方法执行流程

    对应Mapper接口中方法执行流程

    参考

    mybatis源码:https://github.com/mybatis/mybatis-3.git

    相关文章

      网友评论

          本文标题:mybatis源码学习整理

          本文链接:https://www.haomeiwen.com/subject/vvoglftx.html