Mybaits 源码解析 (一)----- 搭建一个mybatis框架(MyBatis HelloWorld)

2022-10-14,,,,

源码分析之前先搭一个mybatis的demo,这个在看源码的时候能起到了很大的作用,因为在看源码的时候,会恍然大悟,为什么要这么配置,为什么要这么写。(老鸟可以跳过这篇)

开发环境的准备

创建maven项目

pom.xml

<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
         xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelversion>4.0.0</modelversion>

    <groupid>com.mybatis.chenhao</groupid>
    <artifactid>mybatisdemo</artifactid>
    <version>1.0-snapshot</version>

    <properties>
        <!-- mybatis版本号 -->
        <mybatis.version>3.4.2</mybatis.version>
    </properties>
    <dependencies>

        <!--mybatis依赖 -->
        <dependency>
            <groupid>org.mybatis</groupid>
            <artifactid>mybatis</artifactid>
            <version>${mybatis.version}</version>
        </dependency>
        <!-- mysql驱动包 -->
        <dependency>
            <groupid>mysql</groupid>
            <artifactid>mysql-connector-java</artifactid>
            <version>5.1.44</version>
        </dependency>

    </dependencies>

</project>

创建mybatis的配置文件

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"></properties>
    <environments default="default">
        <environment id="default">
            <transactionmanager type="jdbc"></transactionmanager>
            <datasource type="pooled">
                <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>

    <mappers>
        <mapper class="mapper.demomapper"></mapper>
    </mappers>
</configuration>

db.properties

jdbc.driver=com.mysql.jdbc.driver
jdbc.url=jdbc:mysql://localhost:3306/demo?useunicode=true&characterencoding=utf8
jdbc.username=chenhao
jdbc.password=123456

entity和mapper

employee

package entity;

/***
 *
 *@author chenhao
 *@description:
 *@date: created in 14:58 2019/10/26
 *@modified by:
 *
 */
public class employee {
    int id;
    string name;

    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;
    }

    @override
    public string tostring() {
        return "employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

employeemapper

package mapper;

import entity.employee;
import java.util.list;

/***
 *
 *@author chenhao
 *@description:
 *@date: created in 14:58 2019/10/26
 *@modified by:
 *
 */
public interface employeemapper {
    list<employee> getall();
}

employeemapper.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="mapper.employeemapper">

    <resultmap id="basemap" type="entity.employee">
        <result property="id" column="id" jdbctype="integer"></result>
        <result property="name" column="name" jdbctype="varchar"></result>

    </resultmap>
    <select id="getall" resultmap="basemap">
        select * from employee
    </select>
</mapper>

测试

public static void main(string[] args) throws ioexception {
    string resource = "mybatis-config.xml";
    inputstream inputstream = resources.getresourceasstream(resource);
    sqlsessionfactory sqlsessionfactory = new sqlsessionfactorybuilder().build(inputstream);
    sqlsession sqlsession = sqlsessionfactory.opensession();
    try {
         employeemapper employeemapper = sqlsession.getmapper(employee.class);
         list<employee> all = employeemapper.getall();
         for (employee item : all)
            system.out.println(item);
    } finally {
        sqlsession.close();
    }
}

测试结果:

employee{id=1, name='name1'}
employee{id=2, name='name2'}
employee{id=3, name='name3'}

好了,mybatis helloworld我们已经搭建完了,后面的源码分析文章我们将以这个为基础来分析

 

《Mybaits 源码解析 (一)----- 搭建一个mybatis框架(MyBatis HelloWorld).doc》

下载本文的Word格式文档,以方便收藏与打印。