這篇文章介紹一個(gè)SpringBoot整合Mybatis-Plus,提供一個(gè)小的Demo供大家參考。
已經(jīng)很久沒有寫文章了,最近家里有點(diǎn)事剛剛處理完,順便也趁機(jī)休息了一段時(shí)間。剛回到公司看了一下碼云,發(fā)現(xiàn)本期碼云封面人員就是Mybatis-Plus團(tuán)隊(duì)苞米豆的負(fù)責(zé)人,如下圖。
忽然想到,正好之前別人跟我說過怎么不出一個(gè)SpringBoot整合Mybatis-Plus的,已經(jīng)很久的事了,正好想起來,這次就弄一個(gè)整合的Demo。
言歸正傳,新建一個(gè)項(xiàng)目。pom文件中加入Mybatis依賴,完整pom如下:
<?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.dalaoyang</groupId>
<artifactId>springboot_mybatisplus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot_mybatisplus</name>
<description>springboot_mybatisplus</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件配置數(shù)據(jù)庫配置和對(duì)應(yīng)Mybatis-Plus實(shí)體信息,配置如下:
##端口號(hào)
server.port=8888
##數(shù)據(jù)庫url
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false
##數(shù)據(jù)庫用戶名
spring.datasource.username=root
##數(shù)據(jù)庫密碼
spring.datasource.password=root
##數(shù)據(jù)庫驅(qū)動(dòng)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
##日志級(jí)別
logging.level.com.dalaoyang.dao.UserMapper=debug
##mybatis-plus mapper xml 文件地址
mybatis-plus.mapper-locations=classpath*:mapper/*Mapper.xml
##mybatis-plus type-aliases 文件地址
mybatis-plus.type-aliases-package=com.dalaoyang.entity
實(shí)體類User代碼如下:
package com.dalaoyang.entity;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.entity
* @email yangyang@dalaoyang.cn
* @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) {
this.id = id;
this.user_name = user_name;
this.user_password = user_password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.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;
}
}
下面要說的都是需要注意的地方,新增一個(gè)MybatisPlus配置類,其中沒有做過多的設(shè)置,只是設(shè)置了一下方言,代碼如下:
package com.dalaoyang.config;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.config
* @email yangyang@dalaoyang.cn
* @date 2018/7/20
*/
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
PaginationInterceptor page = new PaginationInterceptor();
//設(shè)置方言類型
page.setDialectType("mysql");
return page;
}
}
UserMapper繼承了MybatisPlus的BaseMapper,這里面列舉一個(gè)普通的查詢方法getUserList,完整代碼如下:
package com.dalaoyang.dao;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.dalaoyang.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.dao
* @email yangyang@dalaoyang.cn
* @date 2018/7/20
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
List<User> getUserList();
}
新建一個(gè)UserMapper.xml,里面寫getUserList對(duì)應(yīng)sql,代碼如下:
<?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.dalaoyang.dao.UserMapper">
<resultMap id="user" type="com.dalaoyang.entity.User"/>
<parameterMap id="user" type="com.dalaoyang.entity.User"/>
<select id="getUserList" resultMap="user">
SELECT * FROM USER
</select>
</mapper>
最后和往常一樣,新建一個(gè)Controller進(jìn)行測試,完整代碼如下:
package com.dalaoyang.controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dalaoyang.dao.UserMapper;
import com.dalaoyang.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.controller
* @email yangyang@dalaoyang.cn
* @date 2018/7/20
*/
@RestController
public class UserController {
@Autowired
private UserMapper userDao;
//http://localhost:8888/getUserList
@GetMapping("getUserList")
public List<User> getUserList(){
return userDao.getUserList();
}
//http://localhost:8888/getUserListByName?userName=xiaoli
//條件查詢
@GetMapping("getUserListByName")
public List<User> getUserListByName(String userName)
{
Map map = new HashMap();
map.put("user_name", userName);
return userDao.selectByMap(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 = userDao.insert(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 = userDao.updateById(user);
if(index>0){
return "修改用戶成功,影響行數(shù)"+index+"行。";
}else{
return "修改用戶失敗,影響行數(shù)"+index+"行。";
}
}
//http://localhost:8888/getUserById?userId=1
//根據(jù)Id查詢User
@GetMapping("getUserById")
public User getUserById(Integer userId)
{
return userDao.selectById(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<>();
entityWrapper.eq("user_name", "xiaoli");
return userDao.selectPage(page,entityWrapper);
}
}
這里對(duì)上面代碼稍作解釋,其中包含了如下幾個(gè)方法:
1.getUserList :這是普通的Mybatis查詢的方法,沒有用到Mybatis-Plus,這里不做過多解釋。
2.getUserListByName:條件查詢,根據(jù)名稱查詢用戶列表,這里使用到了selectByMap方法,參數(shù)需要傳一個(gè)Map,里面對(duì)應(yīng)寫好需要查詢的字段名與對(duì)應(yīng)查詢值。
3.saveUser :保存用戶,這里使用的是insert方法,需要傳一個(gè)實(shí)體對(duì)象,返回Integer值作為影響行數(shù)。
4.updateUser :修改用戶,這里使用的是updateByIdt方法,需要傳一個(gè)實(shí)體對(duì)象,返回Integer值作為影響行數(shù)。
5.getUserById :根據(jù)Id查詢實(shí)體對(duì)象,需要傳用戶Id。
6.getUserListByPage :條件分頁查詢,使用的是selectPage方法,方法需要一個(gè)分頁對(duì)象Page和一個(gè)條件對(duì)象EntityWrapper。Page放入頁碼和每頁數(shù)量,EntityWrapper使用eq方法放入對(duì)應(yīng)字段名和對(duì)應(yīng)查詢值。
這里只是舉例說明幾個(gè)方法,其中方法還有很多,更多Mybatis-Plus使用請(qǐng)查看官方文檔:http://baomidou.oschina.io/mybatis-plus-doc/#/?id=%E7%AE%80%E4%BB%8B
源碼下載 :大老楊碼云