MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。(官方原文)
mybatis与MyBatis-Plus的关系,就像我们小时候常玩的魂斗罗一样,一个是p1一个是p2,一个是红色一个是蓝色,两者结合天下无敌。
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
<version>8.0.19version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.3.1.tmpversion>
dependency>
<dependency>
@SpringBootApplication
@MapperScan("cloud.xingzhe.springbootmybatisplus.mapper")
public class SpringbootMybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisPlusApplication.class, args);
}
}
@EnableTransactionManagement
@Configuration
@MapperScan("cloud.xingzhe.springbootmybatisplus.mapper")
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
// paginationInterceptor.setOverflow(false);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
// paginationInterceptor.setLimit(500);
// 开启 count 的 join 优化,只针对部分 left join
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor;
}
}
server:
port: 8005
spring:
datasource:
name: springboot
url: jdbc:mysql://127.0.0.1:3306/springboot?&useUnicode=true&characterEncoding=utf8&useSSL=FALSE&serverTimezone=Asia/Shanghai
username: root
password: root
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: cloud.xingzhe.springbootmybatisplus.model
configuration:
map-underscore-to-camel-case: true
CREATE TABLE `sys_user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id' ,
`username` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT '用户名',
`address` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT '地址',
PRIMARY KEY (`id`)
);
INSERT INTO sys_user VALUES('1','张三','广东');
INSERT INTO sys_user VALUES('2','李四','广西');
INSERT INTO sys_user VALUES('3','王五','北京')
package cloud.xingzhe.springbootmybatisplus.mapper;
import cloud.xingzhe.springbootmybatisplus.model.SysUser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface SysUserMapper extends BaseMapper<SysUser> {
}
注意: BaseMapper是MybatisPlus 自带的
<mapper namespace="cloud.xingzhe.springbootmybatisplus.mapper.SysUserMapper">
mapper>
package cloud.xingzhe.springbootmybatisplus.service;
import cloud.xingzhe.springbootmybatisplus.model.SysUser;
import com.baomidou.mybatisplus.extension.service.IService;
public interface SysUserService extends IService<SysUser> {
}
注意: IService是MybatisPlus 自带的
package cloud.xingzhe.springbootmybatisplus.service.impl;
import cloud.xingzhe.springbootmybatisplus.mapper.SysUserMapper;
import cloud.xingzhe.springbootmybatisplus.model.SysUser;
import cloud.xingzhe.springbootmybatisplus.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
}
注意: ServiceImpl
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ApiModel(value = "用户")
@Data
@TableName(value = "sys_user")
public class SysUser {
@ApiModelProperty("id")
@TableId(type = IdType.ASSIGN_ID)
private Long id;
@ApiModelProperty("用户名称")
@TableField("username")
private String username;
@ApiModelProperty("地址")
@TableField("address")
private String address;
}
package cloud.xingzhe.springbootmybatisplus.controller;
import cloud.xingzhe.springbootmybatisplus.model.SysUser;
import cloud.xingzhe.springbootmybatisplus.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Api(tags = "用户管理")
@RestController
public class SysUserController {
@Autowired
private SysUserService sysUserService;
@ApiOperation(value = "用户添加")
@RequestMapping(value = "/create",method = RequestMethod.POST)
public String save(SysUser sysUser){
boolean flag = sysUserService.save(sysUser);
return flag?"创建成功":"创建失败";
}
@ApiOperation(value = "删除用户")
@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
public String delete(String id){
boolean flag = sysUserService.removeById(id);
return flag?"删除成功":"删除失败";
}
@ApiOperation(value = "更新用户")
@RequestMapping(value = "/update",method = RequestMethod.POST)
public String update(SysUser sysUser){
boolean flag = sysUserService.updateById(sysUser);
return flag?"更新成功":"更新失败";
}
@ApiOperation(value = "获取用户列表")
@RequestMapping(value = "/list",method = RequestMethod.GET)
public List<SysUser> list(){
List<SysUser> sysUserList = sysUserService.list();
return sysUserList;
}
@ApiOperation(value = "根据id得到用户详情")
@RequestMapping(value = "/get",method = RequestMethod.GET)
public SysUser get(String id){
SysUser sysUser = sysUserService.getById(id);
return sysUser;
}
@ApiOperation(value = "分页查询")
@RequestMapping(value = "/page",method = RequestMethod.GET)
public Page<SysUser> page(Integer currentPage,Integer limit){
Page<SysUser> page=new Page<>(currentPage,limit);
Page<SysUser> sysUserPage = sysUserService.page(page);
return sysUserPage;
}
@ApiOperation(value = "QueryWrapper查询列表")
@RequestMapping(value = "/queryWrapperList",method = RequestMethod.GET)
public List<SysUser> queryWrapperList(SysUser sysUser){
QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
Map<String,Object> params=new HashMap<>(3);
params.put("username", sysUser.getUsername());
params.put("id", sysUser.getId());
params.put("address", sysUser.getAddress());
queryWrapper.allEq(params,false);
List<SysUser> list = sysUserService.list(queryWrapper);
return list;
}
}
本章:接口文档用到了:swagger2 相应的博文地址:springboog整合swagger2 实现便捷高效的接口文档
javabean用到了: lombok 的注解 相应的博文地址:lombok 一个帮助我们编写javaben中,getter,setter,equals …方法的工具 ideal安装与使用
官方文档:https://mp.baomidou.com/
https://github.com/xingzhewenzi/springboot-examples.git