| | 1 | == '''torque详细配置''' == |
| | 2 | Torque是Apache的公开源代码项目,最开始是Turbine框架的组成部分,后面被独立出来作为一个单独的组件,归入了Apache的DB项目下。Torque的主要功能是实现对数据库的访问,Torque 主要包含两部分:一部分是 Generator,它通过使用xml配置文件,产生应用程序访问数据库的资源,包括创建数据库、表和初始化sql 语句和 java 文件;另外一部分是 Runtime,提供使用这些代码访问数据库的运行时环境。 |
| | 3 | ORM(Object Relational Mapping,对象角色建模 |
| | 4 | torque各版本之间的差距还是挺大的,本文以torque3.3的配置为例 |
| | 5 | http://db.apache.org/torque/download.html下载torque-gen-3.3.zip,torque-3.3.zip。 |
| | 6 | |
| | 7 | torque-gen-3.3.zip主要有torque-gen-3.3.jar,build.properties,default.properties和build-torque.xml。torque-gen-3.3.jar自然是提供Generator运行是的包支持,build-torque.xml是ant的运行文件,它读取了build.properties和default.properties两个配置文件,这两个properties设置了生成访问数据库需要的资源文件和java代码需要用到的环境变量。存储数据库系统的属性,主要有项目名称、数据库类型、数据库链接URL、Driver、用户名、密码及主机名等。 |
| | 8 | torque-3.3.zip解压后有torque-3.3.jar和Torque.properties,torque-3.3.jar提供torque运行时的包支持,Torque.properties是toorque访问数据库时初始化环境需要用到的一些配置内容。 |
| | 9 | |
| | 10 | 综上,核心的配置文件为: |
| | 11 | A. build.properties |
| | 12 | B. *-schema.xml、id-table-schema.xml |
| | 13 | C. Torque.properties |
| | 14 | D. 一般还会有一个defalt.properties |
| | 15 | |
| | 16 | E. build-torque.xml 这个是ant的配置文件,参照这个就可了解到以上四个配置文件的作用 |
| | 17 | |
| | 18 | id-table-schema.xml是Torque的IDBroker服务调用,用于指导Torque生成id_table表,他保存了project-schema.xml中相关ID自动增长的相关内容,实现类似于Oracle中的Sequence的功能。 |
| | 19 | |
| | 20 | torque Genarator的核心任务,可以详细参照build-torque.xml的配置,列举默认常用的如下: |
| | 21 | (1).sql 解析%Torque_home%/schema/*.xml,生成对应的$Torque_home/src/sql/*.sql文件; |
| | 22 | (2).doc 解析%Torque_home%/schema/*.xml,生成对应的$Torque_home/src/sql/*.html文件,描述数据库结构; |
| | 23 | (3).create-db 生成不同平台上产生数据库系统的脚本; |
| | 24 | (4).insert-sql 执行%Torque_home%/schema/*-schema.sql文件到指定数据库; |
| | 25 | (5).sql2xml 解析%Torque_home%/schema/schema.sql文件,产生Torque对应的数据库文件%Torque_home%/schema/schema.xml; |
| | 26 | (5).id-table-init-sql 根据%Torque_home%/schema/id-table-schema.xml文件产生id表的初始化脚本文件; |
| | 27 | |
| | 28 | ANT的运行命令为%Torque_home%/ant -f build-torque.xml $taskname |
| | 29 | |
| | 30 | ant -f build-torque.xml |
| | 31 | ant -f build-torque.xml create-db |
| | 32 | ant -f build-torque.xml id-table-init-sql |
| | 33 | ant -f build-torque.xml insert-sql |
| | 34 | |
| | 35 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| | 36 | |
| | 37 | 实际使用过程中,实际上没必要一定使用torque默认的文件夹结构(即:%Torque%\src\schema),因为所有工作都是有Ant完成的,文件夹的位置也都是在build-torque.xml值指定的。当然,如果你不打算修改build-torque.xml的话,就要使用torque默认的结构和要求的文件名格式。 |
| | 38 | |
| | 39 | Torque 3.3 为例: |
| | 40 | |
| | 41 | build.properties文件: |
| | 42 | #工程名称 |
| | 43 | torque.project = torque |
| | 44 | #数据库类型 |
| | 45 | torque.database = mysql |
| | 46 | |
| | 47 | torque.addGetByNameMethod = true |
| | 48 | torque.addIntakeRetrievable = false |
| | 49 | torque.addSaveMethod = true |
| | 50 | torque.addTimeStamp = true |
| | 51 | torque.basePrefix = Base |
| | 52 | torque.complexObjectModel = true |
| | 53 | torque.useClasspath = true |
| | 54 | torque.useManagers = false |
| | 55 | torque.objectIsCaching = true |
| | 56 | torque.silentDbFetch = true |
| | 57 | torque.generateBeans = false |
| | 58 | torque.beanSuffix = Bean |
| | 59 | torque.enableJava5Features = false |
| | 60 | |
| | 61 | #数据库URL,注意数据库名应该和工程名保持一致 |
| | 62 | torque.database.createUrl = jdbc:mysql://localhost:3306/mydb |
| | 63 | torque.database.buildUrl = jdbc:mysql://localhost:3306/torque |
| | 64 | torque.database.url = jdbc:mysql://localhost:3306/torque |
| | 65 | #数据库驱动类 |
| | 66 | torque.database.driver = com.mysql.jdbc.Driver |
| | 67 | #连接数据库使用的用户名 |
| | 68 | torque.database.user = root |
| | 69 | #连接数据库使用的密码 |
| | 70 | torque.database.password = 229229 |
| | 71 | #数据库的主机IP |
| | 72 | torque.database.host = 127.0.0.1 |
| | 73 | torque.sameJavaName = false |
| | 74 | |
| | 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |
| | 76 | |
| | 77 | project-schema.xml: |
| | 78 | 随便建了一个最简单的表为例: |
| | 79 | |
| | 80 | <?xml version="1.0" encoding="UTF-8" standalone="no"?> |
| | 81 | <!DOCTYPE database SYSTEM "http://db.apache.org/torque/dtd/database_3_3.dtd"> |
| | 82 | |
| | 83 | <database name="torque" defaultIdMethod="idbroker"> |
| | 84 | <table name="user" description="User Table"> |
| | 85 | <column name="id" required="true" primaryKey="true" type="INTEGER" description="User ID"/> |
| | 86 | <column name="name" required="true" type="VARCHAR" size="40" description="User Name"/> |
| | 87 | </table> |
| | 88 | </database> |
| | 89 | |
| | 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |
| | 91 | |
| | 92 | id-table-schema.xml: |
| | 93 | |
| | 94 | <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?> |
| | 95 | <!DOCTYPE database SYSTEM "http://db.apache.org/torque/dtd/database_3_3.dtd"> |
| | 96 | |
| | 97 | <database name="torque"> |
| | 98 | <table name="ID_TABLE" idMethod="idbroker"> |
| | 99 | <column name="ID_TABLE_ID" required="true" primaryKey="true" type="INTEGER"/> |
| | 100 | <column name="TABLE_NAME" required="true" size="255" type="VARCHAR"/> |
| | 101 | <column name="NEXT_ID" type="INTEGER"/> |
| | 102 | <column name="QUANTITY" type="INTEGER"/> |
| | 103 | |
| | 104 | <unique> |
| | 105 | <unique-column name="TABLE_NAME"/> |
| | 106 | </unique> |
| | 107 | |
| | 108 | </table> |
| | 109 | </database> |
| | 110 | 这个配置文件让数据库生成一个叫做ID_TABLE的表 |
| | 111 | |
| | 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |
| | 113 | |
| | 114 | torque.properties: |
| | 115 | |
| | 116 | torque.applicationRoot = . |
| | 117 | |
| | 118 | log4j.category.org.apache.torque = ALL, org.apache.torque |
| | 119 | log4j.appender.org.apache.torque = org.apache.log4j.FileAppender |
| | 120 | log4j.appender.org.apache.torque.file = ${torque.applicationRoot}/logs/torque.log |
| | 121 | log4j.appender.org.apache.torque.layout = org.apache.log4j.PatternLayout |
| | 122 | log4j.appender.org.apache.torque.layout.conversionPattern = %d [%t] %-5p %c - %m%n |
| | 123 | log4j.appender.org.apache.torque.append = false |
| | 124 | |
| | 125 | torque.defaults.pool.maxIdle = 8 |
| | 126 | torque.defaults.pool.maxActive = 10 |
| | 127 | torque.defaults.pool.timeBetweenEvictionRunsMillis= 300000 |
| | 128 | torque.defaults.pool.minEvictableIdleTimeMillis = 3600000 |
| | 129 | //以上部分用默认的就可以了,也就是torque-3.3.zip里面自带的 |
| | 130 | |
| | 131 | //下面灰色背景部分是需要针对自己项目改动的 |
| | 132 | torque.defaults.connection.driver = com.mysql.jdbc.Driver |
| | 133 | torque.defaults.connection.url = jdbc:mysql://localhost:3306/torque |
| | 134 | torque.defaults.connection.user = root |
| | 135 | torque.defaults.connection.password = 229229 |
| | 136 | |
| | 137 | torque.database.default=torque |
| | 138 | torque.database.torque.adapter=mysql |
| | 139 | |
| | 140 | # # Using commons-dbcp |
| | 141 | torque.dsfactory.torque.factory=org.apache.torque.dsfactory.SharedPoolDataSourceFactory |
| | 142 | torque.dsfactory.torque.pool.maxIdle=8 |
| | 143 | torque.dsfactory.torque.pool.maxActive=10 |
| | 144 | torque.dsfactory.torque.pool.testOnBorrow=true |
| | 145 | torque.dsfactory.torque.pool.validationQuery=SELECT 1 |
| | 146 | torque.dsfactory.torque.connection.driver = com.mysql.jdbc.Driver |
| | 147 | torque.dsfactory.torque.connection.url = jdbc:mysql://localhost:3306/torque |
| | 148 | torque.dsfactory.torque.connection.user = root |
| | 149 | torque.dsfactory.torque.connection.password = 229229 |
| | 150 | |
| | 151 | torque.idbroker.clever.quantity=true |
| | 152 | torque.manager.useCache = true |
| | 153 | |
| | 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |
| | 155 | |
| | 156 | defalt.properties: |
| | 157 | 我使用的是wdw.properties配置文件,需要在build.xml中改动一下,将default.properties的路径改为wdw.properties文件路径即可 |
| | 158 | |
| | 159 | torque.home = . |
| | 160 | |
| | 161 | #指定了生成Java文件的包路径 |
| | 162 | torque.targetPackage = javaTorque.wdwtest |
| | 163 | torque.runOnlyOnSchemaChange = false |
| | 164 | |
| | 165 | torque.output.dir = ${torque.home}/src |
| | 166 | torque.schema.dir = ${torque.home}/src/schema |
| | 167 | torque.templatePath = templates |
| | 168 | torque.useClasspath = true |
| | 169 | |
| | 170 | torque.doc.dir = ${torque.output.dir}/doc |
| | 171 | torque.java.dir = ${torque.output.dir} |
| | 172 | torque.java.base.dir = ${torque.java.dir} |
| | 173 | torque.javadoc.dir = ${torque.output.dir}/javadoc |
| | 174 | torque.ojb.dir = ${torque.output.dir}/ojb |
| | 175 | torque.sql.dir = ${torque.output.dir}/sql |
| | 176 | torque.omzip.dir = ${torque.output.dir} |
| | 177 | |
| | 178 | torque.database.manualCreation = false |
| | 179 | torque.saveJavaName = false |
| | 180 | |
| | 181 | torque.addGetByNameMethod = true |
| | 182 | torque.addIntakeRetrievable = false |
| | 183 | torque.retrievableInterface = org.apache.turbine.om.Retrievable |
| | 184 | torque.addSaveMethod = true |
| | 185 | torque.addTimeStamp = true |
| | 186 | torque.basePrefix = Base |
| | 187 | torque.complexObjectModel = true |
| | 188 | torque.saveException = Exception |
| | 189 | torque.useClasspath = false |
| | 190 | # Whether to generate Manager classes for JCS-based caching. |
| | 191 | torque.useManagers = false |
| | 192 | torque.objectIsCaching = true |
| | 193 | torque.silentDbFetch = true |
| | 194 | torque.generateBeans = false |
| | 195 | torque.beanSuffix = Bean |
| | 196 | |
| | 197 | torque.subpackage.base.bean = bean |
| | 198 | torque.subpackage.bean = bean |
| | 199 | torque.subpackage.map = map |
| | 200 | |
| | 201 | torque.omzip.src.base = false |
| | 202 | torque.omzip.src.extension = false |
| | 203 | torque.omzip.bin.base = false |
| | 204 | torque.omzip.bin.extension = false |
| | 205 | torque.omzip.deleteFiles = false |
| | 206 | |
| | 207 | torque.generateDeprecated = true |
| | 208 | |
| | 209 | |
| | 210 | torque.idTableXMLFile = |
| | 211 | torque.initialID = 101 |
| | 212 | torque.initialIDValue = 1000 |
| | 213 | torque.initialIDStep = 10 |
| | 214 | |
| | 215 | # Doc settings |
| | 216 | torque.doc.format = html |
| | 217 | torque.doc.html.normalFont = font-family: Verdana; font-size: 10pt; |
| | 218 | torque.doc.html.fkColor = afe295 |
| | 219 | |
| | 220 | torque.template.sql = sql/base/Control.vm |
| | 221 | torque.template.om = om/Control.vm |
| | 222 | torque.template.om.base = om/ControlBase.vm |
| | 223 | torque.template.idTable = sql/id-table/Control.vm |
| | 224 | torque.template.dataDtd = data/Control.vm |
| | 225 | torque.template.dataDump = data/dump/Control.vm |
| | 226 | torque.template.dataSql = sql/load/Control.vm |
| | 227 | torque.template.doc = doc/Control.vm |
| | 228 | torque.template.sqlDbInit = sql/db-init/Control.vm |
| | 229 | torque.template.ojb = ojb/repository/Control.vm |
| | 230 | torque.template.ojbModel = ojb/model/Control.vm |
| | 231 | |
| | 232 | torque.compile.src.dir = ${torque.java.dir} |
| | 233 | torque.compile.build.dir = bin/classes |
| | 234 | torque.compile.debug = on |
| | 235 | torque.compile.deprecation = off |
| | 236 | torque.compile.optimize = off |
| | 237 | |
| | 238 | torque.schema.sql.includes = *-schema.xml |
| | 239 | torque.schema.sql.excludes = |
| | 240 | torque.schema.doc.includes = *-schema.xml |
| | 241 | torque.schema.doc.excludes = |
| | 242 | torque.schema.create-db.includes = *-schema.xml |
| | 243 | torque.schema.create-db.excludes = |
| | 244 | torque.schema.init-sql.includes = *-schema.xml |
| | 245 | torque.schema.init-sql.excludes = id-table-schema.xml |
| | 246 | torque.schema.om.includes = *-schema.xml |
| | 247 | torque.schema.om.excludes = id-table-schema.xml |
| | 248 | torque.schema.ojb.includes = *-schema.xml |
| | 249 | torque.schema.ojb.excludes = |
| | 250 | |