首页 > 编程知识 正文

java代码模板生成器,java程序设计模板

时间:2024-04-24 11:43:28 阅读:334341 作者:FCQL

本文目录一览:

求助大神,要求用java代码写一个序列生成器

import java.util.HashMap;

import java.security.KeyException;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

/**

 * 类codeKey/code是一个数据库主键生成器,用序列号的方式来产生数据库中需要的主键值。

 * p

 * codeKey/code目前支持的数据库包括Oracle的所有版本、MySql的3.x以上的版本

 * 以及所有支持max()函数的数据库,支持字段类型仅为数字类型的主键,对于字符及其它类型的主键尚不提供支持。

 * p

 * 在使用时只需提供表名、字段名(主键)以及到数据库的JDBC连接,如果想要获得message表的id字段的下一个 主键值时:

 * p

 * blockquote

 * 

 * pre

 *     java.sql.Connection conn = ...;

 *     org.shaoye.common.sql.Key key = org.shaoye.common.sql.Key.getInstance();

 *     int keyValue = key.getNextKey("message", "id", conn);

 *     String sql = "insert into message (id,...) values (" + keyValue + ",...)";

 *     //执行插入操作...

 * /pre

 * 

 * /blockquote

 * p

 * 

 * @author 令少爷(shaoye@vip.sina.com)

 * @since magic 0.1

 */

public final class Key {

/**

 * key的最大值,默认为9223372036854775807,即long类型的最大值

 */

private long max = 9223372036854775807L;

/**

 * key的最小值,默认为1

 * */

private long min = 1L;

/**

 * Key的唯一实例,通过getInstance()方法获得

 * */

private static Key keygen = new Key();

/**

 * KeyInfo类的实例列表,默认容量为5个

 * */

private HashMapString, KeyInfo keyList = new HashMapString, KeyInfo(5); // keyInfo

// 列表

/**

 * 私有的默认构造方法,防止外部构造类的实例

 * */

private Key() {

}

/**

 * 获得Key的唯一实例

 * */

public static Key getInstance() {

return keygen;

}

/**

 * 用指定的表和字段获得key的下一个值,主键的值不得超过2147483647

 * 

 * @param tableName

 *            数据库中的表名,表中必须有一个数字主键

 * @param keyName

 *            表(tableName)中的字段名

 * @param conn

 *            JDBC连接,如果欲获得的key是第一次取值,则必须保证conn能连接到数据库

 * @return key的下一个主键的int值

 * @throws codeKeyException/code

 *         如果表名或字段名不存在、访问数据库错误或key的值大于2147483647时抛出

 */

public int getNextKey(String tableName, String keyName, Connection conn)

throws KeyException {

long value = getNextKeyLong(tableName, keyName, conn);

if (value  2147483647L) {

throw new KeyException(

"Key's value too big,please call getNextKeyLong method!");

}

return (new Long(value)).intValue();

}

/**

 * 用指定的表和字段获得key的下一个值,最大为9223372036854775807

 * @param tableName 数据库中的表名,表中必须有一个数字主键

 * @param keyName 表(tableName)中的字段名

 * @param conn JDBC连接,如果欲获得的key是第一次取值,则必须保证conn能连接到数据库

 * @return key的下一个主键的long值

 * @throws codeKeyException/code 如果表名或字段名不存在或访问数据库错误时抛出

 */

public long getNextKeyLong(String tableName, String keyName, Connection conn)

throws KeyException {

KeyInfo keyinfo;

String item = tableName + "." + keyName;

try {

if (keyList.containsKey(item)) {

keyinfo = (KeyInfo) keyList.get(item);

} else {

keyinfo = new KeyInfo(tableName, keyName, conn);

keyList.put(item, keyinfo);

}

return keyinfo.getNextKey();

} catch (SQLException sqle) {

throw new KeyException(sqle);

}

}

/**

 * 用指定的"表code./code字段"形式的字符串获得key的下一个值,主键的值不得超过2147483647

 * @param tableDotField "表.字段"形式的字符串,如:message.id

 * @param conn JDBC连接,如果欲获得的key是第一次取值,则必须保证conn能连接到数据库

 * @return key的下一个主键的int值

 * @throws codeKeyException/code 如果表名或字段名不存在、访问数据库错误或key的值 大于2147483647时抛出

 */

public int getNextKey(String tableDotField, Connection conn)

throws KeyException {

long value = getNextKeyLong(tableDotField, conn);

if (value  2147483647L) {

throw new KeyException(

"Key's value too big,please call getNextKeyLong method!");

}

return (new Long(value)).intValue();

}

/**

 * 用指定的"表code./code字段"形式的字符串获得key的下一个值,最大为9223372036854775807

 * @param tableDotField "表.字段"形式的字符串,如:message.id

 * @param conn JDBC连接,如果欲获得的key是第一次取值,则必须保证conn能连接到数据库

 * @return key的下一个主键的int值

 * @throws codeKeyException/code 如果表名或字段名不存在或访问数据库错误时抛出

 */

public long getNextKeyLong(String tableDotField, Connection conn)

throws KeyException {

int dot_index = tableDotField.indexOf(".");

if (tableDotField.indexOf(".")  1) {

throw new KeyException("Unknown Key '" + tableDotField + "'!");

}

String tab = tableDotField.substring(0, dot_index);

String key = tableDotField.substring(dot_index);

return getNextKeyLong(tab, key, conn);

}

/**

 * 用指定的表和字段获得key的当前值,主键的值不得超过2147483647

 * @param tableName 数据库中的表名,表中必须有一个数字主键

 * @param keyName 表(tableName)中的字段名

 * @param conn JDBC连接,如果欲获得的key是第一次取值,则必须保证conn能连接到数据库

 * @return key的当前int值

 * @throws codeKeyException/code

 *         如果表名或字段名不存在、访问数据库错误或key的值大于2147483647时抛出

 */

public int getCurrentKey(String tableName, String keyName, Connection conn)

throws KeyException {

long value = getCurrentKeyLong(tableName, keyName, conn);

if (value  2147483647L) {

throw new KeyException(

"Key's value too big,please call getCurrentKeyLong method!");

}

return (new Long(value)).intValue();

}

/**

 * 用指定的表和字段获得key的当前值,最大为9223372036854775807

 * 

 * @param tableName

 *            数据库中的表名,表中必须有一个数字主键

 * @param keyName

 *            表(tableName)中的字段名

 * @param conn

 *            JDBC连接,如果欲获得的key是第一次取值,则必须保证conn能连接到数据库

 * @return key的当前long值

 * @throws codeKeyException/code 如果表名或字段名不存在或访问数据库错误时抛出

 */

public long getCurrentKeyLong(String tableName, String keyName,

Connection conn) throws KeyException {

KeyInfo keyinfo;

String item = tableName + "." + keyName;

try {

synchronized (keyList) {

if (keyList.containsKey(item)) {

keyinfo = (KeyInfo) keyList.get(item);

} else {

keyinfo = new KeyInfo(tableName, keyName, conn);

keyList.put(item, keyinfo);

}

}

return keyinfo.getCurrentKey();

} catch (SQLException sqle) {

throw new KeyException(sqle);

}

}

/**

 * 用指定的"表code./code字段"形式的字符串获得key的当前值,主键的值不得超过2147483647

 * 

 * @param tableDotField

 *            "表.字段"形式的字符串,如:message.id

 * @param conn

 *            JDBC连接,如果欲获得的key是第一次取值,则必须保证conn能连接到数据库

 * @return key的当前int值

 * @throws codeKeyException/code 如果表名或字段名不存在、访问数据库错误或key的值

 *         大于2147483647时抛出

 */

public int getCurrentKey(String tableDotField, Connection conn)

throws KeyException {

long value = getCurrentKeyLong(tableDotField, conn);

if (value  2147483647L) {

throw new KeyException(

"Key's value too big,please call getNextKeyLong method!");

}

return (new Long(value)).intValue();

}

/**

 * 用指定的"表code./code字段"形式的字符串获得key的当前值,最大为9223372036854775807

 * 

 * @param tableDotField

 *            "表.字段"形式的字符串,如:message.id

 * @param conn

 *            JDBC连接,如果欲获得的key是第一次取值,则必须保证conn能连接到数据库

 * @return key的当前int值

 * @throws codeKeyException/code 如果表名或字段名不存在或访问数据库错误时抛出

 */

public long getCurrentKeyLong(String tableDotField, Connection conn)

throws KeyException {

int dot_index = tableDotField.indexOf(".");

if (tableDotField.indexOf(".")  1) {

throw new KeyException("Unknown Key '" + tableDotField + "'!");

}

String tab = tableDotField.substring(0, dot_index);

String key = tableDotField.substring(dot_index);

return getCurrentKeyLong(tab, key, conn);

}

}

/**

 * 内部类,用来存储主键信息

 * */

class KeyInfo {

private long max = 9223372036854775807L;

private long min = 1L;

private long nextKey;

private String tableName;

private String keyName;

private Connection conn = null;

/**

 * keyInfo 对象初始化

 * 

 * @throws KeyException

 */

KeyInfo(String tableName, String keyName, Connection _conn)

throws SQLException, KeyException {

this.tableName = tableName;

this.keyName = keyName;

this.conn = _conn;

retrieveFromDB();

}

int getMax() {

return (new Long(max)).intValue();

}

long getMaxLong() {

return max;

}

int getMin() {

return (new Long(min)).intValue();

}

long getMinLong() {

return min;

}

/**

 * 取下一键值

 */

int getNextKey() {

return (new Long(getNextKeyLong())).intValue();

}

/**

 * 取下一键值

 */

synchronized long getNextKeyLong() {

nextKey++;

return nextKey;

}

/**

 * 取当前键值

 */

synchronized int getCurrentKey() {

return (new Long(nextKey)).intValue();

}

/**

 * 取当前键值

 */

synchronized long getCurrentKeyLong() {

return nextKey;

}

/**

 * 从数据库中取当前最大值

 * 

 * @throws KeyException

 */

void retrieveFromDB() throws SQLException, KeyException {

PreparedStatement pstmt = null;

ResultSet rs = null;

String sql = "select max(" + keyName + ") from " + tableName;

try {

pstmt = conn.prepareStatement(sql);

} catch (Exception ex) {

throw new KeyException("Can't connect DataBase!");

}

try {

rs = pstmt.executeQuery();

} catch (SQLException sqle) {

if (pstmt != null)

pstmt.close();

throw new KeyException("'" + keyName + "' or '" + tableName

+ "' isn't exist in DataBase!", sqle);

}

try {

if (rs.next()) {

nextKey = rs.getLong(1);

if (nextKey  min) {

nextKey = min;

}

} else {

nextKey = min;

}

} catch (SQLException sqle) {

throw (sqle);

} finally {

if (rs != null)

rs.close();

if (pstmt != null)

pstmt.close();

}

}

}

JAVA文件生成器怎么用?(具体步骤)

随便找个地方..

你先双击安装

然后打开那个软件..

把jar放到上面..按开始生成..你jar旁边就有jad了..

你再把jar和jad放到你手机里试试

怎么样了?可不可以呐?

java代码生成器用途

代码生成器就是根据特定的要求制定格式,灵活输出在项目中重复要用到的代码,节省项目时间,现在免费的代码生成器codesmith我经常用的,小玩意,不花钱

大家对Java代码生成器有什么看法?全面回答

Java代码生成器是什么?

我只知道UML可以直接导出生成Java代码。 MyEclipse可以自动生成get,set,constructor方法,不知道这算不算代码生成器?

哪位高手回答,我也顺便学习一下~~~~

java代码生成器能生成c语言代码吗?用什么方法能实现呢?

按照我的理解,可以的,代码生成器是跨平台,而且是跨语言的(至少是跨文本语言的,UML暂时不好说,)。

代码生成器作为一种开发工具,一般不直接作为程序的一部分,通常也不直接或间接(如通过AOP)被程序调用,他是通过编程的方式生成所需要的代码,然后将生成的代码作为源文件,复制到开发工具的代码区,然后进行编译。由于代码生成器是在编译之前运行的,因此它可以跨语言,你不但可以用代码生成器生成高级语言,也可以生成汇编语言,甚至机器码(0、1代码)。

但编写代码生成器是一个难点,它需要你了解目标编程语言的语法。如果别人已经把你需要的代码生成器写好了,你只需按要求使用即可,此时就不需要了解目标编程语言的语法。

JNI是通过Java调用C语言(或其他语言,一般是C),它的实现机制与代码生成器不同(JNI是通过代码调用实现功能,而代码生成器是生成代码,复制代码进行使用),由于C是底层语言,一些底层操作单靠Java无法实现,所以才需要JNI。

以上是我对代码生成器的个人理解,虽然提问时间已过了很久,但依然希望能对你及其他网友有所帮助。

java代码生成器怎么用

zip包,然后自动下载下来

1.预先定义好模板

2.界面输入相关参数

3.解析模板生成代码并下载

最后放出源代码:

package com.et.controller.system.createcode;

import java.util.ArrayList;

import java.util.Date;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import com.et.controller.base.BaseController;

import com.et.util.DelAllFile;

import com.et.util.FileDownload;

import com.et.util.FileZip;

import com.et.util.Freemarker;

import com.et.util.PageData;

import com.et.util.PathUtil;

/**

* 类名称:FreemarkerController

* 创建人:Harries

* 创建时间:2015年1月12日

* @version

*/

@Controller

@RequestMapping(value=”/createCode”)

public class CreateCodeController extends BaseController {

/**

* 生成代码

*/

@RequestMapping(value=”/proCode”)

public void proCode(HttpServletResponse response) throws Exception{

PageData pd = new PageData();

pd = this.getPageData();

/* ============================================================================================= */

String packageName = pd.getString(“packageName”); //包名 ========1

String objectName = pd.getString(“objectName”); //类名 ========2

String tabletop = pd.getString(“tabletop”); //表前缀 ========3

tabletop = null == tabletop?””:tabletop.toUpperCase(); //表前缀转大写

String zindext = pd.getString(“zindex”); //属性总数

int zindex = 0;

if(null != zindext !””.equals(zindext)){

zindex = Integer.parseInt(zindext);

}

ListString[] fieldList = new ArrayListString[](); //属性集合 ========4

for(int i=0; i zindex; i++){

fieldList.add(pd.getString(“field”+i).split(“,fh,”)); //属性放到集合里面

}

MapString,Object root = new HashMapString,Object(); //创建数据模型

root.put(“fieldList”, fieldList);

root.put(“packageName”, packageName); //包名

root.put(“objectName”, objectName); //类名

root.put(“objectNameLower”, objectName.toLowerCase()); //类名(全小写)

root.put(“objectNameUpper”, objectName.toUpperCase()); //类名(全大写)

root.put(“tabletop”, tabletop); //表前缀

root.put(“nowDate”, new Date()); //当前日期

DelAllFile.delFolder(PathUtil.getClasspath()+”admin/ftl”); //生成代码前,先清空之前生成的代码

/* ============================================================================================= */

String filePath = “admin/ftl/code/”; //存放路径

String ftlPath = “createCode”; //ftl路径

/*生成controller*/

Freemarker.printFile(“controllerTemplate.ftl”, root, “controller/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName+”Controller.java”, filePath, ftlPath);

/*生成service*/

Freemarker.printFile(“serviceTemplate.ftl”, root, “service/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName+”Service.java”, filePath, ftlPath);

/*生成mybatis xml*/

Freemarker.printFile(“mapperMysqlTemplate.ftl”, root, “mybatis_mysql/”+packageName+”/”+objectName+”Mapper.xml”, filePath, ftlPath);

Freemarker.printFile(“mapperOracleTemplate.ftl”, root, “mybatis_oracle/”+packageName+”/”+objectName+”Mapper.xml”, filePath, ftlPath);

/*生成SQL脚本*/

Freemarker.printFile(“mysql_SQL_Template.ftl”, root, “mysql数据库脚本/”+tabletop+objectName.toUpperCase()+”.sql”, filePath, ftlPath);

Freemarker.printFile(“oracle_SQL_Template.ftl”, root, “oracle数据库脚本/”+tabletop+objectName.toUpperCase()+”.sql”, filePath, ftlPath);

/*生成jsp页面*/

Freemarker.printFile(“jsp_list_Template.ftl”, root, “jsp/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName.toLowerCase()+”_list.jsp”, filePath, ftlPath);

Freemarker.printFile(“jsp_edit_Template.ftl”, root, “jsp/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName.toLowerCase()+”_edit.jsp”, filePath, ftlPath);

/*生成说明文档*/

Freemarker.printFile(“docTemplate.ftl”, root, “说明.doc”, filePath, ftlPath);

//this.print(“oracle_SQL_Template.ftl”, root); 控制台打印

/*生成的全部代码压缩成zip文件*/

FileZip.zip(PathUtil.getClasspath()+”admin/ftl/code”, PathUtil.getClasspath()+”admin/ftl/code.zip”);

/*下载代码*/

FileDownload.fileDownload(response, PathUtil.getClasspath()+”admin/ftl/code.zip”, “code.zip”);

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。