mybatis源码分析-环境搭建

本文主要搭建一个简单的项目,用于后面的源码探究,熟悉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工程

  
  
    4.0.0  
  
    com.yefengyu.mybatis  
    mybatis  
    1.0-SNAPSHOT  
  
      
          
            org.mybatis  
            mybatis  
            3.5.3  
          
          
            mysql  
            mysql-connector-java  
            5.1.46  
          
      
  

添加 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 getAllDept();  
}

接口实现

  
  
  
      

全局配置文件

  
  
  
      
          
              
              
                  
                  
                  
                  
              
          
      
  
      
          
      

测试

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 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.

你可能感兴趣的:(mybatis,源码分析)