完善maven插件,自动生成增删改差 dao service contoller 层
This commit is contained in:
+45
@@ -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);
|
||||
}
|
||||
}
|
||||
+4
@@ -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 {
|
||||
|
||||
|
||||
+5
-40
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+11
-27
@@ -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<String,String> tablesNameAndDoMainObjectsName;
|
||||
|
||||
public Map<String, String> getTablesNameAndDoMainObjectsName() {
|
||||
return tablesNameAndDoMainObjectsName;
|
||||
}
|
||||
private String servicePackage;
|
||||
|
||||
public void setTablesNameAndDoMainObjectsName(Map<String, String> 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;
|
||||
}
|
||||
|
||||
+132
@@ -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<String, String> 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<String, Object> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -79,12 +79,12 @@ public class MybatisGeneratorExtend {
|
||||
Map<String,Object> 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();
|
||||
|
||||
+65
-36
@@ -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<String, Object> data, String targetFileName,String projectPath) {
|
||||
public static boolean createFile(String templateName, Map<String, Object> 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;
|
||||
}
|
||||
|
||||
+2
-1
@@ -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();
|
||||
|
||||
+7
-1
@@ -6,4 +6,10 @@ db.username=root
|
||||
db.password=studyj2e
|
||||
mybatisGenerator.pojoPackage=pojoPackage
|
||||
mybatisGenerator.daoPackage=daoPackage
|
||||
mybatisGenerator.tablesPrefix=upms
|
||||
mybatisGenerator.tablesPrefix=upms
|
||||
# service controllerËùÐèÒªµÄÅäÖÃ
|
||||
mybatisGenerator.servicePackage=
|
||||
mybatisGenerator.controllerPackage=
|
||||
mybatisGenerator.serviceImplPackage=
|
||||
mybatisGenerator.primaryKeyType=
|
||||
mybatisGenerator.isOverWrite=
|
||||
+65
@@ -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);
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package ${mybatisGenerator.servicePackage};
|
||||
|
||||
import com.micro.fast.common.service.SsmService;
|
||||
|
||||
/**
|
||||
* @author lsy
|
||||
*/
|
||||
public interface ${pojoName}Service<T,ID> extends SsmService<T,ID>{
|
||||
|
||||
}
|
||||
+28
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -25,4 +25,6 @@ public interface SsmMapper<T,ID> {
|
||||
* @return
|
||||
*/
|
||||
List<T> selectByCondition(T record);
|
||||
|
||||
int deleteByPrimaryKeys(List<ID> id);
|
||||
}
|
||||
|
||||
+8
-1
@@ -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<T,ID> {
|
||||
*/
|
||||
ServerResponse update(T pojo);
|
||||
/**
|
||||
* 根据id删除
|
||||
* 根据id删除单个
|
||||
*/
|
||||
ServerResponse deleteById(ID id);
|
||||
|
||||
/**
|
||||
* 根据id删除多个
|
||||
*/
|
||||
ServerResponse deleteByIds(List<ID> ids);
|
||||
}
|
||||
|
||||
+10
-1
@@ -19,7 +19,7 @@ import java.util.List;
|
||||
* @author lsy
|
||||
*/
|
||||
@Setter
|
||||
public abstract class SsmServiceImpl<T, ID extends Serializable, MAPPER extends SsmMapper<T,ID>> implements SsmService<T,ID>{
|
||||
public class SsmServiceImpl<T, ID extends Serializable, MAPPER extends SsmMapper<T,ID>> implements SsmService<T,ID>{
|
||||
|
||||
/**
|
||||
* 这个属性一定要使用子类的覆盖,不然会报空指针
|
||||
@@ -94,4 +94,13 @@ public abstract class SsmServiceImpl<T, ID extends Serializable, MAPPER extends
|
||||
}
|
||||
return ServerResponse.successMsg("删除成功");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerResponse deleteByIds(List<ID> ids) {
|
||||
int i = mapper.deleteByPrimaryKeys(ids);
|
||||
if (i<1){
|
||||
ServerResponse.errorMsg("没有删除任何记录");
|
||||
}
|
||||
return ServerResponse.successMsg("删除成功");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,6 @@ public interface UpmsUserService {
|
||||
* @param username 用户名
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/upms/user/{username}")
|
||||
@GetMapping("/upmsUser/username/{username}")
|
||||
ServerResponse<UpmsUser> getUserByUserName(@PathVariable(name = "username") String username);
|
||||
}
|
||||
|
||||
@@ -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<UpmsSystem,Integer> 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<Integer> ids){
|
||||
return upmsSystemService.deleteByIds(ids);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<UpmsUser, Integer> upmsUserService;
|
||||
|
||||
@Autowired
|
||||
private UpmsUserMapper upmsUserMapper;
|
||||
@Autowired
|
||||
private UpmsUserMapper upmsUserMapper;
|
||||
|
||||
/**
|
||||
* 用户注册,使用Valid注解对upmsUser进行参数校验
|
||||
* BindingResult一定要紧跟校验的对象
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation(value = "添加用户")
|
||||
@PostMapping
|
||||
public ServerResponse<UpmsUser> register(@Valid UpmsUser upmsUser, @RequestParam(defaultValue = "") String repeatPassword) {
|
||||
ServerResponse<UpmsUser> 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<Integer> ids) {
|
||||
return upmsUserService.deleteByIds(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户注册,使用Valid注解对upmsUser进行参数校验
|
||||
* BindingResult一定要紧跟校验的对象
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation(value = "后台用户注册")
|
||||
@PostMapping
|
||||
public ServerResponse<UpmsUser> register(@Valid UpmsUser upmsUser,@RequestParam(defaultValue = "") String repeatPassword) {
|
||||
ServerResponse<UpmsUser> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,4 +27,7 @@ public interface UpmsSystemMapper extends SsmMapper<UpmsSystem,Integer> {
|
||||
|
||||
@Override
|
||||
List<UpmsSystem> selectByCondition(UpmsSystem record);
|
||||
|
||||
@Override
|
||||
int deleteByPrimaryKeys(List<Integer> id);
|
||||
}
|
||||
@@ -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<UpmsUser,Integer>{
|
||||
@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<UpmsUser> selectByCondition(UpmsUser record);
|
||||
|
||||
@Override
|
||||
int deleteByPrimaryKeys(List<Integer> id);
|
||||
|
||||
/**
|
||||
* 根据用户名查找用户
|
||||
* @param username 用户名
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.micro.fast.upms.service;
|
||||
|
||||
import com.micro.fast.common.service.SsmService;
|
||||
import com.micro.fast.upms.pojo.UpmsSystem;
|
||||
|
||||
/**
|
||||
* 系统管理接口
|
||||
|
||||
@@ -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<T,ID> extends SsmService<T,ID> {
|
||||
|
||||
/**
|
||||
* 注册用户
|
||||
|
||||
@@ -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<UpmsUser,Integer,UpmsUserMapper>
|
||||
implements UpmsUserService<UpmsUser,Integer> ,InitializingBean {
|
||||
|
||||
|
||||
@Autowired
|
||||
@@ -78,4 +81,9 @@ public class UpmsUserServiceImpl implements UpmsUserService {
|
||||
return ServerResponse.success();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
super.setMapper(this.upmsUserMapper);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,4 +179,11 @@
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
<delete id="deleteByPrimaryKeys" parameterType="java.util.List">
|
||||
DELETE FROM upms_system
|
||||
WHERE id IN
|
||||
<foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
|
||||
#{item}
|
||||
</foreach>
|
||||
</delete>
|
||||
</mapper>
|
||||
@@ -177,4 +177,31 @@
|
||||
<select id="countEmail" resultType="java.lang.Integer">
|
||||
SELECT COUNT(email) FROM upms_user WHERE email = #{email}
|
||||
</select>
|
||||
<select id="selectByCondition" resultType="com.micro.fast.upms.pojo.UpmsUser">
|
||||
SELECT <include refid="Base_Column_List"/> FROM micro.upms_user
|
||||
<where>
|
||||
<if test="username != null">
|
||||
username like "%"#{username}"%"
|
||||
</if>
|
||||
<if test="realname != null">
|
||||
and realname like "%"#{realname}"%"
|
||||
</if>
|
||||
<if test="phone != null">
|
||||
and phone like "%"#{phone}"%"
|
||||
</if>
|
||||
<if test="email != null">
|
||||
and email like "%"#{email}"%"
|
||||
</if>
|
||||
<if test="sex != null">
|
||||
and sex = #{sex}
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
<delete id="deleteByPrimaryKeys" parameterType="java.util.List">
|
||||
delete from upms_user
|
||||
where id IN
|
||||
<foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
|
||||
#{item}
|
||||
</foreach>
|
||||
</delete>
|
||||
</mapper>
|
||||
@@ -11,6 +11,7 @@ public class UpmsApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user