From c4a6443b375b7d0b697eb4e8ec991e1df160a025 Mon Sep 17 00:00:00 2001 From: SimonSun Date: Wed, 8 Aug 2018 23:22:57 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89Model=E7=94=9F?= =?UTF-8?q?=E6=88=90=EF=BC=9BBasicService=E6=B7=BB=E5=8A=A0=E4=B8=A4?= =?UTF-8?q?=E4=B8=AA=E6=9B=B4=E6=96=B0=E6=96=B9=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 + .../com/simon/common/code/CodeGenerator.java | 49 +++-- .../java/com/simon/common/code/Column.java | 38 ++++ .../simon/common/code/EntityDataModel.java | 54 +++++ .../common/code/FreeMarkerGeneratorUtil.java | 207 ++++++++++++++++++ .../com/simon/common/code/TypeTranslator.java | 105 +++++++++ .../simon/common/service/BasicService.java | 2 + .../com/simon/common/utils/UuidGenerator.java | 21 ++ .../com/simon/common/utils/UuidUtils.java | 16 ++ .../serviceImpl/NewsInfoServiceImpl.java | 10 + .../simon/serviceImpl/NewsTagServiceImpl.java | 10 + .../serviceImpl/ResetPwdInfoServiceImpl.java | 10 + .../serviceImpl/VeriCodeServiceImpl.java | 10 + src/main/resources/templates/code/entity.ftl | 28 +++ src/main/resources/templates/code/mapper.ftl | 0 .../resources/templates/code/service-impl.ftl | 10 + 16 files changed, 561 insertions(+), 15 deletions(-) create mode 100644 src/main/java/com/simon/common/code/Column.java create mode 100644 src/main/java/com/simon/common/code/EntityDataModel.java create mode 100644 src/main/java/com/simon/common/code/FreeMarkerGeneratorUtil.java create mode 100644 src/main/java/com/simon/common/code/TypeTranslator.java create mode 100644 src/main/java/com/simon/common/utils/UuidGenerator.java create mode 100644 src/main/java/com/simon/common/utils/UuidUtils.java create mode 100644 src/main/resources/templates/code/entity.ftl create mode 100644 src/main/resources/templates/code/mapper.ftl diff --git a/pom.xml b/pom.xml index 230bde5..9cca35b 100644 --- a/pom.xml +++ b/pom.xml @@ -226,6 +226,12 @@ freemarker 2.3.27-incubating + + cn.hutool + hutool-core + 4.1.5 + + diff --git a/src/main/java/com/simon/common/code/CodeGenerator.java b/src/main/java/com/simon/common/code/CodeGenerator.java index f9085b4..5e27fc6 100644 --- a/src/main/java/com/simon/common/code/CodeGenerator.java +++ b/src/main/java/com/simon/common/code/CodeGenerator.java @@ -31,13 +31,17 @@ public class CodeGenerator { private static final String MAPPER_INTERFACE_REFERENCE = BASE_PACKAGE + ".common.mapper.MyMapper";//Mapper插件基础接口的完全限定名(第二步提到的核心继承接口Mapper) /*数据库配置*/ - private static final String JDBC_URL = "jdbc:postgresql://127.0.0.1:5432/thymeltetest?useUnicode=true&characterEncoding=UTF-8";//数据库url + /*private static final String JDBC_URL = "jdbc:postgresql://127.0.0.1:5432/thymeltetest?useUnicode=true&characterEncoding=UTF-8";//数据库url private static final String JDBC_USERNAME = "postgres"; private static final String JDBC_PASSWORD = "19961120"; - private static final String JDBC_DIVER_CLASS_NAME = "org.postgresql.Driver"; + private static final String JDBC_DIVER_CLASS_NAME = "org.postgresql.Driver";*/ + private static final String JDBC_URL = "jdbc:mysql://127.0.0.1:3306/thymelte?useUnicode=true&characterEncoding=utf-8&useSSL=false";//数据库url + private static final String JDBC_USERNAME = "root"; + private static final String JDBC_PASSWORD = "19941017"; + private static final String JDBC_DIVER_CLASS_NAME = "com.mysql.jdbc.Driver"; private static final String PROJECT_PATH = System.getProperty("user.dir");//项目在硬盘上的基础路径 - private static final String TEMPLATE_FILE_PATH = PROJECT_PATH + "/src/main/resources/templates/code";//模板位置 + protected static final String TEMPLATE_FILE_PATH = PROJECT_PATH + "/src/main/resources/templates/code";//模板位置 private static final String JAVA_PATH = "/src/test/java"; //java文件路径 private static final String RESOURCES_PATH = "/src/test/resources";//资源文件路径 @@ -47,21 +51,21 @@ public class CodeGenerator { private static final String PACKAGE_PATH_SERVICE_IMPL = packageConvertPath(SERVICE_IMPL_PACKAGE);//生成的Service实现存放路径 private static final String PACKAGE_PATH_CONTROLLER = packageConvertPath(CONTROLLER_PACKAGE);//生成的Controller存放路径 - private static final String AUTHOR = "SimonSun";//@author - private static final String CREATE = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());//@date + protected static final String AUTHOR = "SimonSun";//@author + protected static final String CREATE = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());//@date /*main函数入口,放入表名运行即可生成代码*/ public static void main(String[] args) { //genCode("users", "news_info"); //genCodeByCustomModelName("输入表名","输入自定义Model名称"); - /*genCodeByCustomModelName("news_info","NewsInfo"); +// genCodeByCustomModelName("news_info","NewsInfo"); genCodeByCustomModelName("users", "OauthUser"); - genCodeByCustomModelName("veri_code", "VeriCode"); - genCodeByCustomModelName("reset_pwd_info", "ResetPwdInfo"); - genCodeByCustomModelName("qr_code", "QrCode"); - genCodeByCustomModelName("log_login", "LogLogin"); - genCodeByCustomModelName("news_tag", "NewsTag");*/ +// genCodeByCustomModelName("veri_code", "VeriCode"); +// genCodeByCustomModelName("reset_pwd_info", "ResetPwdInfo"); +// genCodeByCustomModelName("qr_code", "QrCode"); + //genCodeByCustomModelName("log_login", "LogLogin"); +// genCodeByCustomModelName("news_tag", "NewsTag"); } @@ -84,8 +88,8 @@ public class CodeGenerator { */ private static void genCodeByCustomModelName(String tableName, String modelName) { genModelAndMapper(tableName, modelName); - genRepository(tableName, modelName); - genService(tableName, modelName); + //genRepository(tableName, modelName); + //genService(tableName, modelName); //genController(tableName, modelName); } @@ -110,6 +114,7 @@ public class CodeGenerator { pluginConfiguration.addProperty("mappers", MAPPER_INTERFACE_REFERENCE); context.addPluginConfiguration(pluginConfiguration); + //该代码不能满足要求 JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration(); javaModelGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH); javaModelGeneratorConfiguration.setTargetPackage(MODEL_PACKAGE); @@ -128,8 +133,11 @@ public class CodeGenerator { TableConfiguration tableConfiguration = new TableConfiguration(context); tableConfiguration.setTableName(tableName); - if (StringUtils.isNotEmpty(modelName))tableConfiguration.setDomainObjectName(modelName); + if (StringUtils.isNotEmpty(modelName)){ + tableConfiguration.setDomainObjectName(modelName); + } tableConfiguration.setGeneratedKey(new GeneratedKey("id", "Mysql", true, null)); + context.addTableConfiguration(tableConfiguration); List warnings; @@ -151,10 +159,21 @@ public class CodeGenerator { if (generator.getGeneratedJavaFiles().isEmpty() || generator.getGeneratedXmlFiles().isEmpty()) { throw new RuntimeException("生成Model和Mapper失败:" + warnings); } - if (StringUtils.isEmpty(modelName)) modelName = tableNameConvertUpperCamel(tableName); + if (StringUtils.isEmpty(modelName)){ + modelName = tableNameConvertUpperCamel(tableName); + } System.out.println(modelName + ".java 生成成功"); System.out.println(modelName + "Mapper.java 生成成功"); System.out.println(modelName + "Mapper.xml 生成成功"); + + FreeMarkerGeneratorUtil.generatorMvcCode( + JDBC_DIVER_CLASS_NAME, + JDBC_URL, + JDBC_USERNAME, + JDBC_PASSWORD, + tableName, + modelName, + MODEL_PACKAGE); } private static void genRepository(String tableName, String modelName) { diff --git a/src/main/java/com/simon/common/code/Column.java b/src/main/java/com/simon/common/code/Column.java new file mode 100644 index 0000000..2f08fd5 --- /dev/null +++ b/src/main/java/com/simon/common/code/Column.java @@ -0,0 +1,38 @@ +package com.simon.common.code; + +/** + * @author simon + * @create 2018-08-07 21:14 + **/ + +import lombok.Data; + +import java.io.Serializable; + +/** + * 代码生成列实体 + * Created by wangqichang on 2018/5/30. + */ +@Data +public class Column implements Serializable{ + private static final long serialVersionUID = 8051725962502921942L; + /** + * 属性注解 + */ + private String annotation; + + /** + * 属性名 + */ + private String name; + + /** + * 属性类型 + */ + private String type; + + /** + * 属性注释 + */ + private String comment; +} \ No newline at end of file diff --git a/src/main/java/com/simon/common/code/EntityDataModel.java b/src/main/java/com/simon/common/code/EntityDataModel.java new file mode 100644 index 0000000..10a9107 --- /dev/null +++ b/src/main/java/com/simon/common/code/EntityDataModel.java @@ -0,0 +1,54 @@ +package com.simon.common.code; + +/** + * @author simon + * @create 2018-08-07 21:14 + **/ + +import lombok.Data; + +import java.io.Serializable; +import java.util.List; + +/** + * 模板生成属性 + * Created by wangqichang on 2018/5/30. + */ +@Data +public class EntityDataModel implements Serializable { + private static final long serialVersionUID = 32546422336594242L; + /** + * base package + */ + private String entityPackage; + /** + * 文件名后缀 + */ + private String fileSuffix = ".java"; + + /** + * 实体名 + */ + private String entityName; + + /** + * 作者 默认 + */ + private String AUTHOR=CodeGenerator.AUTHOR; + + /** + * 创建时间 + */ + private String CREATE = CodeGenerator.CREATE; + + /** + * 表名 + */ + private String tableName; + + /** + * 字段集合 + */ + private List columns; + +} \ No newline at end of file diff --git a/src/main/java/com/simon/common/code/FreeMarkerGeneratorUtil.java b/src/main/java/com/simon/common/code/FreeMarkerGeneratorUtil.java new file mode 100644 index 0000000..0616837 --- /dev/null +++ b/src/main/java/com/simon/common/code/FreeMarkerGeneratorUtil.java @@ -0,0 +1,207 @@ +package com.simon.common.code; + +/** + * @author simon + * @create 2018-08-07 21:10 + **/ + +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.util.StrUtil; +import freemarker.template.Configuration; +import freemarker.template.Template; +import freemarker.template.TemplateException; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; +import java.sql.*; +import java.util.ArrayList; +import java.util.List; + +/** + * 代码生成工具类 + * Created by wangqichang on 2018/5/30. + */ +@Slf4j +public class FreeMarkerGeneratorUtil { + + /** + * 仅生成dao层 + * + * @param driver + * @param url + * @param user + * @param pwd + */ + public static void generatorMvcCode(String driver, String url, String user, String pwd, String tableName, + String modelName, String basePackage) { + + Connection con = null; + //注册驱动 + try { + Class.forName(driver); + con = DriverManager.getConnection(url, user, pwd); + } catch (Exception e) { + log.error("获取数据连接失败,{}", e.getMessage()); + return; + } + + //获取当前项目路径 + String path = FreeMarkerGeneratorUtil.class.getResource("/").getPath(); + path = StrUtil.sub(path, 1, path.indexOf("/target")); + + //log.info("当前项目路径为:{}", path); + String parentProjectPath = StrUtil.sub(path, 0, path.lastIndexOf("/")); + //获取模板路径 + String templatePath = CodeGenerator.TEMPLATE_FILE_PATH; + //log.info("当前模板路径为:{}", templatePath); + + try { + + String entityDir = null; + //根据实体包名创建目录 + File[] ls = FileUtil.ls(parentProjectPath); + for (File f: ls) { + String currModule = f.toString(); + boolean matches = currModule.matches("(.*?pojo.*?)|(.*?domain.*?)|(.*?entity.*?)"); + if (f.isDirectory()&&matches){ + entityDir = f.toString()+ "/src/test/java/" + basePackage.replace(".", "/"); + break; + } + } + if (StrUtil.isBlank(entityDir)){ + entityDir = path + "/src/test/java/" + basePackage.replace(".", "/"); + } + if (!FileUtil.exist(entityDir)) { + FileUtil.mkdir(entityDir); + log.info("创建目录:{} 成功! ",entityDir); + } + EntityDataModel entityModel = getEntityModel(con, tableName, basePackage, modelName); + //生成每个表实体 + generateCode(entityModel, templatePath, "entity.ftl", entityDir); + + } catch (Exception e) { + log.error("代码生成出错 {}", e.getMessage()); + } + + } + + private static EntityDataModel getEntityModel(Connection con, String tableName, String basePackage, String modelName) + throws Exception { + + //查询表属性,格式化生成实体所需属性 + String sql = ""; + if(getDataBaseType(con) == 1){ + //log.info(con.getCatalog()); + sql = "SELECT table_name, column_name, column_comment, column_type, data_type, column_default, is_nullable " + + "FROM INFORMATION_SCHEMA.COLUMNS " + "WHERE table_name = '" + tableName + "' AND table_schema = '" + con.getCatalog() + "'"; + }else if(getDataBaseType(con) == 2){ + log.info(con.getCatalog()); + sql = "SELECT delta.table_name, delta.column_name, alb.column_comment, alb.column_type, delta.data_type, delta.column_default, delta.is_nullable FROM information_schema.COLUMNS AS delta, ( SELECT C .relname AS table_name, A.attname AS column_name, col_description ( A.attrelid, A.attnum ) AS column_comment, format_type ( A.atttypid, A.atttypmod ) AS column_type, A.attnotnull AS NOTNULL FROM pg_class AS C, pg_attribute AS A WHERE C.relname = '" + tableName + "' AND A.attrelid = C.oid AND A.attnum > 0 ) AS alb WHERE table_schema = 'public' AND delta.TABLE_NAME = '" + tableName + "' AND delta.COLUMN_NAME = alb.column_name"; + } + + + PreparedStatement ps = con.prepareStatement(sql); + ResultSet rs = ps.executeQuery(); + + List columns = new ArrayList<>(); + while (rs.next()) { + Column col = new Column(); + String name = rs.getString("column_name"); + String columnType = rs.getString("column_type"); + String dataType = rs.getString("data_type"); + String comment = rs.getString("column_comment"); + String isNullable = rs.getString("is_nullable"); + + if(StringUtils.isEmpty(comment)){ + comment = name; + } + + String propertyType = null; + if(getDataBaseType(con) == 1){ + propertyType = TypeTranslator.translateMySQL(columnType, dataType); + }else if(getDataBaseType(con) == 2){ + propertyType = TypeTranslator.translatePostgreSQL(columnType, dataType); + }else{ + throw new Exception("暂不支持其他数据库"); + } + + String annotation = null; + if ("id".equals(name)) { + if (propertyType.equalsIgnoreCase("Long")) { + annotation = "@Id\n" + + " @GeneratedValue(generator = \"sequenceId\")\n" + + " @GenericGenerator(name = \"sequenceId\", strategy = \"com.simon.common.utils.snowflake.SequenceId\")"; + }else if(propertyType.equalsIgnoreCase("String")){ + annotation = "@Id\n" + + " @GeneratedValue(generator = \"uuid\")\n" + + " @GenericGenerator(name = \"uuid\", strategy = \"com.simon.common.utils.UuidGenerator\")"; + }else if(propertyType.equalsIgnoreCase("Integer")){ + annotation = "@Id\n" + + " @GeneratedValue(strategy = GenerationType.IDENTITY)"; + }else{ + annotation = "@Id\n" + + " @GeneratedValue(strategy = GenerationType.IDENTITY)"; + } + }else{ + if(isNullable.equalsIgnoreCase("NO")){ + annotation = "@Column(name = \"" + name + "\", nullable = false)"; + }else{ + annotation = "@Column(name = \"" + name + "\")"; + } + } + + col.setName(StrUtil.toCamelCase(name)); + col.setType(propertyType); + col.setAnnotation(annotation); + col.setComment(comment); + columns.add(col); + } + EntityDataModel dataModel = new EntityDataModel(); + dataModel.setEntityPackage(basePackage); + if (StringUtils.isNotEmpty(modelName)) { + dataModel.setEntityName(modelName); + } else { + dataModel.setEntityName(StrUtil.upperFirst(StrUtil.toCamelCase(tableName))); + } + dataModel.setTableName(tableName); + dataModel.setColumns(columns); + return dataModel; + } + + private static void generateCode(EntityDataModel dataModel, String templatePath, String templateName, String outDir) + throws IOException, TemplateException { + + String file = outDir +"/"+ dataModel.getEntityName() + dataModel.getFileSuffix(); + if (FileUtil.exist(file)){ +// log.info("文件:{}已存在,如需覆盖请先对该文件进行", file); +// return; + FileUtil.del(file); + } + //获取模板对象 + Configuration conf = new Configuration(); + File temp = new File(templatePath); + conf.setDirectoryForTemplateLoading(temp); + Template template = conf.getTemplate(templateName); + Writer writer = new FileWriter(file); + //填充数据模型 + template.process(dataModel, writer); + writer.close(); + log.info("代码生成成功,文件位置:{}",file); + } + + public static int getDataBaseType(Connection connection) throws SQLException { + String driverName = connection.getMetaData().getDriverName().toLowerCase(); + //log.info(driverName); + //通过driverName是否包含关键字判断 + if (driverName.contains("mysql")) { + return 1; + } else if (driverName.contains("postgresql")) { + return 2; + } + return -1; + } +} \ No newline at end of file diff --git a/src/main/java/com/simon/common/code/TypeTranslator.java b/src/main/java/com/simon/common/code/TypeTranslator.java new file mode 100644 index 0000000..0178263 --- /dev/null +++ b/src/main/java/com/simon/common/code/TypeTranslator.java @@ -0,0 +1,105 @@ +package com.simon.common.code; + +/** + * 数据表列类型转换为java类型 + * + * @author simon + * @create 2018-08-08 21:09 + **/ + +public class TypeTranslator { + //https://documentation.progress.com/output/DataDirect/DataDirectCloud/index.html#page/queries%2Fmysql-data-types.html%23 + public static String translateMySQL(String columnType, String dataType){ + columnType = columnType.toUpperCase(); + dataType = dataType.toUpperCase(); + if(dataType.equals("BIT")){ + if(columnType.equals("BIT(1)")){ + return "Boolean"; + }else{ + return "Byte[]"; + } + } + if(dataType.equals("TINYINT")){ + if(columnType.equals("TINYINT(1)")){ + return "Boolean"; + }else{ + return "Integer"; + } + } + if(dataType.equals("SMALLINT") || dataType.equals("MEDIUMINT") || dataType.equals("YEAR") || dataType.equals("INT")){ + return "Integer"; + } + if(dataType.contains("INTEGER")){ + if(dataType.contains("UNSIGNED")){ + return "Long"; + }else{ + return "Integer"; + } + } + if(dataType.contains("BIGINT")){ + return "Long"; + } + if(dataType.equals("FLOAT")){ + return "Float"; + } + if(dataType.equals("DOUBLE")){ + return "Double"; + } + if(dataType.equals("DECIMAL") || dataType.equals("NUMERIC")){ + return "BigDecimal"; + } + if(dataType.equals("DATE") || dataType.equals("DATETIME") || dataType.equals("TIMESTAMP") || dataType.equals("TIME")){ + return "Date"; + } + if(dataType.contains("CHAR") || dataType.equals("TEXT")){ + return "String"; + } + if(dataType.contains("BINARY") || dataType.equals("BLOB")){ + return "Byte[]"; + } + return "String"; + } + + //https://documentation.progress.com/output/DataDirect/DataDirectCloud/index.html#page/queries/postgresql-data-types.html + public static String translatePostgreSQL(String columnType, String dataType){ + columnType = columnType.toUpperCase(); + dataType = dataType.toUpperCase(); + + if(dataType.equals("BIT")){ + if(columnType.equals("BIT(1)")){ + return "Boolean"; + }else{ + return "Byte[]"; + } + } + + if(dataType.equals("INTEGER") || dataType.equals("SMALLINT") || dataType.equals("SERIAL")){ + return "Integer"; + } + if(dataType.equals("BIGINT") || dataType.equals("BIGSERIAL")){ + return "Long"; + } + if(dataType.contains("CHARACTER") || dataType.contains("TEXT")){ + return "String"; + } + if(dataType.equals("BOOLEAN")){ + return "Boolean"; + } + if(dataType.equals("DECIMAL") || dataType.equals("NUMERIC")){ + return "BigDecimal"; + } + if(dataType.equals("REAL")){ + return "Float"; + } + if(dataType.contains("DOUBLE")){ + return "Double"; + } + if(dataType.contains("TIME") || dataType.equals("DATE")){ + return "Date"; + } + if(dataType.equals("BYTEA")){ + return "Byte[]"; + } + return "String"; + } +} diff --git a/src/main/java/com/simon/common/service/BasicService.java b/src/main/java/com/simon/common/service/BasicService.java index 986298f..775bbe3 100644 --- a/src/main/java/com/simon/common/service/BasicService.java +++ b/src/main/java/com/simon/common/service/BasicService.java @@ -25,4 +25,6 @@ public interface BasicService { int insertList(List list); int insert(T model); int insertSelective(T model); + int updateByPrimaryKey(T model); + int updateByPrimaryKeySelective(T model); } \ No newline at end of file diff --git a/src/main/java/com/simon/common/utils/UuidGenerator.java b/src/main/java/com/simon/common/utils/UuidGenerator.java new file mode 100644 index 0000000..9eb8768 --- /dev/null +++ b/src/main/java/com/simon/common/utils/UuidGenerator.java @@ -0,0 +1,21 @@ +package com.simon.common.utils; + +import org.hibernate.HibernateException; +import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.id.IdentifierGenerator; + +import java.io.Serializable; + +/** + * uuid generator + * + * @author simon + * @create 2018-08-06 11:31 + **/ + +public class UuidGenerator implements IdentifierGenerator { + @Override + public Serializable generate(SessionImplementor sessionImplementor, Object o) throws HibernateException { + return UuidUtils.getUUID(); + } +} diff --git a/src/main/java/com/simon/common/utils/UuidUtils.java b/src/main/java/com/simon/common/utils/UuidUtils.java new file mode 100644 index 0000000..ac4fd04 --- /dev/null +++ b/src/main/java/com/simon/common/utils/UuidUtils.java @@ -0,0 +1,16 @@ +package com.simon.common.utils; + +import java.util.UUID; + +/** + * UUID生成工具 + * + * @author simon + * @create 2018-08-02 17:18 + **/ + +public class UuidUtils { + public synchronized static String getUUID(){ + return UUID.randomUUID().toString().replace("-", "").toLowerCase(); + } +} diff --git a/src/main/java/com/simon/serviceImpl/NewsInfoServiceImpl.java b/src/main/java/com/simon/serviceImpl/NewsInfoServiceImpl.java index f5c7c56..0afc689 100644 --- a/src/main/java/com/simon/serviceImpl/NewsInfoServiceImpl.java +++ b/src/main/java/com/simon/serviceImpl/NewsInfoServiceImpl.java @@ -85,4 +85,14 @@ public class NewsInfoServiceImpl implements NewsInfoService { public int insertSelective(NewsInfo newsInfo){ return newsInfoMapper.insertSelective(newsInfo); } + + @Override + public int updateByPrimaryKey(NewsInfo newsInfo) { + return newsInfoMapper.updateByPrimaryKey(newsInfo); + } + + @Override + public int updateByPrimaryKeySelective(NewsInfo newsInfo) { + return newsInfoMapper.updateByPrimaryKeySelective(newsInfo); + } } \ No newline at end of file diff --git a/src/main/java/com/simon/serviceImpl/NewsTagServiceImpl.java b/src/main/java/com/simon/serviceImpl/NewsTagServiceImpl.java index 39e020a..50d43e1 100644 --- a/src/main/java/com/simon/serviceImpl/NewsTagServiceImpl.java +++ b/src/main/java/com/simon/serviceImpl/NewsTagServiceImpl.java @@ -85,4 +85,14 @@ public class NewsTagServiceImpl implements NewsTagService { public int insertSelective(NewsTag newsTag){ return newsTagMapper.insertSelective(newsTag); } + + @Override + public int updateByPrimaryKey(NewsTag newsTag) { + return newsTagMapper.updateByPrimaryKey(newsTag); + } + + @Override + public int updateByPrimaryKeySelective(NewsTag newsTag) { + return newsTagMapper.updateByPrimaryKeySelective(newsTag); + } } \ No newline at end of file diff --git a/src/main/java/com/simon/serviceImpl/ResetPwdInfoServiceImpl.java b/src/main/java/com/simon/serviceImpl/ResetPwdInfoServiceImpl.java index 7d8effa..08027eb 100644 --- a/src/main/java/com/simon/serviceImpl/ResetPwdInfoServiceImpl.java +++ b/src/main/java/com/simon/serviceImpl/ResetPwdInfoServiceImpl.java @@ -85,4 +85,14 @@ public class ResetPwdInfoServiceImpl implements ResetPwdInfoService { public int insertSelective(ResetPwdInfo resetPwdInfo){ return resetPwdInfoMapper.insertSelective(resetPwdInfo); } + + @Override + public int updateByPrimaryKey(ResetPwdInfo resetPwdInfo) { + return resetPwdInfoMapper.updateByPrimaryKey(resetPwdInfo); + } + + @Override + public int updateByPrimaryKeySelective(ResetPwdInfo resetPwdInfo) { + return resetPwdInfoMapper.updateByPrimaryKeySelective(resetPwdInfo); + } } \ No newline at end of file diff --git a/src/main/java/com/simon/serviceImpl/VeriCodeServiceImpl.java b/src/main/java/com/simon/serviceImpl/VeriCodeServiceImpl.java index d790999..298c33f 100644 --- a/src/main/java/com/simon/serviceImpl/VeriCodeServiceImpl.java +++ b/src/main/java/com/simon/serviceImpl/VeriCodeServiceImpl.java @@ -88,6 +88,16 @@ public class VeriCodeServiceImpl implements VeriCodeService { return veriCodeMapper.insertSelective(veriCode); } + @Override + public int updateByPrimaryKey(VeriCode veriCode) { + return veriCodeMapper.updateByPrimaryKey(veriCode); + } + + @Override + public int updateByPrimaryKeySelective(VeriCode veriCode) { + return veriCodeMapper.updateByPrimaryKeySelective(veriCode); + } + @Override public VeriCode findByPhone(String phone) { return veriCodeRepository.findByPhone(phone); diff --git a/src/main/resources/templates/code/entity.ftl b/src/main/resources/templates/code/entity.ftl new file mode 100644 index 0000000..67a176d --- /dev/null +++ b/src/main/resources/templates/code/entity.ftl @@ -0,0 +1,28 @@ +package ${entityPackage}; + +import org.hibernate.annotations.GenericGenerator; + +import lombok.Data; +import javax.persistence.*; +import java.io.Serializable; +import java.util.Date; +import java.math.BigDecimal; + +/** +* @author ${AUTHOR} +* @create ${CREATE} +**/ +@Data +@Entity +@Table(name="${tableName}") +public class ${entityName} implements Serializable{ + private static final long serialVersionUID = 1L; +<#list columns as column> + + /** + * ${(column.comment)} + */ + ${(column.annotation)} + private ${column.type} ${column.name}; + +} \ No newline at end of file diff --git a/src/main/resources/templates/code/mapper.ftl b/src/main/resources/templates/code/mapper.ftl new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/templates/code/service-impl.ftl b/src/main/resources/templates/code/service-impl.ftl index 7e3b0b6..f2788e6 100644 --- a/src/main/resources/templates/code/service-impl.ftl +++ b/src/main/resources/templates/code/service-impl.ftl @@ -86,4 +86,14 @@ public class ${modelNameUpperCamel}ServiceImpl implements ${modelNameUpperCamel} public int insertSelective(${modelNameUpperCamel} ${modelNameLowerCamel}){ return ${modelNameLowerCamel}Mapper.insertSelective(${modelNameLowerCamel}); } + + @Override + public int updateByPrimaryKey(${modelNameUpperCamel} ${modelNameLowerCamel}){ + return ${modelNameLowerCamel}Mapper.updateByPrimaryKey(${modelNameLowerCamel}); + } + + @Override + public int updateByPrimaryKeySelective(){ + return ${modelNameLowerCamel}Mapper.updateByPrimaryKeySelective(${modelNameLowerCamel}); + } } \ No newline at end of file -- Gitee From 1d09feffca9da023f64a596c58a0ed905f3c8bba Mon Sep 17 00:00:00 2001 From: SimonSun Date: Wed, 8 Aug 2018 23:49:09 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=90=AF=E5=8A=A8=E7=B1=BB=E4=BD=BF?= =?UTF-8?q?=E7=94=A8@MapperScan=EF=BC=8CMapper=E7=B1=BB=E5=8E=BB=E6=8E=89@?= =?UTF-8?q?Repository=E6=B3=A8=E8=A7=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/simon/mapper/AuthorityMapper.java | 2 -- src/main/java/com/simon/mapper/LogLoginMapper.java | 4 ++-- src/main/java/com/simon/mapper/NewsInfoMapper.java | 1 - src/main/java/com/simon/mapper/NewsTagMapper.java | 1 - src/main/java/com/simon/mapper/OauthUserMapper.java | 5 ++--- src/main/java/com/simon/mapper/QrCodeMapper.java | 1 - src/main/java/com/simon/mapper/ResetPwdInfoMapper.java | 1 - src/main/java/com/simon/mapper/VeriCodeMapper.java | 2 -- 8 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/simon/mapper/AuthorityMapper.java b/src/main/java/com/simon/mapper/AuthorityMapper.java index 10085fa..5898a72 100644 --- a/src/main/java/com/simon/mapper/AuthorityMapper.java +++ b/src/main/java/com/simon/mapper/AuthorityMapper.java @@ -2,9 +2,7 @@ package com.simon.mapper; import com.simon.common.mapper.MyMapper; import com.simon.model.Authority; -import org.springframework.stereotype.Repository; -@Repository public interface AuthorityMapper extends MyMapper { } \ No newline at end of file diff --git a/src/main/java/com/simon/mapper/LogLoginMapper.java b/src/main/java/com/simon/mapper/LogLoginMapper.java index 8ef3419..ebca4be 100644 --- a/src/main/java/com/simon/mapper/LogLoginMapper.java +++ b/src/main/java/com/simon/mapper/LogLoginMapper.java @@ -2,9 +2,9 @@ package com.simon.mapper; import com.simon.common.mapper.MyMapper; import com.simon.model.LogLogin; +import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; -@Repository public interface LogLoginMapper extends MyMapper { - LogLogin selectByPrimaryKey(Long id); + LogLogin selectByPrimaryKey(@Param("id") Long id); } \ No newline at end of file diff --git a/src/main/java/com/simon/mapper/NewsInfoMapper.java b/src/main/java/com/simon/mapper/NewsInfoMapper.java index c9fcd39..485ab40 100644 --- a/src/main/java/com/simon/mapper/NewsInfoMapper.java +++ b/src/main/java/com/simon/mapper/NewsInfoMapper.java @@ -4,7 +4,6 @@ import com.simon.common.mapper.MyMapper; import com.simon.model.NewsInfo; import org.springframework.stereotype.Repository; -@Repository public interface NewsInfoMapper extends MyMapper { } \ No newline at end of file diff --git a/src/main/java/com/simon/mapper/NewsTagMapper.java b/src/main/java/com/simon/mapper/NewsTagMapper.java index 1e87206..7dcf039 100644 --- a/src/main/java/com/simon/mapper/NewsTagMapper.java +++ b/src/main/java/com/simon/mapper/NewsTagMapper.java @@ -4,6 +4,5 @@ import com.simon.common.mapper.MyMapper; import com.simon.model.NewsTag; import org.springframework.stereotype.Repository; -@Repository public interface NewsTagMapper extends MyMapper { } \ No newline at end of file diff --git a/src/main/java/com/simon/mapper/OauthUserMapper.java b/src/main/java/com/simon/mapper/OauthUserMapper.java index b697bda..afb0343 100644 --- a/src/main/java/com/simon/mapper/OauthUserMapper.java +++ b/src/main/java/com/simon/mapper/OauthUserMapper.java @@ -2,9 +2,8 @@ package com.simon.mapper; import com.simon.common.mapper.MyMapper; import com.simon.model.OauthUser; -import org.springframework.stereotype.Repository; +import org.apache.ibatis.annotations.Param; -@Repository public interface OauthUserMapper extends MyMapper { - int updatePwdByPhone(String phone, String password); + int updatePwdByPhone(@Param("phone") String phone, @Param("password") String password); } \ No newline at end of file diff --git a/src/main/java/com/simon/mapper/QrCodeMapper.java b/src/main/java/com/simon/mapper/QrCodeMapper.java index efaf6ce..00f4f6a 100644 --- a/src/main/java/com/simon/mapper/QrCodeMapper.java +++ b/src/main/java/com/simon/mapper/QrCodeMapper.java @@ -4,6 +4,5 @@ import com.simon.common.mapper.MyMapper; import com.simon.model.QrCode; import org.springframework.stereotype.Repository; -@Repository public interface QrCodeMapper extends MyMapper { } \ No newline at end of file diff --git a/src/main/java/com/simon/mapper/ResetPwdInfoMapper.java b/src/main/java/com/simon/mapper/ResetPwdInfoMapper.java index e8258df..ac34b17 100644 --- a/src/main/java/com/simon/mapper/ResetPwdInfoMapper.java +++ b/src/main/java/com/simon/mapper/ResetPwdInfoMapper.java @@ -4,6 +4,5 @@ import com.simon.common.mapper.MyMapper; import com.simon.model.ResetPwdInfo; import org.springframework.stereotype.Repository; -@Repository public interface ResetPwdInfoMapper extends MyMapper { } \ No newline at end of file diff --git a/src/main/java/com/simon/mapper/VeriCodeMapper.java b/src/main/java/com/simon/mapper/VeriCodeMapper.java index 48c7b43..2d8e149 100644 --- a/src/main/java/com/simon/mapper/VeriCodeMapper.java +++ b/src/main/java/com/simon/mapper/VeriCodeMapper.java @@ -2,8 +2,6 @@ package com.simon.mapper; import com.simon.common.mapper.MyMapper; import com.simon.model.VeriCode; -import org.springframework.stereotype.Repository; -@Repository public interface VeriCodeMapper extends MyMapper { } \ No newline at end of file -- Gitee From 7e0c2215e973469186dfef4e0a31178bf6937a07 Mon Sep 17 00:00:00 2001 From: SimonSun Date: Wed, 8 Aug 2018 23:52:10 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0README.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 77498c1..0b7efc8 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,8 @@ Oauth2 Client通常是要被保护的资源,例如app接口。配套的Oauth2 5. 集成swagger2,并配置非全局、无需重复输入的header参数(token),访问[http://localhost:8182/swagger-ui.html](http://localhost:8182/swagger-ui.html),全局Authorize的值为"Bearer "+"access_token",注意"Bearer"和"access_token"之间有一个空格; 6. 集成Redis缓存,默认使用Ehcache缓存,若要切换成Redis缓存,请查看`application.yml`缓存配置注释; 7. 集成阿里大鱼(需要安装阿里大鱼jar,安装方法:运行src/main/resources/jars/install.bat); -8. 代码生成器,运行`com/simon/common/code/CodeGenerator.java`的main方法进行代码生成,代码生成位置默认是当前项目的test文件夹。可生成Model、Mapper、Repository、Service、ServiceImpl、Controller。需要注意的是,生成的Model需要手动添加`@Entity`注解,生成的Mapper需要手动添加`@Repository`注解。 +8. 代码生成器,运行`com/simon/common/code/CodeGenerator.java`的main方法进行代码生成,代码生成位置默认是当前项目的test文件夹。可生成Model、Mapper、Repository、Service、ServiceImpl、Controller。~~需要注意的是,生成的Model需要手动添加`@Entity`注解,生成的Mapper需要手动添加`@Repository`注解。~~ + 请下载与Spring Boot对应版本的oauthserver: -- Gitee