mybatis中SQL块使用/动态条件查询
2016-10-20 18:06
561 查看
<!-- 字段 --> <sql id="field"> `id`, `username`, `password` </sql>
使用:
<include refid="field"/> <pre name="code" class="java"><!-- 通过一个ID搜索 --> <select id="getById" parameterType="int" resultType="UserPojo"> SELECT <include refid="field"/> FROM user WHERE id = #{id} </select>
或者
<sql id="dynamicCondition"> <if test="templateType != null"> AND r.template_type=#{templateType} </if> <if test="roleType != null"> AND r.role_type=#{roleType} </if> <if test="trialId != null"> AND r.trial_id= #{trialId} </if> <if test="status != null"> AND r.status = #{status} </if> <if test="typeId != null"> AND r.type_id= #{typeId} </if> <if test="keyWords != null"> AND r.org_name like #{keyWords} </if> <if test="schoolType != null"> AND r.school_type = #{schoolType} </if> <if test="orgId != null "> AND r.org_id = #{orgId} </if> <if test="orgIdList != null "> AND r.org_id in (${orgIdList}) </if> </sql>
使用:
<select id="getCountyListByCityId" resultMap="reportVOMap" parameterType="map"> select DISTINCT(r.org_id) ,r.org_name,r.view_url from tarea t INNER JOIN report_divided_html_liususu r on t.id = r.org_id where t.pid = #{areaId} <include refid="dynamicCondition" /> </select>
或者:
<sql id="dynamicWhere"> <where> <if test="@Ognl@isNotEmpty(id)"> AND id =#{id} </if> <if test="@Ognl@isNotEmpty(orgId)"> AND org_id = #{orgId} </if> <if test="@Ognl@isNotEmpty(orgName)"> AND org_name LIKE #{orgName} </if> <if test="@Ognl@isNotEmpty(templateType)"> AND template_type = #{templateType} </if> <if test="@Ognl@isNotEmpty(roleType)"> AND role_type = #{roleType} </if> <if test="@Ognl@isNotEmpty(trialId)"> AND trial_id = #{trialId} </if> <if test="@Ognl@isNotEmpty(typeId)"> AND type_id LIKE #{typeId} </if> <if test="@Ognl@isNotEmpty(viewUrl)"> AND view_url LIKE #{viewUrl} </if> <if test="@Ognl@isNotEmpty(reportName)"> AND report_name LIKE #{reportName} </if> <if test="@Ognl@isNotEmpty(provinceId)"> AND province_id =#{provinceId} </if> <if test="@Ognl@isNotEmpty(provinceName)"> AND province_name = #{provinceName} </if> <if test="@Ognl@isNotEmpty(cityId)"> AND city_id =#{cityId} </if> <if test="@Ognl@isNotEmpty(cityName)"> AND city_name LIKE #{cityName} </if> <if test="@Ognl@isNotEmpty(countyId)"> AND county_id =#{countyId} </if> <if test="@Ognl@isNotEmpty(countyName)"> AND county_name LIKE #{countyName} </if> <if test="@Ognl@isNotEmpty(generatedTime)"> AND generated_time =#{generatedTime} </if> <if test="@Ognl@isNotEmpty(begingeneratedTime)"> AND generated_time >=#{begingeneratedTime,jdbcType=TIMESTAMP} </if> <if test="@Ognl@isNotEmpty(endgeneratedTime)"> AND generated_time <![CDATA[ <#{endgeneratedTime,jdbcType=TIMESTAMP}]]> </if> <if test="@Ognl@isNotEmpty(generatedUserId)"> AND generated_user_id =#{generatedUserId} </if> <if test="@Ognl@isNotEmpty(generatedUserName)"> AND generated_user_name LIKE #{generatedUserName} </if> <if test="@Ognl@isNotEmpty(releaseTime)"> AND release_time =#{releaseTime} </if> <if test="@Ognl@isNotEmpty(beginreleaseTime)"> AND release_time >=#{beginreleaseTime,jdbcType=TIMESTAMP} </if> <if test="@Ognl@isNotEmpty(endreleaseTime)"> AND release_time <![CDATA[ <#{endreleaseTime,jdbcType=TIMESTAMP}]]> </if> <if test="@Ognl@isNotEmpty(releaseUserId)"> AND release_user_id =#{releaseUserId} </if> <if test="@Ognl@isNotEmpty(releaseUserName)"> AND release_user_name LIKE #{releaseUserName} </if> <if test="@Ognl@isNotEmpty(status)"> AND status =#{status} </if> </where> </sql>
使用:
<select id="queryListCount" parameterType="map" resultType="Integer"> select count(id) from report_divided_html <include refid="dynamicWhere" /> </select>
相关文章推荐
- MyBatis中的动态SQL,实现不确定条件的CRUD-----使用Map来传递查询的参数
- 使用ADF-BC 实现查询功能之四:通过代码动态设置Where条件参数
- MyBatis order by 动态参数时或使用Like查询时用$而不是#
- 使用反射让linq实现动态查询, 类似拼接sql语句的where 条件
- mybatis使用in语句作为查询条件
- mybatis查询sql中in条件使用(foreach)
- ADF:如何使用VO处理查询条件的值属于某一动态值列表的问题
- Mybatis 多条件查询(模糊查询 使用in查询)
- Mybatis 多条件动态查询
- 使用反射让linq实现动态查询, 类似拼接sql语句的where 条件
- 使用mybatis遇到的关于条件查询">"以及if test传参的使用问题
- MyBatis动态条件查询
- mybatis的动态条件查询
- 使用SQL查询语句时,数组动态赋值SQL IN ()作为条件条件一个
- 使用ADF-BC 实现查询功能之八:通过代码动态设置Criteria条件参数
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
- MyBatis中动态SQL语句完成多条件查询
- eclipse使用mybatis做查询时条件有汉字的不能执行成功
- 基于mybatis的动态条件分页查询
- Hibernate使用原生的动态sql实现带条件的查询分页功能