iBatis学习笔记:(versions 2.2.0 and higher)
配置文件SqlMapConfig.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<!-- Always ensure to use the correct XML header as above! -->
<sqlMapConfig>
<!-- The properties (name=value) in the file specified here can be used placeholders in this config
file (e.g. “${driver}”. The file is relative to the classpath and is completely optional. -->
<properties resource=" examples/sqlmap/maps/SqlMapConfigExample.properties " />
<!-- These settings control SqlMapClient configuration details, primarily to do with transaction
management. They are all optional (more detail later in this document). -->
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="128"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="false"
defaultStatementTimeout="5"
statementCachingEnabled="true"
classInfoCacheEnabled="true"
/>
<!-- This element declares a factory class that iBATIS will use for creating result objects.
This element is optional (more detail later in this document). -->
<resultObjectFactory type="com.mydomain.MyResultObjectFactory" >
<property name="someProperty" value="someValue"/>
</resultObjectFactory>
<!-- Type aliases allow you to use a shorter name for long fully qualified class names. -->
<typeAlias alias="order" type="testdomain.Order"/>
<!-- Configure a datasource to use with this SQL Map using SimpleDataSource.
Notice the use of the properties from the above resource -->
<transactionManager type="JDBC" >
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${driver}"/>
<property name="JDBC.ConnectionURL" value="${url}"/>
<property name="JDBC.Username" value="${username}"/>
<property name="JDBC.Password" value="${password}"/>
<property name="JDBC.DefaultAutoCommit" value="true" />
<property name="Pool.MaximumActiveConnections" value="10"/>
<property name="Pool.MaximumIdleConnections" value="5"/>
<property name="Pool.MaximumCheckoutTime" value="120000"/>
<property name="Pool.TimeToWait" value="500"/>
<property name="Pool.PingQuery" value="select 1 from ACCOUNT"/>
<property name="Pool.PingEnabled" value="false"/>
<property name="Pool.PingConnectionsOlderThan" value="1"/>
<property name="Pool.PingConnectionsNotUsedFor" value="1"/>
</dataSource>
</transactionManager>
<!-- Identify all SQL Map XML files to be loaded by this SQL map. Notice the paths
are relative to the classpath. For now, we only have one… -->
<sqlMap resource="examples/sqlmap/maps/Person.xml" />
</sqlMapConfig>
其中:
<properties>元素指定了一个标准java properties文件的位置,用classpath相对位置或URL形式指定,之后整个配置文件以及所有被包含进来的映射文件都可以用${key}形式的占位符来获取properties文件中的value值。
<settings>元素:
设置一些全局属性,如延迟加载、缓存模式等。
所有可设置属性及其说明如下:
maxRequests This is the maximum number of threads that can execute an SQL
statement at a time. Threads beyond the set value will be blocked until
another thread completes execution. Different DBMS have different
limits, but no database is without these limits. This should usually be at
least 10 times maxTransactions (see below) and should always be greater
than both maxSessions and maxTransactions. Often reducing the
maximum number of concurrent requests can increase performance.
Example: maxRequests= ”256”
Default: 512
maxSessions This is the number of sessions (or clients) that can be active at a given
time. A session is either an explicit session, requested
programmatically, or it is automatic whenever a thread makes use of an
SqlMapClient instance (e.g. executes a statement etc.). This should
always be greater than or equal to maxTransactions and less than
maxRequests. Reducing the maximum number of concurrent sessions
can reduce the overall memory footprint.
Example: maxSessions= ”64”
Default: 128
maxTransactions This is the maximum number of threads that can enter
SqlMapClient.startTransaction() at a time. Threads beyond the set value
will be blocked until another thread exits. Different DBMS have
different limits, but no database is without these limits. This value
should always be less than or equal to maxSessions and always much
less than maxRequests. Often reducing the maximum number of
concurrent transactions can increase performance.
Example: maxTransactions= ”16”
Default: 32
cacheModelsEnabled This setting globally enables or disables all cache models for an
SqlMapClient. This can come in handy for debugging.
Example: cacheModelsEnabled= ”true”
Default: true (enabled)
lazyLoadingEnabled This setting globally enables or disables all lazy loading for an
SqlMapClient. This can come in handy for debugging.
Example: lazyLoadingEnabled= ”true”
Default: true (enabled)
enhancementEnabled This setting enables runtime bytecode enhancement to facilitate
optimized JavaBean property access as well as enhanced lazy loading.
Example: enhancementEnabled = ”true”
Default: false (disabled)
useStatementNamespaces With this setting enabled, you must always refer to mapped statements
by their fully qualified name, which is the combination of the sqlMap
name and the statement name. For example:
queryForObject(“sqlMapName.statementName”);
Example: useStatementNamespaces= ”false”
Default: false (disabled)
defaultStatementTimeout (iBATIS versions 2.2.0 and later)
This setting is an integer value that will be applied as the JDBC query
timeout for all statements. This value can be overridden with the
“statement” attribute of any mapped statement. If not specified, no
query timeout will be set unless specified on the “statement” attribute of
a mapped statement. The specified value is the number of seconds the
driver will wait for a statement to finish. Note that not all drivers
support this setting.
classInfoCacheEnabled With this setting enabled, iBATIS will maintain a cache of introspected
classes. This will lead to a significant reduction in startup time if many
classes are reused.
Example: classInfoCacheEnabled= “true”
Default: true (enabled)
statementCachingEnabled (iBATIS versions 2.3.0 and later)
With this setting enabled, iBATIS will maintain a local cache of
prepared statements. This can lead to significant performance
improvements.
Example: statementCachingEnabled= “true”
Default: true (enabled)
<resultObjectFactory>元素:
指定对象生成工厂类,用于将查询结果封装成对象返回。这是可选设置,若不指定,ibatis将使用Class.newInstance()的方式生成查询结果类。对象工厂类必须实现com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactory接口。
<typeAlias>元素:
用来为一些亢长的类名起“别名”,例如:
<typeAlias alias="shortname" type="com.long.class.path.Class"/>
以后就能用“shortname”来取代“com.long.class.path.Class”了。
除了自己定义别名外,iBatis框架预先定义了一些别名,以方便使用,他们是:
Transaction Manager Aliases:
JDBC com.ibatis.sqlmap.engine.transaction.jdbc.JdbcTransactionConfig
JTA com.ibatis.sqlmap.engine.transaction.jta.JtaTransactionConfig
EXTERNAL com.ibatis.sqlmap.engine.transaction.external.ExternalTransactionConfig
Data Source Factory Aliases:
SIMPLE com.ibatis.sqlmap.engine.datasource.SimpleDataSourceFactory
DBCP com.ibatis.sqlmap.engine.datasource.DbcpDataSourceFactory
JNDI com.ibatis.sqlmap.engine.datasource.JndiDataSourceFactory
<transactionManager>元素:
设置事务类型和<dataSource>元素,如上文所说,预定义的事务类型有JDBC, JTA, EXTERNAL;数据源类型有SIMPLE, DBCP, JNDI;若指定EXTERNAL或JTA,那就还有额外的属性需要设置:
<dataSource>元素:
在transactionManager元素中,定义数据源。预定义三种数据源工厂:SIMPLE, DBCP, JNDI,不过也可以自己写一个。
SIMPLE:在没有容器数据源支持的情况下使用的最简单的数据源实现,具体设置见刚才的例子。
DBCP:使用apache的DBCP数据源,ibatis框架对其直接提供支持,设置方法如下:
<transactionManager type="JDBC">
<dataSource type="DBCP">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- OPTIONAL PROPERTIES BELOW -->
<property name="maxActive" value="10"/>
<property name="maxIdle" value="5"/>
<property name="maxWait" value="60000"/>
<!-- Use of the validation query can be problematic.
If you have difficulty, try without it. -->
<property name="validationQuery" value="select * from ACCOUNT"/>
<property name="logAbandoned" value="false"/>
<property name="removeAbandoned" value="false"/>
<property name="removeAbandonedTimeout" value="50000"/>
<property name="Driver.DriverSpecificProperty" value="SomeValue"/>
</datasource>
</transactionManager>
所有的设置属性请参考:http://jakarta.apache.org/commons/dbcp/configuration.html
注:以‘Driver.’开头的属性会被加入到JDBC的属性中(有些JDBC需要)。
JNDI:指定配置的JNDI数据源。设置格式:
<transactionManager type="JDBC" >
<dataSource type="JNDI">
<property name="DataSource" value="java:comp/env/jdbc/jpetstore"/>
</dataSource>
</transactionManager>
上面只是使用普通的JDBC事务,但通常设置JNDI数据源更愿意用JTA全局事务:
<transactionManager type="JTA" >
<property name="UserTransaction" value="java:/comp/UserTransaction"/>
<dataSource type="JNDI">
<property name="DataSource" value="java:comp/env/jdbc/jpetstore"/>
</dataSource>
</transactionManager>
<sqlMap>元素:
用来包含SQL映射文件或另一个配置文件,以classpath或URL的形式:
<!-- CLASSPATH RESOURCES -->
<sqlMap resource="com/ibatis/examples/sql/Customer.xml" />
<sqlMap resource="com/ibatis/examples/sql/Account.xml" />
<sqlMap resource="com/ibatis/examples/sql/Product.xml" />
<!-- URL RESOURCES -->
<sqlMap url="file:///c:/config/Customer.xml " />
<sqlMap url="file:///c:/config/Account.xml " />
转自:http://pf-miles.iteye.com/blog/82020
分享到:
相关推荐
ibatis知识点总结(常用使用,配置文件详细解说)
自己总结了Spring+Struts+ibatis中的各种问题以及讲解各个配置文件和项目架构
这是本人从实际项目开发中总结出来的,具有很强的实用性,有关Struts,Spring,Ibatis,web 四个文件的详细配置。值得初学者,以及想实际搭建框架者学习。
里面有四个程序 ibatis例子 spring+ibates 配置文件实现 spring+ibates 自动注解 spring+ibates+sybase整合 内容简单 一看就懂
1、mybatis(ibatis3.0)所需要的jar包 2、mybatis官方用户指导手册,中英文版。 3、优点缺点对比,自己总结,希望有参考价值。 4、mybatis使用log4j.xml和log4j.properties两种日志输出方式的配置 5、部分sql文件参考...
1. 从配置文件(通常是XML配置文件中)得到 sessionfactory. 2. 由sessionfactory 产生 session 3. 在session 中完成对数据的增删改查和事务提交等. 4. 在用完之后关闭session 。 5. 在java 对象和 数据库之间有做...
5.15 Martini项目下的ibatis文件配置 5.16 数据库测试FAQ 6 Spring和SQL跟踪 6.1 @Tracer 6.2 FAQ 7 JTester插件的使用 7.1 插件功能 7.2 插件安装 7.3 录制变量的功能 7.4 dbFit插件编辑功能 7.5 从数据库中直接拖...
SWT可交互式Browser控件 JDK配置(注意) RIA简介(第一部分) 在 Eclipse 中嵌入 NASA World Wind Java SDK, 用3DES加密解密 BadPaddingException Web Service 打包生成aar文件 什么是反射...
配置子报表文件 14.7.4.2. 配置子报表数据源 14.7.5. 配置Exporter的参数 15. 集成其它Web框架 15.1. 简介 15.2. 通用配置 15.3. JavaServer Faces 15.3.1. DelegatingVariableResolver 15.3.2. FacesContextUtils ...
类与类之间的关系主要体现在表与表之间的关系进行操作,它们都市对对象进行操作,我们程序中把所有的表与类都映射在一起,它们通过配置文件中的many-to-one、one-to-many、many-to-many、 4. 说下Hibernate的缓存...
2.4.1. 在XML里更为简单的声明性事务配置 2.4.2. JPA 2.4.3. 异步的JMS 2.4.4. JDBC 2.5. Web层 2.5.1. Spring MVC的表单标签库 2.5.2. Spring MVC合理的默认值 2.5.3. Portlet 框架 2.6. 其他特性 2.6.1. ...
ant配置文件实例详解 build.xml 代码 xml version="1.0" encoding="UTF-8"?> <!-- name:对应工程的名字;default:需要的缺省任务(运行"ant"不指明任务时执行的任务) --> by duzn 2007-04-02 回复 (0) Antenna与j2me...
给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 22、spring 与EJB的区别!! 126 八. ...
2.2.1. 更简单的XML配置 2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.4. 中间层 2.4.1. 在XML里更为简单的声明性...
2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 ...
2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 ...