From 29db8621a7634b02739fa1beb890d9cc22ec70b1 Mon Sep 17 00:00:00 2001 From: Peter Ji Date: Fri, 24 Jul 2020 16:52:22 +0800 Subject: [PATCH 1/3] =?UTF-8?q?default=E6=96=B0=E5=A2=9E=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E8=B6=85=E5=AE=9E=E7=94=A8=E6=96=B9=E6=B3=95=EF=BC=9A=20insert?= =?UTF-8?q?Batch=20=E6=89=B9=E9=87=8F=E6=8F=92=E5=85=A5=EF=BC=88MyBatis?= =?UTF-8?q?=E5=8E=9F=E7=94=9Fforeach=E6=96=B9=E6=B3=95=EF=BC=89=20insertOr?= =?UTF-8?q?UpdateBatch=20=E6=89=B9=E9=87=8F=E6=8F=92=E5=85=A5=E6=88=96?= =?UTF-8?q?=E6=8C=89=E4=B8=BB=E9=94=AE=E6=9B=B4=E6=96=B0=EF=BC=88MyBatis?= =?UTF-8?q?=E5=8E=9F=E7=94=9Fforeach=E6=96=B9=E6=B3=95=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resources/template/Default/dao.java.vm | 18 ++++++++++++++++- .../resources/template/Default/mapper.xml.vm | 20 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/main/resources/template/Default/dao.java.vm b/src/main/resources/template/Default/dao.java.vm index c7ccadb..be58b38 100644 --- a/src/main/resources/template/Default/dao.java.vm +++ b/src/main/resources/template/Default/dao.java.vm @@ -57,6 +57,22 @@ public interface $!{tableName} { */ int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); + /** + * 批量新增数据(MyBatis原生foreach方法) + * + * @param List<$!{tableInfo.name}> 实例对象列表 + * @return 影响行数 + */ + int insertBatch(@Param("entities") List<$!{tableInfo.name}> entities); + + /** + * 批量新增或按主键更新数据(MyBatis原生foreach方法) + * + * @param List<$!{tableInfo.name}> 实例对象列表 + * @return 影响行数 + */ + int insertOrUpdateBatch(@Param("entities") List<$!{tableInfo.name}> entities); + /** * 修改数据 * @@ -73,4 +89,4 @@ public interface $!{tableName} { */ int deleteById($!pk.shortType $!pk.name); -} \ No newline at end of file +} diff --git a/src/main/resources/template/Default/mapper.xml.vm b/src/main/resources/template/Default/mapper.xml.vm index 1c45308..6299d5e 100644 --- a/src/main/resources/template/Default/mapper.xml.vm +++ b/src/main/resources/template/Default/mapper.xml.vm @@ -59,6 +59,24 @@ $!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapper")) values (#foreach($column in $tableInfo.otherColumn)#{$!{column.name}}#if($velocityHasNext), #end#end) + + insert into $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end) + values + + (#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end) + + + + + insert into $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end) + values + + (#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end) + + on duplicate key update + #foreach($column in $tableInfo.otherColumn)$!column.obj.name = values($!column.obj.name) #if($velocityHasNext), #end#end + + update $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name} @@ -77,4 +95,4 @@ $!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapper")) delete from $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name} where $!pk.obj.name = #{$!pk.name} - \ No newline at end of file + From 5d0b4b05a4cc7e5802ee993322ed597432a74a75 Mon Sep 17 00:00:00 2001 From: Peter Ji Date: Fri, 24 Jul 2020 18:22:21 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E7=A7=8D=E7=B1=BBMybatisPlus-Mixed=20MybatisPlus=E8=87=AA?= =?UTF-8?q?=E5=B8=A6=E7=9A=84=E6=89=B9=E9=87=8F=E6=8F=92=E5=85=A5=E5=92=8C?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=AE=9E=E5=9C=A8=E5=A4=AA=E6=85=A2=EF=BC=8C?= =?UTF-8?q?Mybatis=E5=8E=9F=E7=94=9F=E7=9A=84foreach=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E5=BF=AB=E5=BE=88=E5=A4=9A(5s=20vs=205min)=E3=80=82=E8=BF=99?= =?UTF-8?q?=E4=B8=AA=E6=A8=A1=E6=9D=BF=E7=B1=BB=E5=9E=8B=E8=87=AA=E5=B8=A6?= =?UTF-8?q?=E5=8E=9F=E7=94=9F=E6=89=B9=E9=87=8F=E6=96=B0=E5=A2=9E=E5=92=8C?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=96=B9=E6=B3=95=EF=BC=8C=E5=BC=A5=E8=A1=A5?= =?UTF-8?q?=E4=BA=86MybatisPlus=E7=9A=84=E4=B8=8D=E8=B6=B3=E3=80=82?= =?UTF-8?q?=E5=90=8C=E6=97=B6=E7=94=9F=E6=88=90mapper.xml=E4=B9=9F?= =?UTF-8?q?=E6=96=B9=E4=BE=BF=E2=80=9C=E5=85=A8=E9=83=BD=E8=A6=81=E2=80=9D?= =?UTF-8?q?=E7=9A=84=E6=88=90=E5=B9=B4=E4=BA=BA=E8=87=AA=E5=B7=B1=E5=86=99?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E6=97=B6=E6=97=A0=E9=9C=80=E5=86=8D=E5=86=99?= =?UTF-8?q?resultMap=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../template/MybatisPlus-Mixed/controller.vm | 97 +++++++++++++++++++ .../template/MybatisPlus-Mixed/dao.vm | 39 ++++++++ .../template/MybatisPlus-Mixed/entity.vm | 41 ++++++++ .../template/MybatisPlus-Mixed/mapper.xml.vm | 42 ++++++++ .../template/MybatisPlus-Mixed/service.vm | 20 ++++ .../template/MybatisPlus-Mixed/serviceImpl.vm | 24 +++++ 6 files changed, 263 insertions(+) create mode 100644 src/main/resources/template/MybatisPlus-Mixed/controller.vm create mode 100644 src/main/resources/template/MybatisPlus-Mixed/dao.vm create mode 100644 src/main/resources/template/MybatisPlus-Mixed/entity.vm create mode 100644 src/main/resources/template/MybatisPlus-Mixed/mapper.xml.vm create mode 100644 src/main/resources/template/MybatisPlus-Mixed/service.vm create mode 100644 src/main/resources/template/MybatisPlus-Mixed/serviceImpl.vm diff --git a/src/main/resources/template/MybatisPlus-Mixed/controller.vm b/src/main/resources/template/MybatisPlus-Mixed/controller.vm new file mode 100644 index 0000000..4546bba --- /dev/null +++ b/src/main/resources/template/MybatisPlus-Mixed/controller.vm @@ -0,0 +1,97 @@ +##导入宏定义 +$!define + +##设置表后缀(宏定义) +#setTableSuffix("Controller") + +##保存文件(宏定义) +#save("/controller", "Controller.java") + +##包路径(宏定义) +#setPackageSuffix("controller") + +##定义服务名 +#set($serviceName = $!tool.append($!tool.firstLowerCase($!tableInfo.name), "Service")) + +##定义实体对象名 +#set($entityName = $!tool.firstLowerCase($!tableInfo.name)) + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.api.ApiController; +import com.baomidou.mybatisplus.extension.api.R; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import $!{tableInfo.savePackageName}.entity.$!tableInfo.name; +import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.io.Serializable; +import java.util.List; + +##表注释(宏定义) +#tableComment("表控制层") +@RestController +@RequestMapping("$!tool.firstLowerCase($!tableInfo.name)") +public class $!{tableName} extends ApiController { + /** + * 服务对象 + */ + @Resource + private $!{tableInfo.name}Service $!{serviceName}; + + /** + * 分页查询所有数据 + * + * @param page 分页对象 + * @param $!entityName 查询实体 + * @return 所有数据 + */ + @GetMapping + public R selectAll(Page<$!tableInfo.name> page, $!tableInfo.name $!entityName) { + return success(this.$!{serviceName}.page(page, new QueryWrapper<>($!entityName))); + } + + /** + * 通过主键查询单条数据 + * + * @param id 主键 + * @return 单条数据 + */ + @GetMapping("{id}") + public R selectOne(@PathVariable Serializable id) { + return success(this.$!{serviceName}.getById(id)); + } + + /** + * 新增数据 + * + * @param $!entityName 实体对象 + * @return 新增结果 + */ + @PostMapping + public R insert(@RequestBody $!tableInfo.name $!entityName) { + return success(this.$!{serviceName}.save($!entityName)); + } + + /** + * 修改数据 + * + * @param $!entityName 实体对象 + * @return 修改结果 + */ + @PutMapping + public R update(@RequestBody $!tableInfo.name $!entityName) { + return success(this.$!{serviceName}.updateById($!entityName)); + } + + /** + * 删除数据 + * + * @param idList 主键结合 + * @return 删除结果 + */ + @DeleteMapping + public R delete(@RequestParam("idList") List idList) { + return success(this.$!{serviceName}.removeByIds(idList)); + } +} \ No newline at end of file diff --git a/src/main/resources/template/MybatisPlus-Mixed/dao.vm b/src/main/resources/template/MybatisPlus-Mixed/dao.vm new file mode 100644 index 0000000..853ab05 --- /dev/null +++ b/src/main/resources/template/MybatisPlus-Mixed/dao.vm @@ -0,0 +1,39 @@ +##导入宏定义 +$!define + +##设置表后缀(宏定义) +#setTableSuffix("Dao") + +##保存文件(宏定义) +#save("/dao", "Dao.java") + +##包路径(宏定义) +#setPackageSuffix("dao") + +import java.util.List; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Param; +import $!{tableInfo.savePackageName}.entity.$!tableInfo.name; + +##表注释(宏定义) +#tableComment("表数据库访问层") +public interface $!{tableName} extends BaseMapper<$!tableInfo.name> { + +/** +* 批量新增数据(MyBatis原生foreach方法) +* +* @param entities List<$!{tableInfo.name}> 实例对象列表 +* @return 影响行数 +*/ +int insertBatch(@Param("entities") List<$!{tableInfo.name}> entities); + +/** +* 批量新增或按主键更新数据(MyBatis原生foreach方法) +* +* @param entities List<$!{tableInfo.name}> 实例对象列表 +* @return 影响行数 +*/ +int insertOrUpdateBatch(@Param("entities") List<$!{tableInfo.name}> entities); + +} diff --git a/src/main/resources/template/MybatisPlus-Mixed/entity.vm b/src/main/resources/template/MybatisPlus-Mixed/entity.vm new file mode 100644 index 0000000..812ad52 --- /dev/null +++ b/src/main/resources/template/MybatisPlus-Mixed/entity.vm @@ -0,0 +1,41 @@ +##导入宏定义 +$!define + +##保存文件(宏定义) +#save("/entity", ".java") + +##包路径(宏定义) +#setPackageSuffix("entity") + +##自动导入包(全局变量) +$!autoImport +import com.baomidou.mybatisplus.extension.activerecord.Model; +import java.io.Serializable; + +##表注释(宏定义) +#tableComment("表实体类") +@SuppressWarnings("serial") +public class $!{tableInfo.name} extends Model<$!{tableInfo.name}> { +#foreach($column in $tableInfo.fullColumn) + #if(${column.comment})//${column.comment}#end + + private $!{tool.getClsNameByFullName($column.type)} $!{column.name}; +#end + +#foreach($column in $tableInfo.fullColumn) +#getSetMethod($column) +#end + +#foreach($column in $tableInfo.pkColumn) + /** + * 获取主键值 + * + * @return 主键值 + */ + @Override + protected Serializable pkVal() { + return this.$!column.name; + } + #break +#end +} \ No newline at end of file diff --git a/src/main/resources/template/MybatisPlus-Mixed/mapper.xml.vm b/src/main/resources/template/MybatisPlus-Mixed/mapper.xml.vm new file mode 100644 index 0000000..0868c9e --- /dev/null +++ b/src/main/resources/template/MybatisPlus-Mixed/mapper.xml.vm @@ -0,0 +1,42 @@ +##引入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 + + + + + + +#foreach($column in $tableInfo.fullColumn) + +#end + + + + + insert into $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end) + values + + (#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end) + + + + + insert into $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end) + values + + (#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end) + + on duplicate key update + #foreach($column in $tableInfo.otherColumn)$!column.obj.name = values($!column.obj.name) #if($velocityHasNext), #end#end + + + diff --git a/src/main/resources/template/MybatisPlus-Mixed/service.vm b/src/main/resources/template/MybatisPlus-Mixed/service.vm new file mode 100644 index 0000000..7a6b28e --- /dev/null +++ b/src/main/resources/template/MybatisPlus-Mixed/service.vm @@ -0,0 +1,20 @@ +##导入宏定义 +$!define + +##设置表后缀(宏定义) +#setTableSuffix("Service") + +##保存文件(宏定义) +#save("/service", "Service.java") + +##包路径(宏定义) +#setPackageSuffix("service") + +import com.baomidou.mybatisplus.extension.service.IService; +import $!{tableInfo.savePackageName}.entity.$!tableInfo.name; + +##表注释(宏定义) +#tableComment("表服务接口") +public interface $!{tableName} extends IService<$!tableInfo.name> { + +} \ No newline at end of file diff --git a/src/main/resources/template/MybatisPlus-Mixed/serviceImpl.vm b/src/main/resources/template/MybatisPlus-Mixed/serviceImpl.vm new file mode 100644 index 0000000..fad1b29 --- /dev/null +++ b/src/main/resources/template/MybatisPlus-Mixed/serviceImpl.vm @@ -0,0 +1,24 @@ +##导入宏定义 +$!define + +##设置表后缀(宏定义) +#setTableSuffix("ServiceImpl") + +##保存文件(宏定义) +#save("/service/impl", "ServiceImpl.java") + +##包路径(宏定义) +#setPackageSuffix("service.impl") + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao; +import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}; +import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service; +import org.springframework.stereotype.Service; + +##表注释(宏定义) +#tableComment("表服务实现类") +@Service("$!tool.firstLowerCase($tableInfo.name)Service") +public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Dao, $!{tableInfo.name}> implements $!{tableInfo.name}Service { + +} \ No newline at end of file From 320659fcef6d229b1c2fc742d20a784d83c1d877 Mon Sep 17 00:00:00 2001 From: Peter Ji Date: Fri, 24 Jul 2020 18:25:40 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=AD=A3default=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?@param=E7=BA=A2=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/template/Default/dao.java.vm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/template/Default/dao.java.vm b/src/main/resources/template/Default/dao.java.vm index be58b38..a42f6a9 100644 --- a/src/main/resources/template/Default/dao.java.vm +++ b/src/main/resources/template/Default/dao.java.vm @@ -60,7 +60,7 @@ public interface $!{tableName} { /** * 批量新增数据(MyBatis原生foreach方法) * - * @param List<$!{tableInfo.name}> 实例对象列表 + * @param entities List<$!{tableInfo.name}> 实例对象列表 * @return 影响行数 */ int insertBatch(@Param("entities") List<$!{tableInfo.name}> entities); @@ -68,7 +68,7 @@ public interface $!{tableName} { /** * 批量新增或按主键更新数据(MyBatis原生foreach方法) * - * @param List<$!{tableInfo.name}> 实例对象列表 + * @param entities List<$!{tableInfo.name}> 实例对象列表 * @return 影响行数 */ int insertOrUpdateBatch(@Param("entities") List<$!{tableInfo.name}> entities);