From c01435a0592842386270c86eb3fe1ea15a0cb9f9 Mon Sep 17 00:00:00 2001 From: zhengjie <201507802@qq.com> Date: Wed, 5 Jun 2019 11:22:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BB=A3=E7=A0=81=E7=94=9F?= =?UTF-8?q?=E6=88=90=E5=99=A8=EF=BC=8C=E6=9B=B4=E6=96=B0sql=E8=84=9A?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/me/zhengjie/utils/GenUtil.java | 10 +- .../template/generator/admin/Controller.ftl | 11 +-- .../generator/admin/QueryCriteria.ftl | 34 +++++++ .../template/generator/admin/QueryService.ftl | 92 ------------------- .../template/generator/admin/Service.ftl | 19 ++++ .../template/generator/admin/ServiceImpl.ftl | 16 ++++ sql/eladmin.sql | 46 ++++------ 7 files changed, 97 insertions(+), 131 deletions(-) create mode 100644 eladmin-system/src/main/resources/template/generator/admin/QueryCriteria.ftl delete mode 100644 eladmin-system/src/main/resources/template/generator/admin/QueryService.ftl diff --git a/eladmin-generator/src/main/java/me/zhengjie/utils/GenUtil.java b/eladmin-generator/src/main/java/me/zhengjie/utils/GenUtil.java index 07e101cc2..b3aad8a0a 100644 --- a/eladmin-generator/src/main/java/me/zhengjie/utils/GenUtil.java +++ b/eladmin-generator/src/main/java/me/zhengjie/utils/GenUtil.java @@ -44,7 +44,7 @@ public static List getAdminTemplateNames() { templateNames.add("Repository"); templateNames.add("Service"); templateNames.add("ServiceImpl"); - templateNames.add("QueryService"); + templateNames.add("QueryCriteria"); templateNames.add("Controller"); return templateNames; } @@ -200,12 +200,12 @@ public static String getAdminFilePath(String templateName, GenConfig genConfig, return packagePath + "service" + File.separator + "dto" + File.separator + className + "DTO.java"; } - if ("Mapper".equals(templateName)) { - return packagePath + "service" + File.separator + "mapper" + File.separator + className + "Mapper.java"; + if ("QueryCriteria".equals(templateName)) { + return packagePath + "service" + File.separator + "dto" + File.separator + className + "QueryCriteria.java"; } - if ("QueryService".equals(templateName)) { - return packagePath + "service" + File.separator + "query" + File.separator + className + "QueryService.java"; + if ("Mapper".equals(templateName)) { + return packagePath + "service" + File.separator + "mapper" + File.separator + className + "Mapper.java"; } if ("Repository".equals(templateName)) { diff --git a/eladmin-system/src/main/resources/template/generator/admin/Controller.ftl b/eladmin-system/src/main/resources/template/generator/admin/Controller.ftl index f31048b46..005c0d4a7 100644 --- a/eladmin-system/src/main/resources/template/generator/admin/Controller.ftl +++ b/eladmin-system/src/main/resources/template/generator/admin/Controller.ftl @@ -3,8 +3,7 @@ package ${package}.rest; import me.zhengjie.aop.log.Log; import ${package}.domain.${className}; import ${package}.service.${className}Service; -import ${package}.service.dto.${className}DTO; -import ${package}.service.query.${className}QueryService; +import ${package}.service.dto.${className}QueryCriteria; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; @@ -13,6 +12,7 @@ import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; + /** * @author ${author} * @date ${date} @@ -24,14 +24,11 @@ public class ${className}Controller { @Autowired private ${className}Service ${changeClassName}Service; - @Autowired - private ${className}QueryService ${changeClassName}QueryService; - @Log("查询${className}") @GetMapping(value = "/${changeClassName}") @PreAuthorize("hasAnyRole('ADMIN','${upperCaseClassName}_ALL','${upperCaseClassName}_SELECT')") - public ResponseEntity get${className}s(${className}DTO resources, Pageable pageable){ - return new ResponseEntity(${changeClassName}QueryService.queryAll(resources,pageable),HttpStatus.OK); + public ResponseEntity get${className}s(${className}QueryCriteria criteria, Pageable pageable){ + return new ResponseEntity(${changeClassName}Service.queryAll(criteria,pageable),HttpStatus.OK); } @Log("新增${className}") diff --git a/eladmin-system/src/main/resources/template/generator/admin/QueryCriteria.ftl b/eladmin-system/src/main/resources/template/generator/admin/QueryCriteria.ftl new file mode 100644 index 000000000..514e92cdd --- /dev/null +++ b/eladmin-system/src/main/resources/template/generator/admin/QueryCriteria.ftl @@ -0,0 +1,34 @@ +package ${package}.service.dto; + +import lombok.Data; +<#if hasTimestamp> +import java.sql.Timestamp; + +<#if hasBigDecimal> +import java.math.BigDecimal; + +<#if queryColumns??> +import me.zhengjie.annotation.Query; + + +/** +* @author ${author} +* @date ${date} +*/ +@Data +public class ${className}QueryCriteria{ +<#if queryColumns??> + <#list queryColumns as column> + + <#if column.columnQuery = '1'> + // 模糊 + @Query(type = Query.Type.INNER_LIKE) + + <#if column.columnQuery = '2'> + // 精确 + @Query + + private ${column.columnType} ${column.changeColumnName}; + + +} \ No newline at end of file diff --git a/eladmin-system/src/main/resources/template/generator/admin/QueryService.ftl b/eladmin-system/src/main/resources/template/generator/admin/QueryService.ftl deleted file mode 100644 index 088b7ca1f..000000000 --- a/eladmin-system/src/main/resources/template/generator/admin/QueryService.ftl +++ /dev/null @@ -1,92 +0,0 @@ -package ${package}.service.query; - -import me.zhengjie.utils.PageUtil; -import ${package}.domain.${className}; -import ${package}.service.dto.${className}DTO; -import ${package}.repository.${className}Repository; -import ${package}.service.mapper.${className}Mapper; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cache.annotation.CacheConfig; -import org.springframework.cache.annotation.Cacheable; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.data.jpa.domain.Specification; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Propagation; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.util.ObjectUtils; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Predicate; -import javax.persistence.criteria.Root; -import java.util.ArrayList; -import java.util.List; - -/** - * @author jie - * @date 2018-12-03 - */ -@Service -@CacheConfig(cacheNames = "${changeClassName}") -@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class) -public class ${className}QueryService { - - @Autowired - private ${className}Repository ${changeClassName}Repository; - - @Autowired - private ${className}Mapper ${changeClassName}Mapper; - - /** - * 分页 - */ - @Cacheable(keyGenerator = "keyGenerator") - public Object queryAll(${className}DTO ${changeClassName}, Pageable pageable){ - Page<${className}> page = ${changeClassName}Repository.findAll(new Spec(${changeClassName}),pageable); - return PageUtil.toPage(page.map(${changeClassName}Mapper::toDto)); - } - - /** - * 不分页 - */ - @Cacheable(keyGenerator = "keyGenerator") - public Object queryAll(${className}DTO ${changeClassName}){ - return ${changeClassName}Mapper.toDto(${changeClassName}Repository.findAll(new Spec(${changeClassName}))); - } - - class Spec implements Specification<${className}> { - - private ${className}DTO ${changeClassName}; - - public Spec(${className}DTO ${changeClassName}){ - this.${changeClassName} = ${changeClassName}; - } - - @Override - public Predicate toPredicate(Root<${className}> root, CriteriaQuery criteriaQuery, CriteriaBuilder cb) { - - List list = new ArrayList(); - -<#if queryColumns??> - <#list queryColumns as column> - if(!ObjectUtils.isEmpty(${changeClassName}.get${column.capitalColumnName}())){ - <#if column.columnQuery = '1'> - /** - * 模糊 - */ - list.add(cb.like(root.get("${column.changeColumnName}").as(${column.columnType}.class),"%"+${changeClassName}.get${column.capitalColumnName}()+"%")); - - <#if column.columnQuery = '2'> - /** - * 精确 - */ - list.add(cb.equal(root.get("${column.changeColumnName}").as(${column.columnType}.class),${changeClassName}.get${column.capitalColumnName}())); - - } - - - Predicate[] p = new Predicate[list.size()]; - return cb.and(list.toArray(p)); - } - } -} \ No newline at end of file diff --git a/eladmin-system/src/main/resources/template/generator/admin/Service.ftl b/eladmin-system/src/main/resources/template/generator/admin/Service.ftl index 9458028ea..f692446e8 100644 --- a/eladmin-system/src/main/resources/template/generator/admin/Service.ftl +++ b/eladmin-system/src/main/resources/template/generator/admin/Service.ftl @@ -2,9 +2,11 @@ package ${package}.service; import ${package}.domain.${className}; import ${package}.service.dto.${className}DTO; +import ${package}.service.dto.${className}QueryCriteria; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; +import org.springframework.data.domain.Pageable; /** * @author ${author} @@ -13,6 +15,23 @@ import org.springframework.cache.annotation.Cacheable; @CacheConfig(cacheNames = "${changeClassName}") public interface ${className}Service { + /** + * queryAll 分页 + * @param criteria + * @param pageable + * @return + */ + @Cacheable(keyGenerator = "keyGenerator") + Object queryAll(${className}QueryCriteria criteria, Pageable pageable); + + /** + * queryAll 不分页 + * @param criteria + * @return + */ + @Cacheable(keyGenerator = "keyGenerator") + public Object queryAll(${className}QueryCriteria criteria); + /** * findById * @param ${pkChangeColName} diff --git a/eladmin-system/src/main/resources/template/generator/admin/ServiceImpl.ftl b/eladmin-system/src/main/resources/template/generator/admin/ServiceImpl.ftl index 2dfcd02be..9f447c274 100644 --- a/eladmin-system/src/main/resources/template/generator/admin/ServiceImpl.ftl +++ b/eladmin-system/src/main/resources/template/generator/admin/ServiceImpl.ftl @@ -14,6 +14,7 @@ import me.zhengjie.utils.ValidationUtil; import ${package}.repository.${className}Repository; import ${package}.service.${className}Service; import ${package}.service.dto.${className}DTO; +import ${package}.service.dto.${className}QueryCriteria; import ${package}.service.mapper.${className}Mapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -27,6 +28,10 @@ import cn.hutool.core.util.IdUtil; <#if !auto && pkColumnType = 'String'> import cn.hutool.core.util.IdUtil; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import me.zhengjie.utils.PageUtil; +import me.zhengjie.utils.QueryHelp; /** * @author ${author} @@ -42,6 +47,17 @@ public class ${className}ServiceImpl implements ${className}Service { @Autowired private ${className}Mapper ${changeClassName}Mapper; + @Override + public Object queryAll(${className}QueryCriteria criteria, Pageable pageable){ + Page<${className}> page = ${changeClassName}Repository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); + return PageUtil.toPage(page.map(${changeClassName}Mapper::toDto)); + } + + @Override + public Object queryAll(${className}QueryCriteria criteria){ + return ${changeClassName}Mapper.toDto(${changeClassName}Repository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); + } + @Override public ${className}DTO findById(${pkColumnType} ${pkChangeColName}) { Optional<${className}> ${changeClassName} = ${changeClassName}Repository.findById(${pkChangeColName}); diff --git a/sql/eladmin.sql b/sql/eladmin.sql index 6d35d89d0..2aa9b3a1e 100644 --- a/sql/eladmin.sql +++ b/sql/eladmin.sql @@ -10,7 +10,7 @@ Target Server Type : MYSQL Target Server Version : 50562 File Encoding : 65001 -Date: 2019-05-26 16:31:04 +Date: 2019-06-05 11:13:41 */ SET FOREIGN_KEY_CHECKS=0; @@ -134,6 +134,11 @@ CREATE TABLE `gen_config` ( PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; +-- ---------------------------- +-- Records of gen_config +-- ---------------------------- +INSERT INTO `gen_config` VALUES ('1', 'jie', '\0', 'eladmin-system', 'me.zhengjie.modules.test', 'E:\\workspace\\me\\front\\eladmin-qt\\src\\views\\test', 'E:\\workspace\\me\\front\\eladmin-qt\\src\\api', null); + -- ---------------------------- -- Table structure for job -- ---------------------------- @@ -176,7 +181,11 @@ CREATE TABLE `log` ( `time` bigint(20) DEFAULT NULL, `username` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=9465 DEFAULT CHARSET=utf8; +) ENGINE=InnoDB AUTO_INCREMENT=10334 DEFAULT CHARSET=utf8; + +-- ---------------------------- +-- Records of log +-- ---------------------------- -- ---------------------------- -- Table structure for menu @@ -193,7 +202,7 @@ CREATE TABLE `menu` ( `icon` varchar(255) DEFAULT NULL COMMENT '图标', `path` varchar(255) DEFAULT NULL COMMENT '链接地址', PRIMARY KEY (`id`) USING BTREE -) ENGINE=InnoDB AUTO_INCREMENT=40 DEFAULT CHARSET=utf8; +) ENGINE=InnoDB AUTO_INCREMENT=41 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of menu @@ -209,7 +218,6 @@ INSERT INTO `menu` VALUES ('8', '2018-12-18 15:19:01', '\0', '系统缓存', 'mo INSERT INTO `menu` VALUES ('9', '2018-12-18 15:19:34', '\0', 'SQL监控', 'monitor/sql/index', '6', '14', 'sqlMonitor', 'druid'); INSERT INTO `menu` VALUES ('10', '2018-12-19 13:38:16', '\0', '组件管理', null, '0', '50', 'zujian', 'components'); INSERT INTO `menu` VALUES ('11', '2018-12-19 13:38:49', '\0', '图标库', 'components/IconSelect', '10', '51', 'icon', 'icon'); -INSERT INTO `menu` VALUES ('12', '2018-12-24 20:37:35', '\0', '实时控制台', 'monitor/log/msg', '6', '16', 'codeConsole', 'msg'); INSERT INTO `menu` VALUES ('14', '2018-12-27 10:13:09', '\0', '邮件工具', 'tools/email/index', '36', '24', 'email', 'email'); INSERT INTO `menu` VALUES ('15', '2018-12-27 11:58:25', '\0', '富文本', 'components/Editor', '10', '52', 'fwb', 'tinymce'); INSERT INTO `menu` VALUES ('16', '2018-12-28 09:36:53', '\0', '图床管理', 'tools/picture/index', '36', '25', 'image', 'pictures'); @@ -265,9 +273,7 @@ INSERT INTO `permission` VALUES ('15', '权限创建', '2018-12-09 20:14:10', 'P INSERT INTO `permission` VALUES ('16', '权限编辑', '2018-12-09 20:15:44', 'PERMISSION_EDIT', '13'); INSERT INTO `permission` VALUES ('17', '权限删除', '2018-12-09 20:15:59', 'PERMISSION_DELETE', '13'); INSERT INTO `permission` VALUES ('18', '缓存管理', '2018-12-17 13:53:25', 'REDIS_ALL', '0'); -INSERT INTO `permission` VALUES ('19', '新增缓存', '2018-12-17 13:53:44', 'REDIS_CREATE', '18'); INSERT INTO `permission` VALUES ('20', '缓存查询', '2018-12-17 13:54:07', 'REDIS_SELECT', '18'); -INSERT INTO `permission` VALUES ('21', '缓存编辑', '2018-12-17 13:54:26', 'REDIS_EDIT', '18'); INSERT INTO `permission` VALUES ('22', '缓存删除', '2018-12-17 13:55:04', 'REDIS_DELETE', '18'); INSERT INTO `permission` VALUES ('23', '图床管理', '2018-12-27 20:31:49', 'PICTURE_ALL', '0'); INSERT INTO `permission` VALUES ('24', '查询图片', '2018-12-27 20:32:04', 'PICTURE_SELECT', '23'); @@ -314,11 +320,7 @@ CREATE TABLE `picture` ( `username` varchar(255) DEFAULT NULL COMMENT '用户名称', `width` varchar(255) DEFAULT NULL COMMENT '图片宽度', PRIMARY KEY (`id`) USING BTREE -) ENGINE=InnoDB DEFAULT CHARSET=utf8; - --- ---------------------------- --- Records of picture --- ---------------------------- +) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for qiniu_config @@ -348,11 +350,7 @@ CREATE TABLE `qiniu_content` ( `update_time` datetime DEFAULT NULL COMMENT '上传或同步的时间', `url` varchar(255) DEFAULT NULL COMMENT '文件url', PRIMARY KEY (`id`) USING BTREE -) ENGINE=InnoDB DEFAULT CHARSET=utf8; - --- ---------------------------- --- Records of qiniu_content --- ---------------------------- +) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for quartz_job @@ -394,7 +392,7 @@ CREATE TABLE `quartz_log` ( `params` varchar(255) DEFAULT NULL, `time` bigint(20) DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE -) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8; +) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of quartz_log @@ -419,7 +417,7 @@ CREATE TABLE `role` ( -- ---------------------------- INSERT INTO `role` VALUES ('1', '2018-11-23 11:04:37', '超级管理员', '系统所有权', '全部', '1'); INSERT INTO `role` VALUES ('2', '2018-11-23 13:09:06', '普通用户', '用于测试菜单与权限', '自定义', '3'); -INSERT INTO `role` VALUES ('4', '2019-05-13 14:16:15', '普通管理员', '普通管理员级别为2,使用该角色新增用户时只能赋予比普通管理员级别低的角色', '全部', '2'); +INSERT INTO `role` VALUES ('4', '2019-05-13 14:16:15', '普通管理员', '普通管理员级别为2,使用该角色新增用户时只能赋予比普通管理员级别低的角色', '自定义', '2'); -- ---------------------------- -- Table structure for roles_depts @@ -438,6 +436,8 @@ CREATE TABLE `roles_depts` ( -- Records of roles_depts -- ---------------------------- INSERT INTO `roles_depts` VALUES ('2', '5'); +INSERT INTO `roles_depts` VALUES ('4', '6'); +INSERT INTO `roles_depts` VALUES ('4', '7'); INSERT INTO `roles_depts` VALUES ('2', '8'); -- ---------------------------- @@ -467,7 +467,6 @@ INSERT INTO `roles_menus` VALUES ('8', '1'); INSERT INTO `roles_menus` VALUES ('9', '1'); INSERT INTO `roles_menus` VALUES ('10', '1'); INSERT INTO `roles_menus` VALUES ('11', '1'); -INSERT INTO `roles_menus` VALUES ('12', '1'); INSERT INTO `roles_menus` VALUES ('14', '1'); INSERT INTO `roles_menus` VALUES ('15', '1'); INSERT INTO `roles_menus` VALUES ('16', '1'); @@ -499,7 +498,6 @@ INSERT INTO `roles_menus` VALUES ('8', '2'); INSERT INTO `roles_menus` VALUES ('9', '2'); INSERT INTO `roles_menus` VALUES ('10', '2'); INSERT INTO `roles_menus` VALUES ('11', '2'); -INSERT INTO `roles_menus` VALUES ('12', '2'); INSERT INTO `roles_menus` VALUES ('14', '2'); INSERT INTO `roles_menus` VALUES ('15', '2'); INSERT INTO `roles_menus` VALUES ('16', '2'); @@ -540,12 +538,10 @@ CREATE TABLE `roles_permissions` ( -- Records of roles_permissions -- ---------------------------- INSERT INTO `roles_permissions` VALUES ('1', '1'); -INSERT INTO `roles_permissions` VALUES ('4', '2'); INSERT INTO `roles_permissions` VALUES ('2', '3'); INSERT INTO `roles_permissions` VALUES ('4', '3'); INSERT INTO `roles_permissions` VALUES ('4', '4'); INSERT INTO `roles_permissions` VALUES ('4', '5'); -INSERT INTO `roles_permissions` VALUES ('4', '6'); INSERT INTO `roles_permissions` VALUES ('2', '8'); INSERT INTO `roles_permissions` VALUES ('2', '14'); INSERT INTO `roles_permissions` VALUES ('2', '20'); @@ -643,8 +639,4 @@ CREATE TABLE `visits` ( `week_day` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `UK_11aksgq87euk9bcyeesfs4vtp` (`date`) -) ENGINE=InnoDB AUTO_INCREMENT=47 DEFAULT CHARSET=utf8; - --- ---------------------------- --- Records of visits --- ---------------------------- +) ENGINE=InnoDB AUTO_INCREMENT=55 DEFAULT CHARSET=utf8;