ibatis动态SQL文的使用

来源:互联网 发布:Java诺基亚 编辑:程序博客网 时间:2024/04/29 02:20

ibatis核心文件SqlMapConfig.xml的使用

――――――――――――――――――――――――――――

1<properties resource="jdbc.properties" /> 

使用参数文件,定义好数据库连接的相关信息,包括DB类型,用户名、密码

 

2<settings>元素的配置,这个元素即设置iBatis的全局配置信息。一般情况使用默认设置即可。

3<typeAlias>元素,给特别长的类名起一个比较短的别名。

4<typeAlias alias="User"type="ibatis.model.User" /> 

5、transactionManager元素做事务。iBatis内置的事务管理器有JDBC,JTA和EXTERNAL。EXTERNAL表示事务管理器是应用程序本身负责,而不是iBatis。

使用type属性就能在transactionManager元素中配置事务管理<transactionManager type="JDBC"></transactionManager> 


Ibatis xml文件里的一元、二元表达式:

Ibatis中的动态SQL,主要分为一元条件和二元条件查询:

一元条件查询关键字:
<isPropertyAvailable> 如果参数有使用则查询条件有效。
<isNotPropertyAvailable>  如果参数没有使用则查询条件有效。
<isNull>      如果参数为NULL则查询条件有效。
<isNotNull>   如果参数不为NULL则查询条件有效。
<isEmpty>     如果参数为空则查询条件有效。
<isNotEmpty>  如果参数不为空则查询条件有效。参数的数据类型为Collection
String 时参数不为NULL或“”。如下所示:
<isNotEmpty prepend=”AND” property=”firstName” >
FIRST_NAME=#firstName#
</isNotEmpty>
一元元素的属性:
prepend:可被覆盖的SQL语句组成部分,添加在语句的前面。
property:被比较的属性

二元条件查询关键字:<isEqual>
如果参数相等于值则查询条件有效。
<isNotEqual>
如果参数不等于值则查询条件有效。
<isGreaterThan>
如果参数大于值则查询条件有效。
<isGreaterEqual>
如果参数等于值则查询条件有效。
<isLessEqual>
如果参数小于值则查询条件有效。如下所示:
<isLessEqual prepend = ”AND” property = ”age” compareValue = ”18” >
ADOLESCENT = ‘TRUE’
</isLessEqual>
二元元素的属性:
prepend:  可被覆盖的SQL语句组成部分,添加在语句的前面。
property:  被比较的属性
compareProperty: 另一个被比较的属性
compareValue:    被比较的值
此外,还有其它的元素:
1.<isParameterPresent>
如果参数类不为NULL则查询条件有效。
<isNotParameterPresent>
Checks to see if the parameter object is not present (null).
实例:
<isNotParameterPresent prepend=”AND”>
EMPLOYEE_TYPE = ‘DEFAULT’
</isNotParameterPresent>[/size]

 

1、普通的一个查询SQL文语句

 

<!--动态条件分页查询-->
        <sql id="sql_count">
                selectcount(*)
        </sql>
        <sql id="sql_select">
                select*
        </sql>
        <sql id="sql_where">
                fromicp
                <dynamicprepend="where">
                        <isNotEmptyprepend="and" property="name">
                                namelike '%$name$%'
                        </isNotEmpty>
                </dynamic>
                <dynamic prepend="">
                        <isNotNullproperty="_start">
                                <isNotNullproperty="_size">
                                        limit#_start#, #_size#
                                </isNotNull>
                        </isNotNull>
                </dynamic>
        </sql>

<!—注意输入参数和返回值类型的使用-->
        <selectid="findByParamsForCount"parameterClass="map"resultClass="int">
                <includerefid="sql_count"/>
                <includerefid="sql_where"/>
        </select>
        <selectid="findByParams" parameterClass="map" resultMap="icp.result_base">
                <includerefid="sql_select"/>
                <includerefid="sql_where"/>
        </select>

 

2、涉及数值范围比较

主要使用了转义标签,防止SQL文中的字符与XML语法相冲突

 <isNotEmptyprepend="and" property="_img_size_ge">
                                <![CDATA[
                                img_size>= #_img_size_ge#
                        ]]>
</isNotEmpty>

 

3、in查询的使用

注意范围值不能再使用#号了,而是使用“$”符号

  <isNotEmptyprepend="and" property="_in_state">
                                statein ('$_in_state$')
                        </isNotEmpty>

 

3、like查询的使用

因为like所涉及的范围,也有可能是一串字符,所以%之间也需要加$来进行区分

<isNotEmptyprepend="and" property="chnameone">
    (chnameone like '%$chnameone$%' or spellinitial like'%$chnameone$%')
</isNotEmpty>
                        

 

 

4、or运算符的使用

注意isEqual的用法

  <isEqualprepend="and" property="_exeable" compareValue="N">
                                <![CDATA[
                                (t.finished='11'    ort.failure=3)
                        ]]>
                        </isEqual>

 


原创粉丝点击