Development artifact EasyCode

Development artifact EasyCode

Preface

Introduction to Easy Code

For java programmers, the daily work is the operation of crud. Every time you have to build MVC three layers, it is still very cumbersome. Here is the tool of the artifact easycode, which can quickly generate code and customize the template, so For developers, it has reduced a lot of things, spent more time on business, and greatly improved efficiency. Now let's build easycode step by step and use it.

1. Open IntelliJ IDEA and create a new maven project

2. Select the project storage directory

3. Download and install the EasyCode plug-in

file->settings->plugins search for Easy Code

After searching, click Install. I installed it here. After the installation is complete, you will restart IDEA.

How to judge whether the installation is successful file->settings->Other settings to see if there is an option of Easy Code

4. Introduce Easy Code template (can be customized according to personal situation or use the default)

5. Create a database, data table

6. Configure the data source (you need to specify the database name and generate a data table)

Click Datbase on the right side of IDEA -> click the plus sign above -> select Data Source. ->Mysql

7. Configure the data source

Note: The connection method needs to use the mysql8 connection method, otherwise the connection may fail

jdbc:mysql://localhost:3306/table?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false

8. The connection is successful as shown in the figure

9. Introduce related pom.xml dependencies of springboot

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> </dependencies> Copy code

Need to use mybatis launcher, so also quote

10. Open the Easy Code plug-in and select the entity, controller, service, serviceimpl, dao to be generated

Right-click the table name->Easy Code->Generate Code

Module: current project module

package: The location where the generated code stores the package

Template: Generated template

11. Check All means to generate all, check Prohibit prompts to prevent a lot of prompt messages from popping up, click OK

12. Generate a template

entity.java code:

##Introducing macro definition $!define $!init ##Use macro definition to set callback (save location and file suffix) #save("/entity", ".java") ##Use macro definition to set package suffix #setPackageSuffix("entity") ##Use global variables to implement default package import $!autoImport import java.io.Serializable; import lombok.Data; ##Use macro definitions to implement class annotation information #tableComment("Entity Class") @Data public class $!{tableInfo.name} implements Serializable { private static final long serialVersionUID = $!tool.serial(); #foreach($column in $tableInfo.fullColumn) #if(${column.comment})/** * ${column.comment} */#end private $!{tool.getClsNameByFullName($column.type)} $!{column.name}; #end } Copy code

Controller interface: the controller.java code is as follows:

##Define initial variables #set($tableName = $tool.append($tableInfo.name, "Controller")) ##Set callback $!callback.setFileName($tool.append($tableName, ".java")) $!callback.setSavePath($tool.append($tableInfo.savePath, "/controller")) ##Get the primary key #if(!$tableInfo.pkColumn.isEmpty()) #set($pk = $tableInfo.pkColumn.get(0)) #end #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}controller; import lombok.extern.slf4j.Slf4j; import com.github.pagehelper.PageInfo; import $!{tableInfo.savePackageName}.response.PageResult; import $!{tableInfo.savePackageName}.response.Result; import $!{tableInfo.savePackageName}.response.StatusCode; import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}; import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service; import org.springframework.web.bind.annotation.*; import org.springframework.util.CollectionUtils; import javax.annotation.Resource; import java.util.List; import java.util.Objects; /** * $!{tableInfo.comment}($!{tableInfo.name}) control layer * * @author protagonist * @since $!time.currTime() */ @RestController @Slf4j @RequestMapping("/$!tool.firstLowerCase($tableInfo.name)") public class $!{tableName} { /** * Service object */ @Resource private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)ServiceImpl; /** * Query a single piece of data through the primary key * * @param $!pk.name primary key * @return single data */ @GetMapping(value = "/get/{$!pk.name}") public Result selectOne(@PathVariable("$!pk.name") $!pk.shortType $!pk.name) { $tableInfo.name result = $!{tool.firstLowerCase($tableInfo.name)}ServiceImpl.selectById(id); if(Objects.nonNull(result)){ return new Result<>(true,StatusCode.OK,"Query successful",result); } return new Result<>(true,StatusCode.ERROR,"Query failed"); } /** * Add a piece of data * * @param $!tool.firstLowerCase($tableInfo.name) entity class * @return Result object */ @PostMapping(value = "/insert") public Result insert(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)) { int result = $!{tool.firstLowerCase($tableInfo.name)}ServiceImpl.insert($!tool.firstLowerCase($tableInfo.name)); if (result> 0) { return new Result<>(true,StatusCode.OK,"add success",result); } return new Result<>(true,StatusCode.ERROR,"Adding failed"); } /** * Modify a piece of data * * @param $!tool.firstLowerCase($tableInfo.name) entity class * @return Result object */ @PutMapping(value = "/update") public Result update(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)) { $tableInfo.name result = $!{tool.firstLowerCase($tableInfo.name)}ServiceImpl.update($!tool.firstLowerCase($tableInfo.name)); if (Objects.nonNull(result)) { return new Result<>(true,StatusCode.OK,"modified successfully",result); } return new Result<>(true,StatusCode.ERROR,"Modification failed"); } /** * Delete a piece of data * * @param $!pk.name primary key * @return Result object */ @DeleteMapping(value = "/delete/{$!pk.name}") public Result delete(@PathVariable("$!pk.name") $!pk.shortType $!pk.name) { int result = $!{tool.firstLowerCase($tableInfo.name)}ServiceImpl.deleteById($!pk.name); if (result> 0) { return new Result<>(true,StatusCode.OK,"Delete successfully",result); } return new Result<>(true,StatusCode.ERROR,"Deletion failed"); } /** * Enquire all * * @return Result object */ @GetMapping(value = "/selectAll") public Result<List<$tableInfo.name>> selectAll() { List<$tableInfo.name> $!tool.firstLowerCase($tableInfo.name)s = $!{tool.firstLowerCase($tableInfo.name)}ServiceImpl.selectAll(); if (CollectionUtils.isEmpty($!tool.firstLowerCase($tableInfo.name)s)) { return new Result<>(true,StatusCode.ERROR,"Query all data failed"); } return new Result<>(true,StatusCode.OK,"Query all data successfully",$!tool.firstLowerCase($tableInfo.name)s); } /** * Paging query * * @param current The data on the zeroth page and the first page of the current page are the same * @param size The number of data on each page * @return Result object */ @GetMapping(value = "/selectPage/{current}/{size}") public Result selectPage(@PathVariable("current") Integer current,@PathVariable("size") Integer size) { PageInfo<$tableInfo.name> page = $!{tool.firstLowerCase($tableInfo.name)}ServiceImpl.selectPage(current, size); if (Objects.nonNull(page)) { return new Result<>(true,StatusCode.OK,"page condition query is successful",new PageResult<>(page.getTotal(),page.getList())); } return new Result<>(true,StatusCode.ERROR,"Paging data query failed"); } } Copy code

The service interface: service.java code is as follows:

##Define initial variables #set($tableName = $tool.append($tableInfo.name, "Service")) ##Set callback $!callback.setFileName($tool.append($tableName, ".java")) $!callback.setSavePath($tool.append($tableInfo.savePath, "/service")) ##Get the primary key #if(!$tableInfo.pkColumn.isEmpty()) #set($pk = $tableInfo.pkColumn.get(0)) #end #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service; import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}; import java.util.List; import com.github.pagehelper.PageInfo; /** * $!{tableInfo.comment}($!{tableInfo.name}) table service interface * * @author protagonist * @since $!time.currTime() */ public interface $!{tableName} { /** * Query a single piece of data by ID * * @param $!pk.name primary key * @return instance object */ $!{tableInfo.name} selectById($!pk.shortType $!pk.name); /** * Paging query * * @param current current page * @param size The number of data on each page * @return object list */ PageInfo<$!{tableInfo.name}> selectPage(int current, int size); /** * Enquire all * * @return object list */ List<$!{tableInfo.name}> selectAll(); /** * Query by entity as a filter condition * * @param $!tool.firstLowerCase($!{tableInfo.name}) instance object * @return object list */ List<$!{tableInfo.name}> selectList($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * New data * * @param $!tool.firstLowerCase($!{tableInfo.name}) instance object * @return affects the number of rows */ int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * Batch add * * @param $!tool.firstLowerCase($!{tableInfo.name})s collection of instance objects * @return affects the number of rows */ int batchInsert(List<$!{tableInfo.name}> $!tool.firstLowerCase($!{tableInfo.name})s); /** * change the data * * @param $!tool.firstLowerCase($!{tableInfo.name}) instance object * @return modification */ $!{tableInfo.name} update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * Delete data by primary key * * @param $!pk.name primary key * @return affects the number of rows */ int deleteById($!pk.shortType $!pk.name); /** * Query the total number of data * * @return Total number of data */ int count(); } Copy code

The serviceImpl implementation class: serviceImpl.java code is as follows:

##Define initial variables #set($tableName = $tool.append($tableInfo.name, "ServiceImpl")) ##Set callback $!callback.setFileName($tool.append($tableName, ".java")) $!callback.setSavePath($tool.append($tableInfo.savePath, "/service/impl")) ##Get the primary key #if(!$tableInfo.pkColumn.isEmpty()) #set($pk = $tableInfo.pkColumn.get(0)) #end #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service.impl; import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}; import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao; import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.stereotype.Service; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import javax.annotation.Resource; import java.util.List; /** * $!{tableInfo.comment}($!{tableInfo.name} table) service implementation class * * @author protagonist * @since $!time.currTime() */ @Service("$!tool.firstLowerCase($!{tableInfo.name})ServiceImpl") public class $!{tableName} implements $!{tableInfo.name}Service { @Resource private $!{tableInfo.name}Dao $!tool.firstLowerCase($!{tableInfo.name})Dao; /** * Query a single piece of data by ID * * @param $!pk.name primary key * @return instance object */ @Override public $!{tableInfo.name} selectById($!pk.shortType $!pk.name) { return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectById($!pk.name); } /** * Paging query * * @param current current page * @param size The number of items per page * @return object list */ @Override public PageInfo<$!{tableInfo.name}> selectPage(int current, int size) { PageHelper.startPage(current,size); List<$!{tableInfo.name}> dataList = $!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectAll(); return new PageInfo<>(dataList); } /** * Check all * * @return a collection of instance objects */ @Override public List<$!{tableInfo.name}> selectAll() { return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectAll(); } /** * Query according to conditions * * @return a collection of instance objects */ @Override public List<$!{tableInfo.name}> selectList($!{tableInfo.name} $!{tool.firstLowerCase($!{tableInfo.name})}) { return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectList($!{tool.firstLowerCase($!{tableInfo.name})}); } /** * New data * * @param $!tool.firstLowerCase($!{tableInfo.name}) instance object * @return instance object */ @Override @Transactional public int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) { return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.insert($!tool.firstLowerCase($!{tableInfo.name})); } /** * Batch add * * @param $!tool.firstLowerCase($!{tableInfo.name})s collection of instance objects * @return effective number */ @Override @Transactional public int batchInsert(List<$!{tableInfo.name}> $!tool.firstLowerCase($!{tableInfo.name})s) { return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.batchInsert($!tool.firstLowerCase($!{tableInfo.name})s); } /** * change the data * * @param $!tool.firstLowerCase($!{tableInfo.name}) instance object * @return instance object */ @Override @Transactional public $!{tableInfo.name} update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) { this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.update($!tool.firstLowerCase($!{tableInfo.name})); return this.selectById($!{tool.firstLowerCase($!{tableInfo.name})}.get$!tool.firstUpperCase($pk.name)()); } /** * Delete data by primary key * * @param $!pk.name primary key * @return is successful */ @Override @Transactional public int deleteById($!pk.shortType $!pk.name) { return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.deleteById($!pk.name); } /** * Query the total number of data * * @return Total number of data */ @Override public int count(){ return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.count(); } } Copy code

dao layer: dao.java code is as follows

##Define initial variables #set($tableName = $tool.append($tableInfo.name, "Dao")) ##Set callback $!callback.setFileName($tool.append($tableName, ".java")) $!callback.setSavePath($tool.append($tableInfo.savePath, "/dao")) ##Get the primary key #if(!$tableInfo.pkColumn.isEmpty()) #set($pk = $tableInfo.pkColumn.get(0)) #end #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}dao; import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import java.util.List; /** * $!{tableInfo.comment}($!{tableInfo.name}) table database access layer * * @author protagonist * @since $!time.currTime() */ @Mapper public interface $!{tableName} { /** * Query a single piece of data by ID * * @param $!pk.name primary key * @return instance object */ $!{tableInfo.name} selectById($!pk.shortType $!pk.name); /** * Enquire all * * @return object list */ List<$!{tableInfo.name}> selectAll(); /** * Query by entity as a filter condition * * @param $!tool.firstLowerCase($!{tableInfo.name}) instance object * @return object list */ List<$!{tableInfo.name}> selectList($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * New data * * @param $!tool.firstLowerCase($!{tableInfo.name}) instance object * @return affects the number of rows */ int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * Batch add * * @param $!tool.firstLowerCase($!{tableInfo.name})s collection of instance objects * @return affects the number of rows */ int batchInsert(List<$!{tableInfo.name}> $!tool.firstLowerCase($!{tableInfo.name})s); /** * change the data * * @param $!tool.firstLowerCase($!{tableInfo.name}) instance object * @return affects the number of rows */ int update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * Delete data by primary key * * @param $!pk.name primary key * @return affects the number of rows */ int deleteById($!pk.shortType $!pk.name); /** * Query the total number of data * * @return Total number of data */ int count(); } Copy code

The mapper.xml code is as follows:

##Introducing mybatis support $!mybatisSupport ##Set save name and save location $!callback.setFileName($tool.append($!{tableInfo.name}, "Dao.xml")) $!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapper")) ##Get the primary key #if(!$tableInfo.pkColumn.isEmpty()) #set($pk = $tableInfo.pkColumn.get(0)) #end <?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="$!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao"> <!-- Result set--> <resultMap type="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}" id="$!{tableInfo.name}Map"> #foreach($column in $tableInfo.fullColumn) <result property="$!column.name" column="$!column.obj.name" jdbcType="$!column.ext.jdbcType"/> #end </resultMap> <!-- Basic fields--> <sql id="Base_Column_List"> #allSqlColumn() </sql> <!-- Query a single--> <select id="selectById" resultMap="$!{tableInfo.name}Map"> select <include refid="Base_Column_List"/> from $!tableInfo.obj.name where $!pk.obj.name = #{$!pk.name} </select> <!-- Query all --> <select id="selectAll" resultMap="$!{tableInfo.name}Map"> select <include refid="Base_Column_List"/> from $!tableInfo.obj.name </select> <!--Query by entity as a filter condition--> <select id="selectList" resultMap="$!{tableInfo.name}Map"> select <include refid="Base_Column_List"/> from $!tableInfo.obj.name <where> #foreach($column in $tableInfo.fullColumn) <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name !=''#end"> and $!column.obj.name = #{$!column.name} </if> #end </where> </select> <!-- Add all rows --> <insert id="insert" keyProperty="$!pk.name" useGeneratedKeys="true"> insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.fullColumn)$!column.obj.name#if($velocityHasNext), #end#end) values (#foreach($column in $tableInfo.fullColumn)#{$!{column.name}}#if($velocityHasNext), #end#end) </insert> <!-- Add in batches--> <insert id="batchInsert"> insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.fullColumn)$!column.obj.name#if($velocityHasNext), #end#end) values <foreach collection="$!tool.firstLowerCase($!{tableInfo.name})s" item="item" index="index" separator=","> ( #foreach($column in $tableInfo.fullColumn) #{item.$!{column.name}}#if($velocityHasNext), #end #end ) </foreach> </insert> <!-- Modify data through the primary key --> <update id="update"> update $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name} <set> #foreach($column in $tableInfo.otherColumn) <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name !=''#end"> $!column.obj.name = #{$!column.name}, </if> #end </set> where $!pk.obj.name = #{$!pk.name} </update> <!--Delete by primary key--> <delete id="deleteById"> delete from $!{tableInfo.obj.name} where $!pk.obj.name = #{$!pk.name} </delete> <!-- Total resultType="int"> select count(*) from $!{tableInfo.obj.name} </select> </mapper> Copy code

testing successfully!

The code is all generated!