使用 EasyCode生成springboot+mybatis基础程序
一、前言
此文将分享我个人使用的一个easycode生成方法,生成之后可以直接运行,这也就意味着,生成的代码会更加规范化。规范化就意味着会有更多的约束。
二、正文
2.1 基础前提
2.1.1springboot配置
<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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://ip:3306/dbname?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
username: xxx
password: xxx
mybatis:
type-aliases-package: xxx.entity
mapper-locations: classpath:mapper/*.xml
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
logging:
level:
top:
yuechenc:
yueduapi:
mapper : debug
2.1.1 基础工具类
以下是我个人封装的pagehelper分页工具类和统一的返回以及swagger配置
- pagehelper工具类
如上图所示,在启动类所在目录下新建目录common.page/common.util 在page目录新建类PageRequest.java
package xxx.common.page;
public class PageRequest {
private int pageNum;
private int pageSize;
public PageRequest(int start, int limit) {
pageNum=start;
pageSize=limit;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}
在page目录新建类PageResult.java
package xxx.common.page;
import java.util.List;
public class PageResult {
private int pageNum;
private int pageSize;
private long totalSize;
private int totalPages;
private List<?> content;
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public long getTotalSize() {
return totalSize;
}
public void setTotalSize(long totalSize) {
this.totalSize = totalSize;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public List<?> getContent() {
return content;
}
public void setContent(List<?> content) {
this.content = content;
}
}
在util目录下新建工具类PageUtils.java
package xxx.common.util;
import com.github.pagehelper.PageInfo;
import xxx.common.page.PageRequest;
import xxx.common.page.PageResult;
public class PageUtils {
public static PageResult getPageResult(PageRequest pageRequest, PageInfo<?> pageInfo) {
PageResult pageResult = new PageResult();
pageResult.setPageNum(pageInfo.getPageNum());
pageResult.setPageSize(pageInfo.getPageSize());
pageResult.setTotalSize(pageInfo.getTotal());
pageResult.setTotalPages(pageInfo.getPages());
pageResult.setContent(pageInfo.getList());
return pageResult;
}
}
- 统一返回体
在common目录下新建Status.java
package xxx.common;
public enum Status {
FAIL("101", "失败")
,GET_FAIL("111", "查询失败")
,ADD_FAIL("121", "添加失败")
,DELETE_FAIL("131", "删除失败")
,UPDATE_FAIL("141", "修改失败")
,SUCCESS("100", "成功")
,GET_SUCCESS("110", "查询成功")
,ADD_SUCCESS("120", "添加成功")
,DELETE_SUCCESS("130", "删除成功")
,UPDATE_SUCCESS("140", "修改成功")
,ERROR("201", "错误")
,USER_NOFOUND("211", "用户不存在")
,ERROR_ACCOUNT("212", "账号或密码错误")
,USER_EXIST("213", "用户已存在")
,USER_LOCK("214", "账号被锁定,请联系管理员")
,IP_LOCK("215", "IP 被锁定,请联系管理员")
,PARAM_ERROR("303", "参数错误")
,Token_Expired("1044", "token Invalid expired");
public String status;
public String msg;
Status(String status, String msg) {
this.status = status;
this.msg = msg;
}
}
在common目录下新建ReturnData.java
package xxx.common;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
public class ReturnData<T> {
private String status;
private String msg;
private T data;
public static <T> ReturnData<T> SUCCESS(T data) {
return new ReturnData<T>(Status.SUCCESS.status, Status.SUCCESS.msg, data);
}
public static <T> ReturnData<T> SUCCESS(String msg) {
return new ReturnData<T>(Status.SUCCESS.status, msg);
}
public static <T> ReturnData<T> SUCCESS() {
return new ReturnData<T>(Status.SUCCESS.status, Status.SUCCESS.msg);
}
public static <T> ReturnData<T> GET_SUCCESS(T data) {
return new ReturnData<T>(Status.GET_SUCCESS.status, Status.GET_SUCCESS.msg, data);
}
public static <T> ReturnData<T> GET_SUCCESS(String msg) {
return new ReturnData<T>(Status.GET_SUCCESS.status, msg);
}
public static <T> ReturnData<T> GET_SUCCESS() {
return new ReturnData<T>(Status.GET_SUCCESS.status, Status.GET_SUCCESS.msg);
}
public static <T> ReturnData<T> ADD_SUCCESS() {
return new ReturnData<T>(Status.ADD_SUCCESS.status, Status.ADD_SUCCESS.msg);
}
public static <T> ReturnData<T> ADD_SUCCESS(T data) {
return new ReturnData<T>(Status.ADD_SUCCESS.status, Status.ADD_SUCCESS.msg,data);
}
public static <T> ReturnData<T> DELETE_SUCCESS() {
return new ReturnData<T>(Status.DELETE_SUCCESS.status, Status.DELETE_SUCCESS.msg);
}
public static <T> ReturnData<T> UPDATE_SUCCESS() {
return new ReturnData<T>(Status.UPDATE_SUCCESS.status, Status.UPDATE_SUCCESS.msg);
}
public static <T> ReturnData<T> UPDATE_SUCCESS(T data) {
return new ReturnData<T>(Status.UPDATE_SUCCESS.status, Status.UPDATE_SUCCESS.msg,data);
}
public static <T> ReturnData<T> FAIL(String msg) {
return new ReturnData<T>(Status.FAIL.status, msg);
}
public static <T> ReturnData<T> FAIL() {
return new ReturnData<T>(Status.FAIL.status, Status.FAIL.msg);
}
public static <T> ReturnData<T> GET_FAIL(String msg) {
return new ReturnData<T>(Status.GET_FAIL.status, msg);
}
public static <T> ReturnData<T> GET_FAIL() {
return new ReturnData<T>(Status.GET_FAIL.status, Status.FAIL.msg);
}
public static <T> ReturnData<T> ADD_FAIL() {
return new ReturnData<T>(Status.ADD_FAIL.status, Status.ADD_FAIL.msg);
}
public static <T> ReturnData<T> DELETE_FAIL() {
return new ReturnData<T>(Status.DELETE_FAIL.status, Status.DELETE_FAIL.msg);
}
public static <T> ReturnData<T> UPDATE_FAIL() {
return new ReturnData<T>(Status.UPDATE_FAIL.status, Status.UPDATE_FAIL.msg);
}
public static <T> ReturnData<T> ERROR(String msg) {
return new ReturnData<T>(Status.ERROR.status, msg);
}
public static <T> ReturnData<T> ERROR() {
return new ReturnData<T>(Status.ERROR.status, Status.ERROR.msg);
}
public ReturnData(String status, String msg, T data) {
this.status = status;
this.msg = msg;
this.data = data;
}
public ReturnData(String status, String msg) {
this.status = status;
this.msg = msg;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
@Override
public String toString() {
return JSON.toJSONString(this);
}
public String toAllString() {
return JSON.toJSONString(this, SerializerFeature.WriteMapNullValue);
}
}
- swagger3配置
在common目录下新建Swagger3Config.java
package xxx.common;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class Swagger3Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("xxx.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger3接口文档")
.description("测试API")
.contact(new Contact("测试API", "http://localhost:8080/swagger-ui/index.html", "1757895437@qq.com"))
.version("1.0")
.build();
}
}
2.2 模板设置
2.2.1安装idea插件:EasyCode
如图所示,打开idea的设置界面,选择插件,搜索EasyCode,点击安装即可
2.2.2 设置模板
如图:依次打开idea设置-> 其他设置->EasyCode-MyBatisCodeHelper->Template Setting 右边的就是生成代码的模板,一次将下列模板粘贴到里面即可:
entity
##引入宏定义
$!define
##使用宏定义设置回调(保存位置与文件后缀)
#save("/entity", ".java")
##使用宏定义设置包后缀
#setPackageSuffix("entity")
##使用全局变量实现默认包导入
$!autoImport
import java.io.Serializable;
##使用宏定义实现类注释信息
#tableComment("实体类")
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
#foreach($column in $tableInfo.fullColumn)
##使用宏定义实现get,set方法
#getSetMethod($column)
#end
@Override
public String toString(){
return "$tableInfo.name {" +
#foreach($column in $tableInfo.fullColumn)
"$column.name : " + $column.name + ", " +
#end
'}';
}
}
dao
##定义初始变量
#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.Param;
import java.util.List;
import java.util.Map;
public interface $!{tableName} {
$!{tableInfo.name} selectById($!pk.shortType $!pk.name);
List<$!{tableInfo.name}> selectPage(@Param("start") int start, @Param("limit") int limit);
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();
}
service
##定义初始变量
#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 java.util.Map;
import $!{tableInfo.savePackageName}.common.page.PageResult;
public interface $!{tableName} {
$!{tableInfo.name} selectById($!pk.shortType $!pk.name);
PageResult selectPage(int start, int limit);
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
##定义初始变量
#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.stereotype.Service;
import $!{tableInfo.savePackageName}.common.page.PageResult;
import $!{tableInfo.savePackageName}.common.page.PageRequest;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import $!{tableInfo.savePackageName}.common.util.PageUtils;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@Service("$!tool.firstLowerCase($!{tableInfo.name})Service")
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 PageResult selectPage(int start, int limit) {
PageRequest pageRequest=new PageRequest(start,limit);
return PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest));
}
private PageInfo<$!{tableInfo.name}> getPageInfo(PageRequest pageRequest) {
int pageNum = pageRequest.getPageNum();
int pageSize = pageRequest.getPageSize();
PageHelper.startPage(pageNum, pageSize);
List<$!{tableInfo.name}> $!{tool.firstLowerCase($!{tableInfo.name})}s = this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectAll();
return new PageInfo<>($!{tool.firstLowerCase($!{tableInfo.name})}s);
}
@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
public int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.insert($!tool.firstLowerCase($!{tableInfo.name}));
}
@Override
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
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
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();
}
}
controller
##定义初始变量
#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.web.bind.annotation.*;
import $!{tableInfo.savePackageName}.common.ReturnData;
import $!{tableInfo.savePackageName}.common.page.PageResult;
import java.util.List;
import javax.annotation.Resource;
@RestController
@RequestMapping("/$!tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} {
@Resource
private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
@RequestMapping(value = "get", method = RequestMethod.GET)
public ReturnData<$tableInfo.name> selectOne($tableInfo.name $!tool.firstLowerCase($tableInfo.name)) {
$tableInfo.name result = $!{tool.firstLowerCase($tableInfo.name)}Service.selectById($!{tool.firstLowerCase($tableInfo.name)}.getId());
if(result != null){
return ReturnData.GET_SUCCESS(result);
}
return ReturnData.GET_FAIL();
}
@RequestMapping(value = "insert", method = RequestMethod.POST)
public ReturnData<$tableInfo.name> insert(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)) {
int result = $!{tool.firstLowerCase($tableInfo.name)}Service.insert($!tool.firstLowerCase($tableInfo.name));
if (result > 0) {
return ReturnData.ADD_SUCCESS();
}
return ReturnData.ADD_FAIL();
}
@RequestMapping(value = "update", method = RequestMethod.PUT)
public ReturnData<$tableInfo.name> update(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)) {
$tableInfo.name result = $!{tool.firstLowerCase($tableInfo.name)}Service.update($!tool.firstLowerCase($tableInfo.name));
if (result != null) {
return ReturnData.UPDATE_SUCCESS(result);
}
return ReturnData.UPDATE_FAIL();
}
@RequestMapping(value = "delete", method = RequestMethod.DELETE)
public ReturnData<$tableInfo.name> delete($tableInfo.name $!tool.firstLowerCase($tableInfo.name)) {
int result = $!{tool.firstLowerCase($tableInfo.name)}Service.deleteById($!{tool.firstLowerCase($tableInfo.name)}.getId());
if (result > 0) {
return ReturnData.DELETE_SUCCESS();
}
return ReturnData.DELETE_FAIL();
}
@RequestMapping(value = "selectAll", method = RequestMethod.GET)
public ReturnData<List<$tableInfo.name>> selectAll() {
List<$tableInfo.name> $!tool.firstLowerCase($tableInfo.name)s = $!{tool.firstLowerCase($tableInfo.name)}Service.selectAll();
if ($!tool.firstLowerCase($tableInfo.name)s != null) {
return ReturnData.GET_SUCCESS($!tool.firstLowerCase($tableInfo.name)s);
}
return ReturnData.GET_FAIL();
}
@RequestMapping(value = "selectPage", method = RequestMethod.GET)
public ReturnData<PageResult> selectPage(Integer start, Integer limit) {
PageResult pageResult = $!{tool.firstLowerCase($tableInfo.name)}Service.selectPage(start, limit);
if (pageResult != null) {
return ReturnData.GET_SUCCESS(pageResult);
}
return ReturnData.GET_FAIL();
}
}
mapper.xml
##引入mybatis支持
$!mybatisSupport
##设置保存名称与保存位置
$!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>
<!-- 查询单个 -->
<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="selectPage" resultMap="$!{tableInfo.name}Map">
select
<include refid="Base_Column_List" />
from $!tableInfo.obj.name
limit #{start},#{limit}
</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>
<!-- 总数 -->
<select id="count" resultType="int">
select count(*) from $!{tableInfo.obj.name}
</select>
</mapper>
2.3 生成方法
- 首先使用idea连接数据库,此处省略,如果有不知道怎么连接的,请参考:mysql连接idea详细教程_idea配置连接数据库的超详细步骤
- 连接之后如图找到自己的数据表
如图选择需要生成代码的表->右键->EasyCodeMybatisCodeHelper->Generate_Code,弹出如下窗口
Module:选择要生成到那个模块 Package:填写自己的包名 Path:工程路径
选择要生成的类,点击ok即可看到工程里已经生成了相应的类 注意:使用mybatis需要在启动类中加入注释
@MapperScan("xxx.dao")
|