一、mybtis-plus配置下载
MyBatis-Plus 是一个 Mybatis 增强版工具,在 MyBatis 上扩充了其他功能没有改变其基本功能,为了简化开发提交效率而存在。
具体的介绍请参见官方文档。
官网文档地址:mybatis-plus
- 添加mybatis-plus依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
- 添加MyBatisPlusConfig配置文件
config包中添加MyBatisPlusConfig配置文件,将原来在mapper中的@mapper注解取消,继承上BaseMapper泛型接口即可。
Mybatis-Plus里的BaseMapper接口,自带crud功能,继承了BaseMapper接口的接口.。
代码如下:
package com.example.demo.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.example.demo.demos.web.demo.mapper")
public class MyBatisPlusConfig {
//配置分页插件
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//数据库类型是MySql,因此参数填写DbType.MYSQL
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
mapper 中如下配置
3. yml 中写如下配置
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
二、mybtis-plus实现增删改查
- 数据增加或修改
在这里插入图片描述
结果:
修改:
注意映射表不要瞎加字段否则容易出现异常
Error querying database. Cause: java.sql.SQLSyntaxErrorException: Unknown column ‘page_num’ in ‘field list’
所有代码
config
package com.example.demo.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.example.demo.demos.web.demo.mapper")
public class MyBatisPlusConfig {
//配置分页插件
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//数据库类型是MySql,因此参数填写DbType.MYSQL
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
controller
package com.example.demo.demos.web.demo.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.demos.web.demo.entity.UserEntity;
import com.example.demo.demos.web.demo.mapper.UserMapper;
import com.example.demo.demos.web.demo.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("user")
public class UserController {
// @Autowired
// private UserMapper userMapper;
@Autowired
public UserService userService;
/* @GetMapping("/")
public List<UserEntity> index(){
return userMapper.findAll();
}*/
//使用mybtis-plus实现查询所有数据
@GetMapping("/")
public List<UserEntity> findAll(){
return userService.list();
}
/* @PostMapping("/add")
//这里做了一个单纯的添加的示例,使用的是mapper中的insert方法
public Integer save(@RequestBody UserEntity userEntity){
return userService.save(userEntity);
}*/
/* @DeleteMapping("/{id}")
public Integer deleteById(@PathVariable Integer id){
return userService.deleteById(id);
}*/
//使用mybtis-plus实现删除
@DeleteMapping("/{id}")
public boolean deleteById(@PathVariable Integer id){
return userService.removeById(id);
}
@PostMapping("/add")
//使用mybtis-plus,注意这里返回的是boolean型
public Boolean save(@RequestBody UserEntity user) {
return userService.saveUser(user);
}
//分页查询
//接口路径user/page?pageNum=1&pageSize=10
//RequestParam接受前台传过来的第几页,每页显示数
/* @GetMapping("/page")
public Map<String,Object> findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
pageNum=(pageNum-1)*pageSize;
List<UserEntity> data=userService.selectPage(pageNum,pageSize);
Integer total=userMapper.selectTotal();
Map<String,Object> res=new HashMap<>();
res.put("data",data);
res.put("total",total);
return res;
}*/
//使用mybtis-plus实现根据ID查找记录
@GetMapping("/{id}")
public UserEntity findOne(@PathVariable Integer id){
return userService.getById(id);
}
//使用mybtis-plus实现模糊查询并分页
@GetMapping("/page")
public IPage<UserEntity> findPage(@RequestParam Integer pageNum,
@RequestParam Integer pageSize,
@RequestParam(defaultValue = "") String username,
@RequestParam(defaultValue = "") String nickname,
@RequestParam(defaultValue = "") String address){
IPage<UserEntity> page=new Page<>(pageNum,pageSize);
QueryWrapper<UserEntity> queryWrapper=new QueryWrapper<>();
queryWrapper.like("username",username);
queryWrapper.like("nickname",nickname);
queryWrapper.like("address",address);
return userService.page(page,queryWrapper);
}
}
entity
package com.example.demo.demos.web.demo.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value="sys_user")
public class UserEntity {
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
/* private Integer pageNum;
private Integer pageSize;*/
private String username;
private String password;
private String email;
private String phone;
private String nickname;
private String address;
private String create_time;
private String avatar;
private String role;
}
mapper
package com.example.demo.demos.web.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.demos.web.demo.entity.UserEntity;
import org.apache.ibatis.annotations.*;
import java.util.List;
//@Mapper 前面配置文件中已经配置 这个注解可以注销但是要继承接口
public interface UserMapper extends BaseMapper<UserEntity> {
/* @Select("select * from sys_user limit #{pageNum},#{pageSize}")
List<UserEntity> selectPage(@Param("pageNum") Integer pageNum,@Param("pageSize") Integer pageSize);
//@Select("select * from sys_user limit #{pageNum},#{pageSize}")
@Select("select * from sys_user")
List<UserEntity> findAll();
@Insert("insert into sys_user(username,password,email,phone,nickname,address,avatar,role) " +
"VALUES(#{username},#{password},#{email},#{phone},#{nickname},#{address},#{avatar},#{role});")
//这里只是做测试使用
int insert(UserEntity userEntity);
int update(UserEntity userEntity);
@Delete("delete from sys_user where id=#{id}")
int deleteById(@Param("id") Integer id);*/
// 记录总数
/* @Select("select count(*) from sys_user")
Integer selectTotal();
*/
}
service
package com.example.demo.demos.web.demo.service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.demos.web.demo.entity.UserEntity;
import com.example.demo.demos.web.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService extends ServiceImpl<UserMapper, UserEntity> {
// @Autowired
// private UserMapper userMapper;
/* public int save(UserEntity userEntity){
//如果user没有id则表明是新增
if(userEntity.getId()==null){
return userMapper.insert(userEntity);
}
//否则就是更新
else {
return userMapper.update(userEntity);
}
}*/
public Boolean saveUser(UserEntity userEntity){
return saveOrUpdate(userEntity);
}
/* public Integer deleteById(Integer id) {
return userMapper.deleteById(id);
}*/
// 分页查找
/* public List selectPage(Integer pageNum, Integer pageSize) {
return userMapper.selectPage(pageNum,pageSize);
}*/
}
项目架构