Introduction to the

Easy Code plug-in is used in IDEA with Mybatis – Plus to quickly generate restful API based on SpringBoot to improve work efficiency.

The auto-generated code is based on single-table operations.

Easy Code address: gitee.com/makejava/Ea…

operation

1. Prepare

Creating a Database

Create a new table and suggest that fields be named with underscores.

You are advised to use ID for the primary key or not. The template will add @tableID (type = idtype.auto) based on whether the primary key is named id. If the primary key is named otherwise, you need to add this annotation yourself

CREATE TABLE `dog` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'id',
  `name` varchar(30) NOT NULL COMMENT 'name',
  `age` int NOT NULL COMMENT 'age',
  `del_flag` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Delete flag,0= undeleted,1= deleted'.PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB COMMENT='dog';
Copy the code

Create a SpringBoot project

Core dependencies are as follows:

< the dependency > < groupId > com. Baomidou < / groupId > < artifactId > mybatis - plus - the boot - starter < / artifactId > < version > 3.3.2 rainfall distribution on 10-12 < / version >  </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>Copy the code

2. Download the plug-in

If you can’t find a plugin in the marketplace, go to plugins.jetbrains.com/, download it and install it from your hard drive. Specific tutorial 100 degrees, many online.

3. Configure the template

When modifying a template, you are advised not to modify the default template. You are advised to copy a template and modify it.

You are advised to add a suffix to the template name, which will be highlighted. For details, see the end of this article.

4. Connect to the database

5. Code generation

After the connection is successful, find the table to generate code, you can first check the configuration, there is no field type mapping error, if there is, you can manually configure it.

Here is a single table configuration, but can also be configured globally.

Once that’s all right, you can click the Generate button

After the table is generated, the configuration information of the table is generated in the IDEA directory. If the generated code has problems, you can delete the generated configuration file and generate the code again.

Note: the generated mapper.xml needs to be modified by removing a “, “.

Once generated, you can test the interface to see how it works and adjust it according to your business needs.

Other advanced gameplay can refer to the official documentation.

Add: template

entity.java
## import macro definition $! Define ## Save ("/entity", ".java") ## package path (macro definition) #setPackageSuffix("entity") # autoImport import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; import java.util.Date; @data.constructor @noargsconstructor @apiModel ("$! {tableInfo.comment}") public class $! {tableInfo.name} implements Serializable { private static final long serialVersionUID = $! tool.serial(); #foreach($column in $tableInfo.fullColumn) #if(${column.comment})/** * ${column.comment} */#end #if(${column.comment})@ApiModelProperty(value = "${column.comment}")#end #if($column.name.equals('id'))@TableId(type = IdType.AUTO)#end private $! {tool.getClsNameByFullName($column.type)} $! {column.name}; #end }Copy the code
mapper.java
## import macro definition $! ## save("/ Mapper") ## save("/ Mapper") "Mapper. Java") # # # package path (macro definition) setPackageSuffix (" Mapper ") import com. Baomidou. Mybatisplus. Core. Mapper. BaseMapper; import $! {tableInfo.savePackageName}.entity.$! tableInfo.name; #tableComment(" tableComment ") public interface $! {tableName} extends BaseMapper<$! tableInfo.name> { }Copy the code
service.java
## import macro definition $! Define ## setTableSuffix(macro definition) #setTableSuffix("Service") "Service. Java") # # # package path (macro definition) setPackageSuffix (" Service ") import com. Baomidou. Mybatisplus. The extension. Service. IService; import $! {tableInfo.savePackageName}.entity.$! tableInfo.name; #tableComment(" tableComment ") public interface $! {tableName} extends IService<$! tableInfo.name> { }Copy the code
serviceImpl.java
## import macro definition $! Define ## setTableSuffix(macro definition) #setTableSuffix("ServiceImpl") "Serviceimp.java ") ## Package path (macro definition) #setPackageSuffix("service.impl") import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import $! {tableInfo.savePackageName}.entity.$! {tableInfo.name}; import $! {tableInfo.savePackageName}.mapper.$! {tableInfo.name}Mapper; import $! {tableInfo.savePackageName}.service.$! {tableInfo.name}Service; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @slf4j @service public class $! {tableName} extends ServiceImpl<$! {tableInfo.name}Mapper, $! {tableInfo.name}> implements $! {tableInfo.name}Service { }Copy the code
controller.java
## import macro definition $! Define ## setTableSuffix(macro definition) #setTableSuffix("Controller") # #set($serviceName = $!) #set($serviceName = $! tool.append($! tool.firstLowerCase($! Set ($entityName = $! tool.firstLowerCase($! tableInfo.name)) import $! {tableInfo.savePackageName}.entity.$! tableInfo.name; import $! {tableInfo.savePackageName}.service.$! {tableInfo.name}Service; import io.swagger.annotations.ApiOperation; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.api.R; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.io.Serializable; import java.util.List; #tableComment(" table control layer [not recommended, if there are new methods, write in the subclass]") public class $! {tableName} {/** * service object */ @autowired $! {tableInfo.name}Service $! {serviceName}; /** * paged query all data ** @param page paged object * @param $! EntityName * @apiOperation */ @apiOperation public R<IPage<$! tableInfo.name>> selectAll(Page<$! tableInfo.name> page, $! tableInfo.name $! entityName) { return R.ok ($! {serviceName}.page(page, new QueryWrapper<>($! entityName))); } /** * @param ID primary key * @return single data */ @apiOperation (" primary key query single data ") @getMapping ("{id}") public R<$! tableInfo.name> selectOne(@PathVariable Serializable id) { return R.ok($! {serviceName}.getById(id)); } /** * add data ** @param $! PostMapping public R<Long> insert(@requestBody $! tableInfo.name $! entityName) { boolean rs = $! {serviceName}.save($! entityName); return R.ok(rs? $! {entityName}.getId():0); } /** * modify data ** @param $! EntityName entity object * @return Modify result */ @apiOperation (" modify data ") @putMapping public R<Boolean> Update (@requestBody $! tableInfo.name $! entityName) { return R.ok($! {serviceName}.updateById($! entityName)); } /** ** @param idList * @return */ @apiOperation (" single/batch delete data ") @deletemapping public R<Boolean> delete(@RequestParam("idList") List<Long> idList) { return R.ok($! {serviceName}.removeByIds(idList)); }}Copy the code
extendController.java
## import macro definition $! Define ## setTableSuffix(macro definition) #setTableSuffix("ExtendController") # #setPackageSuffix("controller") #set($serviceName = $! tool.append($! tool.firstLowerCase($! Set ($entityName = $! tool.firstLowerCase($! tableInfo.name)) import $! {tableInfo.savePackageName}.pojo.$! tableInfo.name; import $! {tableInfo.savePackageName}.service.$! {tableInfo.name}Service; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.api.R; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import lombok.extern.slf4j.Slf4j; import io.swagger.annotations.Api; import java.io.Serializable; import java.util.List; #tableComment(" table control layer extension class, generally generated for the first time, no longer overwrite. This class provides the ability to write your own defined methods. ) @Api(tags = "$! {tableInfo.comment} ") @slf4j @restController @requestMapping ("$! tool.firstLowerCase($! tableInfo.name)") public class $! {tableName} extends $! {tableInfo.name}Controller { }Copy the code
mapper.xml
## MyBatis support $! MybatisSupport ## set save name and save location $! callback.setFileName($tool.append($! {tableInfo.name}, "Mapper.xml")) $! The callback. SetSavePath ($tool. Append ($modulePath, "/ SRC/main/resources/mapper")) # # # to get the primary key if (! $tableInfo.pkColumn.isEmpty()) #set($pk = $tableInfo.pkColumn.get(0)) #end <? 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="$! {tableInfo.savePackageName}.mapper.$! {tableInfo.name}Mapper"> <sql id="Base_Column_List"> #foreach($column in $tableInfo.fullColumn) $! column.obj.name, #end<! </ SQL > <resultMap type="$! {tableInfo.savePackageName}.entity.$! {tableInfo.name}" id="$! {tableInfo.name}BaseResultMap"> #foreach($column in $tableInfo.fullColumn) <result property="$! column.name" column="$! column.obj.name"/> #end </resultMap> </mapper>Copy the code