cai_cf

在不断踩坑中进步

mybatis源码学习之基础工程准备

mybatis源码学习及分析——基础工程准备

转行做Java一年了,基本的框架等会用,但总停留在表面,未能够深入学习。今年需要多多学习主流框架的源码,提高自己的编码水平。

1.Maven依赖

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.1</version>
</dependency>

可选项:slf4j、junit、fastjson、guava等根据自己需要添加。

2.工程结构
这里写图片描述

3.Entity

package com.cumt.mybatisstudy.entity;

import java.util.Date;

/**
 * Created by caicf on 2017/1/3.
 */
public class User {

    private int id;

    private String name;

    private int age;

    private Date createDate;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
}

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="User" type="com.cumt.mybatisstudy.entity.User"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis_study"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="UserMapper.xml"/>
    </mappers>
</configuration>

5.Mapper接口及映射文件

public interface UserMapper {

    User findById(int id);

}

UserMapper.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.cumt.mybatisstudy.mapper.UserMapper">

    <resultMap id="baseRs" type="com.cumt.mybatisstudy.entity.User">
        <id column="id" property="id" javaType="java.lang.Integer"/>
        <id column="name" property="name" javaType="java.lang.String"/>
        <id column="age" property="age" javaType="java.lang.Integer"/>
        <id column="create_date" property="createDate" javaType="java.util.Date"/>
    </resultMap>

    <select id="findById" parameterType="int" resultMap="baseRs">
        select * from `user` where id = #{id}
    </select>
</mapper>

6.创建数据库及数据表

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `create_date` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'as', '12', '2017-01-03 16:30:43');

7.新建测试类用来Debug源码

public class MybatisStart {
    private static final Logger log = LoggerFactory.getLogger(MybatisStart.class);

    @Test
    public void queryTest() throws IOException {
        Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = sqlSessionFactory.openSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        User user = userMapper.findById(1);
        log.info("name= {} ; age= {}", user.getName(), user.getAge());
    }
}

执行queryTest可以看到日志:

这里写图片描述

至此,基本的配置完成,接下来通过断点跟踪源码的执行过程。

附:工程文件地址 http://git.oschina.net/cai_cf/MybatisStudy

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ccf547461296/article/details/53997292
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭