main方法
public class FreemarkerUtils {
public static void main(String[] args) throws Exception {
createFtl("user_role_relation");
}
public static void createFtl(String... str) throws Exception {
for (String string : str) {
String tableName = string.toUpperCase();
/** 生成文件目录 */
String path = "d:\\bean/";
File f = new File(path);
if (!f.exists()) {
f.mkdir();
}
Map<String, Object> map = DBHelper.getTableInfo(tableName);
String clsName = map.get("clsName").toString();
// 执行插值,并输出到指定的输出流中
createFile("sqlxml.ftl", path + clsName + "Mapper.xml", map);
createFile("bean.ftl", path + clsName + ".java", map);
createFile("dao.ftl", path + clsName + "Dao.java", map);
System.out.println("创建完成");
}
}
public static void createFile(String templatName, String fileName, Map<String, Object> map) throws IOException, TemplateException {
@SuppressWarnings("deprecation")
Configuration cfg = new Configuration();
String path = FreemarkerUtils.class.getResource("/").getPath() + "ftlProject";
cfg.setDirectoryForTemplateLoading(new File(path));
Template t = cfg.getTemplate(templatName);
t.process(map, new OutputStreamWriter(new FileOutputStream(fileName)));
}
}
DB工具类
public class DBHelper {
/** 参数 */
private static final String URL = "jdbc:oracle:thin:@127.0.0.1:orcl";
/** 参数 */
private static final String CLSDRIVER = "oracle.jdbc.OracleDriver";
/** 数据库用户名 */
private static final String USERNME = "xx";
/** 数据库密码 */
private static final String PWD = "xx";
private static Connection getConnection() {
Connection connection = null;
try {
Class.forName(CLSDRIVER);
connection = DriverManager.getConnection(URL, USERNME, PWD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
private static ResultSet select(String sql) {
Connection conn = getConnection();
ResultSet rs = null;
try {
Statement cs = conn.createStatement();
rs = cs.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public static Map<String, Object> getTableInfo(String tableName) {
Map<String, Object> item = new LinkedHashMap<String, Object>();
StringBuffer sbsql = new StringBuffer("SELECT T.TABLE_NAME ,");
sbsql.append("T.COLUMN_NAME ,T.DATA_TYPE ,TC.COMMENTS , T.DATA_LENGTH, T.NULLABLE,T.DATA_DEFAULT");
sbsql.append(" FROM user_tab_columns T ,");
sbsql.append(" user_col_comments TC WHERE T.COLUMN_NAME = TC.COLUMN_NAME AND T.TABLE_NAME = TC.TABLE_NAME AND");
sbsql.append(" T.TABLE_NAME = '").append(tableName);
sbsql.append("' AND T.COLUMN_NAME != 'OGG_SEQ_ID' order by column_id ");
// sbsql.append("' AND T.COLUMN_NAME != 'OGG_SEQ_ID' ORDER BY NULLABLE DESC");
System.out.println(sbsql.toString());
ResultSet rs = select(sbsql.toString());
try {
List<Map<String, Object>> clist = new ArrayList<Map<String, Object>>();
int i = 1;
while (rs.next()) {
Map<String, Object> citem = new LinkedHashMap<String, Object>();
/** 字段名 */
String columnName = rs.getString("COLUMN_NAME");
/** 字段描述 */
String columnDesc = rs.getString("COMMENTS");
String columnClsName = changeClumn(columnName);
String columnType = changeDateType(rs.getString("DATA_TYPE"));
String columnClsType = changeClsType(columnType);
String isNull = rs.getString("NULLABLE");
/** 字段名、Java类字段、字段描述、字段类型、Java类字段类型 */
citem.put("columnName", columnName);
citem.put("columnClsName", columnClsName);
citem.put("columnDesc", columnDesc);
citem.put("columnType", columnType);
citem.put("columnClsType", columnClsType);
citem.put("isNull", isNull);
citem.put("curSize", i++);
clist.add(citem);
}
item.put("TABLINFO", clist);
item.put("MAXSIZE", clist.size());
} catch (Exception e) {
e.printStackTrace();
}
item.put("tableName", tableName);
item.put("clsName", changeClsName(tableName));
item.put("curTime", getCurrTime());
item.put("curDate", getCurrDate());
return item;
}
public static String getCurrDate() {
DateFormat df = new SimpleDateFormat("yyyyMMdd");
return df.format(new Date());
}
public static String getCurrTime() {
DateFormat df = new SimpleDateFormat("HHmmss");
return df.format(new Date());
}
private static String changeClsName(String tableName) {
StringBuffer sb = new StringBuffer();
String lowerCase = tableName.toLowerCase();
boolean f = false;
int i = 1;
for (char c : lowerCase.toCharArray()) {
int a = (int) c;
if (a == 95) {
f = true;
i++;
continue;
}
if (f || i == 1) {
sb.append((c + "").toUpperCase());
f = false;
} else {
sb.append(c);
}
i++;
}
return sb.toString();
}
private static String changeClumn(String str) {
StringBuffer sb = new StringBuffer();
String lowerCase = str.toLowerCase();
boolean f = false;
for (char c : lowerCase.toCharArray()) {
int a = (int) c;
if (a == 95) {
f = true;
continue;
}
if (f) {
sb.append((c + "").toUpperCase());
f = false;
} else {
sb.append(c);
}
}
return sb.toString();
}
private static String changeDateType(String dataType) {
if ("VARCHAR2".equalsIgnoreCase(dataType)) {
return JdbcType.VARCHAR.name().toString();
} else if ("CHAR".equalsIgnoreCase(dataType)) {
return JdbcType.CHAR.name().toString();
} else if ("NVARCHAR2".equalsIgnoreCase(dataType)) {
return JdbcType.NVARCHAR.name().toString();
} else if ("CLOB".equalsIgnoreCase(dataType)) {
return JdbcType.CLOB.name().toString();
} else if ("BLOB".equalsIgnoreCase(dataType)) {
return JdbcType.BLOB.name().toString();
} else if ("DATE".equalsIgnoreCase(dataType)) {
return JdbcType.DATE.name().toString();
} else if ("RAW".equalsIgnoreCase(dataType)) {
return JdbcType.BLOB.name().toString();
} else if ("LONG".equalsIgnoreCase(dataType)) {
return JdbcType.BIGINT.name().toString();
} else if ("NUMBER".equalsIgnoreCase(dataType)) {
return JdbcType.NUMERIC.name().toString();
} else if ("UROWID".equalsIgnoreCase(dataType)) {
return JdbcType.BLOB.name().toString();
} else {
return "VARCHAR";
}
}
private static String changeClsType(String dataType) {
if (dataType.equalsIgnoreCase(JdbcType.VARCHAR.name().toString())) {
return String.class.getSimpleName();
} else if (dataType.equalsIgnoreCase(JdbcType.CHAR.name().toString())) {
return String.class.getSimpleName();
} else if (dataType.equalsIgnoreCase(JdbcType.CLOB.name().toString())) {
return String.class.getSimpleName();
} else if (dataType.equalsIgnoreCase(JdbcType.BLOB.name().toString())) {
return String.class.getSimpleName();
} else if (dataType.equalsIgnoreCase(JdbcType.DATE.name().toString())) {
return "java.util.Date";
} else if (dataType.equalsIgnoreCase(JdbcType.BLOB.name().toString())) {
return String.class.getSimpleName();
} else if (dataType.equalsIgnoreCase(JdbcType.BIGINT.name().toString())) {
return Long.class.getSimpleName();
} else if (dataType.equalsIgnoreCase(JdbcType.NUMERIC.name().toString())) {
return BigDecimal.class.getSimpleName();
} else {
return String.class.getSimpleName();
}
}
}
bean.ftl 实体对象的模板
public class ${clsName} extends BaseEntity {
/** 序号 */
private static final long serialVersionUID = 1L;
<#list TABLINFO as x>
/** 属性描述:${x.columnDesc} */
private ${x.columnClsType} ${x.columnClsName};
</#list>
<#list TABLINFO as x>
public ${x.columnClsType} get${x.columnClsName?cap_first}() {
return ${x.columnClsName};
}
public void set${x.columnClsName?cap_first}(${x.columnClsType} ${x.columnClsName}) {
this.${x.columnClsName} = ${x.columnClsName};
}
</#list>
}
dao层的模板
package com.imobpay.base.dao;
import com.imobpay.base.iface.BaseDao;
public interface ${clsName}Dao<T> {
public void insert(T bean);
public void update(T bean);
public void delete(T bean);
public T selectById(T bean);
public List<T> list(T bean);
public Integer listCount(T bean);
}
sqlxml.ftl SQL层的模板
<#macro mapperEl value>${r"#{"}${value}}</#macro>
<?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.imobpay.base.dao.${clsName}Dao">
<#-- 生成查询参数 -->
<sql id="paramSql">
<!-- 此处一定要增加一个查询 -->
<#list TABLINFO as x >
<if test="${x.columnClsName} !=null and ${x.columnClsName} !=''">
AND ${x.columnName} = <@mapperEl x.columnClsName />
</if>
</#list>
</sql>
<#-- 查询单个对像 -->
<select id="selectById" parameterType="com.imobpay.base.entity.${clsName}" resultType="com.imobpay.base.entity.${clsName}">
SELECT * FROM ${tableName} WHERE ROWNUM = 1
<include refid="paramSql"></include>
</select>
<#-- 分页查询 -->
<select id="list" parameterType="com.imobpay.base.entity.${clsName}" resultType="com.imobpay.base.entity.${clsName}">
<if test="pageflag==true">
SELECT * FROM (
SELECT A.*, ROWNUM RN FROM
(
SELECT * FROM ${tableName} WHERE 1=1
<include refid="paramSql"></include>
) A WHERE ROWNUM <= <@mapperEl 'curPage' />*<@mapperEl 'pageSize' />
) WHERE RN >
(<@mapperEl 'curPage' />-1)*<@mapperEl 'pageSize' />
</if>
<if test="pageBlag==false">
SELECT * FROM ${tableName}
WHERE 1=1
<include refid="paramSql"></include>
</if>
</select>
<#-- 分页个数查询 -->
<select id="listCount" parameterType="com.imobpay.base.entity.${clsName}" resultType="Integer">
SELECT count(1) as count FROM ${tableName}
WHERE 1=1
<include refid="paramSql"></include>
</select>
<!-- 添加 -->
<insert id="insert" parameterType="com.imobpay.base.entity.${clsName}">
INSERT INTO
${tableName}(
<#list TABLINFO as x >
<#if x.curSize == MAXSIZE>
${x.columnName}
<#else>${x.columnName},</#if>
</#list>)
VALUES(<#list TABLINFO as x ><#if x.curSize == MAXSIZE>
<@mapperEl x.columnClsName /><#else>
<@mapperEl x.columnClsName />,
</#if></#list>)
</insert>
<!-- 修改 -->
<update id="update">
UPDATE ${tableName}
<trim prefix="SET" suffixOverrides=",">
<#list TABLINFO as x >
<if test="${x.columnClsName} !=null and ${x.columnClsName} !=''">
${x.columnName} = <@mapperEl x.columnClsName /> ,
</if>
</#list>
</trim>
WHERE 1=1
<include refid="paramSql"></include>
</update>
</mapper>
目录结构
运行这个FreemarkerUtils里面的main方法就可以了。
依赖的jar包就是这个几个