浏览 6890 次
精华帖 (1) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-12-29
torque作为orm,我一般是自己手工编写xml文件,通过xml文件,生成sql脚本和java对象文件,但由于在实际项目过程中,很难做到表结构不变动,很多时间需要花在xml文件的维护和java对象文件的更换上,在几个实际项目中使用过后,就放弃了。放弃的原因主要有:1、项目组的兄弟多数比较抵制操作java对象来进行数据库,因为他们看不到自己比较熟悉的sql了;2、使用orm工具多少需要些时间来学习工具的使用,虽然相对于现在hibernate,torque还是比较简单的;3、在项目中,也很难做到数据库设计不变化,另外旧就是我现在所在公司大量使用oracle存储过程;4、在大多数时候,项目总是由最少的人,干尽可能多的事情,所以在很多时候,你不得不放弃一些所谓的技术和理念,用最短的时间将事情搞定,你才能获得上司的认可。尤其在我目前所在公司,一个千万级别的项目,也仅仅是3~5个人来完成,作为项目经理,除了要做数据库设计、存储过程编写、框架搭建、新人培训(因为3~5个人里面也许就你一个是有经验的人)、硬件设备收货、机器上架、网络打通(如果你还会调试路由器,这个事情也属于你来搞定的范畴)协调、各种外系统接口梳理和定义、外系统相关公司协调,呵呵,我经常戏虐公司的项目经理是十项全能选手,因为你的上司还要求你要有销售意识。 扯远了,如何使用torque作为orm我就不写了,我现在一般使用torque作为数据库访问工具,如何将torque引入你的系统(非web应用,独立应用程序),以oracle作为实际例子: 第一步:引入包 classes12.jar log4j-1.2.15.jar commons-beanutils-1.7.0.jar commons-collections-3.1.jar commons-configuration-1.1.jar commons-dbcp-1.2.2.jar(如果你还在使用commons-dbcp-1.2.2.jar以下版本作为数据库连接池,都请更新到commons-dbcp-1.2.2.jar) commons-lang-2.1.jar commons-logging-1.0.4.jar commons-pool-1.2.jar commons-discovery-0.2.jar torque-3.2.jar village-2.0-dev-20030825.jar 第二步:配置torque.properties 和 log4j.properties ,配置文件我一般放在classes目录下 1、torque.properties # ------------------------------------------------------------------- # $Id: Torque.properties,v 1.11.2.2 2004/08/24 04:14:32 seade Exp $ # # This is the configuration file for Torque. # # Note that strings containing "," (comma) characters must backslash # escape the comma (i.e. '\,') # # ------------------------------------------------------------------- torque.applicationRoot = . # ------------------------------------------------------------------- # # L O G G I N G # # ------------------------------------------------------------------- # We use Log4J for all Torque logging and we embed the log4j # properties within our application configuration. # ------------------------------------------------------------------- # This first category is required and the category # must be named 'default'. This is used for all logging # where an explicit category is not specified. log4j.category.org.apache.torque = ALL, org.apache.torque log4j.appender.org.apache.torque = org.apache.log4j.FileAppender #log4j.appender.org.apache.torque.file = d:/work/logs/torque.log log4j.appender.org.apache.torque.layout = org.apache.log4j.PatternLayout log4j.appender.org.apache.torque.layout.conversionPattern = %d [%t] %-5p %c - %m%n log4j.appender.org.apache.torque.append = false # ------------------------------------------------------------------- # # T O R Q U E P R O P E R T I E S # # ------------------------------------------------------------------- # These are your database settings. Look in the # org.apache.torque.pool.* packages for more information. # # The parameters to connect to the default database. You MUST # configure these properly. # ------------------------------------------------------------------- torque.database.default=mydb torque.database.mydb.adapter=oracle # # Using commons-dbcp torque.dsfactory.mydb.factory=org.apache.torque.dsfactory.SharedPoolDataSourceFactory torque.dsfactory.mydb.pool.maxIdle=8 torque.dsfactory.mydb.pool.maxActive=10 torque.dsfactory.mydb.pool.testOnBorrow=true torque.dsfactory.mydb.pool.validationQuery=select * from dual torque.dsfactory.mydb.connection.driver = oracle.jdbc.driver.OracleDriver torque.dsfactory.mydb.connection.url = jdbc:oracle:thin:@192.168.12.186:1521:mydb torque.dsfactory.mydb.connection.user = myname torque.dsfactory.mydb.connection.password = mypwd # Determines if the quantity column of the IDBroker's id_table should # be increased automatically if requests for ids reaches a high # volume. torque.idbroker.clever.quantity=false # Determines whether the managers cache instances of the business objects. # And also whether the MethodResultCache will really cache results. torque.manager.useCache = true 2、log4j.properties # ------------------------------------------------------------------------ # # Logging Configuration # # $Id: log4j.properties,v 1.10 2005/07/27 04:25:09 huangxq Exp $ # # ------------------------------------------------------------------------ # # If we don't know the logging facility, put it into the # ideal.log # log4j.rootLogger = error, stdout # # Application debugging # log4j.category.com.yh = DEBUG, stdout,ideal log4j.additivity.com.ideal = false # # torque goes into torque Log # log4j.category.org.apache.torque = ERROR, torque log4j.additivity.org.apache.torque = false ######################################################################## # # Logfile definitions # ######################################################################## #print out to console log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d [%-5p] [%F] %M : %L -- %m%n # # root.log # log4j.appender.root = org.apache.log4j.RollingFileAppender log4j.appender.root.MaxFileSize=2000KB # Keep one backup file log4j.appender.root.MaxBackupIndex=10 #log4j.appender.root.file = ./logs/root.log log4j.appender.root.layout = org.apache.log4j.PatternLayout log4j.appender.root.layout.conversionPattern =%d [%-5p] [%F] : %L -- %m%n log4j.appender.root.append = false # # yh.log # log4j.appender.yh = org.apache.log4j.RollingFileAppender log4j.appender.yh.MaxFileSize=2000KB # Keep one backup file log4j.appender.yh.MaxBackupIndex=10 log4j.appender.yh.file = ./logs/yh.log log4j.appender.yh.layout = org.apache.log4j.PatternLayout log4j.appender.yh.layout.conversionPattern =%d [%-5p] [%F] : %L -- %m%n log4j.appender.ideal.append = false # # torque.log # log4j.appender.torque = org.apache.log4j.RollingFileAppender log4j.appender.torque.MaxFileSize=2000KB # Keep one backup file log4j.appender.torque.MaxBackupIndex=10 #log4j.appender.torque.file = ./logs/torque.log log4j.appender.torque.layout = org.apache.log4j.PatternLayout log4j.appender.torque.layout.conversionPattern =%d [%-5p] [%F] : %L -- %m%n log4j.appender.torque.append = false 第三步:初始化torque InitServer.java package com.yh.core.socket.server; import java.io.InputStream; import java.util.List; import java.util.Properties; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import com.yh.util.DBUtils; import com.yh.util.FileUtils; import com.workingdogs.village.Record; /** * @author tylzhuang * * TODO To change the template for this generated type comment go to Window - * Preferences - Java - Code Style - Code Templates */ public class InitServer { private static Logger log ; private static final String TORQUE_CONFIG_FILE = "torque.properties"; private static final String LOG4J_CONFIG_FILE = "log4j.properties"; public static boolean init() { // 初始化 boolean flag = true; ClassLoader cl = InitServer.class.getClassLoader(); InputStream logIn = null; InputStream dbIn = null; try { logIn = cl.getResourceAsStream(LOG4J_CONFIG_FILE); Properties p = new Properties(); p.load(logIn); PropertyConfigurator.configure(p); log = Logger.getLogger(InitServer.class); log.debug("InitServer init() log4j init success ---------------" ); //初始化 PropertiesConfiguration dbConfig = new PropertiesConfiguration(); dbIn = cl.getResourceAsStream(TORQUE_CONFIG_FILE); dbConfig.load(dbIn); DBUtils.init(dbConfig); log.debug("InitServer init() Torque init success ---------------" ); String testSql = "select to_char(sysdate,'YYYY-MM-DD HH24:MI:SS') from dual"; String time = null; List list = DBUtils.query(testSql); if(list!=null&&list.size()>0){ Record record = (Record) list.get(0); time = record.getValue(1).asString(); log.debug("from db init-time: "+time); } } catch (Exception ex) { log.error("InitServer Exception when init() - "+ex); flag = false; } finally { FileUtils.closeInputStream(logIn); FileUtils.closeInputStream(dbIn); } return flag; } } DBUtils.java package com.yh.util; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.List; import org.apache.commons.configuration.Configuration; import org.apache.log4j.Logger; import org.apache.torque.Torque; import org.apache.torque.util.BasePeer; /** * @author tylzhuang * * TODO To change the template for this generated type comment go to Window - * Preferences - Java - Code Style - Code Templates */ public class DBUtils { private static Logger log = Logger.getLogger(DBUtils.class.getName()); public DBUtils() { } public static boolean init(String dbConfigFile) { // 初始化torque boolean flag = true; try { Torque.init(dbConfigFile); System.out.println("---------- 初始化torque成功---------"); } catch (Exception exx) { log.error(exx); flag = false; } return flag; } public static boolean init(Configuration dbConfig) { // 初始化torque boolean flag = true; try { Torque.init(dbConfig); System.out.println("---------- 初始化torque成功---------"); } catch (Exception exx) { log.error(exx); flag = false; } return flag; } public static Connection getDBConn() { // 取得数据库链接 Connection conn = null; try { conn = Torque.getConnection(); } catch (Exception exx) { log.error(exx); } return conn; } public static Connection getDBConn(String dbName) { // 取得数据库链接 Connection conn = null; try { conn = Torque.getConnection(dbName); } catch (Exception exx) { log.error(exx); } return conn; } public static Connection getDBConn(boolean autoCommitFlag) { // 取得数据库链接 Connection conn = null; try { conn = Torque.getConnection(); setAutoCommit(conn , autoCommitFlag); } catch (Exception exx) { log.error(exx); } return conn; } public static Connection getDBConn(String dbName, boolean autoCommitFlag) { // 取得数据库链接 Connection conn = null; try { conn = Torque.getConnection(dbName); setAutoCommit(conn , autoCommitFlag); } catch (Exception exx) { log.error(exx); } return conn; } public static void closeDBConn(Connection conn) { // 关闭数据库链接 try { if (conn != null) { setAutoCommit(conn , true);// 无论如何,都应该在连接关闭前,将AutoCommit 设置为 // true Torque.closeConnection(conn); conn = null; } } catch (Exception exx) { log.error(exx); } } public static void closeDBConn(Connection conn, boolean autoCommitFlag) { // 关闭数据库链接 try { if (conn != null) { setAutoCommit(conn , true);// 无论传什么值,都应该在连接关闭前,将AutoCommit 设置为 // true Torque.closeConnection(conn); conn = null; } } catch (Exception exx) { log.error(exx); } } public static void closeDBStatement(Statement s) { // 关闭Statement try { if (s != null) { s.close(); s = null; } } catch (Exception exx) { log.error(exx); } } public static void closeDBObject(Statement s, Connection conn) { // 关闭Statement try { if (s != null) { s.close(); s = null; } } catch (Exception exx) { log.error(exx); } // 关闭数据库链接 try { if (conn != null) { setAutoCommit(conn , true);// 无论如何,都应该在连接关闭前,将AutoCommit 设置为 // true Torque.closeConnection(conn); conn = null; } } catch (Exception exx) { log.error(exx); } } public static void closeDBResultSet(ResultSet r) { // 关闭ResultSet try { if (r != null) { r.close(); r = null; } } catch (Exception exx) { log.error(exx); } } public static void closeDBObject(Statement s, Connection conn, boolean autoCommitFlag) { // 关闭Statement try { if (s != null) { s.close(); s = null; } } catch (Exception exx) { log.error(exx); } // 关闭数据库链接 try { if (conn != null) { setAutoCommit(conn , true);// 无论传什么值,都应该在连接关闭前,将AutoCommit 设置为 // true Torque.closeConnection(conn); conn = null; } } catch (Exception exx) { log.error(exx); } } public static void closeDBObject(Statement s, ResultSet r ,Connection conn, boolean autoCommitFlag) { // 关闭Statement try { if (s != null) { s.close(); s = null; } } catch (Exception exx) { log.error(exx); } // 关闭ResultSet try { if (r != null) { r.close(); r = null; } } catch (Exception exx) { log.error(exx); } // 关闭数据库链接 try { if (conn != null) { setAutoCommit(conn , true);// 无论传什么值,都应该在连接关闭前,将AutoCommit 设置为 // true Torque.closeConnection(conn); conn = null; } } catch (Exception exx) { log.error(exx); } } public static void commit(Connection conn) { try { if (conn != null) { if (conn.getMetaData().supportsTransactions() && conn.getAutoCommit() == false) { conn.commit(); conn.setAutoCommit(true); } } } catch (Exception exx) { log.error(exx); } } public static void rollback(Connection conn) { try { if (conn != null) { if (conn.getMetaData().supportsTransactions() && conn.getAutoCommit() == false) { conn.rollback(); conn.setAutoCommit(true); } } } catch (Exception ee) { log.error(ee); } } public static void setAutoCommit(Connection conn , boolean autoCommitFlag) { try { if (conn.getMetaData().supportsTransactions()) { conn.setAutoCommit(autoCommitFlag); } } catch (Exception e) { log.error(e); } } public static List query(String querySql) { List results = null; try { results = BasePeer.executeQuery(querySql); } catch (Exception ee) { results = null; log.error(ee); } return results; } // singleRecord = true 表示只返回一条记录 // singleRecord = false 表示返回所有记录 public static List query(Connection conn, boolean singleRecord, String querySql) { List results = null; try { if (conn != null) { results = BasePeer.executeQuery(querySql, singleRecord, conn); } } catch (Exception ee) { results = null; log.error(ee); } return results; } // singleRecord = true 表示只返回一条记录 // singleRecord = false 表示返回所有记录 public static List query(Connection conn, String querySql) { List results = null; boolean singleRecord = false; results = query(conn , singleRecord , querySql); return results; } //查询数据库指定配置的数据库 public static List query(String dbName ,String querySql) { List results = null; try { Connection conn = DBUtils.getDBConn(dbName); if(conn != null) results = query(conn , querySql); else results = null; } catch (Exception ee) { results = null; log.error(ee); } return results; } public static int insertOrUpdate(String insertSql) { int results = -1; try { results = BasePeer.executeStatement(insertSql); } catch (Exception ee) { results = -1; log.error(ee); } return results; } public static int insertOrUpdate(Connection conn, String insertSql) { int results = -1; try { if (conn != null) { results = BasePeer.executeStatement(insertSql, conn); } } catch (Exception ee) { results = -1; log.error(ee); } return results; } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |