Mybaits 源码解析 (一)----- 搭建一个mybatis…

2019-10-28 06:32:17来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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

源码分析之前先搭一个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 {
        Employee 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我们已经搭建完了,后面的源码分析文章我们将以这个为基础来分析

 


原文链接:https://www.cnblogs.com/java-chen-hao/p/11743516.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Spring Boot 2 + jpa + mysql例子

下一篇:SpringBoot生命周期管理之停掉应用服务几种方法