本文主要搭建一个简单的项目,用于后面的源码探究,熟悉mybatis的可以略过此章节。

数据库表创建

创建一张表 dept

CREATE TABLE `dept` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `dept_name` varchar(255) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

插入几条数据:

INSERT INTO dept ( dept_name, address )
VALUES
    ( '开发部', '北京' ),
    ( '测试部', '上海' ),
    ( '财务部', '深圳' ),
    ( '运营部', '西安' ),
    ( '后勤部', '广州' );

创建一个maven工程

<?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.yefengyu.mybatis</groupId>  
    <artifactId>mybatis</artifactId>  
    <version>1.0-SNAPSHOT</version>  
  
    <dependencies>  
        <dependency>  
            <groupId>org.mybatis</groupId>  
            <artifactId>mybatis</artifactId>  
            <version>3.5.3</version>  
        </dependency>  
        <dependency>  
            <groupId>mysql</groupId>  
            <artifactId>mysql-connector-java</artifactId>  
            <version>5.1.46</version>  
        </dependency>  
    </dependencies>  
  
</project>

添加 mybatis 依赖,本次使用目前最新的 3.5.3 版本。

新建实体 Dept 和数据库表对应

package com.yefengyu.mybatis;  
  
public class Dept {  
    private Long id;  
    private String deptName;  
    private String address;  
  
    public Long getId() {  
        return id;  
    }  
  
    public void setId(Long id) {  
        this.id = id;  
    }  
  
    public String getDeptName() {  
        return deptName;  
    }  
  
    public void setDeptName(String deptName) {  
        this.deptName = deptName;  
    }  
  
    public String getAddress() {  
        return address;  
    }  
  
    public void setAddress(String address) {  
        this.address = address;  
    }  
  
    @Override  
    public String toString() {  
        return "Dept{" +  
                "id=" + id +  
                ", deptName='" + deptName + '\'' +  
                ", address='" + address + '\'' +  
                '}';  
    }
}

新建接口

package com.yefengyu.mybatis;  
  
import java.util.List;  
  
public interface DeptMapper {  
    List<Dept> getAllDept();  
}

接口实现

<?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.yefengyu.mybatis.DeptMapper">  
    <select id="getAllDept" resultType="com.yefengyu.mybatis.Dept">  
        select id, dept_name deptName, address  
        from dept  
    </select>  
</mapper>

全局配置文件

<?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>  
    <environments default="default">  
        <environment id="default">  
            <transactionManager type="JDBC"/>  
            <dataSource type="POOLED">  
                <property name="driver" value="com.mysql.jdbc.Driver"/>  
                <property name="url" value="jdbc:mysql://localhost:3306/yefengyu?useUnicode=true"/>  
                <property name="username" value="root"/>  
                <property name="password" value="000000"/>  
            </dataSource>  
        </environment>  
    </environments>  
  
    <mappers>  
        <mapper resource="mappers/DeptMapper.xml"/>  
    </mappers>  
</configuration>

测试

package com.yefengyu.mybatis;  
  
import org.apache.ibatis.io.Resources;  
import org.apache.ibatis.session.SqlSession;  
import org.apache.ibatis.session.SqlSessionFactory;  
import org.apache.ibatis.session.SqlSessionFactoryBuilder;  
  
import java.io.IOException;  
import java.io.InputStream;  
import java.util.List;  
  
public class Main {  
    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 {  
            DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class);  
            List<Dept> deptList = deptMapper.getAllDept();  
            System.out.println(deptList);  
        } finally {  
            sqlSession.close();  
        }  
    }  
}

结果:

[Dept{id=1, deptName='开发部', address='北京'}, Dept{id=2, deptName='测试部', address='上海'}, Dept{id=3, deptName='财务部', address='深圳'}, Dept{id=4, deptName='运营部', address='西安'}, Dept{id=5, deptName='后勤部', address='广州'}]

本次环境搭建到此为止,后面将深入源码分析mybatis.


指尖改变世界
27 声望6 粉丝

« 上一篇
Java NIO 缓冲区
下一篇 »
Thrift入门