diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/mojo/GeneratorServiceAndControllerMojo.java b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/mojo/GeneratorServiceAndControllerMojo.java new file mode 100644 index 0000000..dc5ca59 --- /dev/null +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/mojo/GeneratorServiceAndControllerMojo.java @@ -0,0 +1,45 @@ +package com.micro.fast.mybatis.generator.extend.mojo; + +import com.micro.fast.mybatis.generator.extend.service.GeneratorServiceController; +import com.micro.fast.mybatis.generator.extend.util.PropertiesUtil; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.Mojo; + +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +/** + * 生成service层和controller层 + * @author lsy + */ +@Mojo(name = "generateSC") +public class GeneratorServiceAndControllerMojo extends AbstractMojo { + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + //获取项目的绝对路径 + String projectPath =null; + Map pluginContext = getPluginContext(); + Set set = pluginContext.entrySet(); + Iterator iterator = set.iterator(); + while (iterator.hasNext()){ + Object next = iterator.next(); + String s = next.toString(); + String[] split = s.split(" "); + for (String s1 : split) { + if (s1.endsWith("pom.xml")){ + int lastIndexOf = s1.lastIndexOf("pom.xml"); + projectPath = s1.substring(0,lastIndexOf-1); + } + } + } + System.out.println(projectPath); + //加载插件所需要的配置文件 + new PropertiesUtil().loadProperties(projectPath); + //生成service层controller层java类 + GeneratorServiceController.generator(projectPath); + } +} diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/mojo/MybatisGeneratorExtendMojo.java b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/mojo/MybatisGeneratorExtendMojo.java index 5925202..8282cf0 100644 --- a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/mojo/MybatisGeneratorExtendMojo.java +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/mojo/MybatisGeneratorExtendMojo.java @@ -12,6 +12,10 @@ import java.util.Iterator; import java.util.Map; import java.util.Set; +/** + * 李守余 + * @author lsy + */ @Mojo(name = "generateFiles") public class MybatisGeneratorExtendMojo extends AbstractMojo { diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/Db.java b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/Db.java index 54b5568..0ae160b 100644 --- a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/Db.java +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/Db.java @@ -1,8 +1,13 @@ package com.micro.fast.mybatis.generator.extend.pojo; +import lombok.Getter; +import lombok.Setter; + /** * mybatisGenerator所需要的数据库连接配置 */ +@Getter +@Setter public class Db { private String driverLocation; @@ -14,44 +19,4 @@ public class Db { private String password; private String driverClassName; - - public String getDriverClassName() { - return driverClassName; - } - - public void setDriverClassName(String driverClassName) { - this.driverClassName = driverClassName; - } - - public String getDriverLocation() { - return driverLocation; - } - - public void setDriverLocation(String driverLocation) { - this.driverLocation = driverLocation; - } - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } } diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/MybatisGenerator.java b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/MybatisGenerator.java index e5f18fe..ab141c2 100644 --- a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/MybatisGenerator.java +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/pojo/MybatisGenerator.java @@ -1,11 +1,17 @@ package com.micro.fast.mybatis.generator.extend.pojo; +import lombok.Getter; +import lombok.Setter; + import java.util.Map; /** * mybatisGenerator的配置参数对象 */ +@Getter +@Setter public class MybatisGenerator { + private String pojoPackage; private String daoPackage; @@ -14,35 +20,13 @@ public class MybatisGenerator { private Map tablesNameAndDoMainObjectsName; - public Map getTablesNameAndDoMainObjectsName() { - return tablesNameAndDoMainObjectsName; - } + private String servicePackage; - public void setTablesNameAndDoMainObjectsName(Map tablesNameAndDoMainObjectsName) { - this.tablesNameAndDoMainObjectsName = tablesNameAndDoMainObjectsName; - } + private String controllerPackage; - public String getPojoPackage() { - return pojoPackage; - } + private String serviceImplPackage; - public void setPojoPackage(String pojoPackage) { - this.pojoPackage = pojoPackage; - } + private String primaryKeyType; - public String getDaoPackage() { - return daoPackage; - } - - public void setDaoPackage(String daoPackage) { - this.daoPackage = daoPackage; - } - - public String getTablesPrefix() { - return tablesPrefix; - } - - public void setTablesPrefix(String tablesPrefix) { - this.tablesPrefix = tablesPrefix; - } + private String isOverWrite; } diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/service/GeneratorServiceController.java b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/service/GeneratorServiceController.java new file mode 100644 index 0000000..598a0da --- /dev/null +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/service/GeneratorServiceController.java @@ -0,0 +1,132 @@ +package com.micro.fast.mybatis.generator.extend.service; + +import com.micro.fast.mybatis.generator.extend.db.JdbcConnection; +import com.micro.fast.mybatis.generator.extend.pojo.MybatisGenerator; +import com.micro.fast.mybatis.generator.extend.util.FreemarkerUtil; +import com.micro.fast.mybatis.generator.extend.util.JarUtil; +import com.micro.fast.mybatis.generator.extend.util.PropertiesUtil; + +import java.io.File; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.HashMap; +import java.util.Map; + +/** + * 生成service层controller层 + * + * @author lsy + */ +public class GeneratorServiceController { + + private static MybatisGenerator mybatisGenerator; + + static { + // 初始化 + if (mybatisGenerator == null) { + mybatisGenerator = new MybatisGenerator(); + } + mybatisGenerator.setPojoPackage( + PropertiesUtil.getProperty("mybatisGenerator.pojoPackage")); + mybatisGenerator.setDaoPackage( + PropertiesUtil.getProperty("mybatisGenerator.daoPackage")); + mybatisGenerator.setTablesPrefix( + PropertiesUtil.getProperty("mybatisGenerator.tablesPrefix")); + mybatisGenerator.setServicePackage( + PropertiesUtil.getProperty("mybatisGenerator.servicePackage")); + mybatisGenerator.setServiceImplPackage( + PropertiesUtil.getProperty("mybatisGenerator.serviceImplPackage")); + mybatisGenerator.setControllerPackage( + PropertiesUtil.getProperty("mybatisGenerator.controllerPackage")); + mybatisGenerator.setControllerPackage( + PropertiesUtil.getProperty("mybatisGenerator.primaryKeyType")); + mybatisGenerator.setIsOverWrite( + PropertiesUtil.getProperty("mybatisGenerator.isOverWrite")); + + } + + /** + * 生成service层controller层 + * + * @param projectPath + */ + public static void generator(String projectPath) { + Connection jdbcConection = JdbcConnection.getJdbcConection(); + Statement statement = null; + ResultSet resultSet = null; + try { + //1.创建语句执行sql + statement = jdbcConection.createStatement(); + String sql = "SHOW TABLES"; + resultSet = statement.executeQuery(sql); + //2.处理sql返回的结果, + // 实体类的名字和实体类作为变量的名称 + Map pojoNameAndVarPojoName = new HashMap(); + while (resultSet.next()) { + String tableName = resultSet.getString(1); + if (tableName.startsWith(mybatisGenerator.getTablesPrefix())) { + String pojoName = ""; + String varPojoName = ""; + String[] strings = tableName.split("_"); + for (int i = 0; i < strings.length; i++) { + String string = strings[0]; + char c = string.charAt(0); + String s = String.valueOf(c); + String upperCase = s.toUpperCase(); + String firstUpper = string.replaceFirst(s, upperCase); + pojoName += firstUpper; + if (i==0){ + String s1 = s.toLowerCase(); + String firstLower = string.replaceFirst(s, s1); + varPojoName += firstLower; + }else { + varPojoName += firstUpper; + } + } + pojoNameAndVarPojoName.put(pojoName,varPojoName); + } + } + // 根据模板创建实体类 + Map mybatisGeneratorModel = new HashMap<>(); + mybatisGeneratorModel.put("mybatisGenerator",mybatisGenerator); + pojoNameAndVarPojoName.forEach((pojoName,varPojoName)-> + { + mybatisGeneratorModel.put("pojoName",pojoName); + mybatisGeneratorModel.put("varPojoName",varPojoName); + // 生成java文件 + FreemarkerUtil.createFile("SsmService.ftl",mybatisGeneratorModel,pojoName+"Service.java",projectPath,mybatisGenerator.getServicePackage(),mybatisGenerator.getIsOverWrite()); + FreemarkerUtil.createFile("SsmServiceImpl.ftl",mybatisGeneratorModel,pojoName+"ServiceImpl.java",projectPath,mybatisGenerator.getServiceImplPackage(),mybatisGenerator.getIsOverWrite()); + FreemarkerUtil.createFile("SsmController.ftl",mybatisGeneratorModel,pojoName+"Controller.java",projectPath,mybatisGenerator.getControllerPackage(),mybatisGenerator.getIsOverWrite()); + }); + // 删除模版文件 + File file1 = new File(JarUtil.getThisJarPathParent() + "/templates" + "/SsmService.ftl"); + if (file1.exists()) { + file1.delete(); + } + File file2 = new File(JarUtil.getThisJarPathParent() + "/templates" + "/SsmServiceImpl.ftl"); + if (file2.exists()) { + file2.delete(); + } + File file3 = new File(JarUtil.getThisJarPathParent() + "/templates" + "/SsmController.ftl"); + if (file3.exists()) { + file3.delete(); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + //3.关闭流 + try { + resultSet.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + try { + statement.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } +} diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/service/MybatisGeneratorExtend.java b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/service/MybatisGeneratorExtend.java index 1e1d0ab..90a5413 100644 --- a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/service/MybatisGeneratorExtend.java +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/service/MybatisGeneratorExtend.java @@ -79,12 +79,12 @@ public class MybatisGeneratorExtend { Map mybatisGeneratorModel = new HashMap<>(); mybatisGeneratorModel.put("db",MybatisGeneratorExtend.db); mybatisGeneratorModel.put("mybatisGenerator",MybatisGeneratorExtend.mybatisGenerator); - boolean mybatisGeneratorFileSuccess = FreemarkerUtil.createFile("mybatisGenerator.ftl", mybatisGeneratorModel, "mybatisGenerator.properties",projectPath); + boolean mybatisGeneratorFileSuccess = FreemarkerUtil.createFile("mybatisGenerator.ftl", mybatisGeneratorModel, "mybatisGenerator.properties",projectPath,null,mybatisGenerator.getIsOverWrite()); File file = new File(JarUtil.getThisJarPathParent() + "/templates" + "/mybatisGenerator.ftl"); if (file.exists()){ file.delete(); } - boolean generatorConfigFileSuccess = FreemarkerUtil.createFile("generatorConfig.ftl", mybatisGeneratorModel, "generatorConfig.xml",projectPath); + boolean generatorConfigFileSuccess = FreemarkerUtil.createFile("generatorConfig.ftl", mybatisGeneratorModel, "generatorConfig.xml",projectPath,null,mybatisGenerator.getIsOverWrite()); File file1 = new File(JarUtil.getThisJarPathParent() + "/templates" + "/generatorConfig.ftl"); if (file1.exists()){ file1.delete(); diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/util/FreemarkerUtil.java b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/util/FreemarkerUtil.java index 9d6ad15..6caf54c 100644 --- a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/util/FreemarkerUtil.java +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/util/FreemarkerUtil.java @@ -2,6 +2,7 @@ package com.micro.fast.mybatis.generator.extend.util; import freemarker.core.ParseException; import freemarker.template.*; +import org.apache.commons.lang3.StringUtils; import java.io.*; import java.util.Map; @@ -50,7 +51,7 @@ public class FreemarkerUtil { * @param targetFileName * @return */ - public static boolean createFile(String templateName, Map data, String targetFileName,String projectPath) { + public static boolean createFile(String templateName, Map data, String targetFileName,String projectPath,String packageName,String isOverWrite) { Template template = null; Writer out = null; //1.获取当前项目classpath路径 @@ -63,43 +64,71 @@ public class FreemarkerUtil { e.printStackTrace(); } } - String targetFileUrl = resourcesDir + "/" + targetFileName; + // 判断生成文件的路径 + String targetFileUrl = null; + if (packageName ==null){ + // 默认生成在resources文件下 + targetFileUrl = resourcesDir + "/" + targetFileName; + }else{ + String packageRelativeDir = packageName.replaceAll("\\.","/"); + String packageDir = projectPath+"/src/main/java/"+packageRelativeDir; + File file1 = new File(packageDir); + // 创建包 + if (!file1.exists()){ + file1.mkdirs(); + } + targetFileUrl = packageDir+"/"+targetFileName; + } //2.如果目标文件存在就先删除目标文件 - File targetFile = new File(targetFileUrl); - if (targetFile.exists()){ - targetFile.delete(); - } - try { - //3.获取模板文件对象 - template = cfg.getTemplate(templateName); - //4.获取输出流对象 - out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFileUrl))); - //5.向模板对象中填充数据生成文件 - template.process(data,out); - } catch (FileNotFoundException e) { - e.printStackTrace(); - return false; - } catch (MalformedTemplateNameException e) { - e.printStackTrace(); - return false; - } catch (ParseException e) { - e.printStackTrace(); - return false; - } catch (IOException e) { - e.printStackTrace(); - return false; - } catch (TemplateException e) { - e.printStackTrace(); - return false; - } finally { - try { - out.flush(); - out.close(); - } catch (IOException e) { - e.printStackTrace(); - return false; + boolean isCreate = true; + File targetFile = new File(targetFileUrl); + if (StringUtils.isNotBlank(isOverWrite)&& isOverWrite.equals("true")){ + if (targetFile.exists()){ + targetFile.delete(); + } + isCreate = true; + }else{ + if (targetFile.exists()){ + // 有的话就不生成 + isCreate =false; + }else{ + // 没有的话就生成 + isCreate = true; + } } - } + if (isCreate){ + try { + //3.获取模板文件对象 + template = cfg.getTemplate(templateName); + //4.获取输出流对象 + out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFileUrl))); + //5.向模板对象中填充数据生成文件 + template.process(data,out); + } catch (FileNotFoundException e) { + e.printStackTrace(); + return false; + } catch (MalformedTemplateNameException e) { + e.printStackTrace(); + return false; + } catch (ParseException e) { + e.printStackTrace(); + return false; + } catch (IOException e) { + e.printStackTrace(); + return false; + } catch (TemplateException e) { + e.printStackTrace(); + return false; + } finally { + try { + out.flush(); + out.close(); + } catch (IOException e) { + e.printStackTrace(); + return false; + } + } + } //创建成功 return true; } diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/util/JarUtil.java b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/util/JarUtil.java index 56d3fc6..d1adc4e 100644 --- a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/util/JarUtil.java +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/util/JarUtil.java @@ -37,7 +37,8 @@ public class JarUtil { JarEntry jarEntry = null; while ((jarEntry = jarInputStream.getNextJarEntry())!=null){ String fileName = jarEntry.getName(); - if (fileName.endsWith("generatorConfig.ftl")||fileName.endsWith("mybatisGenerator.ftl")){ + //解压.ftl模板 + if (fileName.endsWith(".ftl")){ File file = new File(getThisJarPathParent() +"/"+fileName); if (file.exists()){ file.delete(); diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/resources/mybatisStart.properties b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/resources/mybatisStart.properties index a1b9b5f..2c00812 100644 --- a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/resources/mybatisStart.properties +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/resources/mybatisStart.properties @@ -6,4 +6,10 @@ db.username=root db.password=studyj2e mybatisGenerator.pojoPackage=pojoPackage mybatisGenerator.daoPackage=daoPackage -mybatisGenerator.tablesPrefix=upms \ No newline at end of file +mybatisGenerator.tablesPrefix=upms +# service controllerËùÐèҪµÄÅäÖà +mybatisGenerator.servicePackage= +mybatisGenerator.controllerPackage= +mybatisGenerator.serviceImplPackage= +mybatisGenerator.primaryKeyType= +mybatisGenerator.isOverWrite= \ No newline at end of file diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/resources/templates/SsmController.ftl b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/resources/templates/SsmController.ftl new file mode 100644 index 0000000..0ca17d6 --- /dev/null +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/resources/templates/SsmController.ftl @@ -0,0 +1,65 @@ +package ${mybatisGenerator.controllerPackage}; + +import com.micro.fast.boot.starter.common.response.BaseConst; +import com.micro.fast.boot.starter.common.response.ServerResponse; +import ${mybatisGenerator.pojoPackage}.${pojoName}; +import ${mybatisGenerator.servicePackage}.${pojoName}Service; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.hibernate.validator.constraints.NotBlank; +import org.hibernate.validator.constraints.NotEmpty; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.BindException; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; +import java.util.List; + +/** +* +* @author lsy +*/ +@Api("${varPojoName}") +@RestController +@RequestMapping("${varPojoName}") +public class ${pojoName}Controller { + + @Autowired + private ${pojoName}Service<${pojoName},${mybatisGenerator.primaryKeyType}> ${pojoName}Service; + + @ApiOperation("添加信息") + @PostMapping + public ServerResponse add${pojoName}(@Valid ${pojoName} ${varPojoName}) throws BindException { + return ${varPojoName}Service.add(${varPojoName}); + } + + @ApiOperation("根据id查询详细信息") + @GetMapping("/{id}") + public ServerResponse get${pojoName}ById( + @PathVariable(value = "id") @NotBlank(message = BaseConst.BASEMSG_PREFIX+"请传入id") String id){ + return ${varPojoName}Service.getById(${mybatisGenerator.primaryKeyType}.valueOf(id)); + } + + @ApiOperation("根据条件分页查询") + @GetMapping + public ServerResponse get${pojoName}ByCondition(${pojoName} ${varPojoName}, + @RequestParam(defaultValue = "1",required = false) int pageNum, + @RequestParam(defaultValue = "10",required = false)int pageSize, + @RequestParam(required = false) String orderBy){ + return ${varPojoName}Service.getByCondition(${varPojoName},pageNum,pageSize,orderBy); + } + + @ApiOperation("修改信息") + @PutMapping("/{id}") + public ServerResponse update${pojoName}(${pojoName} ${varPojoName}, + @NotBlank(message = BaseConst.BASEMSG_PREFIX+"请传入id") String id){ + ${varPojoName}.setId(${mybatisGenerator.primaryKeyType}.valueOf(id)); + return ${varPojoName}Service.update(${varPojoName}); + } + + @ApiOperation("根据id删除,传入数组") + @DeleteMapping + public ServerResponse delete${pojoName}(@NotEmpty(message = BaseConst.BASEMSG_PREFIX+"请传入ids") List<${mybatisGenerator.primaryKeyType}> ids){ + return ${varPojoName}Service.deleteByIds(ids); + } +} diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/resources/templates/SsmService.ftl b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/resources/templates/SsmService.ftl new file mode 100644 index 0000000..abe94f2 --- /dev/null +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/resources/templates/SsmService.ftl @@ -0,0 +1,10 @@ +package ${mybatisGenerator.servicePackage}; + +import com.micro.fast.common.service.SsmService; + +/** +* @author lsy +*/ +public interface ${pojoName}Service extends SsmService{ + +} \ No newline at end of file diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/resources/templates/SsmServiceImpl.ftl b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/resources/templates/SsmServiceImpl.ftl new file mode 100644 index 0000000..275f3e0 --- /dev/null +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/resources/templates/SsmServiceImpl.ftl @@ -0,0 +1,28 @@ +package ${mybatisGenerator.serviceImplPackage}; + +import com.micro.fast.common.service.impl.SsmServiceImpl; +import ${mybatisGenerator.daoPackage}.${pojoName}Mapper; +import ${mybatisGenerator.pojoPackage}.${pojoName}; +import ${mybatisGenerator.servicePackage}.${pojoName}Service; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** +* @author lsy +*/ +@Service +public class ${pojoName}ServiceImpl extends SsmServiceImpl<${pojoName},${mybatisGenerator.primaryKeyType},${pojoName}Mapper> +implements ${pojoName}Service<${pojoName},${mybatisGenerator.primaryKeyType}>,InitializingBean { + + @Autowired + private ${pojoName}Mapper mapper; + + /** + * 在这个bean初始化完成后覆盖父类的mapper + */ + @Override + public void afterPropertiesSet() throws Exception { + super.setMapper(this.mapper); + } +} diff --git a/boot-starter/boot-starter-ssm/src/main/java/com/micro/fast/common/dao/SsmMapper.java b/boot-starter/boot-starter-ssm/src/main/java/com/micro/fast/common/dao/SsmMapper.java index 70ce26b..badad16 100644 --- a/boot-starter/boot-starter-ssm/src/main/java/com/micro/fast/common/dao/SsmMapper.java +++ b/boot-starter/boot-starter-ssm/src/main/java/com/micro/fast/common/dao/SsmMapper.java @@ -25,4 +25,6 @@ public interface SsmMapper { * @return */ List selectByCondition(T record); + + int deleteByPrimaryKeys(List id); } diff --git a/boot-starter/boot-starter-ssm/src/main/java/com/micro/fast/common/service/SsmService.java b/boot-starter/boot-starter-ssm/src/main/java/com/micro/fast/common/service/SsmService.java index 10db3e2..48ab266 100644 --- a/boot-starter/boot-starter-ssm/src/main/java/com/micro/fast/common/service/SsmService.java +++ b/boot-starter/boot-starter-ssm/src/main/java/com/micro/fast/common/service/SsmService.java @@ -2,6 +2,8 @@ package com.micro.fast.common.service; import com.micro.fast.boot.starter.common.response.ServerResponse; +import java.util.List; + /** * ssm框架service基础操作接口 * @author lsy @@ -24,7 +26,12 @@ public interface SsmService { */ ServerResponse update(T pojo); /** - * 根据id删除 + * 根据id删除单个 */ ServerResponse deleteById(ID id); + + /** + * 根据id删除多个 + */ + ServerResponse deleteByIds(List ids); } diff --git a/boot-starter/boot-starter-ssm/src/main/java/com/micro/fast/common/service/impl/SsmServiceImpl.java b/boot-starter/boot-starter-ssm/src/main/java/com/micro/fast/common/service/impl/SsmServiceImpl.java index d4a828d..55facd2 100644 --- a/boot-starter/boot-starter-ssm/src/main/java/com/micro/fast/common/service/impl/SsmServiceImpl.java +++ b/boot-starter/boot-starter-ssm/src/main/java/com/micro/fast/common/service/impl/SsmServiceImpl.java @@ -19,7 +19,7 @@ import java.util.List; * @author lsy */ @Setter -public abstract class SsmServiceImpl> implements SsmService{ +public class SsmServiceImpl> implements SsmService{ /** * 这个属性一定要使用子类的覆盖,不然会报空指针 @@ -94,4 +94,13 @@ public abstract class SsmServiceImpl ids) { + int i = mapper.deleteByPrimaryKeys(ids); + if (i<1){ + ServerResponse.errorMsg("没有删除任何记录"); + } + return ServerResponse.successMsg("删除成功"); + } } diff --git a/ms/oauth/src/main/java/com/micro/fast/oauth/feign/UpmsUserService.java b/ms/oauth/src/main/java/com/micro/fast/oauth/feign/UpmsUserService.java index 5189c03..cde2a30 100644 --- a/ms/oauth/src/main/java/com/micro/fast/oauth/feign/UpmsUserService.java +++ b/ms/oauth/src/main/java/com/micro/fast/oauth/feign/UpmsUserService.java @@ -19,6 +19,6 @@ public interface UpmsUserService { * @param username 用户名 * @return */ - @GetMapping("/upms/user/{username}") + @GetMapping("/upmsUser/username/{username}") ServerResponse getUserByUserName(@PathVariable(name = "username") String username); } diff --git a/ms/upms/src/main/java/com/micro/fast/upms/controller/UpmsSystemController.java b/ms/upms/src/main/java/com/micro/fast/upms/controller/UpmsSystemController.java index 2f5470e..2c85b53 100644 --- a/ms/upms/src/main/java/com/micro/fast/upms/controller/UpmsSystemController.java +++ b/ms/upms/src/main/java/com/micro/fast/upms/controller/UpmsSystemController.java @@ -7,12 +7,13 @@ import com.micro.fast.upms.service.UpmsSystemService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.hibernate.validator.constraints.NotBlank; +import org.hibernate.validator.constraints.NotEmpty; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.BindException; -import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; +import java.util.List; /** * 系统管理 @@ -20,21 +21,21 @@ import javax.validation.Valid; */ @Api("系统管理") @RestController -@RequestMapping("/system") +@RequestMapping("/upmsSystem") public class UpmsSystemController { @Autowired private UpmsSystemService upmsSystemService; - @ApiOperation("添加系统") + @ApiOperation("添加信息") @PostMapping - public ServerResponse addSystem(@Valid UpmsSystem upmsSystem,BindingResult errors) throws BindException { + public ServerResponse addSystem(@Valid UpmsSystem upmsSystem) throws BindException { upmsSystem.setCtime(System.currentTimeMillis()); upmsSystem.setStatus((byte)1); return upmsSystemService.add(upmsSystem); } - @ApiOperation("根据id查询系统详细信息") + @ApiOperation("根据id查询详细信息") @GetMapping("/{id}") public ServerResponse getSystemById( @PathVariable(value = "id") @NotBlank(message = BaseConst.BASEMSG_PREFIX+"请传入id") String id){ @@ -50,7 +51,7 @@ public class UpmsSystemController { return upmsSystemService.getByCondition(upmsSystem,pageNum,pageSize,orderBy); } - @ApiOperation("修改系统信息") + @ApiOperation("修改信息") @PutMapping("/{id}") public ServerResponse updateSystem(UpmsSystem upmsSystem, @NotBlank(message = BaseConst.BASEMSG_PREFIX+"请传入id") String id){ @@ -58,10 +59,10 @@ public class UpmsSystemController { return upmsSystemService.update(upmsSystem); } - @ApiOperation("根据id删除系统,多个使用逗号分割") - @DeleteMapping("/{id}") + @ApiOperation("根据id删除,传入数组") + @DeleteMapping public ServerResponse deleteSystem( - @NotBlank(message = BaseConst.BASEMSG_PREFIX+"请传入id") String id){ - return upmsSystemService.deleteById(Integer.valueOf(id)); + @NotEmpty(message = BaseConst.BASEMSG_PREFIX+"请传入id") List ids){ + return upmsSystemService.deleteByIds(ids); } } diff --git a/ms/upms/src/main/java/com/micro/fast/upms/controller/UpmsUserController.java b/ms/upms/src/main/java/com/micro/fast/upms/controller/UpmsUserController.java index efa4093..d6a9435 100644 --- a/ms/upms/src/main/java/com/micro/fast/upms/controller/UpmsUserController.java +++ b/ms/upms/src/main/java/com/micro/fast/upms/controller/UpmsUserController.java @@ -1,6 +1,5 @@ package com.micro.fast.upms.controller; -import com.micro.fast.boot.starter.common.exception.UserException; import com.micro.fast.boot.starter.common.response.BaseConst; import com.micro.fast.boot.starter.common.response.ServerResponse; import com.micro.fast.boot.starter.common.util.ExceptionUtil; @@ -9,58 +8,91 @@ import com.micro.fast.upms.pojo.UpmsUser; import com.micro.fast.upms.service.UpmsUserService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; -import org.apache.commons.lang3.StringUtils; import org.hibernate.validator.constraints.NotBlank; +import org.hibernate.validator.constraints.NotEmpty; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.validation.BindingResult; -import org.springframework.validation.FieldError; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import javax.validation.constraints.NotNull; +import java.util.List; /** * 权限管理系统用户管理 * * @author lsy */ -@Api(description = "后台用户api") +@Api(description = "后台用户管理") @RestController -@RequestMapping(value = "/upms/user") +@RequestMapping(value = "/upmsUser") public class UpmsUserController { - /** - * 用户服务接口 - */ - @Autowired - private UpmsUserService upmsUserService; + /** + * 用户服务接口 + */ + @Autowired + private UpmsUserService upmsUserService; - @Autowired - private UpmsUserMapper upmsUserMapper; + @Autowired + private UpmsUserMapper upmsUserMapper; + + /** + * 用户注册,使用Valid注解对upmsUser进行参数校验 + * BindingResult一定要紧跟校验的对象 + * + * @return + */ + @ApiOperation(value = "添加用户") + @PostMapping + public ServerResponse register(@Valid UpmsUser upmsUser, @RequestParam(defaultValue = "") String repeatPassword) { + ServerResponse register = upmsUserService.register(upmsUser, repeatPassword); + return register; + } + + @ApiOperation(value = "根据用户名称获取用户详细信息") + @GetMapping("/username/{username}") + public ServerResponse getUserByName( + @PathVariable(name = "username") @NotNull(message = ExceptionUtil.split + "用户名不能为空") String username) { + UpmsUser upmsUser = upmsUserMapper.selectByUsername(username); + if (upmsUser == null) { + return ServerResponse.errorMsg("该用户不存在!"); + } + if (upmsUser.getLocked() == 1) { + return ServerResponse.errorMsg("账户被锁定"); + } + return ServerResponse.successData(upmsUser); + } + + @ApiOperation("根据id查询详细信息") + @GetMapping("/{id}") + public ServerResponse getUserById( + @PathVariable(value = "id") @NotBlank(message = BaseConst.BASEMSG_PREFIX + "请传入id") String id) { + return upmsUserService.getById(Integer.valueOf(id)); + } + + @ApiOperation("根据条件分页查询") + @GetMapping + public ServerResponse getUserByCondition(UpmsUser upmsUser, + @RequestParam(defaultValue = "1", required = false) int pageNum, + @RequestParam(defaultValue = "10", required = false) int pageSize, + @RequestParam(required = false) String orderBy) { + return upmsUserService.getByCondition(upmsUser, pageNum, pageSize, orderBy); + } + + @ApiOperation("修改信息") + @PutMapping("/{id}") + public ServerResponse updateUser(UpmsUser upmsUser, + @NotBlank(message = BaseConst.BASEMSG_PREFIX + "请传入id") String id) { + upmsUser.setId(Integer.valueOf(id)); + return upmsUserService.update(upmsUser); + } + + @ApiOperation("根据id删除,传入数组") + @DeleteMapping + public ServerResponse deleteUser( + @NotEmpty(message = BaseConst.BASEMSG_PREFIX + "请传入id") List ids) { + return upmsUserService.deleteByIds(ids); + } - /** - * 用户注册,使用Valid注解对upmsUser进行参数校验 - * BindingResult一定要紧跟校验的对象 - * @return - */ - @ApiOperation(value = "后台用户注册") - @PostMapping - public ServerResponse register(@Valid UpmsUser upmsUser,@RequestParam(defaultValue = "") String repeatPassword) { - ServerResponse register = upmsUserService.register(upmsUser, repeatPassword); - return register; - } - @ApiOperation(value = "根据用户名称获取用户详细信息") - @GetMapping("/{username}") - public ServerResponse getUserByName( - @PathVariable(name = "username") @NotNull(message = ExceptionUtil.split+"用户名不能为空") String username){ - UpmsUser upmsUser = upmsUserMapper.selectByUsername(username); - if (upmsUser ==null){ - return ServerResponse.errorMsg("该用户不存在!"); - } - if (upmsUser.getLocked() == 1){ - return ServerResponse.errorMsg("账户被锁定"); - } - return ServerResponse.successData(upmsUser); - } } diff --git a/ms/upms/src/main/java/com/micro/fast/upms/dao/UpmsSystemMapper.java b/ms/upms/src/main/java/com/micro/fast/upms/dao/UpmsSystemMapper.java index 75ccbbd..5bc69b0 100644 --- a/ms/upms/src/main/java/com/micro/fast/upms/dao/UpmsSystemMapper.java +++ b/ms/upms/src/main/java/com/micro/fast/upms/dao/UpmsSystemMapper.java @@ -27,4 +27,7 @@ public interface UpmsSystemMapper extends SsmMapper { @Override List selectByCondition(UpmsSystem record); + + @Override + int deleteByPrimaryKeys(List id); } \ No newline at end of file diff --git a/ms/upms/src/main/java/com/micro/fast/upms/dao/UpmsUserMapper.java b/ms/upms/src/main/java/com/micro/fast/upms/dao/UpmsUserMapper.java index 9e7099c..7b851d3 100644 --- a/ms/upms/src/main/java/com/micro/fast/upms/dao/UpmsUserMapper.java +++ b/ms/upms/src/main/java/com/micro/fast/upms/dao/UpmsUserMapper.java @@ -1,24 +1,39 @@ package com.micro.fast.upms.dao; +import com.micro.fast.common.dao.SsmMapper; import com.micro.fast.upms.pojo.UpmsUser; import feign.Param; +import java.util.List; + /** * @author lsy */ -public interface UpmsUserMapper { +public interface UpmsUserMapper extends SsmMapper{ + @Override int deleteByPrimaryKey(Integer id); + @Override int insert(UpmsUser record); + @Override int insertSelective(UpmsUser record); + @Override UpmsUser selectByPrimaryKey(Integer id); + @Override int updateByPrimaryKeySelective(UpmsUser record); + @Override int updateByPrimaryKey(UpmsUser record); + @Override + List selectByCondition(UpmsUser record); + + @Override + int deleteByPrimaryKeys(List id); + /** * 根据用户名查找用户 * @param username 用户名 diff --git a/ms/upms/src/main/java/com/micro/fast/upms/service/UpmsSystemService.java b/ms/upms/src/main/java/com/micro/fast/upms/service/UpmsSystemService.java index 6777698..dac7a75 100644 --- a/ms/upms/src/main/java/com/micro/fast/upms/service/UpmsSystemService.java +++ b/ms/upms/src/main/java/com/micro/fast/upms/service/UpmsSystemService.java @@ -1,7 +1,6 @@ package com.micro.fast.upms.service; import com.micro.fast.common.service.SsmService; -import com.micro.fast.upms.pojo.UpmsSystem; /** * 系统管理接口 diff --git a/ms/upms/src/main/java/com/micro/fast/upms/service/UpmsUserService.java b/ms/upms/src/main/java/com/micro/fast/upms/service/UpmsUserService.java index 99fcc28..9f8b4b3 100644 --- a/ms/upms/src/main/java/com/micro/fast/upms/service/UpmsUserService.java +++ b/ms/upms/src/main/java/com/micro/fast/upms/service/UpmsUserService.java @@ -1,13 +1,15 @@ package com.micro.fast.upms.service; import com.micro.fast.boot.starter.common.response.ServerResponse; +import com.micro.fast.common.service.SsmService; import com.micro.fast.upms.pojo.UpmsUser; +import org.apache.avro.ipc.trace.ID; /** * 权限管理系统用户服务接口 * @author lsy */ -public interface UpmsUserService { +public interface UpmsUserService extends SsmService { /** * 注册用户 diff --git a/ms/upms/src/main/java/com/micro/fast/upms/service/impl/UpmsUserServiceImpl.java b/ms/upms/src/main/java/com/micro/fast/upms/service/impl/UpmsUserServiceImpl.java index 8ab5468..6cbaf0c 100644 --- a/ms/upms/src/main/java/com/micro/fast/upms/service/impl/UpmsUserServiceImpl.java +++ b/ms/upms/src/main/java/com/micro/fast/upms/service/impl/UpmsUserServiceImpl.java @@ -5,9 +5,11 @@ import com.micro.fast.boot.starter.common.exception.UserException; import com.micro.fast.boot.starter.common.response.BaseConst.SystemResponse; import com.micro.fast.boot.starter.common.response.BaseConst.UserResponse; import com.micro.fast.boot.starter.common.response.ServerResponse; +import com.micro.fast.common.service.impl.SsmServiceImpl; import com.micro.fast.upms.dao.UpmsUserMapper; import com.micro.fast.upms.pojo.UpmsUser; import com.micro.fast.upms.service.UpmsUserService; +import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; @@ -15,8 +17,9 @@ import org.springframework.stereotype.Service; /** * @author lsy */ -@Service(value = "upmsUserServiceImpl") -public class UpmsUserServiceImpl implements UpmsUserService { +@Service +public class UpmsUserServiceImpl extends SsmServiceImpl + implements UpmsUserService ,InitializingBean { @Autowired @@ -78,4 +81,9 @@ public class UpmsUserServiceImpl implements UpmsUserService { return ServerResponse.success(); } } + + @Override + public void afterPropertiesSet() throws Exception { + super.setMapper(this.upmsUserMapper); + } } diff --git a/ms/upms/src/main/resources/mappers/UpmsSystemMapper.xml b/ms/upms/src/main/resources/mappers/UpmsSystemMapper.xml index 20efb92..4c8157f 100644 --- a/ms/upms/src/main/resources/mappers/UpmsSystemMapper.xml +++ b/ms/upms/src/main/resources/mappers/UpmsSystemMapper.xml @@ -179,4 +179,11 @@ + + DELETE FROM upms_system + WHERE id IN + + #{item} + + \ No newline at end of file diff --git a/ms/upms/src/main/resources/mappers/UpmsUserMapper.xml b/ms/upms/src/main/resources/mappers/UpmsUserMapper.xml index 250b8bb..4c8e6f1 100644 --- a/ms/upms/src/main/resources/mappers/UpmsUserMapper.xml +++ b/ms/upms/src/main/resources/mappers/UpmsUserMapper.xml @@ -177,4 +177,31 @@ + + + delete from upms_user + where id IN + + #{item} + + \ No newline at end of file diff --git a/ms/upms/src/test/java/com/micro/fast/upms/UpmsApplicationTests.java b/ms/upms/src/test/java/com/micro/fast/upms/UpmsApplicationTests.java index aae6340..b43bd3f 100644 --- a/ms/upms/src/test/java/com/micro/fast/upms/UpmsApplicationTests.java +++ b/ms/upms/src/test/java/com/micro/fast/upms/UpmsApplicationTests.java @@ -11,6 +11,7 @@ public class UpmsApplicationTests { @Test public void contextLoads() { + } } diff --git a/ms/upms/src/test/java/com/micro/fast/upms/controller/UpmsSystemControllerTest.java b/ms/upms/src/test/java/com/micro/fast/upms/controller/UpmsSystemControllerTest.java new file mode 100644 index 0000000..60e9afd --- /dev/null +++ b/ms/upms/src/test/java/com/micro/fast/upms/controller/UpmsSystemControllerTest.java @@ -0,0 +1,79 @@ +package com.micro.fast.upms.controller; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MockMvcBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import java.util.Date; + +import static org.junit.Assert.*; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class UpmsSystemControllerTest { + + @Autowired + private WebApplicationContext wac; + + private MockMvc mvc; + + /** + * 每个单元测试前构建mvc环境 + */ + @Before + public void init(){ + mvc = MockMvcBuilders.webAppContextSetup(wac).build(); + } + + /** + * 测试添加系统 + * @throws Exception + */ + @Test + public void addSystem() throws Exception { + //前台传入的是长整形 + Date date = new Date(); + String content = "{'name':'tom','password':'','birthday':'" + date.getTime() + "'}"; + try { + String tom = mvc.perform(MockMvcRequestBuilders.post("/user") + .contentType(MediaType.APPLICATION_JSON_UTF8).content(content)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.jsonPath("$.name").value("tom")) + .andReturn().getResponse().getContentAsString(); + //后台返回的日期格式也是长整形 + System.out.println(tom); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void getSystemById() throws Exception { + + } + + @Test + public void getSystemByCondition() throws Exception { + + } + + @Test + public void updateSystem() throws Exception { + } + + @Test + public void deleteSystem() throws Exception { + } + +} \ No newline at end of file diff --git a/ms/upms/src/test/java/com/micro/fast/upms/service/impl/UpmsUserServiceImplTest.java b/ms/upms/src/test/java/com/micro/fast/upms/service/impl/UpmsUserServiceImplTest.java deleted file mode 100644 index 62489e7..0000000 --- a/ms/upms/src/test/java/com/micro/fast/upms/service/impl/UpmsUserServiceImplTest.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.micro.fast.upms.service.impl; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -import static org.junit.Assert.*; - -@RunWith(SpringRunner.class) -@SpringBootTest -public class UpmsUserServiceImplTest { - - - @Test - public void checkUsername() throws Exception { - - } - - @Test - public void checkEmail() throws Exception { - - } - -} \ No newline at end of file