正文:

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

1. 开发环境的准备

1.1 创建maven项目

pom.xml

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

1.2 创建mybatis的配置文件

mybatis-config.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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

1
2
3
4
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8
jdbc.username=chenhao
jdbc.password=123456

1.3 entity和mapper

Employee

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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>

2. 测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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(EmployeeMapper.class);
List<Employee> all = employeeMapper.getAll();
for (Employee item : all)
System.out.println(item);
} finally {
sqlSession.close();
}
}

测试结果:

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

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


本站由 卡卡龙 使用 Stellar 1.29.1主题创建

本站访问量 次. 本文阅读量 次.