第五讲
快速开发
创建之前说过了所以这边不讲了,有疑问的可以取之前的博客里面有。
这里主要介绍开发crud的接口
同样的的步骤写实体类
package com.example.demo.domain;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* ┏┓ ┏┓
* ┏┛┻━━━┛┻┓
* ┃ ┃
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┃
* ┃ ┻ ┃
* ┃ ┃ @Auther: wangshenghui
* ┗━┓ ┏━┛ @Date: 2022/08/08/9:33
* ┃ ┃神兽保佑
* ┃ ┃代码无BUG!
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*
*/
@Data
@NoArgsConstructor
// 下划线识别不了
@TableName("wc_user")
public class User {
private Integer id;
private String username;
private String account;
private String password;
}
编写实现接口
package com.example.demo.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* ┏┓ ┏┓
* ┏┛┻━━━┛┻┓
* ┃ ┃
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┃
* ┃ ┻ ┃
* ┃ ┃ @Auther: wangshenghui
* ┗━┓ ┏━┛ @Date: 2022/08/08/9:43
* ┃ ┃神兽保佑
* ┃ ┃代码无BUG!
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*
*/
@Mapper
@Component
// 在MyBatis Plus 里面提供一个接口BaseMapper需要一个泛型对应哪个实体类
public interface UserDao extends BaseMapper<User> {
}
编写server层
package com.example.demo.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.domain.User;
/**
* ┏┓ ┏┓
* ┏┛┻━━━┛┻┓
* ┃ ┃
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┃
* ┃ ┻ ┃
* ┃ ┃ @Auther: wangshenghui
* ┗━┓ ┏━┛ @Date: 2022/08/09/22:26
* ┃ ┃神兽保佑
* ┃ ┃代码无BUG!
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*
*/
public interface IUserService extends IService<User> {
boolean saveUser(User user);
boolean modify(User user);
boolean delete(Integer id);
IPage<User> getPage(int currentPage, int pageSize);
}
编写server层中的implement
package com.example.demo.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.dao.UserDao;
import com.example.demo.domain.User;
import com.example.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* ┏┓ ┏┓
* ┏┛┻━━━┛┻┓
* ┃ ┃
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┃
* ┃ ┻ ┃
* ┃ ┃ @Auther: wangshenghui
* ┗━┓ ┏━┛ @Date: 2022/08/09/22:29
* ┃ ┃神兽保佑
* ┃ ┃代码无BUG!
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements IUserService {
@Autowired
private UserDao userDao;
@Override
public boolean saveUser(User user) {
return userDao.insert(user)>0;
}
@Override
public boolean modify(User user) {
return userDao.updateById(user)>0;
}
@Override
public boolean delete(Integer id) {
return userDao.deleteById(id)>0;
}
@Override
public IPage<User> getPage(int currentPage, int pageSize) {
IPage page = new Page(currentPage,pageSize);
userDao.selectPage(page,null);
return page;
}
}
编写过滤器
package com.example.demo.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* ┏┓ ┏┓
* ┏┛┻━━━┛┻┓
* ┃ ┃
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┃
* ┃ ┻ ┃
* ┃ ┃ @Auther: wangshenghui
* ┗━┓ ┏━┛ @Date: 2022/08/09/6:53
* ┃ ┃神兽保佑
* ┃ ┃代码无BUG!
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*
*/
@Configuration
//配置类这样里面的配置信息可以被读取。
public class MPConfig {
// 拦截器 limt
@Bean
// 第三方Bean mp的拦截器,当你需要定义多个拦截器时候可以创建
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
编写controller类
package com.example.demo.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo.domain.User;
import com.example.demo.service.IUserService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* ┏┓ ┏┓
* ┏┛┻━━━┛┻┓
* ┃ ┃
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┃
* ┃ ┻ ┃
* ┃ ┃ @Auther: wangshenghui
* ┗━┓ ┏━┛ @Date: 2022/08/10/6:48
* ┃ ┃神兽保佑
* ┃ ┃代码无BUG!
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*
*/
@RestController
@RequestMapping("users")
public class UserController {
@Autowired
private IUserService iUserService;
// 查询全部使用User泛型且请求为get
@GetMapping
public List<User> getAll(){
return iUserService.list();
}
//添加操作post
@PostMapping
public Boolean save(@RequestBody User user){
return iUserService.save(user);
}
@PutMapping
public Boolean update(@RequestBody User user){
return iUserService.update(user,null);
}
@DeleteMapping("/{id}")
public Boolean delete(@PathVariable Integer id){
return iUserService.removeById(id);
}
// 出现了两个get请求所以需要声明请求不同的地方
@GetMapping("/{id}")
public User getById(@PathVariable Integer id){
return iUserService.getById(id);
}
// 分页
@GetMapping("{currentPage}/{pageSize}")
public IPage<User> getPage(@PathVariable int currentPage,@PathVariable int pageSize){
return iUserService.getPage(currentPage,pageSize);
}
}
目录结构
postman测试
测试一个查询所有接口