wueasy框架之MyBatis使用

使用wueasy框架过程中,怎么使用MyBatis对数据库进行操作?

引入数据库客户端工具

1
2
3
4
5
<dependency>
<groupId>com.wueasy</groupId>
<artifactId>wueasy-database-client</artifactId>
<version>2.0.0</version>
</dependency>

新增数据库映射

编写数据库表结构映射实体bean。

为了简化自定义sql,这里使用的是通用Mapper,部分属性需要使用注解方式,注解可参考以下api文档https://github.com/abel533/Mapper/wiki/2.2-mapping

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.wueasy.demo.entity;

import java.io.Serializable;
import javax.persistence.Id;
import javax.persistence.Table;
import tk.mybatis.mapper.annotation.KeySql;

@Table(name = "user")
public class User implements Serializable {

private static final long serialVersionUID = 1L;

/**
* 主键id
*/
@Id
@KeySql(useGeneratedKeys = true)
private Long id;

/**
* 姓名
*/
private String name;

/**
* 年龄
*/
private Integer age;

/**
* 邮箱
*/
private String email;

/**
* 主键id
*/
public Long getId() {
return id;
}

/**
* 主键id
*/
public void setId(Long id) {
this.id = id;
}

/**
* 姓名
*/
public String getName() {
return name;
}

/**
* 姓名
*/
public void setName(String name) {
this.name = name;
}

/**
* 年龄
*/
public Integer getAge() {
return age;
}

/**
* 年龄
*/
public void setAge(Integer age) {
this.age = age;
}

/**
* 邮箱
*/
public String getEmail() {
return email;
}

/**
* 邮箱
*/
public void setEmail(String email) {
this.email = email;
}

}

新增mapper接口

接口需要继承通用Mapper接口,Mapper接口提供了常见的增删改查方法,可以直接使用。

1
2
3
4
5
6
7
8
package com.wueasy.demo.mapper;

import com.wueasy.demo.entity.User;
import tk.mybatis.mapper.common.Mapper;

public interface UserMapper extends Mapper<User> {

}

自定义sql

对于一些特殊的需求或者复杂的sql,通用mapper接口无法满足时,我们可以通过自定义sql方式实现。

mapper接口中新增一个查询方法

1
2
3
4
5
6

public interface UserMapper extends Mapper<User> {

List<User> selectByName(@Param("name")String name);
}

创建mapper.xml文件

  • 首先创建一个mapper.xml文件,对应mapper接口。

mapper.xml文件内容如下,和普通mybatis文件一样。

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
<?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.wueasy.demo.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.wueasy.demo.entity.User">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="age" property="age" />
<result column="email" property="email" />
</resultMap>

<sql id="Base_Column_List">
id
,name
,age
,email
</sql>

<select id="selectByName" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user where name = #{name}
order by id desc
</select>

</mapper>

数据库连接配置

配置数据库连接信息和mybatis扫描信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
wueasy:
datasource :
demo :
mybatis:
basePackage: com.wueasy.demo.mapper
mapperLocations: classpath*:mybatis/wueasy/demo/*.xml
connection:
url : jdbc:mysql://localhost:3306/easy_data?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf-8
username : easyadmin
password : easyadmin
#连接池配置
initialSize : 10 #初始化大小
minIdle : 10 #最小连接
maxActive : 20 #最大连接
maxWait : 60000 #配置获取连接等待超时的时间
timeBetweenEvictionRunsMillis : 2000 #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
minEvictableIdleTimeMillis : 600000 #配置一个连接在池中最小生存的时间,单位是毫秒
maxEvictableIdleTimeMillis : 600000 #配置一个连接在池中最大生存的时间,单位是毫秒
validationQuery : SELECT 1 from dual
testWhileIdle : true
testOnBorrow : false
testOnReturn : false
keepAlive : true

测试接口

使用junit测试接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=Application.class)
public class DbTests {

@Autowired
private UserMapper userMapper;

@Test
public void testObj() {

try{
//查询全部
System.err.println(JsonHelper.toJSONString(userMapper.selectAll()));

}catch(Exception e) {
e.printStackTrace();
}

}
}