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

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

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

1.Maven依赖

<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
    <version>5.1.39version>
dependency>
<dependency>
    <groupId>org.mybatisgroupId>
    <artifactId>mybatisartifactId>
    <version>3.4.1version>
dependency>

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

2.工程结构
mybatis源码学习之基础工程准备_第1张图片

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




<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



<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可以看到日志:

mybatis源码学习之基础工程准备_第2张图片

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

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

你可能感兴趣的:(框架学习,mybatis源码,mybatis,java,mysql,源码,框架)