1.配置文件中编写配置信息
server:
port: 8080
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///test?serverTimezone=UTC
username: root
password: 123456
mybatis-plus:
global-config:
db-config:
# 默认雪花算法
id-type: auto
# 开启日志
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
2.编写实体类
3.编写dao接口
@Mapper
public interface AccountDao extends BaseMapper<Account> {
}
4.编写service接口
public interface IAccountService extends IService<Account> {
//自定义方法
public IPage<Account> getPage(Integer current,Integer size);
}
5.编写实现类
@Service
public class IAccountServiceImpl extends ServiceImpl<AccountDao, Account> implements IAccountService {
@Autowired
private AccountDao accountDao;
@Override
public IPage<Account> getPage(Integer current, Integer size) {
IPage page=new Page(current,size);
accountDao.selectPage(page,null);
return page;
}
}
6.编写统一返回格式类工具
@Data
public class R {
// 统一表现层数据
private Boolean flag;
private Object data;
public R(Boolean flag) {
this.flag = flag;
}
public R(Boolean flag, Object data) {
this.flag = flag;
this.data = data;
}
public R() {
}
}
7.编写Controller类
@RestController
@RequestMapping("/account")
public class AccountController {
@Autowired
private IAccountService accountService;
@GetMapping
public R getAll(){
return new R(true,accountService.list());
}
@PostMapping
public R save(@RequestBody Account account){
return new R(accountService.save(account));
}
@PutMapping
public R update(@RequestBody Account account){
return new R(accountService.updateById(account));
}
@DeleteMapping("{id}")
public R delete(@PathVariable Integer id){
return new R(accountService.removeById(id));
}
// 自动补/
@GetMapping("{id}")
public R getById(@PathVariable Integer id){
return new R(true,accountService.getById(id));
}
@GetMapping("{c}/{s}")
public R pageList(@PathVariable Integer c,@PathVariable Integer s){
return new R(true,accountService.getPage(c,s));
}
}
8.使用postman进行接口测验