- 浏览: 37005 次
- 性别:
- 来自: 苏州
文章分类
最新评论
-
zhanghq0717:
挺好的
js 操作map对象 -
2006204shao:
成课程创建
UML建模之时序图 -
李_俊:
没有实际意义,有什么地方可以用呢?我们不写sun一样的API函 ...
js 操作map对象
ibatis学习
映射文件总体形式:
<?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=”Product”>
<cacheModel id=”productCache” type=”LRU”>
<flushInterval hours=”24”/>
<property name=”size” value=”1000” />
</cacheModel>
<typeAlias alias=”product” type=”com.ibatis.example.Product” />
<parameterMap id=”productParam” class=”product”>
<parameter property=”id”/>
</parameterMap>
<resultMap id=”productResult” class=”product”>
<result property=”id” column=”PRD_ID”/>
<result property=”description” column=”PRD_DESCRIPTION”/>
</resultMap>
<select id=”getProduct” parameterMap=”productParam”
resultMap=”productResult” cacheModel=”product-cache”>
select * from PRODUCT where PRD_ID = ?
</select>
</sqlMap>
映射的查询语句形式:
<statement id=”statementName”
[parameterClass=”some.class.Name”]
[resultClass=”some.class.Name”]
[parameterMap=”nameOfParameterMap”]
[resultMap=”nameOfResultMap”]
[cacheModel=”nameOfCache”]
[timeout=“5”]>
select * from PRODUCT where PRD_ID = [?|#propertyName#]
order by [$simpleDynamic$]
</statement>
<insert id=”insertTestProduct” >
insert into PRODUCT (PRD_ID, PRD_DESCRIPTION) values (1, “Shih Tzu”)
</insert>
<select id="getPersonsByAge" parameterClass=”int” resultClass="examples.domain.Person">
SELECT *
FROM PERSON
WHERE AGE <![CDATA[ > ]]> #value#
</select>
下面的表格说明了所有支持的查询语句形式及其属性:
可定义SQL Fragments对部分SQL语句进行复用:
<sql id="selectItem_fragment">
FROM items
WHERE parentid = 6
</sql>
<select id="selectItemCount" resultClass="int">
SELECT COUNT(*) AS total
<include refid="selectItem_fragment"/>
</select>
<select id="selectItems" resultClass="Item">
SELECT id, name
<include refid="selectItem_fragment"/>
</select>
对某些数据库的自动增长键有支持,但不同的支持策略,例:
<!—Oracle SEQUENCE Example -->
<insert id="insertProduct-ORACLE" parameterClass="com.domain.Product">
<selectKey resultClass="int" >
SELECT STOCKIDSEQUENCE.NEXTVAL AS ID FROM DUAL
</selectKey>
insert into PRODUCT (PRD_ID,PRD_DESCRIPTION)
values (#id#,#description#)
</insert>
<!— Microsoft SQL Server IDENTITY Column Example -->
<insert id="insertProduct-MS-SQL" parameterClass="com.domain.Product">
insert into PRODUCT (PRD_DESCRIPTION)
values (#description#)
<selectKey resultClass="int" >
SELECT @@IDENTITY AS ID
</selectKey>
</insert>
更具体内容参见帮助文档。
存储过程:
<parameterMap id="swapParameters" class="map" >
<parameter property="email1" jdbcType="VARCHAR" javaType="java.lang.String" mode="INOUT"/>
<parameter property="email2" jdbcType="VARCHAR" javaType="java.lang.String" mode="INOUT"/>
</parameterMap>
<procedure id="swapEmailAddresses" parameterMap="swapParameters" >
{call swap_email_address (?, ?)}
</procedure>
存储过程影响数据库表,同时也影响参数对象,mode若是INOUT或OUT,参数对象会被改变,否则不改变。
对于映射查询,iBatis建议:参数使用inline-parameter,也就是parameterClass(不是parameterMap),而结果使用resultMap(不是resultClass)。
parameterMap格式:
<parameterMap id=”parameterMapName” [class=”com.domain.Product”]>
<parameter property =”propertyName” [jdbcType=”VARCHAR”] [javaType=”string”]
[nullValue=“-9999”]
[typeName=”{REF or user-defined type}”]
[resultMap=someResultMap]
[mode=IN|OUT|INOUT]
[typeHandler=someTypeHandler]
[numericScale=2]/>
<parameter …… />
<parameter …… />
</parameterMap>
resultMap格式:
<resultMap id=”resultMapName” class=”some.domain.Class”
[extends=”parent-resultMap”]
[groupBy=“some property list”]>
<result property=”propertyName” column=”COLUMN_NAME”
[columnIndex=”1”] [javaType=”int”] [jdbcType=”NUMERIC”]
[nullValue=”-999999”] [select=”someOtherStatement”]
[resultMap=“someOtherResultMap”]
[typeHandler=“com.mydomain.MyTypehandler”]
/>
<result ……/>
<result ……/>
<result ……/>
</resultMap>
java.util.Map接口的实现类也可作为parameterMap或resultMap的class。
复杂查询:
N+1查询(结合cache的话会比连接查询好,没有cache很糟):
<resultMap id=”get-product-result” class=”com.ibatis.example.Product”>
<result property=”id” column=”PRD_ID”/>
<result property=”description” column=”PRD_DESCRIPTION”/>
<result property=”category” column=”PRD_CAT_ID ” select=”getCategory”/>
</resultMap>
<resultMap id=”get-category-result” class=”com.ibatis.example.Category”>
<result property=”id” column=”CAT_ID”/>
<result property=”description” column=”CAT_DESCRIPTION”/>
</resultMap>
<select id=”getProduct” parameterClass=”int” resultMap=”get-product-result”>
select * from PRODUCT where PRD_ID = #value#
</select>
<select id=”getCategory” parameterClass=”int” resultMap=”get-category-result”>
select * from CATEGORY where CAT_ID = #value#
</select>
连接查询(iBatis解决复杂查询的通常方式,但有cache时候可能不如N+1查询):
<resultMap id=”get-product-result” class=”com.ibatis.example.Product”>
<result property=”id” column=”PRD_ID”/>
<result property=”description” column=”PRD_DESCRIPTION”/>
<result property=”category.id” column=”CAT_ID ” />
<result property=”category.description” column=”CAT_DESCRIPTION ” />
</resultMap>
<select id=”getProduct” parameterClass=”int” resultMap=”get-product-result”>
select *
from PRODUCT, CATEGORY
where PRD_CAT_ID=CAT_ID
and PRD_ID = #value#
</select>
或者:
<resultMap id=”get-product-result” class=”com.ibatis.example.Product”>
<result property=”id” column=”PRD_ID”/>
<result property=”description” column=”PRD_DESCRIPTION”/>
<result property=”category” resultMap=“get-category-result” />
</resultMap>
<resultMap id=”get-category-result” class=”com.ibatis.example.Category”>
<result property=”id” column=”CAT_ID ” />
<result property=”description” column=”CAT_DESCRIPTION ” />
</resultMap>
<select id=”getProduct” parameterClass=”int” resultMap=”get-product-result”>
select *
from PRODUCT, CATEGORY
where PRD_CAT_ID=CAT_ID
and PRD_ID = #value#
</select>
复杂集合查询:
N+1法:
<resultMap id=”get-category-result” class=”com.ibatis.example.Category”>
<result property=”id” column=”CAT_ID”/>
<result property=”description” column=”CAT_DESCRIPTION”/>
<result property=”productList” column=”CAT_ID ” select=” getProductsByCatId”/>
</resultMap>
<resultMap id=”get-product-result” class=”com.ibatis.example.Product”>
<result property=”id” column=”PRD_ID”/>
<result property=”description” column=”PRD_DESCRIPTION”/>
</resultMap>
<select id=”getCategory” parameterClass=”int” resultMap=”get-category-result”>
select * from CATEGORY where CAT_ID = #value#
</select>
<select id=”getProductsByCatId” parameterClass=”int” resultMap=”get-product-result”>
select * from PRODUCT where PRD_CAT_ID = #value#
</select>
groupBy法:
<sqlMap namespace="ProductCategory">
<resultMap id=”categoryResult” class=”com.ibatis.example.Category” groupBy=”id”>
<result property=”id” column=”CAT_ID”/>
<result property=”description” column=”CAT_DESCRIPTION”/>
<result property=”productList” resultMap=”ProductCategory.productResult”/>
</resultMap>
<resultMap id=”productResult” class=”com.ibatis.example.Product”>
<result property=”id” column=”PRD_ID”/>
<result property=”description” column=”PRD_DESCRIPTION”/>
</resultMap>
<select id=”getCategory” parameterClass=”int” resultMap=”categoryResult”>
select C.CAT_ID, C.CAT_DESCRIPTION, P.PRD_ID, P.PRD_DESCRIPTION
from CATEGORY C
left outer join PRODUCT P
on C.CAT_ID = P.PRD_CAT_ID
where CAT_ID = #value#
</select>
</sqlMap>
注:groupBy属性不能和queryForPaginatedList()调用同时使用。
N+1查询中的符合查询:
<resultMap id=”get-order-result” class=”com.ibatis.example.Order”>
<result property=”id” column=”ORD_ID”/>
<result property=”customerId” column=”ORD_CST_ID”/>
…
<result property=”payments” column=”{itemId=ORD_ID, custId=ORD_CST_ID} ”
select=”getOrderPayments”/>
</resultMap>
<select id=”getOrderPayments” resultMap=”get-payment-result”>
select * from PAYMENT
where PAY_ORD_ID = #itemId#
and PAY_CST_ID = #custId#
</select>
数据类型表:
自定义TypeHandler:
public class YesNoBoolTypeHandlerCallback implements TypeHandlerCallback {
private static final String YES = "Y";
private static final String NO = "N";
public Object getResult(ResultGetter getter)
throws SQLException {
String s = getter.getString();
if (YES.equalsIgnoreCase(s)) {
return new Boolean (true);
} else if (NO.equalsIgnoreCase(s)) {
return new Boolean (false);
} else {
throw new SQLException (
"Unexpected value " + s + " found where " + YES + " or " + NO + " was expected.");
}
}
public void setParameter(ParameterSetter setter, Object parameter)
throws SQLException {
boolean b = ((Boolean)parameter).booleanValue();
if (b) {
setter.setString(YES);
} else {
setter.setString(NO);
}
}
public Object valueOf(String s) {
if (YES.equalsIgnoreCase(s)) {
return new Boolean (true);
} else {
return new Boolean (false);
}
}
然后在sqlMapConfig文件里加上如下配置:
<typeHandler
javaType="boolean"
jdbcType=”VARCHAR”
callback="org.apache.ibatis.sqlmap.extensions.YesNoBoolTypeHandlerCallback"/>
缓存设置:
<cacheModel id="product-cache" type ="LRU" readOnly=”true” serialize=”false”>
<flushInterval hours="24"/>
<flushOnExecute statement="insertProduct"/>
<flushOnExecute statement="updateProduct"/>
<flushOnExecute statement="deleteProduct"/>
<property name=”cache-size” value=”1000” />
</cacheModel>
<select id=”getProductList” cacheModel=”product-cache”>
select * from PRODUCT where PRD_CAT_ID = #value#
</select>
Readonly属性默认为true,但若想要在返回的对象上做更改的话,设为false。
Serialize属性设置为false后,该cache为全局共享cache,这种cache不能是readOnly的(因为没意义)。
Cache的种类:
MEMORY——java特殊引用类实现的cache
<cacheModel id="product-cache" type="MEMORY">
<flushInterval hours="24"/>
<flushOnExecute statement="insertProduct"/>
<flushOnExecute statement="updateProduct"/>
<flushOnExecute statement="deleteProduct"/>
<property name=”reference-type” value=”WEAK” />
</cacheModel>
所有的referencetype:
LRU——最少使用算法的cache:
<cacheModel id="product-cache" type="LRU">
<flushInterval hours="24"/>
<flushOnExecute statement="insertProduct"/>
<flushOnExecute statement="updateProduct"/>
<flushOnExecute statement="deleteProduct"/>
<property name=”size” value=”1000” />
</cacheModel>
FIFO——先进先出算法的cache:
<cacheModel id="product-cache" type="FIFO">
<flushInterval hours="24"/>
<flushOnExecute statement="insertProduct"/>
<flushOnExecute statement="updateProduct"/>
<flushOnExecute statement="deleteProduct"/>
<property name=”size” value=”1000” />
</cacheModel>
OSCACHE——插件cache,opensymphony的cache实现,参见:http://www.opensymphony.com/oscache/
<cacheModel id="product-cache" type="OSCACHE">
<flushInterval hours="24"/>
<flushOnExecute statement="insertProduct"/>
<flushOnExecute statement="updateProduct"/>
<flushOnExecute statement="deleteProduct"/>
</cacheModel>
构造动态SQL语句:
<dynamic>元素: 总的包含元素,并提供最外层的开头预设字、开始字符串和结束字符串。
prepend – 加在本体前面的预设字符 (可选)
open – 标识本体开始的开始字符串(可选)
close – 标识本题结束的结束字符串 (可选)
dynamic元素的removeFirstPrepend属性是强制的,所以它的第一个子元素的prepend字符串在生成语句的时候总是会被取消掉。
二元操作标记:
<isEqual> 检查一个属性和一个给定值或另一个属性是否相等。
<isNotEqual>检查一个属性和一个给定值或另一个属性是否不相等。
<isGreaterThan> 检查一个属性是否大于一个给定值或另一个属性。
<isGreaterEqual> 检查一个属性是否大于或等于一个给定值或另一个属性。
<isLessThan> 检查一个属性是否小于一个给定值或另一个属性。
<isLessEqual> 检查一个属性是否小于或等于一个给定值或另一个属性。
属性:
prepend – the overridable SQL part that will be prepended to the statement (optional)
property – the property to be compared (required)
compareProperty – the other property to be compared (required or compareValue)
compareValue – the value to be compared (required or compareProperty)
removeFirstPrepend – removes the prepend of the first nested content producing tag (true|false, optional)
open – the string with which to open the entire resulting body content (optional)
close – the string with which to close the entire resulting body content (optional)
举例:
<isLessEqual prepend=”AND” property=”age” compareValue=”18”>
ADOLESCENT = ‘TRUE’
</isLessEqual>
一元判断标记:
<isPropertyAvailable> Checks if a property is available (i.e is a property of the parameter bean)
<isNotPropertyAvailable> Checks if a property is unavailable (i.e not a property of the parameter bean)
<isNull> Checks if a property is null.
<isNotNull> Checks if a property is not null.
<isEmpty> Checks to see if the value of a Collection, String or String.valueOf() property
is null or empty (“” or size() < 1).
<isNotEmpty> Checks to see if the value of a Collection, String or String.valueOf() property
is not null and not empty (“” or size() < 1).
属性:
prepend – the overridable SQL part that will be prepended to the statement (optional)
property – the property to be checked (required)
removeFirstPrepend – removes the prepend of the first nested content producing tag (true|false, optional)
open – the string with which to open the entire resulting body content (optional)
close – the string with which to close the entire resulting body content (optional)
举例:
<isNotEmpty prepend=”AND” property=”firstName” >
FIRST_NAME=#firstName#
</isNotEmpty>
其它标记:
<isParameterPresent> Checks to see if the parameter object is present (not null).
<isNotParameterPresent> Checks to see if the parameter object is not present (null).
属性:
prepend – the overridable SQL part that will be prepended to the statement (optional)
removeFirstPrepend – removes the prepend of the first nested content producing tag (true|false, optional)
open – the string with which to open the entire resulting body content (optional)
close – the string with which to close the entire resulting body content (optional)
举例:
<isNotParameterPresent prepend=”AND”>
EMPLOYEE_TYPE = ‘DEFAULT’
</isNotParameterPresent>
<iterate> Iterates over a property that is an implementation java.util.Collection, or
java.util.Iterator, or is an array.
例:
<iterate prepend=”AND” property=”userNameList”
open=”(” close=”)” conjunction=”OR”>
username=#userNameList[]#
</iterate>
当集合类单独被当作参数传进此查询时,也可以这么写:
<iterate prepend=”AND” open=”(” close=”)” conjunction=”OR”>
username=#[]#
</iterate>
也可以像这样选择集合中对象的属性:
<iterate prepend=”AND” property=”userList”
open=”(” close=”)” conjunction=”OR”>
firstname=#userList[].firstName# and
lastname=#userList[].lastName#
</iterate>
该标签还可以自己嵌套:
<dynamic prepend="where">
<iterate property="orConditions" conjunction="or">
(
<iterate property="orConditions[].conditions"
conjunction="and">
$orConditions[].conditions[].condition$
#orConditions[].conditions[].value#
</iterate>
)
</iterate>
</dynamic>
若只是想生成一些简单的动态sql语句,这样写就可以:
<select id=”getProduct” resultMap=”get-product-result”>
select * from PRODUCT order by $preferredOrder$
</select>
注意“$”,不是“#”,这是和为preparedStatement赋值的普通属性的取得的不同之处。
转自:http://pf-miles.iteye.com/blog/82348
<?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=”Product”>
<cacheModel id=”productCache” type=”LRU”>
<flushInterval hours=”24”/>
<property name=”size” value=”1000” />
</cacheModel>
<typeAlias alias=”product” type=”com.ibatis.example.Product” />
<parameterMap id=”productParam” class=”product”>
<parameter property=”id”/>
</parameterMap>
<resultMap id=”productResult” class=”product”>
<result property=”id” column=”PRD_ID”/>
<result property=”description” column=”PRD_DESCRIPTION”/>
</resultMap>
<select id=”getProduct” parameterMap=”productParam”
resultMap=”productResult” cacheModel=”product-cache”>
select * from PRODUCT where PRD_ID = ?
</select>
</sqlMap>
映射的查询语句形式:
<statement id=”statementName”
[parameterClass=”some.class.Name”]
[resultClass=”some.class.Name”]
[parameterMap=”nameOfParameterMap”]
[resultMap=”nameOfResultMap”]
[cacheModel=”nameOfCache”]
[timeout=“5”]>
select * from PRODUCT where PRD_ID = [?|#propertyName#]
order by [$simpleDynamic$]
</statement>
<insert id=”insertTestProduct” >
insert into PRODUCT (PRD_ID, PRD_DESCRIPTION) values (1, “Shih Tzu”)
</insert>
<select id="getPersonsByAge" parameterClass=”int” resultClass="examples.domain.Person">
SELECT *
FROM PERSON
WHERE AGE <![CDATA[ > ]]> #value#
</select>
下面的表格说明了所有支持的查询语句形式及其属性:
可定义SQL Fragments对部分SQL语句进行复用:
<sql id="selectItem_fragment">
FROM items
WHERE parentid = 6
</sql>
<select id="selectItemCount" resultClass="int">
SELECT COUNT(*) AS total
<include refid="selectItem_fragment"/>
</select>
<select id="selectItems" resultClass="Item">
SELECT id, name
<include refid="selectItem_fragment"/>
</select>
对某些数据库的自动增长键有支持,但不同的支持策略,例:
<!—Oracle SEQUENCE Example -->
<insert id="insertProduct-ORACLE" parameterClass="com.domain.Product">
<selectKey resultClass="int" >
SELECT STOCKIDSEQUENCE.NEXTVAL AS ID FROM DUAL
</selectKey>
insert into PRODUCT (PRD_ID,PRD_DESCRIPTION)
values (#id#,#description#)
</insert>
<!— Microsoft SQL Server IDENTITY Column Example -->
<insert id="insertProduct-MS-SQL" parameterClass="com.domain.Product">
insert into PRODUCT (PRD_DESCRIPTION)
values (#description#)
<selectKey resultClass="int" >
SELECT @@IDENTITY AS ID
</selectKey>
</insert>
更具体内容参见帮助文档。
存储过程:
<parameterMap id="swapParameters" class="map" >
<parameter property="email1" jdbcType="VARCHAR" javaType="java.lang.String" mode="INOUT"/>
<parameter property="email2" jdbcType="VARCHAR" javaType="java.lang.String" mode="INOUT"/>
</parameterMap>
<procedure id="swapEmailAddresses" parameterMap="swapParameters" >
{call swap_email_address (?, ?)}
</procedure>
存储过程影响数据库表,同时也影响参数对象,mode若是INOUT或OUT,参数对象会被改变,否则不改变。
对于映射查询,iBatis建议:参数使用inline-parameter,也就是parameterClass(不是parameterMap),而结果使用resultMap(不是resultClass)。
parameterMap格式:
<parameterMap id=”parameterMapName” [class=”com.domain.Product”]>
<parameter property =”propertyName” [jdbcType=”VARCHAR”] [javaType=”string”]
[nullValue=“-9999”]
[typeName=”{REF or user-defined type}”]
[resultMap=someResultMap]
[mode=IN|OUT|INOUT]
[typeHandler=someTypeHandler]
[numericScale=2]/>
<parameter …… />
<parameter …… />
</parameterMap>
resultMap格式:
<resultMap id=”resultMapName” class=”some.domain.Class”
[extends=”parent-resultMap”]
[groupBy=“some property list”]>
<result property=”propertyName” column=”COLUMN_NAME”
[columnIndex=”1”] [javaType=”int”] [jdbcType=”NUMERIC”]
[nullValue=”-999999”] [select=”someOtherStatement”]
[resultMap=“someOtherResultMap”]
[typeHandler=“com.mydomain.MyTypehandler”]
/>
<result ……/>
<result ……/>
<result ……/>
</resultMap>
java.util.Map接口的实现类也可作为parameterMap或resultMap的class。
复杂查询:
N+1查询(结合cache的话会比连接查询好,没有cache很糟):
<resultMap id=”get-product-result” class=”com.ibatis.example.Product”>
<result property=”id” column=”PRD_ID”/>
<result property=”description” column=”PRD_DESCRIPTION”/>
<result property=”category” column=”PRD_CAT_ID ” select=”getCategory”/>
</resultMap>
<resultMap id=”get-category-result” class=”com.ibatis.example.Category”>
<result property=”id” column=”CAT_ID”/>
<result property=”description” column=”CAT_DESCRIPTION”/>
</resultMap>
<select id=”getProduct” parameterClass=”int” resultMap=”get-product-result”>
select * from PRODUCT where PRD_ID = #value#
</select>
<select id=”getCategory” parameterClass=”int” resultMap=”get-category-result”>
select * from CATEGORY where CAT_ID = #value#
</select>
连接查询(iBatis解决复杂查询的通常方式,但有cache时候可能不如N+1查询):
<resultMap id=”get-product-result” class=”com.ibatis.example.Product”>
<result property=”id” column=”PRD_ID”/>
<result property=”description” column=”PRD_DESCRIPTION”/>
<result property=”category.id” column=”CAT_ID ” />
<result property=”category.description” column=”CAT_DESCRIPTION ” />
</resultMap>
<select id=”getProduct” parameterClass=”int” resultMap=”get-product-result”>
select *
from PRODUCT, CATEGORY
where PRD_CAT_ID=CAT_ID
and PRD_ID = #value#
</select>
或者:
<resultMap id=”get-product-result” class=”com.ibatis.example.Product”>
<result property=”id” column=”PRD_ID”/>
<result property=”description” column=”PRD_DESCRIPTION”/>
<result property=”category” resultMap=“get-category-result” />
</resultMap>
<resultMap id=”get-category-result” class=”com.ibatis.example.Category”>
<result property=”id” column=”CAT_ID ” />
<result property=”description” column=”CAT_DESCRIPTION ” />
</resultMap>
<select id=”getProduct” parameterClass=”int” resultMap=”get-product-result”>
select *
from PRODUCT, CATEGORY
where PRD_CAT_ID=CAT_ID
and PRD_ID = #value#
</select>
复杂集合查询:
N+1法:
<resultMap id=”get-category-result” class=”com.ibatis.example.Category”>
<result property=”id” column=”CAT_ID”/>
<result property=”description” column=”CAT_DESCRIPTION”/>
<result property=”productList” column=”CAT_ID ” select=” getProductsByCatId”/>
</resultMap>
<resultMap id=”get-product-result” class=”com.ibatis.example.Product”>
<result property=”id” column=”PRD_ID”/>
<result property=”description” column=”PRD_DESCRIPTION”/>
</resultMap>
<select id=”getCategory” parameterClass=”int” resultMap=”get-category-result”>
select * from CATEGORY where CAT_ID = #value#
</select>
<select id=”getProductsByCatId” parameterClass=”int” resultMap=”get-product-result”>
select * from PRODUCT where PRD_CAT_ID = #value#
</select>
groupBy法:
<sqlMap namespace="ProductCategory">
<resultMap id=”categoryResult” class=”com.ibatis.example.Category” groupBy=”id”>
<result property=”id” column=”CAT_ID”/>
<result property=”description” column=”CAT_DESCRIPTION”/>
<result property=”productList” resultMap=”ProductCategory.productResult”/>
</resultMap>
<resultMap id=”productResult” class=”com.ibatis.example.Product”>
<result property=”id” column=”PRD_ID”/>
<result property=”description” column=”PRD_DESCRIPTION”/>
</resultMap>
<select id=”getCategory” parameterClass=”int” resultMap=”categoryResult”>
select C.CAT_ID, C.CAT_DESCRIPTION, P.PRD_ID, P.PRD_DESCRIPTION
from CATEGORY C
left outer join PRODUCT P
on C.CAT_ID = P.PRD_CAT_ID
where CAT_ID = #value#
</select>
</sqlMap>
注:groupBy属性不能和queryForPaginatedList()调用同时使用。
N+1查询中的符合查询:
<resultMap id=”get-order-result” class=”com.ibatis.example.Order”>
<result property=”id” column=”ORD_ID”/>
<result property=”customerId” column=”ORD_CST_ID”/>
…
<result property=”payments” column=”{itemId=ORD_ID, custId=ORD_CST_ID} ”
select=”getOrderPayments”/>
</resultMap>
<select id=”getOrderPayments” resultMap=”get-payment-result”>
select * from PAYMENT
where PAY_ORD_ID = #itemId#
and PAY_CST_ID = #custId#
</select>
数据类型表:
自定义TypeHandler:
public class YesNoBoolTypeHandlerCallback implements TypeHandlerCallback {
private static final String YES = "Y";
private static final String NO = "N";
public Object getResult(ResultGetter getter)
throws SQLException {
String s = getter.getString();
if (YES.equalsIgnoreCase(s)) {
return new Boolean (true);
} else if (NO.equalsIgnoreCase(s)) {
return new Boolean (false);
} else {
throw new SQLException (
"Unexpected value " + s + " found where " + YES + " or " + NO + " was expected.");
}
}
public void setParameter(ParameterSetter setter, Object parameter)
throws SQLException {
boolean b = ((Boolean)parameter).booleanValue();
if (b) {
setter.setString(YES);
} else {
setter.setString(NO);
}
}
public Object valueOf(String s) {
if (YES.equalsIgnoreCase(s)) {
return new Boolean (true);
} else {
return new Boolean (false);
}
}
然后在sqlMapConfig文件里加上如下配置:
<typeHandler
javaType="boolean"
jdbcType=”VARCHAR”
callback="org.apache.ibatis.sqlmap.extensions.YesNoBoolTypeHandlerCallback"/>
缓存设置:
<cacheModel id="product-cache" type ="LRU" readOnly=”true” serialize=”false”>
<flushInterval hours="24"/>
<flushOnExecute statement="insertProduct"/>
<flushOnExecute statement="updateProduct"/>
<flushOnExecute statement="deleteProduct"/>
<property name=”cache-size” value=”1000” />
</cacheModel>
<select id=”getProductList” cacheModel=”product-cache”>
select * from PRODUCT where PRD_CAT_ID = #value#
</select>
Readonly属性默认为true,但若想要在返回的对象上做更改的话,设为false。
Serialize属性设置为false后,该cache为全局共享cache,这种cache不能是readOnly的(因为没意义)。
Cache的种类:
MEMORY——java特殊引用类实现的cache
<cacheModel id="product-cache" type="MEMORY">
<flushInterval hours="24"/>
<flushOnExecute statement="insertProduct"/>
<flushOnExecute statement="updateProduct"/>
<flushOnExecute statement="deleteProduct"/>
<property name=”reference-type” value=”WEAK” />
</cacheModel>
所有的referencetype:
LRU——最少使用算法的cache:
<cacheModel id="product-cache" type="LRU">
<flushInterval hours="24"/>
<flushOnExecute statement="insertProduct"/>
<flushOnExecute statement="updateProduct"/>
<flushOnExecute statement="deleteProduct"/>
<property name=”size” value=”1000” />
</cacheModel>
FIFO——先进先出算法的cache:
<cacheModel id="product-cache" type="FIFO">
<flushInterval hours="24"/>
<flushOnExecute statement="insertProduct"/>
<flushOnExecute statement="updateProduct"/>
<flushOnExecute statement="deleteProduct"/>
<property name=”size” value=”1000” />
</cacheModel>
OSCACHE——插件cache,opensymphony的cache实现,参见:http://www.opensymphony.com/oscache/
<cacheModel id="product-cache" type="OSCACHE">
<flushInterval hours="24"/>
<flushOnExecute statement="insertProduct"/>
<flushOnExecute statement="updateProduct"/>
<flushOnExecute statement="deleteProduct"/>
</cacheModel>
构造动态SQL语句:
<dynamic>元素: 总的包含元素,并提供最外层的开头预设字、开始字符串和结束字符串。
prepend – 加在本体前面的预设字符 (可选)
open – 标识本体开始的开始字符串(可选)
close – 标识本题结束的结束字符串 (可选)
dynamic元素的removeFirstPrepend属性是强制的,所以它的第一个子元素的prepend字符串在生成语句的时候总是会被取消掉。
二元操作标记:
<isEqual> 检查一个属性和一个给定值或另一个属性是否相等。
<isNotEqual>检查一个属性和一个给定值或另一个属性是否不相等。
<isGreaterThan> 检查一个属性是否大于一个给定值或另一个属性。
<isGreaterEqual> 检查一个属性是否大于或等于一个给定值或另一个属性。
<isLessThan> 检查一个属性是否小于一个给定值或另一个属性。
<isLessEqual> 检查一个属性是否小于或等于一个给定值或另一个属性。
属性:
prepend – the overridable SQL part that will be prepended to the statement (optional)
property – the property to be compared (required)
compareProperty – the other property to be compared (required or compareValue)
compareValue – the value to be compared (required or compareProperty)
removeFirstPrepend – removes the prepend of the first nested content producing tag (true|false, optional)
open – the string with which to open the entire resulting body content (optional)
close – the string with which to close the entire resulting body content (optional)
举例:
<isLessEqual prepend=”AND” property=”age” compareValue=”18”>
ADOLESCENT = ‘TRUE’
</isLessEqual>
一元判断标记:
<isPropertyAvailable> Checks if a property is available (i.e is a property of the parameter bean)
<isNotPropertyAvailable> Checks if a property is unavailable (i.e not a property of the parameter bean)
<isNull> Checks if a property is null.
<isNotNull> Checks if a property is not null.
<isEmpty> Checks to see if the value of a Collection, String or String.valueOf() property
is null or empty (“” or size() < 1).
<isNotEmpty> Checks to see if the value of a Collection, String or String.valueOf() property
is not null and not empty (“” or size() < 1).
属性:
prepend – the overridable SQL part that will be prepended to the statement (optional)
property – the property to be checked (required)
removeFirstPrepend – removes the prepend of the first nested content producing tag (true|false, optional)
open – the string with which to open the entire resulting body content (optional)
close – the string with which to close the entire resulting body content (optional)
举例:
<isNotEmpty prepend=”AND” property=”firstName” >
FIRST_NAME=#firstName#
</isNotEmpty>
其它标记:
<isParameterPresent> Checks to see if the parameter object is present (not null).
<isNotParameterPresent> Checks to see if the parameter object is not present (null).
属性:
prepend – the overridable SQL part that will be prepended to the statement (optional)
removeFirstPrepend – removes the prepend of the first nested content producing tag (true|false, optional)
open – the string with which to open the entire resulting body content (optional)
close – the string with which to close the entire resulting body content (optional)
举例:
<isNotParameterPresent prepend=”AND”>
EMPLOYEE_TYPE = ‘DEFAULT’
</isNotParameterPresent>
<iterate> Iterates over a property that is an implementation java.util.Collection, or
java.util.Iterator, or is an array.
例:
<iterate prepend=”AND” property=”userNameList”
open=”(” close=”)” conjunction=”OR”>
username=#userNameList[]#
</iterate>
当集合类单独被当作参数传进此查询时,也可以这么写:
<iterate prepend=”AND” open=”(” close=”)” conjunction=”OR”>
username=#[]#
</iterate>
也可以像这样选择集合中对象的属性:
<iterate prepend=”AND” property=”userList”
open=”(” close=”)” conjunction=”OR”>
firstname=#userList[].firstName# and
lastname=#userList[].lastName#
</iterate>
该标签还可以自己嵌套:
<dynamic prepend="where">
<iterate property="orConditions" conjunction="or">
(
<iterate property="orConditions[].conditions"
conjunction="and">
$orConditions[].conditions[].condition$
#orConditions[].conditions[].value#
</iterate>
)
</iterate>
</dynamic>
若只是想生成一些简单的动态sql语句,这样写就可以:
<select id=”getProduct” resultMap=”get-product-result”>
select * from PRODUCT order by $preferredOrder$
</select>
注意“$”,不是“#”,这是和为preparedStatement赋值的普通属性的取得的不同之处。
转自:http://pf-miles.iteye.com/blog/82348
相关推荐
《iBatis学习资料汇总》 iBatis,作为一个轻量级的持久层框架,它在Java开发领域中扮演着重要的角色。这个框架允许开发者将SQL语句与Java代码分离,提高了开发效率并降低了维护成本。本文将深入探讨iBatis的核心...
IBatis3.0学习IBatis3.0学习IBatis3.0学习IBatis3.0学习IBatis3.0学习IBatis3.0学习IBatis3.0学习
### 关于ibatis学习文档的关键知识点 #### 一、ibatis简介 - **定义**:ibatis是一个开源的持久层框架,它简化了Java应用程序与数据库之间的交互过程,但相较于Hibernate等其他ORM(对象关系映射)框架,ibatis更...
标题"ibatis学习锦集"表明这是一个关于iBatis学习资源的集合,涵盖了多种学习材料,可能包括文档、示例代码、教程等。描述中提到"很全面!很强大!IBATIS最新最全开发指南 - 通俗易懂IBATIS教程,ibatis基础,ibatis...
标题 "ibatis学习IBATIS好资料" 涉及的核心知识点是关于iBATIS,一个流行的开源Java持久层框架,它将SQL映射到Java对象,实现了数据访问层(DAL)的简化。这个资源包提供了对iBATIS深入学习的各种材料,包括jar包和...
### iBatis 学习笔记知识点总结 #### 一、iBatis 概念与特点 **1.1 iBatis 定义** - **iBatis** 是一个基于 Java 的开源持久层框架,它专注于 SQL 映射,提供了一种将对象与数据库交互过程中的 SQL 语句进行分离的...
2. **Ibatis学习指南**:这可能是中文版的学习资料,针对初学者提供了详尽的入门教程和进阶指南。学习指南通常会涵盖Ibatis的基本概念,如SqlMapConfig.xml配置文件、Mapper接口的使用、SqlSession的操作,以及...
标题 "ibatis学习小例子" 暗示我们即将探讨的是关于Ibatis,一个轻量级的Java持久层框架,它允许开发者将SQL语句直接嵌入到Java代码中,从而简化了数据库操作。Ibatis提供了灵活的映射机制,避免了对JDBC的繁琐处理...
【ibatis学习总结】 在Java应用中,ORM(对象关系映射)框架是连接数据库与业务逻辑的关键。相比像Hibernate和Apache OJB这样的"一站式"ORM解决方案,iBATIS提供了一种更为灵活的"半自动化"策略。iBATIS允许开发者...
IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得
【ibatis学习资料及个人学习笔记】 Ibatis,作为一个轻量级的持久层框架,它在Java开发领域中占有重要地位。本资料包是针对Ibatis的学习资源集合,旨在帮助初学者快速掌握这一强大的数据库操作工具。Ibatis的核心...
【标题】"ibatis学习时的项目"是一个基于Java的Web项目,主要目的是为了学习和实践iBatis这个持久层框架。iBatis是MyBatis的前身,它提供了一个SQL映射框架,使得开发者可以将SQL语句直接写在XML配置文件中,实现了...
这篇“ibatis学习笔记(一)”可能是博主对Ibatis基础概念、安装配置以及基本使用的介绍,让我们通过标签“源码”和“工具”来深入探讨Ibatis的相关知识。 首先,Ibatis是一个轻量级的Java ORM(对象关系映射)框架...
### ibatis 学习小结笔记 #### 一、ibatis 概述 ibatis 是一个基于 Java 的持久层框架,它提供了一种简便的方式来处理关系型数据库与 Java 对象之间的映射(O/R Mapping)。ibatis 在设计上强调的是 SQL 语句的...
本教程“关于ibatis学习入门的小实例”旨在帮助初学者快速理解并掌握Ibatis的基本用法和核心概念。以下是一些关键知识点的详细介绍: 1. **配置文件**:Ibatis的配置文件(myibatis-config.xml)是整个系统的入口,...
这份学习资料旨在为初学者和有经验的开发者提供全面的iBatis学习资源。通过这些资料,你可以了解到如何在项目中有效地利用iBatis进行数据库操作,包括但不限于SQL映射、动态SQL、事务管理以及对象关系映射等关键概念...
【Ibatis 学习资源详解】 Ibatis 是一个优秀的持久层框架,它允许开发者将 SQL 语句直接写在配置文件中,与 Java 代码分离,使得 SQL 语句的编写和维护更加方便。本学习资料针对 Ibatis 的基本操作,包括增、删、改...
本指南是为初学者设计的,旨在提供一个简洁明了的Ibatis学习路径。 一、Ibatis简介 Ibatis的核心理念是将SQL语句和业务逻辑分离,这样可以使得代码更易于维护和扩展。它避免了JDBC的繁琐过程,如建立和关闭连接、预...
本文旨在深入解析iBATIS映射文件的基础概念及其重要性,为初学者构建一个清晰的学习路径。 ### iBATIS映射文件:核心中的核心 iBATIS映射文件(Data Map XML File)是iBATIS框架的灵魂所在,它允许开发者明确地...