前文说了SpringBoot的使用Jpa操作数据库,今天要说是SpringBoot集目前比较受欢迎的持久层框架Mybatis,我个人对mybatis是比较喜欢的,接下来我们在SpringBoot中集成它,我们依旧使用mysql做例子,编写一个简单的用户模块的CRUD的例子。

项目依赖包的引入

1
2
3
4
5
<dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
     <version>1.2.0</version>
</dependency>

配置文件写入配置

打开项目下的application.yml配置文件,添加以下配置:
mybatis:
type-aliases-package: com.maxbill.web.model
mapper-locations: classpath:com/maxbill/web/mapper/*.xml
注意:type-aliases-package配置别名扫描包, mapper-locations配置mapper配置文件

设计数据库表结构

使用Mybatis-Generator生成相应代码

我们使用mybatis-generator插件来生成dao层的mapper文件和用户model类;mybatis-generator使用有三种方式:1.命令行,2.eclipse插件,3.maven插件;我们的工程是使用maven构建的,所以我们使用四三种方式:

在pom中引入mybatis-generator依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!--mybatis-generator插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>

修改插件的配置文件

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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 数据库驱动包位置 -->
<classPathEntry location="D:\Java\Javafiles\JAR\mysql-connector-java-5.1.7-bin.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>

<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/blog"
userId="root"
password="admin">
</jdbcConnection>

<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>

<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="com.maxbill.web.model" targetProject="d:\Data">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>

<!-- 生成的映射文件包名和位置 -->
<sqlMapGenerator targetPackage="com.maxbill.web.mapper" targetProject="d:\Data">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>

<!-- 生成DAO的包名和位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.maxbill.web.mapper"
targetProject="d:\Data">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>

<table tableName="s_user" domainObjectName="User" enableCountByExample="true"
enableUpdateByExample="true"
enableDeleteByExample="true"
enableSelectByExample="true"
selectByExampleQueryId="true"/>
</context>
</generatorConfiguration>

使用maven命令生成代码


看到以下的输出日志,编译成功!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building boot 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- mybatis-generator-maven-plugin:1.3.5:generate (default-cli) @ boot ---
[INFO] Connecting to the Database
[INFO] Introspecting table s_user
[INFO] Generating Example class for table s_user
[INFO] Generating Record class for table s_user
[INFO] Generating Mapper Interface for table s_user
[INFO] Generating SQL Map for table s_user
[INFO] Saving file UserMapper.xml
[INFO] Saving file UserExample.java
[INFO] Saving file User.java
[INFO] Saving file UserMapper.java
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.922 s
[INFO] Finished at: 2017-03-16T20:16:47+08:00
[INFO] Final Memory: 12M/155M
[INFO] ------------------------------------------------------------------------

打开磁盘目录我们看到已经生成了我们需要的文件。
注意:mybatis-generator有人开发了gui版,github地址:https://github.com/astarring/mybatis-generator-gui,需要的可以使用gui版:

编写service层逻辑代码

用户service接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.maxbill.base.service;

import com.maxbill.base.model.User;

public interface UserService {

int saveUser(User user);

int deleteUser(String userid);

int deleteUser(User user);

User findUserById(String userid);
}

用户service实现类

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.maxbill.base.service.impl;

import com.maxbill.base.mapper.UserMapper;
import com.maxbill.base.model.User;
import com.maxbill.base.service.UserService;
import org.springframework.stereotype.Service;

/**
* @func 用户模块业务逻辑实现层
* @user MaxBill
* @date 2017-03-15
* @mail 1370581389@qq.com
*/
@Service
public class UserServiceImpl implements UserService {

private UserMapper userMapper;

/**
* 添加用户
*/
public int saveUser(User user) {
return this.userMapper.insertSelective(user);
}

/**
* 删除用户
*/
public int deleteUser(String userid) {
return this.userMapper.deleteByPrimaryKey(userid);
}

/**
* 更新用户
*/
public int updateUser(User user) {
return this.userMapper.updateByPrimaryKeySelective(user);
}

/**
* 按id查新用户
*/
public User findUserById(String userid) {
return this.userMapper.selectByPrimaryKey(userid);
}

}

编写用户控制器接口方法,和上一章中一样,这里不再赘述。

在启动器中添加mapper扫描

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.maxbill;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.maxbill.web.mapper")//扫描mapper
public class BootApplication {

public static void main(String\[\] args) {
SpringApplication.run(BootApplication.class, args);
}
}

启动项目,测试各接口都正常。
以上就是Spring Boot集成Mybatis对数据库的数据的CRUD操作。下一篇主要说一下集成pagehelper分页插件。