iBATIS配置文件说明

sql-map-config配置文件,可以查看sql-map-config-2.dtd可以有的标签和属性

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"
/>
<!-- 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>

 相关标签的详细说明会在我上传的iBATIS中文版开发指南第10页开始介绍,英文版的官方可以下载,很多地方都说这个文件的作用相当于hibernate里的hibernate.cfg.xml文件,在实际应用中也可以综合到applicationContext.xml文件中。

 

下面是sqlmap配置文件

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
   "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="Person">

	<statement id="getAllUsers" resultClass="com.air.Account">
		SELECT * FROM USER_ACCOUNT order by USERID 
	</statement>
	
</sqlMap>

 这是最简单的一个配置文件,一般根据实体类的名称或功能会有多个这样的文件,防止id有冲突,一般都会指定namespace,多个文件只要在sql-map-config.xml文件里加入多个<sqlMap resource="examples/sqlmap/maps/Person.xml" />就可以了。 

上面使用的<statement>元素是个通用声明,可以用于任何类型的SQL语句。通常,使用具体的statement类型是个好主意。如<select>,<insert>,<update>,<delete>,<procedure>,各标签的详细说明请参见中文文档的第19页

 

 

为了重复使用sql代码片段,我们可以将<sql>,<include>标签结合使用,如下面这样

<sql id="selectItem_fragment">
FROM items
WHERE parentid = 6
</sql>
<select id="selectItemCount" resultClass="int">
SELECT COUNT(*) AS total
<include refid="selectItem_fragment"/>
</select>

 SQL显然是mapped statement中最重要的部分,可以使用对于数据库和JDBC Driver合法的任意SQL语句。只要JDBC Driver支持,可以使用任意的函数,甚至是多条语句。因为SQL语句是嵌在XML文档中的,因此有些特殊的字符不能直接使用,例如大于号和小于号(<>)。幸运的是,解决的办法很简单,只需将包含特殊字符的SQL语句放在XML的CDATA区里面就可以了。

<statement id="getPersonsByAge" parameterClass=”int” resultClass="examples.domain.Person">
<![CDATA[
SELECT *
FROM PERSON
WHERE AGE > #value#
]]>
</statement>

 

你可能感兴趣的:(sql,Hibernate,xml,ibatis,jdbc)