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 " />
<sqlMap url="file:///c:/config/Product.xml" />
分享到:
相关推荐
对于复杂场景,如多表联查、分页、存储过程等,你可以继续深入学习XML配置文件的高级用法,例如使用`<association>`, `<collection>`处理嵌套结果,使用`<resultMap>`定义复杂的映射关系等。 总的来说,iBATIS的XML...
IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得
ibatis学习笔记 ibatis学习笔记 ibatis学习笔记 ibatis学习笔记 ibatis学习笔记 ibatis学习笔记 ibatis学习笔记
1.iBatis2学习笔记:基本原理和配置.doc 2.iBatis2学习笔记:与Spring2的整合.doc 3.iBatis2学习笔记:单表映射 .doc 4.iBatis2学习笔记:SqlMap的配置总结(18条).doc 5.iBatis2学习笔记:入参和返回值的问题.doc ...
### ibatis配置文件详解 #### 一、ibatis概述 ibatis,又称MyBatis,是一种优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。ibatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。ibatis可以...
**iBATIS的配置文件** 主要包含SQL Map配置文件和属性配置。SQL Map配置文件定义了属性、JDBC DataSources和SQL Maps。属性配置通常包括数据库连接池的配置,例如Jakarta DBCP。配置文件中的`<properties>`元素用于...
### ibatis配置文件信息 #### 一、简介 在Java开发领域中,ibatis(现称为MyBatis)是一款优秀的持久层框架,它通过XML或注解的方式将接口方法与SQL语句绑定起来,实现对象关系映射(ORM)功能。ibatis的主要优势...
### iBatis 学习笔记知识点总结 #### 一、iBatis 概念与特点 **1.1 iBatis 定义** - **iBatis** 是一个基于 Java 的开源持久层框架,它专注于 SQL 映射,提供了一种将对象与数据库交互过程中的 SQL 语句进行分离的...
这篇“ibatis学习笔记(一)”可能是博主对Ibatis基础概念、安装配置以及基本使用的介绍,让我们通过标签“源码”和“工具”来深入探讨Ibatis的相关知识。 首先,Ibatis是一个轻量级的Java ORM(对象关系映射)框架...
### ibatis 学习小结笔记 #### 一、ibatis 概述 ibatis 是一个基于 Java 的持久层框架,它提供了一种简便的方式来处理关系型数据库与 Java 对象之间的映射(O/R Mapping)。ibatis 在设计上强调的是 SQL 语句的...
通过本文的学习笔记,我们可以了解到 iBatis 在简化数据库访问的同时提供了足够的灵活性。尽管 iBatis 相比 Hibernate 在自动化程度上略显不足,但对于需要高度定制 SQL 查询的场景来说,iBatis 的优势十分明显。...
标题 "ibatis配置文件自动加载组件" 涉及的核心技术是MyBatis的自动配置加载功能,这在开发过程中极大地提高了效率,使得开发者无需每次修改XML映射文件后手动重启服务。MyBatis是一个优秀的Java持久层框架,它简化...
标题:“ibatis配置文件” 描述:“详细的ibatis配置文件,初来乍到,先打个招呼...” 在本文中,我们将深入探讨ibatis配置文件的关键组成部分及其如何影响ibatis框架的运行机制。ibatis(现在通常称为MyBatis)是...
### ibatis配置文件、映射文件详解 #### 1. SQL Map Config 文件详解 在ibatis框架中,`sqlMapConfig.xml`是一个非常重要的配置文件,它主要用于设置ibatis的全局配置信息,包括数据库连接信息、环境配置以及其它...
下面我们将详细探讨Ibatis配置文件模板中的关键组成部分,包括`SqlMap.properties`、`SqlMapConfig.xml`以及与JavaBean的映射文件。 1. **SqlMap.properties** 这是Ibatis的属性配置文件,主要用于存放数据库连接...
通过本篇学习笔记,我们深入了解了 ibatis 的配置文件结构、常用的操作方式以及一些高级特性如命名空间和缓存机制。这些知识点对于初学者来说至关重要,它们不仅有助于快速掌握 ibatis 的使用方法,还能够帮助开发者...
这个ssi框架的配置文件模板集合提供了一套完整的Spring、Struts2和iBatis集成的配置示例,便于开发者快速搭建项目,避免逐一查找和配置。通过理解这些配置文件的作用和结构,可以帮助开发者更好地理解和优化Java Web...
### IBATIS学习笔记知识点详解 #### 一、IBATIS简介 iBatis是一个用于Java的数据持久化框架,类似于Hibernate、JDO和EJB等技术。它的主要特点是将对象映射为SQL语句,这使得开发人员可以更加灵活地控制SQL的执行,...