- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- IDEA必備開(kāi)發(fā)神器之EasyCode
1、打開(kāi)IntelliJ IDEA 新建一個(gè)maven工程
2、選擇工程存放目錄
3、下載安裝EasyCode插件
file->settings->plugins 搜索Easy Code
搜索到后點(diǎn)擊Install 我這里安裝過(guò)了 安裝完成會(huì )讓你重啟IDEA。
如何判斷是否安裝成功 file->settings->Other settings 看是否有Easy Code這個(gè)選項
4、引入Easy Code模板 (可以根據個(gè)人情況定制 也可以使用默認的)
6、配置數據源(需要指定數據庫名稱(chēng),要生成數據表)
點(diǎn)擊IDEA右側的Datbase->點(diǎn)擊上方的加號->選擇Data Source.->Mysql
7、配置數據源
注意:連接方式需要采用mysql8的連接方式,不然可能連接失敗jdbc:mysql://localhost:3306/table?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false
8、連接成功如圖所示
9、引入springboot的相關(guān)pom.xml依賴(lài)
<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>
需要用到mybatis的啟動(dòng)器,所以也一起引用
10、打開(kāi)Easy Code插件 選要生成的entity,controller,service,serviceimpl,dao
右擊表名->Easy Code->Generate Code
Module:當前項目模塊
package:生成代碼存放包的位置
Template:生成的模板
11、勾選All表示生成所有,勾選禁止提示,防止彈出很多個(gè)提示信息,點(diǎn)擊OK
12、生成模板
entity.java代碼:
##引入宏定義 $!define $!init ##使用宏定義設置回調(保存位置與文件后綴) #save("/entity", ".java") ##使用宏定義設置包后綴 #setPackageSuffix("entity") ##使用全局變量實(shí)現默認包導入 $!autoImport import java.io.Serializable; import lombok.Data; ##使用宏定義實(shí)現類(lèi)注釋信息 #tableComment("實(shí)體類(lèi)") @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 }
controller接口:controller.java代碼如下:
##定義初始變量 #set($tableName = $tool.append($tableInfo.name, "Controller")) ##設置回調 $!callback.setFileName($tool.append($tableName, ".java")) $!callback.setSavePath($tool.append($tableInfo.savePath, "/controller")) ##拿到主鍵 #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})控制層 * * @author protagonist * @since $!time.currTime() */ @RestController @Slf4j @RequestMapping("/$!tool.firstLowerCase($tableInfo.name)") public class $!{tableName} { /** * 服務(wù)對象 */ @Resource private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)ServiceImpl; /** * 通過(guò)主鍵查詢(xún)單條數據 * * @param $!pk.name 主鍵 * @return 單條數據 */ @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,"查詢(xún)成功",result); } return new Result<>(true,StatusCode.ERROR,"查詢(xún)失敗"); } /** * 新增一條數據 * * @param $!tool.firstLowerCase($tableInfo.name) 實(shí)體類(lèi) * @return Result對象 */ @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,"新增成功",result); } return new Result<>(true,StatusCode.ERROR,"新增失敗"); } /** * 修改一條數據 * * @param $!tool.firstLowerCase($tableInfo.name) 實(shí)體類(lèi) * @return Result對象 */ @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,"修改成功",result); } return new Result<>(true,StatusCode.ERROR,"修改失敗"); } /** * 刪除一條數據 * * @param $!pk.name 主鍵 * @return Result對象 */ @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,"刪除成功",result); } return new Result<>(true,StatusCode.ERROR,"刪除失敗"); } /** * 查詢(xún)全部 * * @return Result對象 */ @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,"查詢(xún)全部數據失敗"); } return new Result<>(true,StatusCode.OK,"查詢(xún)全部數據成功",$!tool.firstLowerCase($tableInfo.name)s); } /** * 分頁(yè)查詢(xún) * * @param current 當前頁(yè) 第零頁(yè)和第一頁(yè)的數據是一樣 * @param size 每一頁(yè)的數據條數 * @return Result對象 */ @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,"分頁(yè)條件查詢(xún)成功",new PageResult<>(page.getTotal(),page.getList())); } return new Result<>(true,StatusCode.ERROR,"分頁(yè)查詢(xún)數據失敗"); } }
service接口:service.java 代碼如下:
##定義初始變量 #set($tableName = $tool.append($tableInfo.name, "Service")) ##設置回調 $!callback.setFileName($tool.append($tableName, ".java")) $!callback.setSavePath($tool.append($tableInfo.savePath, "/service")) ##拿到主鍵 #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})表服務(wù)接口 * * @author protagonist * @since $!time.currTime() */ public interface $!{tableName} { /** * 通過(guò)ID查詢(xún)單條數據 * * @param $!pk.name 主鍵 * @return 實(shí)例對象 */ $!{tableInfo.name} selectById($!pk.shortType $!pk.name); /** * 分頁(yè)查詢(xún) * * @param current 當前頁(yè) * @param size 每一頁(yè)數據的條數 * @return 對象列表 */ PageInfo<$!{tableInfo.name}> selectPage(int current, int size); /** * 查詢(xún)全部 * * @return 對象列表 */ List<$!{tableInfo.name}> selectAll(); /** * 通過(guò)實(shí)體作為篩選條件查詢(xún) * * @param $!tool.firstLowerCase($!{tableInfo.name}) 實(shí)例對象 * @return 對象列表 */ List<$!{tableInfo.name}> selectList($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * 新增數據 * * @param $!tool.firstLowerCase($!{tableInfo.name}) 實(shí)例對象 * @return 影響行數 */ int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * 批量新增 * * @param $!tool.firstLowerCase($!{tableInfo.name})s 實(shí)例對象的集合 * @return 影響行數 */ int batchInsert(List<$!{tableInfo.name}> $!tool.firstLowerCase($!{tableInfo.name})s); /** * 修改數據 * * @param $!tool.firstLowerCase($!{tableInfo.name}) 實(shí)例對象 * @return 修改 */ $!{tableInfo.name} update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * 通過(guò)主鍵刪除數據 * * @param $!pk.name 主鍵 * @return 影響行數 */ int deleteById($!pk.shortType $!pk.name); /** * 查詢(xún)總數據數 * * @return 數據總數 */ int count(); }
serviceImpl 實(shí)現類(lèi):serviceImpl .java代碼如下:
##定義初始變量 #set($tableName = $tool.append($tableInfo.name, "ServiceImpl")) ##設置回調 $!callback.setFileName($tool.append($tableName, ".java")) $!callback.setSavePath($tool.append($tableInfo.savePath, "/service/impl")) ##拿到主鍵 #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}表)服務(wù)實(shí)現類(lèi) * * @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; /** * 通過(guò)ID查詢(xún)單條數據 * * @param $!pk.name 主鍵 * @return 實(shí)例對象 */ @Override public $!{tableInfo.name} selectById($!pk.shortType $!pk.name) { return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectById($!pk.name); } /** * 分頁(yè)查詢(xún) * * @param current 當前頁(yè) * @param size 每一頁(yè)的條數 * @return 對象列表 */ @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); } /** * 查詢(xún)所有 * * @return 實(shí)例對象的集合 */ @Override public List<$!{tableInfo.name}> selectAll() { return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectAll(); } /** * 根據條件查詢(xún) * * @return 實(shí)例對象的集合 */ @Override public List<$!{tableInfo.name}> selectList($!{tableInfo.name} $!{tool.firstLowerCase($!{tableInfo.name})}) { return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectList($!{tool.firstLowerCase($!{tableInfo.name})}); } /** * 新增數據 * * @param $!tool.firstLowerCase($!{tableInfo.name}) 實(shí)例對象 * @return 實(shí)例對象 */ @Override @Transactional public int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) { return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.insert($!tool.firstLowerCase($!{tableInfo.name})); } /** * 批量新增 * * @param $!tool.firstLowerCase($!{tableInfo.name})s 實(shí)例對象的集合 * @return 生效的條數 */ @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); } /** * 修改數據 * * @param $!tool.firstLowerCase($!{tableInfo.name}) 實(shí)例對象 * @return 實(shí)例對象 */ @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)()); } /** * 通過(guò)主鍵刪除數據 * * @param $!pk.name 主鍵 * @return 是否成功 */ @Override @Transactional public int deleteById($!pk.shortType $!pk.name) { return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.deleteById($!pk.name); } /** * 查詢(xún)總數據數 * * @return 數據總數 */ @Override public int count(){ return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.count(); } }
dao層:dao.java代碼如下
##定義初始變量 #set($tableName = $tool.append($tableInfo.name, "Dao")) ##設置回調 $!callback.setFileName($tool.append($tableName, ".java")) $!callback.setSavePath($tool.append($tableInfo.savePath, "/dao")) ##拿到主鍵 #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})表數據庫訪(fǎng)問(wèn)層 * * @author protagonist * @since $!time.currTime() */ @Mapper public interface $!{tableName} { /** * 通過(guò)ID查詢(xún)單條數據 * * @param $!pk.name 主鍵 * @return 實(shí)例對象 */ $!{tableInfo.name} selectById($!pk.shortType $!pk.name); /** * 查詢(xún)全部 * * @return 對象列表 */ List<$!{tableInfo.name}> selectAll(); /** * 通過(guò)實(shí)體作為篩選條件查詢(xún) * * @param $!tool.firstLowerCase($!{tableInfo.name}) 實(shí)例對象 * @return 對象列表 */ List<$!{tableInfo.name}> selectList($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * 新增數據 * * @param $!tool.firstLowerCase($!{tableInfo.name}) 實(shí)例對象 * @return 影響行數 */ int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * 批量新增 * * @param $!tool.firstLowerCase($!{tableInfo.name})s 實(shí)例對象的集合 * @return 影響行數 */ int batchInsert(List<$!{tableInfo.name}> $!tool.firstLowerCase($!{tableInfo.name})s); /** * 修改數據 * * @param $!tool.firstLowerCase($!{tableInfo.name}) 實(shí)例對象 * @return 影響行數 */ int update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * 通過(guò)主鍵刪除數據 * * @param $!pk.name 主鍵 * @return 影響行數 */ int deleteById($!pk.shortType $!pk.name); /** * 查詢(xún)總數據數 * * @return 數據總數 */ int count(); }
mapper.xml代碼如下:
##引入mybatis支持 $!mybatisSupport ##設置保存名稱(chēng)與保存位置 $!callback.setFileName($tool.append($!{tableInfo.name}, "Dao.xml")) $!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapper")) ##拿到主鍵 #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"> <!-- 結果集 --> <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> <!-- 基本字段 --> <sql id="Base_Column_List"> #allSqlColumn() </sql> <!-- 查詢(xún)單個(gè) --> <select id="selectById" resultMap="$!{tableInfo.name}Map"> select <include refid="Base_Column_List" /> from $!tableInfo.obj.name where $!pk.obj.name = #{$!pk.name} </select> <!-- 查詢(xún)全部 --> <select id="selectAll" resultMap="$!{tableInfo.name}Map"> select <include refid="Base_Column_List" /> from $!tableInfo.obj.name </select> <!--通過(guò)實(shí)體作為篩選條件查詢(xún)--> <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> <!-- 新增所有列 --> <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> <!-- 批量新增 --> <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> <!-- 通過(guò)主鍵修改數據 --> <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> <!--通過(guò)主鍵刪除--> <delete id="deleteById"> delete from $!{tableInfo.obj.name} where $!pk.obj.name = #{$!pk.name} </delete> <!-- 總數 resultType="int"> select count(*) from $!{tableInfo.obj.name} </select> </mapper>
測試成功!
代碼就全部生成出來(lái)了!
到此這篇關(guān)于Java必備開(kāi)發(fā)神器之EasyCode的文章就介紹到這了,更多相關(guān)開(kāi)發(fā)神器EasyCode內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站