02-MyBatis-环境搭建

MyBatis 源码

MyBatis官方文档
源码下载地址(推荐使用码云中转下载)
mybatis-parent项目下载地址

  1. 选择自己所需要的MyBatis版本下载
  1. 查看pom文件中所对应的mybatis-parent项目版本
  1. 根据tag下载所需要的项目包。
  2. 导入idea,选择jdk进行编译。
  3. 新建一个maven模块

pom文件引入依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<dependencies>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!-- jdbc的包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<!-- 查看日志文件的log4j包 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<!-- 测试用的依赖包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.21.0-GA</version>
</dependency>
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
  1. 项目结构
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.mybatis.pojo;

/**
* @author qcc
* @Package com.mybatis.pojo
* @Description: TODO
* @date
*/
public class User {
private Integer id;

private String username;

private String password;

private String context;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getContext() {
return context;
}

public void setContext(String context) {
this.context = context;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", context='" + context + '\'' +
'}';
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

package com.mybatis.utils;

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

/**
* @author qcc
* @Package com.mybatis.utils
* @Description: TODO
* @date
*/
public class MybatisUtil {
private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();
private static SqlSessionFactory sqlSessionFactory;

static {
try {
// 读取mybatis核心配置文件
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
// 创建会话工厂
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static SqlSession getSession() {
// 获取会话
SqlSession sqlSession = threadLocal.get();
if (sqlSession == null) {
// 从会话工厂获取session
sqlSession = sqlSessionFactory.openSession();
// 绑定会话工厂
threadLocal.set(sqlSession);
}
return sqlSession;
}

public static void close() {
SqlSession sqlSession = threadLocal.get();
if (sqlSession != null) {
sqlSession.close();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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" >
<!-- namespace命名必须是唯一的,前期可以直接以所映射的pojo类的唯一路径来命名 -->
<mapper namespace="com.mybatis.pojo.User">
<!--
mybaits通过自己手动写sql语句来对数据库进行操作,select就是查询操作
同样有update,delete,insert
id:标识符,测试时调用的标识符来调用sql语句,相当于方法名
resultType:返回值类型
parameterType:传入的参数类型
如果是引用数据类型的话,需要传入完整的路径.如:java.lang.String以及对象com.mybatis.pojo.User
在下方的条件需要以#{}来占位,如果传入类型是对象型,需要与对象的属性名一致
-->
<select id="findStudentById" resultType="com.mybatis.pojo.User" parameterType="int">
select * from user where id=#{id}
</select>
<select id="findAll" resultType="com.mybatis.pojo.User">
select * from user
</select>
</mapper>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?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>
<!-- 接收数据库的数据源(元数据) default默认读取以下的配置 -->
<environments default="mysql">
<!-- 针对不同的数据库来配置元数据 id:区分元数据 -->
<!-- 配置MySQL -->
<environment id="mysql">
<!-- 事务的管理交给jdbc管理 -->
<transactionManager type="jdbc"/>
<!-- 使用properties加载properties中的属性 -->
<dataSource type="pooled">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>

<!-- 配置Oracle -->
<!-- <environment id="oracle">-->
<!-- &lt;!&ndash; 事务的管理交给odbc管理 &ndash;&gt;-->
<!-- <transactionManager type="jdbc"/>-->
<!-- <dataSource type="pooled">-->
<!-- <property name="driver" value="com.mysql.jdbc.Driver"/>-->
<!-- <property name="url" value="jdbc:oracle:thin:@localhost:1521/test"/>-->
<!-- <property name="username" value="scott"/>-->
<!-- <property name="password" value="123456"/>-->
<!-- </dataSource>-->
<!-- </environment>-->
</environments>
<mappers>
<mapper resource="mapper/userMapper.xml"/>
</mappers>
</configuration>
1
2
3
4
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/a_test
username=root
password=root
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
Navicat Premium Data Transfer

Source Server : localhost
Source Server Type : MySQL
Source Server Version : 50729
Source Host : localhost:3306
Source Schema : a_test

Target Server Type : MySQL
Target Server Version : 50729
File Encoding : 65001

Date: 18/05/2020 23:16:53
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` bigint(32) NOT NULL,
`username` varchar(255) DEFAULT NULL,
`password` varchar(32) DEFAULT NULL,
`context` varchar(1000) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

SET FOREIGN_KEY_CHECKS = 1;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.mybatis.test;

import com.mybatis.pojo.User;
import com.mybatis.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
* @author qcc
* @Package com.mybatis.test
* @Description: TODO
* @date
*/
public class TestMybatis01 {
private SqlSession session = null;

@Before
public void before() {
//获取会话
session = MybatisUtil.getSession();
}

@Test
public void select() {
/**
* 从会话中调用映射文件中的sql语句,为了防止不同的映射文件标识符重复,通过namespace.标识符来调用
* 如果后面有参数的话,在后面添加参数
*/
User user = session.selectOne("com.mybatis.pojo.User.findStudentById",1);
System.out.println(user);
List<User> studentList = session.selectList("com.mybatis.pojo.User.findAll");
for (User student2 : studentList) {
System.out.println(student2);
}
}

@After
public void after() {
session.commit();
MybatisUtil.close();
}
}

  1. 运行TestMybatis01中的select(),结果如图所示

至此,mybatis源码阅读项目建立成功。


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!