IDEA必备开发神器之EasyCode
1、打开IntelliJ IDEA 新建一个maven工程
2、选择工程存放目录
3、下载安装EasyCode插件
file->settings->plugins 搜索Easy Code
搜索到后点击Install 我这里安装过了 安装完成会让你重启IDEA。
如何判断是否安装成功 file->settings->Other settings 看是否有Easy Code这个选项
4、引入Easy Code模板 (可以根据个人情况定制 也可以使用默认的)
5、创建数据库,数据表
6、配置数据源(需要指定数据库名称,要生成数据表)
点击IDEA右侧的Datbase->点击上方的加号->选择Data Source.->Mysql
7、配置数据源
注意:连接方式需要采用mysql8的连接方式,不然可能连接失败jdbc:mysql://localhost:3306/table?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false
8、连接成功如图所示
9、引入springboot的相关pom.xml依赖
<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的启动器,所以也一起引用
10、打开Easy Code插件 选要生成的entity,controller,service,serviceimpl,dao
右击表名->Easy Code->Generate Code
Module:当前项目模块
package:生成代码存放包的位置
Template:生成的模板
11、勾选All表示生成所有,勾选禁止提示,防止弹出很多个提示信息,点击OK
12、生成模板
entity.java代码:
##引入宏定义
$!define
$!init
##使用宏定义设置回调(保存位置与文件后缀)
#save("/entity", ".java")
##使用宏定义设置包后缀
#setPackageSuffix("entity")
##使用全局变量实现默认包导入
$!autoImport
import java.io.Serializable;
import lombok.Data;
##使用宏定义实现类注释信息
#tableComment("实体类")
@Data
public class $!{tableInfo.name} implements Serializable {
private static final long serialVersionUID = $!tool.serial();
#foreach($column in $tableInfo.fullColumn)
#if(${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;
@RestController
@Slf4j
@RequestMapping("/$!tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} {
@Resource
private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)ServiceImpl;
@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,"查询成功",result);
}
return new Result<>(true,StatusCode.ERROR,"查询失败");
}
@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,"新增失败");
}
@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,"修改失败");
}
@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,"删除失败");
}
@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,"查询全部数据失败");
}
return new Result<>(true,StatusCode.OK,"查询全部数据成功",$!tool.firstLowerCase($tableInfo.name)s);
}
@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,"分页条件查询成功",new PageResult<>(page.getTotal(),page.getList()));
}
return new Result<>(true,StatusCode.ERROR,"分页查询数据失败");
}
}
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;
public interface $!{tableName} {
$!{tableInfo.name} selectById($!pk.shortType $!pk.name);
PageInfo<$!{tableInfo.name}> selectPage(int current, int size);
List<$!{tableInfo.name}> selectAll();
List<$!{tableInfo.name}> selectList($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
int batchInsert(List<$!{tableInfo.name}> $!tool.firstLowerCase($!{tableInfo.name})s);
$!{tableInfo.name} update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
int deleteById($!pk.shortType $!pk.name);
int count();
}
serviceImpl 实现类: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;
@Service("$!tool.firstLowerCase($!{tableInfo.name})ServiceImpl")
public class $!{tableName} implements $!{tableInfo.name}Service {
@Resource
private $!{tableInfo.name}Dao $!tool.firstLowerCase($!{tableInfo.name})Dao;
@Override
public $!{tableInfo.name} selectById($!pk.shortType $!pk.name) {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectById($!pk.name);
}
@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);
}
@Override
public List<$!{tableInfo.name}> selectAll() {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectAll();
}
@Override
public List<$!{tableInfo.name}> selectList($!{tableInfo.name} $!{tool.firstLowerCase($!{tableInfo.name})}) {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectList($!{tool.firstLowerCase($!{tableInfo.name})});
}
@Override
@Transactional
public int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.insert($!tool.firstLowerCase($!{tableInfo.name}));
}
@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);
}
@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)());
}
@Override
@Transactional
public int deleteById($!pk.shortType $!pk.name) {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.deleteById($!pk.name);
}
@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;
@Mapper
public interface $!{tableName} {
$!{tableInfo.name} selectById($!pk.shortType $!pk.name);
List<$!{tableInfo.name}> selectAll();
List<$!{tableInfo.name}> selectList($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
int batchInsert(List<$!{tableInfo.name}> $!tool.firstLowerCase($!{tableInfo.name})s);
int update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
int deleteById($!pk.shortType $!pk.name);
int count();
}
mapper.xml代码如下:
##引入mybatis支持
$!mybatisSupport
##设置保存名称与保存位置
$!callback.setFileName($tool.append($!{tableInfo.name}, "Dao.xml"))
$!callback.setSavePath($tool.append($modulePath, "/class="lazy" data-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>
<!-- 查询单个 -->
<select id="selectById" resultMap="$!{tableInfo.name}Map">
select
<include refid="Base_Column_List" />
from $!tableInfo.obj.name
where $!pk.obj.name = #{$!pk.name}
</select>
<!-- 查询全部 -->
<select id="selectAll" resultMap="$!{tableInfo.name}Map">
select
<include refid="Base_Column_List" />
from $!tableInfo.obj.name
</select>
<!--通过实体作为筛选条件查询-->
<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>
<!-- 通过主键修改数据 -->
<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 id="deleteById">
delete from $!{tableInfo.obj.name} where $!pk.obj.name = #{$!pk.name}
</delete>
<!-- 总数 resultType="int">
select count(*) from $!{tableInfo.obj.name}
</select>
</mapper>
测试成功!
代码就全部生成出来了!
到此这篇关于Java必备开发神器之EasyCode的文章就介绍到这了,更多相关开发神器EasyCode内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341