这篇文章介绍一个SpringBoot整合Mybatis-Plus,提供一个小的Demo供大家参考。
已经很久没有写文章了,最近家里有点事刚刚处理完,顺便也趁机休息了一段时间。刚回到公司看了一下码云,发现本期码云封面人员就是Mybatis-Plus团队苞米豆的负责人,如下图。
忽然想到,正好之前别人跟我说过怎么不出一个SpringBoot整合Mybatis-Plus的,已经很久的事了,正好想起来,这次就弄一个整合的Demo。
言归正传,新建一个项目。pom文件中加入Mybatis依赖,完整pom如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.">
<modelVersion>4.0.0</modelVersion>
<groupId></groupId>
<artifactId>springboot_mybatisplus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot_mybatisplus</name>
<description>springboot_mybatisplus</description>
<parent>
<groupId></groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<>UTF-8</>
<>UTF-8</>
<>1.8</>
</properties>
<dependencies>
<dependency>
<groupId></groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId></groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
复制代码
配置文件配置数据库配置和对应Mybatis-Plus实体信息,配置如下:
##端口号
=8888
##数据库url
=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false
##数据库用户名
=root
##数据库密码
=root
##数据库驱动
-class-name=
##日志级别
=debug
##mybatis-plus mapper xml 文件地址
-locations=classpath*:mapper/*
##mybatis-plus type-aliases 文件地址
-aliases-package=
复制代码
实体类User代码如下:
package ;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package
* @email yangyang@
* @date 2018/7/20
*/
public class User {
private int id;
private String user_name;
private String user_password;
public User() {
}
public User(String user_name, String user_password) {
this.user_name = user_name;
this.user_password = user_password;
}
public User(int id, String user_name, String user_password) {
= id;
this.user_name = user_name;
this.user_password = user_password;
}
public int getId() {
return id;
}
public void setId(int id) {
= id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_password() {
return user_password;
}
public void setUser_password(String user_password) {
this.user_password = user_password;
}
}
复制代码
下面要说的都是需要注意的地方,新增一个MybatisPlus配置类,其中没有做过多的设置,只是设置了一下方言,代码如下:
package ;
import ;
import ;
import ;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package
* @email yangyang@
* @date 2018/7/20
*/
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
PaginationInterceptor page = new PaginationInterceptor();
//设置方言类型
("mysql");
return page;
}
}
复制代码
UserMapper继承了MybatisPlus的BaseMapper,这里面列举一个普通的查询方法getUserList,完整代码如下:
package ;
import ;
import ;
import ;
import ;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package
* @email yangyang@
* @date 2018/7/20
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
List<User> getUserList();
}
复制代码
新建一个,里面写getUserList对应sql,代码如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/" >
<mapper namespace="">
<resultMap id="user" type=""/>
<parameterMap id="user" type=""/>
<select id="getUserList" resultMap="user">
SELECT * FROM USER
</select>
</mapper>
复制代码
最后和往常一样,新建一个Controller进行测试,完整代码如下:
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package
* @email yangyang@
* @date 2018/7/20
*/
@RestController
public class UserController {
@Autowired
private UserMapper userDao;
//http://localhost:8888/getUserList
@GetMapping("getUserList")
public List<User> getUserList(){
return ();
}
//http://localhost:8888/getUserListByName?userName=xiaoli
//条件查询
@GetMapping("getUserListByName")
public List<User> getUserListByName(String userName)
{
Map map = new HashMap();
("user_name", userName);
return (map);
}
//http://localhost:8888/saveUser?userName=xiaoli&userPassword=111
//保存用户
@GetMapping("saveUser")
public String saveUser(String userName,String userPassword)
{
User user = new User(userName,userPassword);
Integer index = (user);
if(index>0){
return "新增用户成功。";
}else{
return "新增用户失败。";
}
}
//http://localhost:8888/updateUser?id=5&userName=xiaoli&userPassword=111
//修改用户
@GetMapping("updateUser")
public String updateUser(Integer id,String userName,String userPassword)
{
User user = new User(id,userName,userPassword);
Integer index = (user);
if(index>0){
return "修改用户成功,影响行数"+index+"行。";
}else{
return "修改用户失败,影响行数"+index+"行。";
}
}
//http://localhost:8888/getUserById?userId=1
//根据Id查询User
@GetMapping("getUserById")
public User getUserById(Integer userId)
{
return (userId);
}
//http://localhost:8888/getUserListByPage?pageNumber=1&pageSize=2
//条件分页查询
@GetMapping("getUserListByPage")
public List<User> getUserListByPage(Integer pageNumber,Integer pageSize)
{
Page<User> page =new Page<>(pageNumber,pageSize);
EntityWrapper<User> entityWrapper = new EntityWrapper<>();
("user_name", "xiaoli");
return (page,entityWrapper);
}
}
复制代码
这里对上面代码稍作解释,其中包含了如下几个方法:
:这是普通的Mybatis查询的方法,没有用到Mybatis-Plus,这里不做过多解释。
:条件查询,根据名称查询用户列表,这里使用到了selectByMap方法,参数需要传一个Map,里面对应写好需要查询的字段名与对应查询值。
:保存用户,这里使用的是insert方法,需要传一个实体对象,返回Integer值作为影响行数。
:修改用户,这里使用的是updateByIdt方法,需要传一个实体对象,返回Integer值作为影响行数。 :根据Id查询实体对象,需要传用户Id。
:条件分页查询,使用的是selectPage方法,方法需要一个分页对象Page和一个条件对象EntityWrapper。Page放入页码和每页数量,EntityWrapper使用eq方法放入对应字段名和对应查询值。
这里只是举例说明几个方法,其中方法还有很多,更多Mybatis-Plus使用请查看官方文档:/mybatis-plu…
源码下载 :大老杨码云
个人网站: