<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.2version>
dependency>
MyBatisPlusConfig.java
@Configuration //组件,添加到容器
@MapperScan("com.xqh.mapper") //开启mapper接口扫描
public class MybatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:/mapper/*.xml
UserMapper.java
@Repository
public interface UserMapper extends BaseMapper<User> {
}
UserService.java
@Service
public class UserService extends ServiceImpl<UserMapper,User> {}
启动主程序,看看能不能跑起来
出现bug
Error creating bean with name ‘userController’: Unsatisfied dependency expressed through field ‘userService’;…not found class User…
解决:
这种报错一般就是没有实体层、service层、mapper层没有映射上,此时我们的mapper已经写好了地址,所以加一个实体类的位置就可以了,在配置文件application.yml中补充:
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:/mapper/*.xml
type-aliases-package: com.xqh.entity # 实体类也映射上
重新启动,启动成功,整合MP成功
User.java
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "user")
public class User {
@TableId(type = IdType.AUTO)
private Integer id;
private String username;
//密码可以不展示, @JsonIgnore 意思就是给前端传数据时忽略某个字段
@JsonIgnore
private String password;
private String nickname;
private String email;
private String phone;
private String address;
}
UserMapper.xml
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xqh.mapper.UserMapper">
mapper>
@RestController //返回json
@RequestMapping("/user") //加前缀,这下面的所有接口都要加/user前缀
public class UserController {
@Autowired
private UserService userService;
//插入和修改操作 , 需要外界发送数据,用post
@PostMapping
public boolean save(@RequestBody User user){
return userService.saveOrUpdate(user);
}
//查询所有数据
@GetMapping
public List<User> findAll(){
return userService.list();
}
//删除
@DeleteMapping("/{id}")
public boolean delete(@PathVariable Integer id){
return userService.removeById(id);
}
}
@Service
public class UserService extends ServiceImpl<UserMapper,User> {}
同样,UserMapper.java也不用写代码,省去很多重复的工作
启动主程序,打开swagger页面测试接口 http://localhost:8081/swagegr-ui.html
分页需要自己重写一个方法,不过也很方便有了MP
//分页查询
@GetMapping("/page")
public IPage<User> findPage(@RequestParam Integer pageNum,
@RequestParam Integer pageSize,
@RequestParam(defaultValue = "") String username,
@RequestParam(defaultValue = "") String nickname){
return userService.findPage(pageNum,pageSize,username,nickname);
}
public IPage<User> findPage( Integer pageNum,
Integer pageSize,
String username,
String nickname) {
IPage<User> page = new Page<>(pageNum, pageSize);
QueryWrapper<User>queryWrapper=new QueryWrapper<>();
if (! "".equals(username)){
queryWrapper.like("username",username);
}
if (! "".equals(nickname)){
queryWrapper.like("nickname",nickname);
}
// queryWrapper.like("username",username);
// queryWrapper.like("nickname",nickname);
return this.page(page,queryWrapper);
//queryWrapper.or().like("nickname",nickname);//出现or,那么前面的条件都失效...只要or后面成立的都会筛选出来,慎用
Parameter ‘username’ not found. Available parameters are [ew, page, param1, param2]
测试分页的时候,报500错误,控制台给出的错误信息