This article is a sub-chapter of the personal development framework for SpringBoot2.1. Please read the personal development framework for SpringBoot2.1
Back-end project address: personal application development framework for SpringBoot2.1
Front-end project address: yWH-vuE-admin
After we implement the function of code generation, for a Web project, we also need to carry out a simple encapsulation of the format of the return front end Result, all the returned types are in the same format, and the code we automatically generate can inherit our custom base controller and other classes for our own extension.
Base enumeration class
Create basic BaseEnum enum classes in the base package of the common submodule to define the description information to maintain in the enumeration, try not to directly appear magic values in the code (such as some encoding, Chinese, direct constants, etc.), later enumeration constant classes can also be written in this mode.
package com.ywh.common.base;
/** * CreateTime: 2018-12-15 16:06 * ClassName: BaseEnum * Package: com.ywh.common.base * Describe: * Base enumeration class **@author YWH
*/
public enum BaseEnum {
/** * Background processing successfully */
SUCCESS("Background processing successful!".200()... Omit the code, please go to Github to see the specific code/** * 404 Error blocking */
NO_HANDLER("This page is dead! Interface not found".404);
private String msg;
private int index;
BaseEnum(String msg, int index) {
this.msg = msg;
this.index = index;
}
public String getMsg(a) {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getIndex(a) {
return index;
}
public void setIndex(int index) {
this.index = index; }}Copy the code
The encapsulation front end returns the JSON format
Create the Result class under the Common Utils package as the return object of the front-end, and the direct return object of the Controller is all Result.
package com.ywh.common.utils;
import com.ywh.common.base.BaseEnum;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/** * CreateTime: 2018-12-15 15:46 * ClassName: Result * Package: com.ywh.mon.utils * Describe: *@author YWH
*/
public class Result extends Object implements Serializable{
private static final long serialVersionUID = 1348172152215944560L;
/** * return the status code, 200 is correct, 100 is failed */
private int code;
/** * returns processing information, success or failure */
private String msg;
/** * Returns true on success and false */ on failure
private Boolean success;
/** * The data returned to the front end */
private Map<String, Object> extend = new HashMap<String ,Object>();
/** * Successfully returned JSON package *@paramValue Original data *@returnJson format * /
public static Result successJson(Object value){
Result results = new Result();
results.setCode(BaseEnum.SUCCESS.getIndex());
results.setMsg(BaseEnum.SUCCESS.getMsg());
results.setSuccess(true);
results.getExtend().put("data",value);
return results;
}
/** * Failed to return json package *@returnJson format * /
public static Result errorJson(a){
Result results = new Result();
results.setCode(BaseEnum.FAIL.getIndex());
results.setSuccess(false);
results.setMsg(BaseEnum.FAIL.getMsg());
returnresults; }... Omit the code, please go to Github to see the specific code. }Copy the code
Write a method in the ExampleController of core to test our front-end structure with Postman
@RestController
@RequestMapping("ExampleController")
public class ExampleController{
@GetMapping("findAll")
public Result findAll(a){
return Result.successJson("It worked"); }}Copy the code
{
"extend": {
"data": "It worked"
},
"msg": "Background processing successful!"."code": 200."success": true
}
Copy the code
MybatisPlus paging plugin
MybatisPlus provides a pagination plugin, which is also easy to use. Create a configuration class to use the pagination query provided by MybatisPlus. Create the MybatisPlusConfig class under the Config package of common
package com.ywh.common.config;
/** * CreateTime: 2018-12-18 20:39 * ClassName: MybatisPlusConfig * Package: com.ywh.common.config * Describe: * Configuration classes for MybatisPlus * *@author YWH
*/
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
/** ** paging plug-in *@return* /
@Bean
public PaginationInterceptor paginationInterceptor(a) {
return newPaginationInterceptor(); }}Copy the code
By creating the BasePage class in the Entity under Common, which is the receiving Entity class for the front end incoming parameters, we can control how many pieces of data are displayed on the current page and on a page
package com.ywh.common.entity;
import lombok.Data;
/** * CreateTime: 2018-11-22 10:23 * ClassName: BasePage * Package: com.ywh.mon. Entity * Describe: * Paging entity class **@author YWH
*/
@Data
public class BasePage {
/** * Current number of pages */
private Integer size;
/** * Number of pages */
private Integer current;
}
Copy the code
Define basic controllers, services, and so on
For our auto-generated code we can inherit my custom Controller, etc., create the following classes in the common base package, encapsulated as follows:
BaseController, BaseService, BaseServiceImpl, BaseDao: Because they are too long to post, the rest of the code can be seen on Github
package com.ywh.common.base;
/** * CreateTime: 2018-11-21 9:09 * ClassName: BaseController * Package: com.ywh.common.base * Describe: All controllers inherit from this class. If there are any common methods, * getList addData addDataBatch updateById updateByColumn deleteByColumn deleteById deleteByIds *@author YWH
*/
public class BaseController<Service extends IService.T> {
private static final Logger log = LoggerFactory.getLogger(BaseMapper.class);
@Autowired
private Service service;
/** * get all data *@returnReturn front-end JSON data */
@GetMapping("getList")
public Result getList(a){
List<T> list = service.list();
return Result.successJson(list);
}
/** ** *@paramPn-paged entity class *@returnReturn front-end JSON data */
@GetMapping("getPageList")
public Result getPageList(@RequestBody BasePage pn){
Page<T> pojo = new Page<T>(pn.getCurrent(),pn.getSize());
IPage<T> page = service.page(pojo);
log.info("Total number ------>" + page.getTotal());
log.info("Current page number ------>" + page.getCurrent());
log.info("Current number of pages displayed ------>" + page.getSize());
log.info("Data ------>" + page.getRecords());
returnResult.successJson(page); }... Omit code, please go to Github to see the specific code}Copy the code
We can write some generic CRUD methods in the base classes. Once we have the base classes, our automatically generated code does not inherit our defined base classes at generation time, so we need to modify the CodeGenerator utility classes so that they do. Add the following code to the policy configuration of CodeGenerator
// Inherit custom controller, service, IMPl, DAO
strategy.setSuperControllerClass("com.ywh.common.base.BaseController");
strategy.setSuperServiceClass("com.ywh.common.base.BaseService");
strategy.setSuperServiceImplClass("com.ywh.common.base.BaseServiceImpl");
strategy.setSuperMapperClass("com.ywh.common.base.BaseDao");
Copy the code
The test case
Now that we’ve added the base class and the paging plugin, let’s test that it works, delete the generated Controller files, set mybatisplus.properties to True, regenerate the code and test our query through Postman. Paging queries, adding data, etc.
You can see THAT I have three pieces of data, but I stipulated that when only one piece of data is displayed on the previous page, the paging query is already in effect, and I won’t test any other methods here.