日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java增删改一键生成_easyCode(java自动生成增删改查代码)

發(fā)布時間:2024/9/27 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java增删改一键生成_easyCode(java自动生成增删改查代码) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

【實例簡介】讀取數(shù)據(jù)庫表結(jié)構(gòu) 自動生成增刪改查代碼

【實例截圖】

【核心代碼】

package com.isoftstone.autocoding;

import com.isoftstone.autocoding.common.Column;

import com.isoftstone.autocoding.common.Table;

import com.isoftstone.autocoding.utils.CamelCaseUtils;

import com.isoftstone.autocoding.utils.FileHelper;

import com.mysql.jdbc.StringUtils;

import freemarker.template.Configuration;

import freemarker.template.Template;

import org.apache.log4j.Logger;

import javax.swing.filechooser.FileSystemView;

import java.io.*;

import java.sql.Connection;

import java.sql.DatabaseMetaData;

import java.sql.ResultSet;

import java.text.SimpleDateFormat;

import java.util.*;

public class EasyCodeApplication {

private Logger logger = Logger.getLogger(this.getClass());

private Properties properties;

/**

* 讀取配置文件

*

* @throws Exception

*/

public EasyCodeApplication() throws Exception {

properties = new Properties();

String fileDir = this.getClass().getClassLoader().getResource("generator.xml").getFile();

properties.loadFromXML(new FileInputStream(fileDir));

}

/**

* 解析數(shù)據(jù)表

*

* @param tableName

* @return

* @throws Exception

*/

public Table parseTable(String tableName) throws Exception {

String driverName = properties.getProperty("jdbc.driver");

String userName = properties.getProperty("jdbc.username");

String userPwd = properties.getProperty("jdbc.password");

String dbURL = properties.getProperty("jdbc.url");

String catalog = properties.getProperty("jdbc.catalog");

String schema = properties.getProperty("jdbc.schema");

schema = schema == null ? "%" : schema;

String column = "%";

logger.debug("driver>>" driverName);

logger.debug("url>>" dbURL);

logger.debug("name>>" userName);

logger.debug("password>>" userPwd);

logger.debug("catalog>>" catalog);

logger.debug("schema>>" schema);

Class.forName(driverName);

Connection conn = java.sql.DriverManager.getConnection(dbURL, userName, userPwd);

DatabaseMetaData dmd = conn.getMetaData();

ResultSet rs = dmd.getColumns(catalog, schema, tableName, column);

List columns = new ArrayList();

while (rs.next()) {

Column c = new Column();

c.setLabel(rs.getString("REMARKS"));

String name = rs.getString("COLUMN_NAME");

c.setName(CamelCaseUtils.toCamelCase(name));

c.setDbName(name);

String dbType = rs.getString("TYPE_NAME");

int columnSize = rs.getInt("COLUMN_SIZE");

if (dbType.equals("TINYINT") && columnSize > 1) {

c.setType("Integer");

} else if (dbType.equals("TINYINT") && columnSize == 1) {

c.setType("Boolean");

} else {

String type = properties.getProperty(dbType);

c.setType(type == null ? "String" : type);

}

c.setDbType(dbType);

c.setLength(rs.getInt("COLUMN_SIZE"));

c.setDecimalDigits(rs.getInt("DECIMAL_DIGITS"));

c.setNullable(rs.getBoolean("NULLABLE"));

columns.add(c);

}

List pkColumns = new ArrayList();

ResultSet pkrs = dmd.getPrimaryKeys(catalog, schema, tableName);

while (pkrs.next()) {

Column c = new Column();

String name = pkrs.getString("COLUMN_NAME");

c.setName(CamelCaseUtils.toCamelCase(name));

c.setDbName(name);

pkColumns.add(c);

}

conn.close();

Table t = new Table();

String prefiex = properties.getProperty("tableRemovePrefixes");

String name = tableName;

if (prefiex != null && !"".equals(prefiex)) {

name = tableName.split(prefiex)[0];

}

t.setName(CamelCaseUtils.toCamelCase(name));

t.setDbName(tableName);

t.setColumns(columns);

t.setPkColumns(pkColumns);

return t;

}

/**

*

Discription:[生成映射文件和實體類]

* Created on 2019年4月4日

*

* @param tableName 要聲稱映射文件和實體類的表名稱

* @param tableDescAndCat 表描述

* @throws Exception

*/

public void gen(String tableName, String tableDescAndCat, String id, String modelId) throws Exception {

Configuration cfg = new Configuration(Configuration.VERSION_2_3_21);

String outRoot = properties.getProperty("outRoot");

//當(dāng)輸出地址為null時,文件放到桌面

if (StringUtils.isNullOrEmpty(outRoot)) {

File desktopDir = FileSystemView.getFileSystemView() .getHomeDirectory();

outRoot = desktopDir.getAbsolutePath() "/Desktop/EasyCodeDemo";

}

String basePackage = properties.getProperty("basePackage");

//獲取當(dāng)前日期

SimpleDateFormat sm_date = new SimpleDateFormat("yyyy年MM月dd日");

SimpleDateFormat sm_year = new SimpleDateFormat("yyyy年");

//將首字母轉(zhuǎn)為大寫

StringBuffer buffer = new StringBuffer();

String namePart1 = modelId.substring(0, 1).toUpperCase();

String namePart2 = modelId.substring(1);

buffer.append(namePart1 namePart2);

System.out.println(buffer);

Map root = new HashMap();

Table t = this.parseTable(tableName);

t.setTableDesc(tableDescAndCat.split("_")[0]);

root.put("table", t);

root.put("className", t.getNameUpper());

root.put("classNameLower", t.getName());

root.put("primaryKey", id);

root.put("modelId", modelId);

root.put("modelIdFirstUpper", buffer);

root.put("package", basePackage);

root.put("date", sm_date.format(new Date()));

root.put("year", sm_year.format(new Date()));

root.put("author", "wangchun");

root.put("email", "chunwangi@isoftstone.com");

String templateDir = this.getClass().getClassLoader().getResource("templates").getPath();

File tdf = new File(templateDir);

List files = FileHelper.findAllFile(tdf);

for (File f : files) {

String parentDir = "";

if (f.getParentFile().compareTo(tdf) != 0) {

parentDir = f.getParent().split("templates")[1];

}

cfg.setClassForTemplateLoading(this.getClass(), "/templates" parentDir);

Template template = cfg.getTemplate(f.getName());

template.setEncoding("UTF-8");

String parentFileDir = FileHelper.genFileDir(parentDir, root);

parentFileDir = parentFileDir.replace(".", "/");

String file = FileHelper.genFileDir(f.getName(), root).replace(".ftl", ".java");

System.out.println(file);

File newFile = FileHelper.makeFile(outRoot parentFileDir "/" file);

Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile), "UTF-8"));

template.process(root, out);

logger.debug("已生成文件:" outRoot parentFileDir "/" file);

}

}

public static void main(String[] args) throws Exception {

long time = System.currentTimeMillis();

EasyCodeApplication g = new EasyCodeApplication();

Map map = new HashMap();

map.put("tr_usergroup_menu", "用戶組菜單表");

Iterator> it = map.entrySet().iterator();

while (it.hasNext()) {

Map.Entry e = it.next();

//設(shè)置數(shù)據(jù)庫主鍵字段

g.gen(e.getKey(), e.getValue(), "id", "id");

}

System.out.println("-------------------模版文件生成完畢,時間:" (System.currentTimeMillis() - time) "毫秒 ----------------!!!");

}

}

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的java增删改一键生成_easyCode(java自动生成增删改查代码)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。