<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.whackon.generator</groupId> <artifactId>generator-mysql</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.12.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.7.1</version> </plugin> <plugin> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.0.0</version> </plugin> </plugins> </pluginManagement> </build> </project>
server: port: 80 spring: application: name: generator-mysql main: allow-bean-definition-overriding: true thymeleaf: cache: false encoding: UTF-8 prefix: classpath:/view/ suffix: .html
system.properties
###### Freemarker 模板路径 freemarker.template.path=templates ###### JDBC 连接信息 jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url.prefix=jdbc:mysql:// jdbc.url.suffix=?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true
package com.whackon.generator.controller; import com.whackon.generator.pojo.vo.GeneratorVO; import com.whackon.generator.service.GeneratorService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * <b>生成器控制层类</b> * @author Arthur * @version 1.0.0 */ @Controller("generatorController") public class GeneratorController { @Autowired private GeneratorService generatorService; @Autowired private HttpServletRequest request; @Autowired private HttpServletResponse response; /** * <b>转发到首页面</b> * @return * @throws Exception */ @GetMapping("/") public String forwardPage() throws Exception { return "generator_index"; } /** * <b>生成代码文件</b> * @param generatorVO * @return * @throws Exception */ @PostMapping("/generate") @ResponseBody public boolean createCodeFile(GeneratorVO generatorVO) throws Exception { try { generatorService.generateCodeFile(generatorVO); return true; } catch (Exception e) { e.printStackTrace(); } return false; } }
package com.whackon.generator.dao; import com.whackon.generator.pojo.entity.TableBean; import java.sql.Connection; import java.util.List; /** * <b>生成器数据持久层接口</b> * @author Arthur * @version 1.0.0 */ public interface GeneratorDao { /** * <b>根据数据库名获得该库所有表字段信息</b> * @param dbName * @param connection * @return * @throws Exception */ List<TableBean> findTableBeanListByDBName(String dbName, Connection connection) throws Exception; }
package com.whackon.generator.dao.impl; import com.whackon.generator.dao.GeneratorDao; import com.whackon.generator.pojo.entity.ColumnProperty; import com.whackon.generator.pojo.entity.TableBean; import com.whackon.generator.util.DataTypeUtil; import com.whackon.generator.util.NameUtil; import org.springframework.stereotype.Component; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; /** * <b>生成器数据持久层接口实现类</b> * @author Arthur * @version 1.0.0 */ @Component("generatorDao") public class GeneratorDaoImpl implements GeneratorDao { /** * <b>根据数据库名获得该库所有表字段信息</b> * @param dbName * @param connection * @return * @throws Exception */ @Override public List<TableBean> findTableBeanListByDBName(String dbName, Connection connection) throws Exception { List<TableBean> tableBeanList = new ArrayList<TableBean>(); // 根据数据库名查询所有的数据表信息 String SQL = "select table_name tableName, table_comment tableComment from information_schema.tables " + "where table_schema=?"; // 使用 Connection 创建 PreparedStatement 对象 PreparedStatement statement = connection.prepareStatement(SQL); statement.setString(1, dbName); // 查询获得结果 ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { // 创建 TableBean 对象 TableBean tableBean = new TableBean(); // 分别设定属性 tableBean.setTableName(resultSet.getString("tableName")); tableBean.setTableComment(resultSet.getString("tableComment")); tableBean.setTableAlias(NameUtil.getTableAliasNameFromTable(tableBean.getTableName())); tableBean.setBeanName(NameUtil.getBeanNameFromTable(tableBean.getTableName())); // 根据数据库表,获得该表的所有字段 findColPropListByTable(dbName, tableBean, connection); tableBeanList.add(tableBean); } return tableBeanList; } private void findColPropListByTable(String dbName, TableBean tableBean, Connection connection) throws Exception { List<ColumnProperty> colPropList = new ArrayList<ColumnProperty>(); String SQL = "select table_name tableName, column_name columnName, data_type dataType, " + "column_comment columnComment from information_schema.columns " + "where table_schema=? and table_name=? order by ordinal_position asc"; // 使用 Connection 创建 PreparedStatement 对象 PreparedStatement statement = connection.prepareStatement(SQL); statement.setString(1, dbName); statement.setString(2, tableBean.getTableName()); // 进行查询 ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { ColumnProperty colProp = new ColumnProperty(); colProp.setColName(resultSet.getString("columnName")); colProp.setColDataType(resultSet.getString("dataType")); colProp.setColComment(resultSet.getString("columnComment")); colProp.setTableName(resultSet.getString("tableName")); colProp.setTableAlias(tableBean.getTableAlias()); colProp.setPropName(NameUtil.getPropertyNameFromColumn(colProp.getColName())); DataTypeUtil.getPropertyDataTypeFromColumn(colProp, tableBean); colProp.setGetterMethodName("get" + colProp.getPropName().substring(0,1).toUpperCase() + colProp.getPropName().substring(1, colProp.getPropName().length())); colProp.setSetterMethodName("set" + colProp.getGetterMethodName().substring( 3, colProp.getGetterMethodName().length())); colProp.setBeanName(tableBean.getBeanName()); colPropList.add(colProp); } tableBean.setColPropList(colPropList); } }
package com.whackon.generator.pojo.entity; import java.io.Serializable; /** * <b>字段属性信息</b> */ public class ColumnProperty implements Serializable { private String colName; // 字段名 private String colDataType; // 字段类型 private String colComment; // 字段注释 private String tableName; // 数据表名 private String tableAlias; // 数据表别名 private String propName; // 属性名 private String propFullDataType; // 属性类型全限定名 private String propShortDataType; // 属性类型短命 private String getterMethodName; // get 方法名 private String setterMethodName; // set 方法名 private String beanName; // 实体类名 public String getColName() { return colName; } public void setColName(String colName) { this.colName = colName; } public String getColDataType() { return colDataType; } public void setColDataType(String colDataType) { this.colDataType = colDataType; } public String getColComment() { return colComment; } public void setColComment(String colComment) { this.colComment = colComment; } public String getTableName() { return tableName; } public void setTableName(String tableName) { this.tableName = tableName; } public String getTableAlias() { return tableAlias; } public void setTableAlias(String tableAlias) { this.tableAlias = tableAlias; } public String getPropName() { return propName; } public void setPropName(String propName) { this.propName = propName; } public String getPropFullDataType() { return propFullDataType; } public void setPropFullDataType(String propFullDataType) { this.propFullDataType = propFullDataType; } public String getPropShortDataType() { return propShortDataType; } public void setPropShortDataType(String propShortDataType) { this.propShortDataType = propShortDataType; } public String getGetterMethodName() { return getterMethodName; } public void setGetterMethodName(String getterMethodName) { this.getterMethodName = getterMethodName; } public String getSetterMethodName() { return setterMethodName; } public void setSetterMethodName(String setterMethodName) { this.setterMethodName = setterMethodName; } public String getBeanName() { return beanName; } public void setBeanName(String beanName) { this.beanName = beanName; } }
package com.whackon.generator.pojo.entity; import java.io.Serializable; import java.util.List; /** * <b>数据表对象实体信息</b> */ public class TableBean implements Serializable { private static final long serialVersionUID = 1360869488379493240L; private String tableName; // 数据库表明 private String tableComment; // 数据库表注释 private String tableAlias; // 数据表别名 private String beanName; // 类名 private boolean hasDate; // 是否包含有 Date 类型 private List<ColumnProperty> colPropList; // 对应字段属性列表 public String getTableName() { return tableName; } public void setTableName(String tableName) { this.tableName = tableName; } public String getTableComment() { return tableComment; } public void setTableComment(String tableComment) { this.tableComment = tableComment; } public String getTableAlias() { return tableAlias; } public void setTableAlias(String tableAlias) { this.tableAlias = tableAlias; } public String getBeanName() { return beanName; } public void setBeanName(String beanName) { this.beanName = beanName; } public boolean getHasDate() { return hasDate; } public void setHasDate(boolean hasDate) { this.hasDate = hasDate; } public List<ColumnProperty> getColPropList() { return colPropList; } public void setColPropList(List<ColumnProperty> colPropList) { this.colPropList = colPropList; } }
package com.whackon.generator.pojo.enums; /** * <b>名称类型枚举信息</b> * @author Arthur * @version 1.0.0 */ public enum NameTypeEnum { NAME_ENTITY, NAME_PROPERTY, NAME_GET_METHOD, NAME_SET_METHOD }
package com.whackon.generator.pojo.vo; import java.io.Serializable; /** * <b>代码生成视图对象</b> */ public class GeneratorVO implements Serializable { private static final long serialVersionUID = 5342443082452383889L; private String dbIP; // 数据库连接 IP 地址 private String dbPort; // 数据库连接端口号 private String dbName; // 数据库名 private String dbUser; // 数据库登录用户名 private String dbPassword; // 数据库登录密码 private String outFolderPath; // 生成文件文件夹 public String getDbIP() { return dbIP; } public void setDbIP(String dbIP) { this.dbIP = dbIP; } public String getDbPort() { return dbPort; } public void setDbPort(String dbPort) { this.dbPort = dbPort; } public String getDbName() { return dbName; } public void setDbName(String dbName) { this.dbName = dbName; } public String getDbUser() { return dbUser; } public void setDbUser(String dbUser) { this.dbUser = dbUser; } public String getDbPassword() { return dbPassword; } public void setDbPassword(String dbPassword) { this.dbPassword = dbPassword; } public String getOutFolderPath() { return outFolderPath; } public void setOutFolderPath(String outFolderPath) { this.outFolderPath = outFolderPath; } }
package com.whackon.generator.service; import com.whackon.generator.pojo.vo.GeneratorVO; /** * <b>生成器业务层接口</b> * @author Arthur * @version 1.0.0 */ public interface GeneratorService { /** * <b>根据用户信息生成代码文件</b> * @param generatorVO * @return * @throws Exception */ boolean generateCodeFile(GeneratorVO generatorVO) throws Exception; }
package com.whackon.generator.service.impl; import com.whackon.generator.dao.GeneratorDao; import com.whackon.generator.pojo.entity.TableBean; import com.whackon.generator.pojo.vo.GeneratorVO; import com.whackon.generator.service.GeneratorService; import com.whackon.generator.util.ConnectionUtil; import com.whackon.generator.util.FileUtil; import com.whackon.generator.util.FreemarkerUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.io.File; import java.sql.Connection; import java.util.List; /** * <b>生成器业务层接口</b> * @author Arthur * @version 1.0.0 */ @Component("generatorService") public class GeneratorServiceImpl implements GeneratorService { @Autowired private GeneratorDao generatorDao; /** * <b>根据用户信息生成代码文件</b> * @param generatorVO * @return * @throws Exception */ @Override public boolean generateCodeFile(GeneratorVO generatorVO) throws Exception { // 获得数据库连接对象 Connection connection = ConnectionUtil.getConnection(generatorVO.getDbIP(), generatorVO.getDbPort() , generatorVO.getDbName(), generatorVO.getDbUser(), generatorVO.getDbPassword()); // 查询数据库信息 List<TableBean> tableBeanList = generatorDao.findTableBeanListByDBName(generatorVO.getDbName(), connection); // 循环数据库表信息,生成相关文件 for (TableBean tableBean : tableBeanList) { // 生成实体类文件 FileUtil.createFolder(generatorVO.getOutFolderPath() + File.separator + "entity"); File entityFile = FileUtil.createFile(generatorVO.getOutFolderPath() + File.separator + "entity" + File.separator + tableBean.getBeanName() + ".java"); FreemarkerUtil.generateCodeFile(tableBean, entityFile, "entity"); // 生成视图类文件 FileUtil.createFolder(generatorVO.getOutFolderPath() + File.separator + "vo"); File voFile = FileUtil.createFile(generatorVO.getOutFolderPath() + File.separator + "vo" + File.separator + tableBean.getBeanName() + "VO.java"); FreemarkerUtil.generateCodeFile(tableBean, voFile, "vo"); // 生成 DAO 接口文件 FileUtil.createFolder(generatorVO.getOutFolderPath() + File.separator + "dao"); File daoFile = FileUtil.createFile(generatorVO.getOutFolderPath() + File.separator + "dao" + File.separator + tableBean.getBeanName() + "Dao.java"); FreemarkerUtil.generateCodeFile(tableBean, daoFile, "dao"); // 生成 Mapper 映射文件 FileUtil.createFolder(generatorVO.getOutFolderPath() + File.separator + "mapper"); File mapperFile = FileUtil.createFile(generatorVO.getOutFolderPath() + File.separator + "mapper" + File.separator + tableBean.getBeanName() + ".Mapper.xml"); FreemarkerUtil.generateCodeFile(tableBean, mapperFile, "mapper"); } return true; } }
package com.whackon.generator.util; import java.sql.Connection; import java.sql.DriverManager; /** * <b>数据库连接工具类</b> * @author Arthur * @version 1.0.0 */ public class ConnectionUtil { static { try { Class.forName(SystemConstants.JDBC_DRIVER); } catch (Exception e) { e.printStackTrace(); } } /** * <b>根据用户所给定连接信息,获得 Connection 对象</b> * @param dbIP * @param dbPort * @param dbName * @param dbUser * @param dbPassword * @return */ public static Connection getConnection(String dbIP, String dbPort, String dbName, String dbUser, String dbPassword) { // 根据用户信息拼接完整的 URL //jdbc.driverClass=com.mysql.cj.jdbc.Driver //jdbc.url=jdbc:mysql://localhost:3306/ums_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false&AllowPublicKeyRetrieval=true //jdbc.username=root //jdbc.password=1234 String url = SystemConstants.JDBC_URL_PREFIX + dbIP + ":" + dbPort + "/" + dbName + SystemConstants.JDBC_URL_SUFFIX; try { // 根据连接信息创建连接对象 return DriverManager.getConnection(url, dbUser, dbPassword); } catch (Exception e) { e.printStackTrace(); } return null; } }
package com.whackon.generator.util; import com.whackon.generator.pojo.entity.ColumnProperty; import com.whackon.generator.pojo.entity.TableBean; /** * <b>数据类型工具类</b> * @author Arthur * @version 1.0.0 */ public class DataTypeUtil { /** * <b>根据数据库字段获得属性类型</b> * @param colProp * @param tableBean */ public static void getPropertyDataTypeFromColumn(ColumnProperty colProp, TableBean tableBean) { if ("int".equalsIgnoreCase(colProp.getColDataType())) { colProp.setPropShortDataType("Integer"); colProp.setPropFullDataType("java.lang.Integer"); } else if ("varchar".equalsIgnoreCase(colProp.getColDataType())) { colProp.setPropShortDataType("String"); colProp.setPropFullDataType("java.lang.String"); } else if ("decimal".equalsIgnoreCase(colProp.getColDataType())) { colProp.setPropShortDataType("Double"); colProp.setPropFullDataType("java.lang.Double"); } else if ("date".equalsIgnoreCase(colProp.getColDataType()) || "timestamp".equals(colProp.getColDataType())) { colProp.setPropShortDataType("Date"); colProp.setPropFullDataType("java.util.Date"); tableBean.setHasDate(true); } } }
package com.whackon.generator.util; import java.io.File; /** * <b>文件操作工具类</b> * @author Arthur * @version 1.0.0 */ public class FileUtil { /** * <b>根据目录路径创建目录 File 对象</b> * @param folderPath * @return */ public static File createFolder(String folderPath) { try { // 根据路径创建 File 对象 File folder = new File(folderPath); if (!folder.exists()) { folder.mkdirs(); } return folder; } catch (Exception e) { e.printStackTrace(); } return null; } /** * <b>根据文件路径创建文件 File 对象</b> * @param filePath * @return */ public static File createFile(String filePath) { try { // 根据路径创建 File 对象 File file = new File(filePath); if (!file.exists()) { file.createNewFile(); } return file; } catch (Exception e) { e.printStackTrace(); } return null; } }
package com.whackon.generator.util; import freemarker.template.Configuration; import freemarker.template.Template; import org.springframework.util.ResourceUtils; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; /** * <b>Freemarker 工具类</b> * @author Arthur * @version 1.0.0 */ public class FreemarkerUtil { /** * <b>根据用户参数、输出文件夹路径、输出文件和模板生成代码文件</b> * @param param * @param outFile * @param templateName * @return */ public static boolean generateCodeFile(Object param, File outFile, String templateName) { // 创建 Freemarker 的 Configuration 对象 Configuration configuration = new Configuration(Configuration.VERSION_2_3_31); try { String templatePath = ResourceUtils.getURL("classpath:").getPath() + SystemConstants.FREEMARKER_TEMPLATE_PATH; // 加载模板所在路径 configuration.setDirectoryForTemplateLoading(new File(templatePath)); // 设置编码格式为 UTF-8 configuration.setDefaultEncoding("UTF-8"); // 根据生成所使用的模板名称获得模板对象 Template template = configuration.getTemplate(templateName + ".ftl"); // 根据输出文件 File 对象,创建输出流 BufferedWriter bw = new BufferedWriter(new FileWriter(outFile)); // 生成代码文件 template.process(param, bw); bw.close(); return true; } catch (Exception e) { e.printStackTrace(); } return false; } }
package com.whackon.generator.util; /** * <b>名称工具类</b> */ public class NameUtil { /** * <b>根据数据库表名获得表的别名</b> * @param tableName * @return */ public static String getTableAliasNameFromTable(String tableName) { // 使用 “_” 对数据库表名进行切割 String[] tableNameArys = tableName.split("_"); StringBuffer sb = new StringBuffer(); for (String tableNamePart : tableNameArys) { sb.append(tableNamePart.substring(0, 1).toLowerCase()); } return sb.toString(); } /** * <b>根据数据表名获得类名</b> * @param tableName * @return */ public static String getBeanNameFromTable(String tableName) { // 使用 “_” 对数据库表名进行切割 String[] tableNameArys = tableName.split("_"); if (tableNameArys.length == 1) { // 此时数据表只有一个名字 return tableName.substring(0,1).toUpperCase() + tableName.substring(1, tableName.length()); } else { // 数据库表有若干部分组成 // 去掉第一个部分的前缀信息 StringBuffer sb = new StringBuffer(); for (int i = 1; i < tableNameArys.length; i++) { sb.append(tableNameArys[i].substring(0, 1).toUpperCase()); sb.append(tableNameArys[i].substring(1, tableNameArys[i].length())); } return sb.toString(); } } public static String getPropertyNameFromColumn(String colName) { String[] colNameArys = colName.split("_"); StringBuffer sb = new StringBuffer(); // 生成属性名 sb.append(colNameArys[0].substring(0,1).toLowerCase()); sb.append(colNameArys[0].substring(1,colNameArys[0].length())); if (colNameArys.length > 1) { for (int i = 1; i < colNameArys.length; i++) { sb.append(colNameArys[i].substring(0, 1).toUpperCase()); sb.append(colNameArys[i].substring(1, colNameArys[i].length())); } } return sb.toString(); } }
package com.whackon.generator.util; import java.util.Properties; /** * <b>系统常量工具类</b> * @author Arthur * @version 1.0.0 */ public class SystemConstants { private static Properties props = new Properties(); static { try { props.load(SystemConstants.class.getClassLoader().getResourceAsStream("props/system.properties")); } catch (Exception e) { e.printStackTrace(); } } /** * <b>Freemarker 模板路径</b> */ public static final String FREEMARKER_TEMPLATE_PATH = props.getProperty("freemarker.template.path"); /** * <b>JDBC 连接信息:连接驱动类</b> */ public static final String JDBC_DRIVER = props.getProperty("jdbc.driver"); /** * <b>JDBC 连接信息:链接地址前缀</b> */ public static final String JDBC_URL_PREFIX = props.getProperty("jdbc.url.prefix"); /** * <b>JDBC 连接信息:链接地址后缀</b> */ public static final String JDBC_URL_SUFFIX = props.getProperty("jdbc.url.suffix"); }
package com.whackon.generator; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * <b>代码生成器 MySQL 版启动类</b> * @author Arthur * @version 1.0.0 */ @SpringBootApplication public class GeneratorStarter { public static void main(String[] args) { SpringApplication.run(GeneratorStarter.class, args); } }
模板
import org.springframework.stereotype.Repository; import java.util.List; /** * <b>${tableComment}数据持久层接口</b> * @author lenovo * @version 1.0.0 */ @Repository public interface ${beanName}Dao { /** * <b>根据查询对象查询列表</b> * @param query * @return * @throws Exception */ List<${beanName}> findListByQuery(${beanName} query) throws Exception; /** * <b>保存对象信息</b> * @param entity * @return * @throws Exception */ int save(${beanName} entity) throws Exception; /** * <b>修改对象信息</b> * @param entity * @return * @throws Exception */ int update(${beanName} entity) throws Exception; }
<#if hasDate> import java.util.Date; </#if> /** * <b>${tableComment}实体信息</b> * @author lenovo * @version 1.0.0 */ public class ${beanName} extends BaseEntity { <#list colPropList as colProp> private ${colProp.propShortDataType} ${colProp.propName}; // ${colProp.colComment} </#list> public static ${beanName} getEntity(${beanName}VO vo) { // 将视图属性切换为实体属性 ${beanName} entity = new ${beanName}(); // 使用 Spring 的 beanUtils 对普通属性进行切换 BeanUtils.copyProperties(vo, entity); // 切换关联属性 entity.setStatus(vo.getStatus()); entity.setCreateAdmin(vo.getCreateAdmin()); entity.setCreateTime(vo.getCreateTime()); entity.setUpdateAdmin(vo.getUpdateAdmin()); entity.setUpdateTime(vo.getUpdateTime()); return entity; } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.ekgc.medial.clinic.admin.dao.${beanName}Dao"> <select id="findListByQuery" parameterType="${beanName}" resultMap="resultMap"> select <#list colPropList as colProp> ${tableAlias}.${colProp.colName}, </#list> from ${tableName} ${tableAlias} <where> <trim suffixOverrides="and"> <#list colPropList as colProp> <if test="${colProp.propName} != null and ${colProp.propName} != ''">${tableAlias}.${colProp.colName}=<#noparse>#{</#noparse>${colProp.propName}<#noparse>}</#noparse> and </if> </#list> </trim> </where> </select> <insert id="save" parameterType="${beanName}"> insert into ${tableName}( <trim suffixOverrides=","> <#list colPropList as colProp> <if test="${colProp.propName} != null and ${colProp.propName} != ''">${colProp.colName}, </if> </#list> </trim> ) values( <trim suffixOverrides=","> <#list colPropList as colProp> <if test="${colProp.propName} != null and ${colProp.propName} != ''"><#noparse>#{</#noparse>${colProp.propName}<#noparse>}</#noparse>, </if> </#list> </trim> ) </insert> <update id="update" parameterType="${beanName}"> update ${tableName} <set> <trim suffixOverrides=","> <#list colPropList as colProp> <if test="${colProp.propName} != null and ${colProp.propName} != ''">${colProp.colName}=<#noparse>#{</#noparse>${colProp.propName}<#noparse>}</#noparse>, </if> </#list> </trim> </set> <where> <trim suffixOverrides="and"> <if test="id != null and id != ''">id=<#noparse>#{</#noparse>id<#noparse>}</#noparse> and </if> </trim> </where> </update> <resultMap id="resultMap" type="${beanName}"> <id property="id" javaType="java.lang.String" column="id"/> <#list colPropList as colProp> <result property="${colProp.propName}" javaType="${colProp.propFullDataType}" column="${colProp.colName}"/> </#list> </resultMap> </mapper>
<#if hasDate> import java.util.Date; </#if> /** * <b>${tableComment}视图信息</b> * @author lenovo * @version 1.0.0 */ @ApiModel(value = "急诊功能模块 - ${tableComment}视图信息") public class ${beanName}VO extends BaseVO { <#list colPropList as colProp> @ApiModelProperty(value = "${colProp.colComment}") private ${colProp.propShortDataType} ${colProp.propName}; // ${colProp.colComment} </#list> public static ${beanName}VO getVO(${beanName} entity) { // 将实体属性切换为视图属性 ${beanName}VO vo = new ${beanName}VO(); // 使用 Spring 的 beanUtils 对普通属性进行切换 BeanUtils.copyProperties(entity, vo); // 切换关联属性 vo.setStatus(entity.getStatus()); vo.setCreateAdmin(entity.getCreateAdmin()); vo.setCreateTime(entity.getCreateTime()); vo.setUpdateAdmin(entity.getUpdateAdmin()); vo.setUpdateTime(entity.getUpdateTime()); return vo; } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" type="text/css" href="/css/font-awesome.min.css"> <link rel="stylesheet" type="text/css" href="/css/bootstrap.min.css"> <script type="application/javascript" src="/js/jquery-3.6.0.min.js"></script> <script type="application/javascript" src="/js/bootstrap.min.js"></script> <style rel="stylesheet" type="text/css"> div { width: 642px; height: 590px; border-radius: 30px; padding: 50px; margin: auto; position: absolute; top: 0; bottom: 0; left: 0; right: 0; } caption { text-align: center; } table { border-collapse: separate; border-spacing: 10px; } .columntd { text-align: right; font: bold normal 18px 微软雅黑; } </style> <script type="application/javascript"> function createFile() { var formData = $("#myForm").serialize(); $.ajax({ url: "/generate", type: "post", data: formData, dataType: "json", success: function (data) { if (data) { alert("生成成功!"); } else { alert("生成失败!"); } } }); } </script> </head> <body> <div> <form id="myForm"> <table> <caption> <h1>Stupid 代码生成器</h1> <span>【MySQL 版 v1.0.0】</span> </caption> <tr> <td class="columntd"> <span>数据库 IP 地址:</span> </td> <td> <input type="text" id="dbIP" name="dbIP" class="form-control" style="width: 300px;" placeholder="请填写您的数据库连接 IP 地址"/> </td> </tr> <tr> <td class="columntd"> <span>数据库连接端口号:</span> </td> <td> <input type="text" id="dbPort" name="dbPort" class="form-control" style="width: 300px;" placeholder="请填写您的数据库连接端口号"/> </td> </tr> <tr> <td class="columntd"> <span>连接数据库名:</span> </td> <td> <input type="text" id="dbName" name="dbName" class="form-control" style="width: 300px;" placeholder="请填写您要连接的数据库名"/> </td> </tr> <tr> <td class="columntd"> <span>数据库登录用户名:</span> </td> <td> <input type="text" id="dbUser" name="dbUser" class="form-control" style="width: 300px;" placeholder="请填写您的数据库登录用户名"/> </td> </tr> <tr> <td class="columntd"> <span>数据库登录密码:</span> </td> <td> <input type="text" id="dbPassword" name="dbPassword" class="form-control" style="width: 300px;" placeholder="请填写您的数据库登录密码"/> </td> </tr> <tr> <td class="columntd"> <span>生成文件夹路径:</span> </td> <td> <input type="text" id="outFolderPath" name="outFolderPath" class="form-control" style="width: 300px;" placeholder="请填写生成文件的存放路径"/> </td> </tr> <tr> <td colspan="2" class="columntd"> <a href="javascript:createFile();" class="btn btn-success"> <i class="fa fa-check"></i> <span>生成文件</span> </a> </td> </tr> </table> </form> </div> </body> </html>