`
zhangfeilo
  • 浏览: 397451 次
  • 性别: Icon_minigender_1
  • 来自: 昆明
社区版块
存档分类
最新评论

iBatis动态SQL标签用法

阅读更多

1、动态SQL片段
通过SQL片段达到代码复用
        <!-- 动态条件分页查询 --> 
        <sql id="sql_count"> 
                select count(*) 
        </sql> 
        <sql id="sql_select"> 
                select * 
        </sql> 
        <sql id="sql_where"> 
                from icp 
                <dynamic prepend="where"> 
                        <isNotEmpty prepend="and" property="name"> 
                                name like '%$name$%' 
                        </isNotEmpty> 
                        <isNotEmpty prepend="and" property="path"> 
                                path like '%path$%' 
                        </isNotEmpty> 
                        <isNotEmpty prepend="and" property="area_id"> 
                                area_id = #area_id# 
                        </isNotEmpty> 
                        <isNotEmpty prepend="and" property="hided"> 
                                hided = #hided# 
                        </isNotEmpty> 
                </dynamic> 
                <dynamic prepend=""> 
                        <isNotNull property="_start"> 
                                <isNotNull property="_size"> 
                                        limit #_start#, #_size# 
                                </isNotNull> 
                        </isNotNull> 
                </dynamic> 
        </sql> 
        <select id="findByParamsForCount" parameterClass="map" resultClass="int"> 
                <include refid="sql_count"/> 
                <include refid="sql_where"/> 
        </select> 
        <select id="findByParams" parameterClass="map" resultMap="icp.result_base"> 
                <include refid="sql_select"/> 
                <include refid="sql_where"/> 
        </select>
 
2、数字范围查询
所传参数名称是捏造所得,非数据库字段,比如_img_size_ge、_img_size_lt字段
                        <isNotEmpty prepend="and" property="_img_size_ge"> 
                                <![CDATA[ 
                                img_size >= #_img_size_ge# 
                        ]]> 
                        </isNotEmpty> 
                        <isNotEmpty prepend="and" property="_img_size_lt"> 
                                <![CDATA[ 
                                img_size < #_img_size_lt# 
                        ]]> 
                        </isNotEmpty> 
 
多次使用一个参数也是允许的
                        <isNotEmpty prepend="and" property="_now"> 
                                <![CDATA[ 
                                            execplantime >= #_now# 
                                     ]]> 
                        </isNotEmpty> 
                        <isNotEmpty prepend="and" property="_now"> 
                                <![CDATA[ 
                                            closeplantime <= #_now# 
                                     ]]> 
                        </isNotEmpty>
 
3、时间范围查询
                        <isNotEmpty prepend="" property="_starttime"> 
                                <isNotEmpty prepend="and" property="_endtime"> 
                                        <![CDATA[ 
                                        createtime >= #_starttime# 
                                        and createtime < #_endtime# 
                                 ]]> 
                                </isNotEmpty> 
                        </isNotEmpty> 
 
4、in查询
                        <isNotEmpty prepend="and" property="_in_state"> 
                                state in ('$_in_state$') 
                        </isNotEmpty>
 
5、like查询
                        <isNotEmpty prepend="and" property="chnameone"> 
                                (chnameone like '%$chnameone$%' or spellinitial like '%$chnameone$%') 
                        </isNotEmpty> 
                        <isNotEmpty prepend="and" property="chnametwo"> 
                                chnametwo like '%$chnametwo$%' 
                        </isNotEmpty> 
 
6、or条件
                        <isEqual prepend="and" property="_exeable" compareValue="N"> 
                                <![CDATA[ 
                                (t.finished='11'    or t.failure=3) 
                        ]]> 
                        </isEqual>
 
                        <isEqual prepend="and" property="_exeable" compareValue="Y"> 
                                <![CDATA[ 
                                t.finished in ('10','19') and t.failure<3 
                        ]]> 
                        </isEqual>
 
7、where子查询
                        <isNotEmpty prepend="" property="exprogramcode"> 
                                <isNotEmpty prepend="" property="isRational"> 
                                        <isEqual prepend="and" property="isRational" compareValue="N"> 
                                                code not in 
                                                (select t.contentcode 
                                                from cms_ccm_programcontent t 
                                                where t.contenttype='MZNRLX_MA' 
                                                and t.programcode = #exprogramcode#) 
                                        </isEqual> 
                                </isNotEmpty> 
                        </isNotEmpty>
 
        <select id="findByProgramcode" parameterClass="string" resultMap="cms_ccm_material.result"> 
                select * 
                from cms_ccm_material 
                where code in 
                (select t.contentcode 
                from cms_ccm_programcontent t 
                where t.contenttype = 'MZNRLX_MA' 
                and programcode = #value#) 
                order by updatetime desc 
        </select>
 
9、函数的使用
        <!-- 添加 --> 
        <insert id="insert" parameterClass="RuleMaster"> 
                insert into rulemaster( 
                name, 
                createtime, 
                updatetime, 
                remark 
                ) values ( 
                #name#, 
                now(), 
                now(), 
                #remark# 
                ) 
                <selectKey keyProperty="id" resultClass="long"> 
                        select LAST_INSERT_ID() 
                </selectKey> 
        </insert> 
        <!-- 更新 --> 
        <update id="update" parameterClass="RuleMaster"> 
                update rulemaster set 
                name = #name#, 
                updatetime = now(), 
                remark = #remark# 
                where id = #id# 
        </update>
 
10、map结果集 
        <!-- 动态条件分页查询 --> 
        <sql id="sql_count"> 
                select count(a.*) 
        </sql> 
        <sql id="sql_select"> 
                select a.id                vid, 
                a.img             imgurl, 
                a.img_s         imgfile, 
                b.vfilename vfilename, 
    b.name            name, 
                c.id                sid, 
                c.url             url, 
                c.filename    filename, 
                c.status        status 
        </sql> 
        <sql id="sql_where"> 
                From secfiles c, juji b, videoinfo a 
                where 
                a.id = b. videoid 
                and b.id = c.segmentid 
                and c.status = 0 
                order by a.id asc,b.id asc,c.sortnum asc 
                <dynamic prepend=""> 
                        <isNotNull property="_start"> 
                                <isNotNull property="_size"> 
                                        limit #_start#, #_size# 
                                </isNotNull> 
                        </isNotNull> 
                </dynamic> 
        </sql> 
        <!-- 返回没有下载的记录总数 --> 
        <select id="getUndownFilesForCount" parameterClass="map" resultClass="int"> 
                <include refid="sql_count"/> 
                <include refid="sql_where"/> 
        </select> 
        <!-- 返回没有下载的记录 --> 
        <select id="getUndownFiles" parameterClass="map" resultClass="java.util.HashMap"> 
                <include refid="sql_select"/> 
                <include refid="sql_where"/> 
        </select>

分享到:
评论

相关推荐

    ibatis动态SQL标签用法

    iBatis动态SQL标签用法 iBatis是Java持久层框架,提供了动态SQL标签来实现动态查询。动态SQL标签可以根据不同的条件生成不同的SQL语句,从而提高查询效率和灵活性。 动态SQL片段 iBatis提供了动态SQL片段的功能,...

    ibatis打印sql

    标题 "ibatis打印sql" 涉及到的是在使用iBATIS(一个轻量级的持久层框架)时如何调试和查看SQL语句的方法。iBATIS允许开发者编写SQL语句并将其与Java代码集成,以实现灵活的数据访问。在开发过程中,为了调试和优化...

    iBATIS-SqlMaps

    提供的两个文件"iBATIS-SqlMaps-2_cn.pdf"和"iBATIS-SqlMaps-2-Tutorial_cn.pdf"很可能是iBATIS SqlMaps的中文教程和指南,它们可以帮助读者深入理解iBATIS 的工作原理和使用方法,包括基本概念、配置、API使用、...

    ibatis-SqlMaps-开发指南-version 1.0 及 ibatis-SqlMaps-开发指南-version 2

    4. **动态SQL**:展示了如何使用iBATIS的动态SQL特性,通过`&lt;if&gt;`、`&lt;choose&gt;`、`&lt;when&gt;`、`&lt;otherwise&gt;`等标签来编写灵活的查询条件。 5. **结果映射**:介绍了如何映射SQL查询结果到Java对象,包括基本类型、复杂...

    Ibatis常用sql语句

    根据给定的文件信息,以下是对“Ibatis常用...Ibatis通过其动态SQL标签如`iterate`, `isNotNull`, `dynamic`等提供了极高的灵活性,能够有效应对复杂多变的业务需求。掌握这些基本用法对于提升Ibatis开发效率至关重要。

    iBATIS-SqlMaps详解

    在iBATIS中,SQL语句可以动态化,通过使用、、、等标签实现条件判断,极大地增强了SQL的灵活性。例如,你可以根据传入的参数动态决定是否添加某个WHERE子句。 除了基本的SQL操作,iBATIS还支持存储过程的调用。通过...

    iBATIS-SqlMaps-2 PDF文档资料中文

    2. **SqlMap设计**:讲解SqlMap XML文件的结构和元素,如`&lt;sql&gt;`、`&lt;select&gt;`、`&lt;insert&gt;`、`&lt;update&gt;`和`&lt;delete&gt;`标签的用法,以及动态SQL的实现。 3. **参数映射**:讨论如何使用`&lt;parameterMap&gt;`和`&lt;resultMap&gt;...

    ibatis sql 语句的编写

    通过本文的学习,我们了解了ibatis的基本使用方法,包括如何编写各种类型的SQL语句,并掌握了动态SQL的相关技巧。这些知识对于初学者来说是非常有用的,能够帮助他们在实际项目中更加灵活地处理数据库操作问题。随着...

    iBATIS-SqlMaps-2_cn.rar

    5. **动态SQL**:探索 iBATIS 如何通过`&lt;if&gt;`、`&lt;choose&gt;`、`&lt;when&gt;`、`&lt;otherwise&gt;`等标签实现条件判断和动态SQL生成。 6. **事务管理**:理解 iBATIS 的事务控制机制,包括自动和手动提交、回滚和隔离级别设置。 ...

    ibatis的dynamicSQL中,关于prepend的使用

    在探讨ibatis中的动态SQL(Dynamic SQL)及`prepend`的使用时,我们首先需要对ibatis有一个基本的理解。ibatis是一种开源的数据访问层框架,它简化了Java应用程序与数据库之间的交互过程。通过使用XML配置文件来定义...

    iBATIS-SqlMaps中文教程集合

    - 动态SQL:讲解iBATIS的动态SQL功能,如if、choose、where、trim等标签,实现灵活的SQL构建。 - ResultMap与AutoMapping:探讨ResultMap如何映射复杂的结果集,以及自动映射机制的使用。 - ParameterMap与参数...

    ibatis sql语句对条件中特殊字符% # 处理

    通过对ibatis框架下SQL语句中特殊字符处理方法的详细分析,我们可以看到,合理地使用字符串转义处理结合动态SQL标签,可以有效地解决特殊字符带来的问题。这样不仅可以保证查询的准确性,还可以提高代码的健壮性和...

    sqlserver Ibatis XML自动生成工具

    SQLServer Ibatis XML自动生成工具是一款实用的开发辅助软件,主要针对Java开发人员,特别是那些在项目中使用Ibatis作为持久层框架的开发者。这款工具能够显著提高开发效率,通过自动化的方式生成Ibatis所需的XML...

    iBATIS SQL Maps 开发指南

    iBATIS SQL Maps 开发指南是一本专注于介绍iBATIS SQL Map用法的详细教程,旨在帮助开发者深入了解和高效利用这一强大的数据访问框架。iBATIS SQL Map是Java开发中的一个关键组件,它允许程序员将SQL语句与Java代码...

    ibatis +sql2005 一个小项目

    3. **动态SQL**:iBATIS支持动态SQL,可以通过条件标签(如`if`, `where`, `choose`, `when`, `otherwise`等)来构建灵活的SQL语句,根据实际传入参数决定SQL的结构。 4. **接口绑定**:在Java代码中,我们可以创建...

    ibatis(mybatis)的学习工程 带详细注释

    通过这个学习工程,你可以逐步了解并掌握MyBatis的基本使用方法,以及如何结合实际业务编写SQL映射文件和Java代码。它不仅涵盖了MyBatis的基础功能,还涉及到一些高级特性,如缓存、拦截器等。有了这个项目作为参考...

    IBATIS SQL Maps开发指北

    提供的"ibatis 开发指南.pdf"应包含了关于如何创建和使用SQL Maps的详细步骤,包括配置环境、编写SQL Maps文件、调用SQL Maps方法等,是学习和使用IBATIS的重要参考资料。 **8. 实战应用** "分页资料.txt", "ibatis...

    ibatis总结 ibatis ibatis ibatis ibatis

    在SQL映射文件中,可以使用动态SQL来处理复杂的查询条件,如`&lt;if&gt;`、`&lt;choose&gt;`、`&lt;when&gt;`、`&lt;otherwise&gt;`等标签。 2. 数据库代码规范: - 在编写SQL时,应避免使用全模糊匹配`LIKE '%text%'`,而应尽可能使用带有...

    iBATIS SQL Maps 入门教程.rar

    本入门教程将引导您逐步掌握iBATIS的核心概念和使用方法,让数据库交互不再成为开发过程中的难题。 1. **iBATIS 概述** iBATIS 是一个轻量级的Java框架,它主要负责数据库的访问。通过XML配置文件或注解,开发者...

Global site tag (gtag.js) - Google Analytics