idea使用easyCode生成代碼(根據(jù)mybatis-plus模板創(chuàng)建自己的模板)
前言
easyCode代碼生成器,減少低價(jià)值搬磚。
聲明下:自定義模板內(nèi)有大量自定義類,所以可以借鑒,不能直接使用。
一、安裝easyCode插件
setting下載插件,重啟idea,點(diǎn)開dataBase連接上數(shù)據(jù)庫。
針對有一定基礎(chǔ)的程序員哈,小白的話,建議先老老實(shí)實(shí)手敲代碼。
二、生成代碼-使用默認(rèn)模板
1: dataBase連接上數(shù)據(jù)庫后,選中某個(gè)表,右鍵->EasyCode->Generate Code,出現(xiàn)下面這個(gè)彈出框:
Module:是哪個(gè)項(xiàng)目模塊
Package:生成的代碼放在哪個(gè)包下
Path:生成代碼所在的路勁
紅框:這個(gè)是我自定義的模板(后面會(huì)怎么弄),這里可以選擇默認(rèn)的模板,如:MybatisPlus ,Default
Template:勾選需要生成哪些文件
2:選擇完成后,點(diǎn)擊OK就可以了。
三、生成代碼-自定義模板
File->settings->Other Settings->EasyCode->Template
我是復(fù)制了MybatisPlus- Mixed,然后重名為:MybatisPlus-lt。在此基礎(chǔ)上修改的模板內(nèi)容
注意1:模板內(nèi)使用#if()進(jìn)行邏輯判斷
我配置了字段自動(dòng)插入,需要在字段屬性上加額外的注解,所以這里用到了
#if($!{column.name} == ‘delFlag') @TableLogic @TableField(fill = FieldFill.INSERT) #end
注意2:模板內(nèi)獲取對象的小寫單詞
因?yàn)樵趯?shí)現(xiàn)類里有實(shí)現(xiàn)方法,所以需要使用到bookMapper,但是如何獲取到Book對象的小寫單詞是個(gè)問題:
##定義實(shí)體對象名(駝峰) #set($entityName = $!tool.firstLowerCase($!tableInfo.name))
說明下:
實(shí)現(xiàn)類里期望結(jié)果生成這個(gè)代碼:bookMapper.deleteBatchIds(idList); 錯(cuò)誤的: 下面這句的生成結(jié)果是:bookMapper; $!tool.firstLowerCase($!tableInfo.name))Mapper.deleteBatchIds(idList); 正確的: ##定義實(shí)體對象名 #set($entityName = $!tool.firstLowerCase($!tableInfo.name)) 這樣去使用: $!{entityName}Mapper.deleteBatchIds(idList);
例子:
注意3:修改Mapping.xml文件的位置
將BookMapping.xml 放在/src/main/resources/mapper/book目錄下
我的自定義模板,可借鑒,不可直接用
模板內(nèi)使用到了較多的自定義類,所以大家可以借鑒下,全盤照搬肯定是不行的。
自定義異常:CheckException
自定義返回類:BaseResponse
自定義查詢類:BaseQuery
自定義:Msg
分頁插件:PageHelper
id生成器:IdWorker
1: entity.java.vm
##導(dǎo)入宏定義 $!{define.vm} ##保存文件(宏定義) #save("/entity", ".java") ##包路徑(宏定義) #setPackageSuffix("entity") ##自動(dòng)導(dǎo)入包(全局變量) $!autoImport import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableLogic; import com.tao.base.BaseQuery; import lombok.Data; import java.util.Date; ##表注釋(宏定義) #tableComment("表實(shí)體類") @Data public class $!{tableInfo.name} extends BaseQuery { #foreach($column in $tableInfo.fullColumn) #if(${column.comment}) /** * ${column.comment} **/ #end #if($!{column.name} == 'delFlag') @TableLogic @TableField(fill = FieldFill.INSERT) #end #if($!{column.name} == 'createTime') @TableField(fill = FieldFill.INSERT) #end #if($!{column.name} == 'updateTime') @TableField(fill = FieldFill.INSERT_UPDATE) #end private $!{tool.getClsNameByFullName($column.type)} $!{column.name}; #end }
2: mapper.java.vm
##導(dǎo)入宏定義 $!{define.vm} ##設(shè)置表后綴(宏定義) #setTableSuffix("Mapper") ##保存文件(宏定義) #save("/dao", "Mapper.java") ##包路徑(宏定義) #setPackageSuffix("dao") import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Mapper; import $!{tableInfo.savePackageName}.entity.$!tableInfo.name; ##表注釋(宏定義) #tableComment("表數(shù)據(jù)庫訪問層") @Mapper public interface $!{tableName} extends BaseMapper<$!tableInfo.name> { }
3: service.java.vm
##導(dǎo)入宏定義 $!{define.vm} ##設(shè)置表后綴(宏定義) #setTableSuffix("Service") ##保存文件(宏定義) #save("/service", "Service.java") ##包路徑(宏定義) #setPackageSuffix("service") import com.baomidou.mybatisplus.extension.service.IService; import $!{tableInfo.savePackageName}.entity.$!tableInfo.name; ##表注釋(宏定義) #tableComment("表服務(wù)接口") public interface $!{tableName} extends IService<$!tableInfo.name> { /** * 根據(jù)ids刪除多條數(shù)據(jù) **/ void deleteByIds(String ids); }
4: serviceImpl.java.vm
##導(dǎo)入宏定義 $!{define.vm} ##設(shè)置表后綴(宏定義) #setTableSuffix("ServiceImpl") ##保存文件(宏定義) #save("/service/impl", "ServiceImpl.java") ##包路徑(宏定義) #setPackageSuffix("service.impl") ##定義實(shí)體對象名 #set($entityName = $!tool.firstLowerCase($!tableInfo.name)) import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Mapper; import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}; import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; ##表注釋(宏定義) #tableComment("表服務(wù)實(shí)現(xiàn)類") @Service("$!tool.firstLowerCase($tableInfo.name)Service") public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Mapper, $!{tableInfo.name}> implements $!{tableInfo.name}Service { @Resource private $!{tableInfo.name}Mapper ${entityName}Mapper; /** * 根據(jù)ids刪除多條數(shù)據(jù) **/ @Override public void deleteByIds(String ids) { String[] idsArr = ids.split(","); if (idsArr.length > 0) { List<Long> idList = Stream.of(idsArr).map(Long::valueOf).collect(Collectors.toList()); $!{entityName}Mapper.deleteBatchIds(idList); } } }
5: controller.java.vm
##導(dǎo)入宏定義 $!{define.vm} ##設(shè)置表后綴(宏定義) #setTableSuffix("Controller") ##保存文件(宏定義) #save("/controller", "Controller.java") ##包路徑(宏定義) #setPackageSuffix("controller") ##定義服務(wù)名 #set($serviceName = $!tool.append($!tool.firstLowerCase($!tableInfo.name), "Service")) ##定義實(shí)體對象名 #set($entityName = $!tool.firstLowerCase($!tableInfo.name)) import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.tao.Enum.Msg; import com.tao.base.BaseQuery; import com.tao.base.BaseResponse; import $!{tableInfo.savePackageName}.entity.$!tableInfo.name; import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service; import com.tao.exception.CheckException; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.annotations.Param; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; ##表注釋(宏定義) #tableComment("表控制層") @RestController @RequestMapping("$!tool.firstLowerCase($!tableInfo.name)") public class $!{tableName} { /** * 服務(wù)對象 */ @Autowired private $!{tableInfo.name}Service $!{serviceName}; /** * 列表查詢不分頁 查詢?nèi)? **/ @RequestMapping(value = "list", method = RequestMethod.POST) public BaseResponse list(@RequestBody $!tableInfo.name model) { //條件查詢 QueryWrapper<$!tableInfo.name> queryWrapper = new QueryWrapper(); //動(dòng)態(tài)sql // if(StringUtils.isNotBlank(model.getAuthor())){ // queryWrapper.lambda().eq($!tableInfo.name::getAuthor,model.getAuthor()); // } //排序 queryWrapper.lambda().orderByDesc($!tableInfo.name::getCreateTime); //執(zhí)行查詢 List<$!tableInfo.name> list = $!{serviceName}.list(queryWrapper); //封裝查詢結(jié)果 return new BaseResponse(Msg.SUCCESS, list); } /** * 列表查詢 分頁 * 只能查詢自己的 **/ @RequestMapping(value = "listPage", method = RequestMethod.POST) public BaseResponse listPage(@RequestBody $!tableInfo.name model) { //沒有傳分頁參數(shù)時(shí),默認(rèn)設(shè)置查詢前10條數(shù)據(jù) if (null == model.getPageSize()) { PageHelper.startPage(BaseQuery.DEFAULT_PAGE, BaseQuery.DEFAULT_SIZE); } else { PageHelper.startPage(model.getCurrentPage(), model.getPageSize()); } //條件查詢 QueryWrapper<$!tableInfo.name> queryWrapper = new QueryWrapper(); //動(dòng)態(tài)sql if (StringUtils.isNotBlank(model.getAuthor())) { queryWrapper.lambda().eq($!tableInfo.name::getAuthor, model.getAuthor()); } //排序 queryWrapper.lambda().orderByDesc($!tableInfo.name::getCreateTime); //執(zhí)行查詢 List<$!tableInfo.name> list = $!{serviceName}.list(queryWrapper); PageInfo<$!tableInfo.name> pageInfo = new PageInfo<>(list); //封裝查詢結(jié)果 return new BaseResponse(Msg.SUCCESS, pageInfo.getTotal(), pageInfo.getList()); } /** * 保存 **/ @RequestMapping(value = "insert", method = RequestMethod.POST) public BaseResponse insert(@RequestBody $!tableInfo.name model) { //設(shè)置默認(rèn)id if (model.getId() == null) { model.setId(IdWorker.getId()); } $!{serviceName}.save(model); //封裝查詢結(jié)果 return new BaseResponse(Msg.SUCCESS, model.getId()); } /** * 根據(jù)id更新 **/ @RequestMapping(value = "updateById", method = RequestMethod.PUT) public BaseResponse updateById(@RequestBody $!tableInfo.name model) throws Exception { //字段檢查 if (null == (model.getId())) { throw new CheckException(Msg.CHECK_ATTRIBUTE_FAIL, "id不能為空"); } $!{serviceName}.updateById(model); //封裝查詢結(jié)果 return new BaseResponse(Msg.SUCCESS); } /** * 根據(jù)id查詢 **/ @RequestMapping(value = "getById/{id}", method = RequestMethod.GET) public BaseResponse getById(@PathVariable("id") Long id) { $!tableInfo.name temp = $!{serviceName}.getById(id); //封裝查詢結(jié)果 return new BaseResponse(Msg.SUCCESS, temp); } /** * 根據(jù)id更新 **/ @RequestMapping(value = "deleteById/{id}", method = RequestMethod.DELETE) public BaseResponse deleteById(@PathVariable("id") Long id) { $!{serviceName}.removeById(id); //封裝查詢結(jié)果 return new BaseResponse(Msg.SUCCESS); } /** * 根據(jù)多個(gè)id刪除 **/ // @RequestMapping(value = "deleteByIds/{ids}", method = RequestMethod.DELETE) // public BaseResponse deleteByIds(@PathVariable("ids") String ids) throws Exception { @RequestMapping(value = "deleteByIds", method = RequestMethod.DELETE) public BaseResponse deleteByIds(@Param("ids") String ids) throws Exception { //業(yè)務(wù)檢查 if (StringUtils.isEmpty(ids)) { throw new CheckException(Msg.CHECK_ATTRIBUTE_FAIL, "請傳入正確的ids"); } $!{serviceName}.deleteByIds(ids); //封裝查詢結(jié)果 return new BaseResponse(Msg.SUCCESS); } }
6: mapping.xml.vm
##引入mybatis支持 $!{mybatisSupport.vm} ##定義實(shí)體對象名 #set($entityName = $!tool.firstLowerCase($!tableInfo.name)) ##設(shè)置保存名稱與保存位置 $!callback.setFileName($tool.append($!{tableInfo.name}, "Mapping.xml")) $!callback.setSavePath($tool.append($modulePath, $tool.append("/src/main/resources/mapper/",$entityName))) ##拿到主鍵 #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}Mapper"> <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> <!-- 批量插入 --> <!-- <insert id="insertBatch" keyProperty="$!pk.name" useGeneratedKeys="true"> insert into $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end) values <foreach collection="entities" item="entity" separator=","> (#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end) </foreach> </insert> --> </mapper>
到此這篇關(guān)于idea使用easyCode生成代碼(根據(jù)mybatis-plus模板創(chuàng)建自己的模板)的文章就介紹到這了,更多相關(guān)idea easyCode生成代碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springBoot項(xiàng)目打包idea的多種方法
這篇文章主要介紹了springBoot項(xiàng)目打包idea的多種方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07SpringCloud Sleuth實(shí)現(xiàn)分布式請求鏈路跟蹤流程詳解
這篇文章主要介紹了SpringCloud Sleuth實(shí)現(xiàn)分布式請求鏈路跟蹤流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-11-11Java中g(shù)etSuperclass()方法的使用與原理解讀
文章介紹了Java中的getSuperclass()方法,該方法用于獲取一個(gè)類的直接父類,通過理解其使用方式、工作原理以及實(shí)際應(yīng)用場景,可以更好地利用反射機(jī)制處理類的繼承關(guān)系,實(shí)現(xiàn)動(dòng)態(tài)類型檢查、類加載以及序列化等功能2025-01-01Spring?Security實(shí)現(xiàn)HTTP認(rèn)證
本文主要介紹了Spring?Security實(shí)現(xiàn)HTTP認(rèn)證,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>2022-06-06elasticsearch+logstash并使用java代碼實(shí)現(xiàn)日志檢索
這篇文章主要介紹了elasticsearch+logstash并使用java代碼實(shí)現(xiàn)日志檢索,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02利用5分鐘快速搭建一個(gè)springboot項(xiàng)目的全過程
Spring Boot的監(jiān)控能夠使開發(fā)者更好地掌控應(yīng)用程序的運(yùn)行狀態(tài),下面這篇文章主要給大家介紹了關(guān)于如何利用5分鐘快速搭建一個(gè)springboot項(xiàng)目的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05springboot2.0以上調(diào)度器配置線程池的實(shí)現(xiàn)
這篇文章主要介紹了springboot2.0以上調(diào)度器配置線程池的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12