preface

Fleet Management similar SaaS platform, from 0 to 1, continues..

In the last article, WE went to the tenant settlement, which is already the actual business scene, and then entered the main information of the tenant in the design, the status history table of the tenant. This article was originally written about SaaS billing rules, but this part still needs to be designed according to the requirements, so it is not needed. So let’s do a share from the code generator.

(I am Java after midnight, in nuggets this share experience, those who rely on copy of the handling of the author, without permission, do not copy the article)

Code generation

Program ape all know, even life copy code is a waste of time, has been a waste of hair CtrlC CtrlV waste, the main energy should be used in business logic, structure improvement above, then a set of code generation tools, is often suitable for, or is already a necessary tool for the program ape.

scenario

For example, add a business table, then the table already has, if WE use Mybatis then those basic SQL, table corresponding Bean object, CRUD basic methods these are basically the same, so this kind of code is to need my automatic generation code tools to complete.

practice

It is estimated that the Internet can search a piece of code generation tools, but from the principle or thinking, to understand, there will be some growth value for each program.

Train of thought

Table -> get the fields, generate the basic TableNameMapping. XML, and the corresponding Bean object, and then generate the Service, control, and so on

To prepare

Velocity is a Java-based template engine that uses the *. Vm suffix. It allows anyone to simply use the templatelanguage to refer to objects defined by Java code.

TableNameMapping.xml.vm

The mapping file

<? The XML version = "1.0" encoding = "utf-8"? > <! DOCTYPE mapper PUBLIC "- / / mybatis.org//DTD mapper / 3.0 / EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > < mapper namespace="${package}.${moduleName}.dao.${className}Dao"> <resultMap type="${package}.${moduleName}.entity.${className}Entity" id="${classname}Map"> #foreach($column in $columns) <result property="${column.attrname}" column="${column.columnName}"/> #end </resultMap> </mapper>Copy the code

TableNameDao.java.vm

The Dao content

package ${package}.${moduleName}.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import ${package}.${moduleName}.entity.${className}Entity;

/**
 * ${comments}
 * @author ${author}
 * @date ${datetime}
 */
@Mapper
public interface ${className}Dao extends BaseMapper<${className}Entity> {
   
}
Copy the code

TableNameEntity.java.vm

Entity table object; Entity table object; Entity table object; Entity table object

package ${package}.${moduleName}.entity; import lombok.Data; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; #if(${hasBigDecimal}) import java.math.BigDecimal; #end import java.io.Serializable; import java.util.Date; /** * Objects: ${comments} * @author ${author} * @date ${datetime} */ @TableName("${tableName}") @Data public class ${className}Entity implements Serializable { private static final long serialVersionUID = 1L; #foreach ($column in $columns) /** * $column.comments */ #if($column.columnName == $pk.columnName) @TableId #end private  $column.attrType $column.attrname; #end }Copy the code

TableNameService.java.vm

Table corresponding interface class

package ${package}.${moduleName}.service; import com.baomidou.mybatisplus.extension.service.IService; import ${mainPath}.common.utils.PageUtils; import ${package}.${moduleName}.entity.${className}Entity; import java.util.Map; /** * ${comments} * @author ${author} * @date ${datetime} */ public interface ${className}Service extends IService<${className}Entity> {@param params * @return */ PageUtils queryPage(Map<String, Object> params); }Copy the code

TableNameServiceImpl.java.vm

Interface implementation class

package ${package}.${moduleName}.service.impl; import org.springframework.stereotype.Service; import java.util.Map; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import ${mainPath}.common.utils.PageUtils; import ${mainPath}.common.utils.Query; import ${package}.${moduleName}.dao.${className}Dao; import ${package}.${moduleName}.entity.${className}Entity; import ${package}.${moduleName}.service.${className}Service; /** * ${comments} * @author ${author} * @date ${datetime} */ public class ${className}ServiceImpl extends ServiceImpl<${className}Dao, ${className}Entity> implements ${className}Service {@param params * @override public PageUtils queryPage(Map<String, Object> params) { IPage<${className}Entity> page = this.page( new Query<${className}Entity>().getPage(params), new QueryWrapper<${className}Entity>() ); return new PageUtils(page); }}Copy the code

TableNameController.java.vm

Finally, the control layer, the general add, delete, modify and check entry:

package ${package}.${moduleName}.controller; import java.util.Arrays; import java.util.Map; import io.renren.common.validator.ValidatorUtils; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; 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 ${package}.${moduleName}.entity.${className}Entity; import ${package}.${moduleName}.service.${className}Service; import ${mainPath}.common.utils.PageUtils; import ${mainPath}.common.utils.R; /** * ${comments} * @author ${author} * @date ${datetime} */ @RestController @RequestMapping("${moduleName}/${pathName}") public class ${className}Controller { @Autowired private ${className}Service ${classname}Service; @requestMapping ("/list") @requirespermissions ("${moduleName}:${pathName}:list") public R list(@RequestParam Map<String, Object> params){ PageUtils page = ${classname}Service.queryPage(params); return R.ok().put("page", page); } / / @requestMapping ("/info/{${pk.attrName}}") @respermissions ("${moduleName}:${pathName}:info") public R info(@PathVariable("${pk.attrname}") ${pk.attrType} ${pk.attrname}){ ${className}Entity ${classname} = ${classname}Service.getById(${pk.attrname}); return R.ok().put("${classname}", ${classname}); } /** * New @requestMapping ("/save") @requirespermissions ("${moduleName}:${pathName}:save") public R save(@requestBody)  ${className}Entity ${classname}){ ${classname}Service.save(${classname}); return R.ok(); } / @requestMapping ("/update") @requirespermissions ("${moduleName}:${pathName}:update") public R update(@RequestBody ${className}Entity ${classname}){ ValidatorUtils.validateEntity(${classname}); ${classname}Service.updateById(${classname}); return R.ok(); } @requestMapping ("/delete") @requirespermissions ("${moduleName}:${pathName}:delete") public R delete(@RequestBody ${pk.attrType}[] ${pk.attrname}s){ ${classname}Service.removeByIds(Arrays.asList(${pk.attrname}s)); return R.ok(); }}Copy the code

conclusion

There are a lot of examples of code generation, and there are open source versions of templates where you can modify your own templates, which is a relatively good way to do it. Template sharing, next we continue to generate code logic.

The SaaS system is built from zero to one, and the next part continues at….