介绍:
根据表结构生成mybatis对应的实现体和Mpper.xml文件,我们使用的是mysql数据库
需要的jar包 freemarker.jar mysql.jar 如附件
1. 数据库连接类
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /** * 数据库连接设置 * * @author king * 2014-5-5 */ public class DBConnectionUtils { private static Connection conn = null; public static Connection getJDBCConnection() { if (null == conn) { try { Class.forName("com.mysql.jdbc.Driver"); // String url = "jdbc:mysql://127.0.0.1:3306/risk_monitor";// String user = "root"; String password = "root"; conn = DriverManager.getConnection(url, user, password); } catch (Exception e) { e.printStackTrace(); } } return conn; } public static void close(){ if(null!=conn){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
2. column类
public class Column { private String columnName; private String columnType; private String attributeName; private String attributeType; public String getColumnName() { return columnName; } public void setColumnName(String columnName) { this.columnName = columnName; } public String getColumnType() { return columnType; } public void setColumnType(String columnType) { this.columnType = columnType; } public String getAttributeName() { return attributeName; } public void setAttributeName(String attributeName) { this.attributeName = attributeName; } public String getAttributeType() { return attributeType; } public void setAttributeType(String attributeType) { this.attributeType = attributeType; } }
3. generator类
import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import freemarker.template.Configuration; import freemarker.template.DefaultObjectWrapper; import freemarker.template.Template; public class Generator { private Configuration cfg; private Connection connection; private void initial() { try { if (null == cfg) { cfg = new Configuration(); } cfg.setDirectoryForTemplateLoading(new File( "src/com/chinaebi/codegen/template")); connection = DBConnectionUtils.getJDBCConnection(); } catch (IOException e) { e.printStackTrace(); } cfg.setObjectWrapper(new DefaultObjectWrapper()); } public void generator() throws Exception { initial(); cfg.setDefaultEncoding("utf-8"); Template entityTemplate = cfg.getTemplate("entity.ftl"); Template sqlTemplate = cfg.getTemplate("sql.ftl"); List<Map<String, Object>> templates = generatorTemplateData(); for (Map<String, Object> o : templates) { // Writer out = new OutputStreamWriter(System.out);// // 打印到控制台 // Writer outFile = new OutputStreamWriter(new FileOutputStream( // "src/com/chinaebi/codegen/template/"+o.get("tableName")+".java"), "gb2312"); // entityTemplate.process(o, out); // entityTemplate.process(o, outFile); // out.flush(); //输出到文件 File beanFile = new File("src/com/chinaebi/codegen/template/entity/"+o.get("beanName")+".java"); Writer beanWriter = new FileWriter(beanFile); entityTemplate.process(o, beanWriter); beanWriter.close(); File sqlFile = new File("src/com/chinaebi/codegen/template/sql/Tbl_"+o.get("beanName")+"Mapper.xml"); Writer sqlWriter = new FileWriter(sqlFile); sqlTemplate.process(o, sqlWriter); sqlWriter.close(); System.out.println("生成:"+o.get("beanName")); } } private List<Map<String, Object>> generatorTemplateData() throws Exception { DatabaseMetaData dbmd = connection.getMetaData(); List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>(); String[] tables = { "Table" }; ResultSet tableSet = dbmd.getTables(null, null, "%", tables);// while (tableSet.next()) { Map<String, Object> map = new HashMap<String, Object>(); String tableName = tableSet.getString("TABLE_NAME"); ResultSet columnSet = dbmd.getColumns(null, "%", tableName, "%"); List<Column> columns = new ArrayList<Column>();// while (columnSet.next()) { String columnName = columnSet.getString("COLUMN_NAME"); String attributeName = handlerColumnName(columnSet.getString("COLUMN_NAME")); String columnType = columnSet.getString("TYPE_NAME"); String attributeType = handlerColumnType(columnSet.getString("TYPE_NAME")); Column column = new Column(); column.setColumnName(columnName); column.setColumnType(columnType); column.setAttributeName(attributeName); column.setAttributeType(attributeType); columns.add(column); } map.put("tableName", tableName); map.put("beanName", handlerTableName(tableName)); map.put("columns", columns); lists.add(map); } connection.close(); return lists; } public static String handlerColumnName(String oldName) { String[] arrays = oldName.split("_"); String newName = ""; if (arrays.length > 0) { newName = arrays[0]; } for (int i = 1; i < arrays.length; i++) { newName += (arrays[i].substring(0, 1).toUpperCase() + arrays[i] .substring(1, arrays[i].length())); } return newName; } public static String handlerTableName(String oldName) { String[] arrays = oldName.split("_"); String newName = ""; for (int i = 0; i < arrays.length; i++) { newName += (arrays[i].substring(0, 1).toUpperCase() + arrays[i] .substring(1, arrays[i].length())); } return newName; } public static String handlerColumnType(String oldType) { if (oldType.toUpperCase().startsWith("VARCHAR")) { return "String"; } if (oldType.toUpperCase().startsWith("INT")) { return "int"; } if (oldType.toUpperCase().startsWith("DATETIME")) { return "Date"; }if (oldType.toUpperCase().startsWith("CHAR")) { return "String"; } if (oldType.toUpperCase().startsWith("TINYINT")) { return "int"; } if (oldType.toUpperCase().startsWith("BIT")) { return "int"; } if (oldType.toUpperCase().startsWith("BIGINT")) { return "BigInteger"; } return oldType; } }
4.运行方法
/** * 运行方法 * * @author king * 2014-5-5 */ public class Main { public static void main(String[] args) throws Exception { Generator gen = new Generator(); gen.generator(); } }
4. 模板
entity.ftl
/** * 对应表 ${tableName} * @author king * */ public class ${beanName} implements java.io.Serializable{ <#list columns as item> private ${item.attributeType} ${item.attributeName};/*对应表中${item.columnName}*/ </#list> <#list columns as item> public ${item.attributeType} get${item.attributeName?cap_first}(){ return ${item.attributeName}; } public void set${item.attributeName?cap_first}(${item.attributeType} ${item.attributeName}){ this.${item.attributeName} = ${item.attributeName}; } </#list> }
sql.ftl
<?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"> <!--表 ${tableName}操作方法--> <mapper namespace="${beanName}Manager"> <resultMap type="${beanName}" id="${beanName}Map"> <#list columns as item> <result property="${item.attributeName}" column="${item.columnName}" /> </#list> </resultMap> <!--分页查询 记录--> <select id="queryPage" parameterType="${beanName}" resultMap="${beanName}Map"> select * from ${tableName} t where 1=1 <#list columns as item> <if test="${item.attributeName} != null and ${item.attributeName} != ''"> and t.${item.columnName} = ${'#'}{${item.attributeName}} </if> </#list> limit ${'#'}{startRow},${'#'}{limit} </select> <!--分页查询 计数--> <select id="queryPageCount" parameterType="${beanName}" resultType="int"> select count(*) from ${tableName} t where 1=1 <#list columns as item> <if test="${item.attributeName} != null and ${item.attributeName} != ''"> and t.${item.columnName} = ${'#'}{${item.attributeName}} </if> </#list> </select> <!--查询方法--> <select id="select" parameterType="${beanName}" resultMap="${beanName}Map"> select * from ${tableName} t where 1=1 <#list columns as item> <if test="${item.attributeName} != null and ${item.attributeName} != ''"> and t.${item.columnName} = ${'#'}{${item.attributeName}} </if> </#list> </select> <!--添加方法--> <insert id="insert" parameterType="${beanName}"> insert into ${tableName}( <#list columns as item> ${item.columnName}, </#list> ) values(<#list columns as item> ${'#'}{${item.attributeName}}, </#list>) </insert> <!--更新方法--> <update id="update" parameterType="${beanName}"> update ${tableName} set <#list columns as item> <if test="${item.attributeName} != null and ${item.attributeName} != ''"> t.${item.columnName} = ${'#'}{${item.attributeName}}, </if> </#list> where 1=1 <#list columns as item> <if test="${item.attributeName} != null and ${item.attributeName} != ''"> and t.${item.columnName} = ${'#'}{${item.attributeName}} </if> </#list> </update> <!--删除方法--> <delete id="delete" parameterType="${beanName}"> delete from ${tableName} where 1=1 <#list columns as item> <if test="${item.attributeName} != null and ${item.attributeName} != ''"> and t.${item.columnName} = ${'#'}{${item.attributeName}} </if> </#list> </delete> </mapper>
相关推荐
代码生成器支持model、Mapper、Dao、Service、ServiceImpl、Controller 一次性的生成,支持同时生成多张表,此代码生成器依赖JAR包比较少,除了必用Freemarker 和JDBC之外基本上完全个人编写
freemarker实现代码生成器详细教程
基于Freemarker模板的代码生成器实体映射xml的ftl模板文件
源码 用于Mybatis框架下生成Model.java和Mapper.xml文件, 内含采用mysql数据库建表文件, 分别在ModelHandler类和MapperXMLHandler类中的main方法中执行。 文件存放路径D://generatordata。
附件包含了mybatis-plus自动生成代码,基于freemarker的模板。包括常规的curd。 【特点】针对每个接口都有对应的入参和出参,不公用entity。 如,add接口,则入参会自动生成为addEntityReq。具体可下载模板进行查看...
基于Freemarker模板的纯Java代码实现的代码生成器工具
项目下载后自行修改...启动项目后进入http://xxxx.xxxx.xxxx:xxx/swagger-ui.html查看相关接口,支持通过接口设置数据库链接切换后自动重启项目,代码生成器支持model、Mapper、Dao层生成后会打包成Zip文件提供下载。
使用Spring boot + FreeMarker 实现代码生成功能,完整案例可以查看 eladmin 项目,教程地址: https://www.ydyno.com/archives/1177.html
FreeMarker,Struts,代码生成器
根据数据库表直接生成Controller、model、service、mapper、mybatis的xml文件。 使用方法:加压下载文件,导入eclipse,项目中附了一份txt的说明文档
通过freemaker生成javabean,action,service,dao,struts2配置文件,spring配置文件模板。
可根据数据库表生成C#和JAVA的实体类,根据数据库连接串自动连接数据库
Java利用Freemarker模板自动生成dto、dao、rowmapper、bo、service代码,代码可以直接复制到项目中使用,代码是基于pring框架的
使用代码生成器,只需要先按照下面的数据库设计规范设计好数据库,然后使用代码生成器就能一键生成项目代码,生成的代码有:实体类、DAO接口及实现类、Service接口及实现类、Controller代码框架。
基于Freemarker模板的代码生成器前台页面的ftl模板文件
一套基于FreeMarker技术实现的Java快速开发的代码生成器。 FreeMarker主要两大作用,一种是动态内容静态化。一种是代码生成。 本例的应用是后者,目的是为了节省业务开发时间,对简单的业务代码快速实现。
又名JAVA在线代码生成平台、sql转java、大狼狗代码生成器、mybatis在线生成器、SQL转Java JPA、MYBATIS实现类代码生成平台 Author powered by Moshow郑锴(大狼狗) , Description Based on SpringBoot2+Freemarker #...
基于Freemarker模板的代码生成器后台(控制层,业务层,持久层)ftl模板文件
基于Freemarker的Java代码生成器
自己使用Freemaker模板引擎设计的工具jar包,压缩文件中有说明文件,按照Readme文件进行配置,高度自定义,方便快捷,敏捷开发。企业级开发中使用Spring相关框架的代码快速生成器!你值得拥有~ ps:解压密码:code_g