继上一篇完成数据库初始化之后,我们需要配置工程的一些基本配置元素。
先建立包结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| ├── annotations ├── configurations ├── controllers ├── exceptions ├── interceptors ├── mappers ├── pojos │ ├── bos │ ├── dos │ ├── dtos │ ├── enums │ ├── transfers //mapstruct转换器专用 │ └── vos └── services
|
建立一套从service
、mapper
、vo
、do
的访问代码,这里以sys_user
表为例。
业务模型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package org.chaos.template.pojos.dos;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data;
import java.util.Date;
@TableName("sys_user") @Data public class UserDO { @TableId(type = IdType.AUTO) private Long id;
private String username;
private String password;
private String nickname;
private Date createTime;
private Date updateTime;
private String deleteStatus; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package org.chaos.template.pojos.vos;
import lombok.Data;
import java.util.Date;
@Data public class UserVO { private Long id;
private String username;
private String password;
private String nickname;
private Date createTime;
private Date updateTime;
private String deleteStatus; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package org.chaos.template.pojos.transfers;
import org.chaos.template.pojos.dos.UserDO; import org.chaos.template.pojos.vos.UserVO; import org.mapstruct.Mapper; import org.mapstruct.ReportingPolicy; import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) public interface UserTransfer { UserTransfer INSTANCE = Mappers.getMapper(UserTransfer.class);
UserVO mapToVO(UserDO record); List<UserVO> mapToVOs(List<UserDO> records);
UserDO mapToDO(UserVO data); List<UserDO> mapToDOs(List<UserVO> datas); }
|
数据访问
1 2 3 4 5 6 7
| package org.chaos.template.mappers;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.chaos.template.pojos.dos.UserDO;
public interface UserMapper extends BaseMapper<UserDO> { }
|
业务服务
1 2 3 4 5 6 7 8 9 10
| package org.chaos.template.services;
import com.baomidou.mybatisplus.extension.service.IService; import org.chaos.template.pojos.dos.UserDO;
public interface UserService extends IService<UserDO> { }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| package org.chaos.template.services;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.chaos.template.mappers.UserMapper; import org.chaos.template.pojos.dos.UserDO; import org.springframework.stereotype.Service;
@Service public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements UserService { }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.MySqlDialect; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
@Configuration public class MybatisPlusConfig {
@Bean public MybatisPlusInterceptor paginationInnerInterceptor() { MybatisPlusInterceptor page = new MybatisPlusInterceptor(); PaginationInnerInterceptor interceptor = new PaginationInnerInterceptor(); interceptor.setDialect(new MySqlDialect()); page.addInnerInterceptor(interceptor); return page; } }
|
接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| package org.chaos.template.controllers;
import com.baomidou.mybatisplus.core.metadata.OrderItem; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.apache.commons.lang3.StringUtils; import org.chaos.template.exceptions.BusinessException; import org.chaos.template.pojos.dos.UserDO; import org.chaos.template.pojos.dtos.BaseResult; import org.chaos.template.pojos.enums.ErrorEnum; import org.chaos.template.pojos.transfers.UserTransfer; import org.chaos.template.pojos.vos.UserVO; import org.chaos.template.services.UserService; import org.chaos.template.utils.HttpUtils; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;
import java.util.Collections; import java.util.List;
@RestController @RequestMapping("/user") public class UserController {
private final UserService userService;
public UserController(UserService userService) { this.userService = userService; }
@GetMapping("/list") public BaseResult<List<UserVO>> listUser( @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "20") int pageSize, @RequestParam(required = false, defaultValue = "id") String orderBy, @RequestParam(required = false, defaultValue = "true") Boolean asc) { OrderItem orderItem = new OrderItem(orderBy, asc); final Page<UserDO> pageParam = HttpUtils.getPageParam(page, pageSize, Collections.singletonList(orderItem)); final Page<UserDO> pageRecords = userService.page(pageParam); final List<UserVO> userVOS = UserTransfer.INSTANCE.mapToVOs(pageRecords.getRecords()); return BaseResult.success(userVOS).pageInfo(pageRecords); }
@PostMapping("/add") public BaseResult<String> addUser(@RequestBody UserVO requestParam) { UserDO data = UserTransfer.INSTANCE.mapToDO(requestParam); if (userService.save(data)) { return BaseResult.success("success"); } else { return BaseResult.fail(ErrorEnum.BUSINESS_ERROR, "failed"); } }
@PostMapping("/update") public BaseResult<String> updateUser(@RequestBody UserVO requestParam) { UserDO data = UserTransfer.INSTANCE.mapToDO(requestParam); userService.updateById(data); return BaseResult.success("success"); }
@PostMapping("/delete") public BaseResult<String> deleteUser(String id) { if (StringUtils.isBlank(id)) { return BaseResult.businessError(new BusinessException("id is mandatory")); } userService.removeById(id); return BaseResult.success("success"); }
@PostMapping("/batchDelete") public BaseResult<String> deleteUsers(@RequestBody List<String> ids) { if (CollectionUtils.isEmpty(ids)) { return BaseResult.businessError(new BusinessException("ids is mandatory")); } userService.removeBatchByIds(ids); return BaseResult.success("success"); } }
|
这里的list有多种写法,常规的都是GetMapping
作为入口,通常业务上我们会有一系列的查询条件封装成form
表单提交来查询,可以将form
表单中的查询条件与分页条件分离开来。如下所示:
1 2 3
| @GetMapping("/list") public BaseResult<List<UserVO>> listUser(UserVO queryCondition @PageableDefault(value = 20, sort = {"id"}, direction = Sort.Direction.ASC) Pageable pageble)
|
这样可以支持多个orderBy
条件。
这样我们就完成了一个标准的业务接口,后续我们会使用一些模版化生成工具来替代一些重复劳动。
参考
Database Generator