一、插件安装
1.这个不多说,idea安装插件应该都会,如有疑问可自行查阅相关文档
二、模板配置
easyCode有自带的模板生成文件,生成的分页语句是mysql的语法
这里我们新建一个模板来适应我们的项目 新创建一个group name 命名为myTemplate 在myTemplate下面新建模板文件
1.mapper.xml.vm
##引入mybatis支持
$!{mybatisSupport.vm}
##设置保存名称与保存位置
$!callback.setFileName($tool.append($!{tableInfo.name}, "Mapper.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}.mapper.$!{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>
<!--查询单个-->
<select id="selectByPrimaryKey" resultMap="$!{tableInfo.name}Map">
select
#allSqlColumn()
from $!tableInfo.obj.name
where $!pk.obj.name = #{$!pk.name}
</select>
<!--查询指定行数据-->
<select id="queryAll" resultMap="$!{tableInfo.name}Map">
select
#allSqlColumn()
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>
<!--统计总行数-->
<select id="count" resultType="java.lang.Long">
select count(1)
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="insertSelective" keyProperty="$!pk.name" useGeneratedKeys="true">
insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)
values (#foreach($column in $tableInfo.otherColumn)#{$!{column.name}}#if($velocityHasNext), #end#end)
</insert>
<insert id="insertBatch" keyProperty="$!pk.name" useGeneratedKeys="true">
insert all
<foreach collection="list" item="item" separator=",">
into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)
values
(#foreach($column in $tableInfo.otherColumn)#{item.$!{column.name}}#if($velocityHasNext), #end#end)
</foreach>
select 1 from dual
</insert>
<!--通过主键修改数据-->
<update id="updateByPrimaryKeySelective">
update $!{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="deleteByPrimaryKey">
delete from $!{tableInfo.obj.name} where $!pk.obj.name = #{$!pk.name}
</delete>
</mapper>
2.entity.java.vm
##引入宏定义
$!{define.vm}
##使用宏定义设置回调(保存位置与文件后缀)
#save("/entity", ".java")
##使用宏定义设置包后缀
#setPackageSuffix("entity")
##使用全局变量实现默认包导入
$!{autoImport.vm}
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
##使用宏定义实现类注释信息
#tableComment("实体类")
@ApiModel(value = "$!{tableInfo.name}", description = "$!{tableInfo.comment}")
public class $!{tableInfo.name} implements Serializable {
private static final long serialVersionUID = $!tool.serial();
#foreach($column in $tableInfo.fullColumn)
#if(${column.comment})#end
@ApiModelProperty(value = "${column.comment}")
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
#foreach($column in $tableInfo.fullColumn)
##使用宏定义实现get,set方法
#getSetMethod($column)
#end
}
3.service.java.vm
##定义初始变量
#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 com.github.pagehelper.PageInfo;
public interface $!{tableName} {
$!{tableInfo.name} selectByPrimaryKey($!pk.shortType $!pk.name);
PageInfo<$!{tableInfo.name}> queryByPage(int pageNum, int pageSize, $!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
$!{tableInfo.name} insertSelective($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
$!{tableInfo.name} updateByPrimaryKeySelective($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
int deleteByPrimaryKey($!pk.shortType $!pk.name);
}
4.serviceImpl.java.vm
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "ServiceImpl"))
##设置回调
$!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 $!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
@Service("$!tool.firstLowerCase($!{tableInfo.name})Service")
public class $!{tableName} implements $!{tableInfo.name}Service {
@Resource
private $!{tableInfo.name}Mapper mapper;
@Override
public $!{tableInfo.name} selectByPrimaryKey($!pk.shortType $!pk.name) {
return this.mapper.selectByPrimaryKey($!pk.name);
}
@Override
public PageInfo<$!{tableInfo.name}> queryByPage(int pageNum, int pageSize, $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}) {
PageHelper.startPage(pageNum,pageSize);
return new PageInfo<>(this.mapper.queryAll($!{tool.firstLowerCase($tableInfo.name)}));
}
@Override
public $!{tableInfo.name} insertSelective($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
this.mapper.insertSelective($!tool.firstLowerCase($!{tableInfo.name}));
return $!tool.firstLowerCase($!{tableInfo.name});
}
@Override
public $!{tableInfo.name} updateByPrimaryKeySelective($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
this.mapper.updateByPrimaryKeySelective($!tool.firstLowerCase($!{tableInfo.name}));
return this.selectByPrimaryKey($!{tool.firstLowerCase($!{tableInfo.name})}.get$!tool.firstUpperCase($pk.name)());
}
@Override
public int deleteByPrimaryKey($!pk.shortType $!pk.name) {
return this.mapper.deleteByPrimaryKey($!pk.name);
}
}
5.mapper.java.vm
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Mapper"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/mapper"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}mapper;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface $!{tableName} {
$!{tableInfo.name} selectByPrimaryKey($!pk.shortType $!pk.name);
List<$!{tableInfo.name}> queryAll($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
long count($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
int insertSelective($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
int insertBatch(@Param("list") List<$!{tableInfo.name}> list);
int updateByPrimaryKeySelective($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
int deleteByPrimaryKey($!pk.shortType $!pk.name);
}
6.controller.java.vm
##定义初始变量
#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 $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import javax.annotation.Resource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.pagehelper.PageInfo;
@RestController
@RequestMapping("/$!tool.firstLowerCase($tableInfo.name)")
@Api(value = "$!{tableInfo.comment}($!{tableInfo.name}")
public class $!{tableName} {
@Resource(name = "$!tool.firstLowerCase($!{tableInfo.name})Service")
private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
@ApiOperation(value = "分页查询$!{tableInfo.comment}数据")
@GetMapping("/page/{pageNum}/{pageSize}")
public ResponseEntity<List> queryByPage(@ApiParam(name = "pageNum", value = "当前页") @PathVariable(name="pageNum") int pageNum, @ApiParam(name = "pageSize", value = "每页条数") @PathVariable(name="pageSize") int pageSize, @ApiParam(name = "$!{tool.firstLowerCase($tableInfo.name)}", value = "$!{tableInfo.comment}对象") $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}) {
PageInfo<$!{tableInfo.name}> pageInfo = this.$!{tool.firstLowerCase($tableInfo.name)}Service.queryByPage(pageNum,pageSize,$!{tool.firstLowerCase($tableInfo.name)});
return ResponseEntity.ok(pageInfo.getList());
}
@ApiOperation(value = "通过$!{tool.firstLowerCase($!pk.name)}主键查询单条$!{tableInfo.comment}数据")
@GetMapping("/{$!{tool.firstLowerCase($!pk.name)}}")
public ResponseEntity<$!{tableInfo.name}> selectByPrimaryKey(@ApiParam(name = "$!{tool.firstLowerCase($!pk.name)}", value = "$!pk.name主键") @PathVariable(name="$!pk.name") $!pk.shortType $!{tool.firstLowerCase($!pk.name)}) {
return ResponseEntity.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.selectByPrimaryKey($!pk.name));
}
@ApiOperation(value = "新增单条$!{tableInfo.comment}数据")
@PostMapping
public ResponseEntity<$!{tableInfo.name}> insertSelective(@ApiParam(name = "$!{tool.firstLowerCase($tableInfo.name)}", value = "$!{tableInfo.comment}对象") @RequestBody $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}) {
return ResponseEntity.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.insertSelective($!{tool.firstLowerCase($tableInfo.name)}));
}
@ApiOperation(value = "通过主键$!{tool.firstLowerCase($!pk.name)}修改单条$!{tableInfo.comment}数据")
@PutMapping
public ResponseEntity<$!{tableInfo.name}> updateByPrimaryKeySelective(@ApiParam(name = "$!{tool.firstLowerCase($tableInfo.name)}", value = "$!{tableInfo.comment}") @RequestBody $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}) {
return ResponseEntity.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.updateByPrimaryKeySelective($!{tool.firstLowerCase($tableInfo.name)}));
}
@ApiOperation(value = "通过$!{tool.firstLowerCase($!pk.name)}主键删除单条$!{tableInfo.comment}数据")
@DeleteMapping("/{$!{tool.firstLowerCase($!pk.name)}}")
public ResponseEntity<Integer> deleteByPrimaryKey(@ApiParam(name = "$!{tool.firstLowerCase($!pk.name)}", value = "$!{tool.firstLowerCase($!pk.name)}主键") @PathVariable(name="$!pk.name") $!pk.shortType $!{tool.firstLowerCase($!pk.name)}) {
return ResponseEntity.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.deleteByPrimaryKey($!{tool.firstLowerCase($!pk.name)}));
}
}
三、新建springBoot项目
1.springBoot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
2.项目依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
<!--oracle数据库驱动-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>6.0</version>
</dependency>
<!--swagger相关依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!--mybatis分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.1</version>
</dependency>
</dependencies>
3.yml文件配置
注意替换数据库ip与账号密码
server:
port: 8088
spring:
application:
name: demo
datasource:
password: bbb
username: aaa
url: jdbc:oracle:thin:@***.**.*.***:1521:aaa
driver-class-name: oracle.jdbc.driver.OracleDriver
mybatis:
mapper-locations: classpath:mapper
4.启动类
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.example.demo.mapper")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
四、代码生成
1.数据库连接
加粗样式
填写数据库连接信息,可以点击test测试一下,有个出现绿色的√代表连接成功
2.一键生成代码
找到我们要生成的表,右键选择EasyCode,然后点击Generate Code 填写文件生成的位置信息,如果点击ok的时候报错可以在上一步的时候先进行config table 红色文件为自动生成的
3.接口访问测试
启动项目访问
|