前言
什么是Mybatis?维基百科是这样定义的:Mybatis是一个Java持久化框架,它通过XML描述符或注解把对象与储存过程或SQL语句关联起来。通俗点说就是Mybatis是对JPA(Java持久化API,一个Java应用程序接口规范)的实现,是为了简化数据库操作过程而衍生出来的产品。
关于Mybatis的教程,推荐到官方文档看:Mybatis官方文档
项目结构
添加依赖包
本文中用到的Jar依赖包有:Mybatis包、Mysql驱动包。
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<?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>ddnd</groupId>
<artifactId>ddnd</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
</dependencies>
</project>
创建Mybatis配置文件
新建Mybatis-config.xml文件,完成对Mybatis的配置。其中cn/ddnd/www/Entity/User.xml
是包含要映射的类的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<?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>
<typeAliases>
<typeAlias type="cn.ddnd.www.Entity.User" alias="User"></typeAlias>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/spring?serverTimezone=GMT%2B8" />
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="cn/ddnd/www/Entity/User.xml"></mapper>
</mappers>
</configuration>
创建数据库和数据表
本文中使用Mysql,为了演示方便,仅创建个简单的数据库spring,还有数据表user。
数据表创建SQL代码。1
2
3
4
5CREATE TABLE `user` (
`email` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
新建数据表对应的实体类
为了实现与数据库中的user表进行关系映射,这里新建一个User类。
User.java: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
38package cn.ddnd.www.Entity;
public class User {
String email;
String name;
String password;
User(String email, String name, String password){
this.email = email;
this.name = name;
this.password = password;
}
public void setPassword(String password) {
this.password = password;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
}
新建User类的对应映射层
在User类的路径下新建User.xml完成对对象的映射。resultType="User"
中的User
为Mybatis配置文件中的<typeAlias type="cn.ddnd.www.Entity.User" alias="User"></typeAlias>
所指定的User
别名。1
2
3
4
5
6
7
8
9<?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="cn.ddnd.www.Entity.User">
<select id="getUser" parameterType="String" resultType="User">
select * from user where email = #{email}
</select>
</mapper>
测试类
新建一个测试类,查找邮箱是xue8@qq.com的用户信息。
test.java: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
37import cn.ddnd.www.Entity.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.apache.ibatis.io.Resources;
import sun.awt.geom.AreaOp;
import java.io.Reader;
import java.io.IOException;
public class test {
private static Reader reader;
private static SqlSessionFactory sqlSessionFactory;
static{
try{
reader = Resources.getResourceAsReader("Mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}catch (IOException e){
e.printStackTrace();
}
}
@Test
public void a() throws IOException {
SqlSession sqlSession = sqlSessionFactory.openSession();
try{
User user = (User) sqlSession.selectOne("cn.ddnd.www.Entity.User.getUser", "xue8@qq.com");
System.out.println("用户的邮箱是:" + user.getEmail() + ",用户的名称是:" + user.getName() + ",用户的密码是:" + user.getPassword());
}finally {
sqlSession.close();
}
}
}
数据表信息:
测试结果: