对mybastis 的理解1--xml中的动态sql设计
2014-11-15 18:13
190 查看
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="org.xnat.BaseDao">
<!-- 新增v1(以v开头的是版本的意思)-->
<insert id="insert">
insert into ${tableName}
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
${item.key}
</foreach>
values
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item.value}
</foreach>
</insert>
<!-- 新增v1_2
useGeneratedKeys="true" keyProperty="id"
-->
<insert id="insert_v1_2" parameterType="map">
insert into ${tableName}
<foreach item="item" index="index" collection="autoMaps" open="(" separator="," close=")">
${item.key}
</foreach>
values
<foreach item="item" index="index" collection="autoMaps" open="(" separator="," close=")">
#{item.value}
</foreach>
<selectKey resultType="int" keyProperty="autoKey" order="AFTER">
select LAST_INSERT_ID() as autoKey
</selectKey>
<!--
-->
</insert>
<!-- 查询select_v4-->
<select id="select_v4" resultType="hashmap">
select
<!-- 选择的字段 -->
<foreach collection="selectFields" item="item" separator=",">`${item}`</foreach>
from ${tableName}
<!-- 条件 -->
<where>
<if test="conditions != null">
<foreach collection="conditions" item="condition" separator=" and ">
<!--
${condition.key} ${condition.denotation} #{condition.value}
这种不能适应 denotation 为 "in" value 为 "(v1, v2)"
${condition.key} ${condition.denotation} ${condition.value}
这种不能适应 ${condition.value} 为字符串
-->
`${condition.key}` ${condition.denotation} #{condition.value}
</foreach>
</if>
</where>
<!-- 分组 list<String>-->
<if test="group != null">
group by
<foreach collection="group" item="item" separator=", ">
${item}
</foreach>
</if>
<!-- group 后的having 条件 -->
<if test="havingSql != null and group != null">
${havingSql}
</if>
<!-- 排序 -->
<if test="sort != null">
order by
<foreach collection="sort" item="item" separator=", ">
${item.key} ${item.value}
</foreach>
</if>
<!-- 分页 -->
<if test="page != null">
LIMIT #{page.start}, #{page.limit}
</if>
</select>
<!-- 查询select_v4_2-->
<select id="select_v4_2" resultType="hashmap">
select
<!-- 选择的字段 -->
<foreach collection="selectFields" item="item" separator=",">`${item}`</foreach>
from ${tableName}
<!-- 条件 -->
<if test="conditionSql != null">
${conditionSql}
</if>
<!-- 分组 list<String>-->
<if test="group != null">
group by
<foreach collection="group" item="item" separator=", ">
${item}
</foreach>
</if>
<!-- group 后的having 条件 -->
<if test="havingSql != null and group != null">
${havingSql}
</if>
<!-- 排序 -->
<if test="sort != null">
order by
<foreach collection="sort" item="item" separator=", ">
${item.key} ${item.value}
</foreach>
</if>
<!-- 分页 -->
<if test="page != null">
LIMIT #{page.start}, #{page.limit}
</if>
</select>
<!-- 删除_v3-->
<delete id="delete_v3">
delete from ${tableName}
<where>
<if test="conditions != null">
<foreach item="item" index="index" collection="conditions" separator=" and ">
${item.key}${item.denotation}#{item.value}
</foreach>
</if>
</where>
</delete>
<!-- 删除_v3-->
<delete id="delete_v3_2">
delete from ${tableName}
<if test="conditionSql != null">${conditionSql}</if>
</delete>
<!-- 更新_v2-->
<update id="update_v2">
update ${tableName}
<set>
<foreach item="item" index="index" collection="list" separator=",">
${item.key}=#{item.value}
</foreach>
</set>
<where>
<if test="conditions != null">
<foreach item="item" index="index" collection="conditions" separator=" and ">
${item.key} ${item.denotation} #{item.value}
</foreach>
</if>
</where>
</update>
<!-- 更新_v2-->
<update id="update_v2_2">
update ${tableName}
<set>
<foreach item="item" index="index" collection="list" separator=",">
${item.key}=#{item.value}
</foreach>
</set>
<if test="conditionSql != null">${conditionSql}</if>
</update>
<!-- 查询实体存在的数量-->
<select id="getTotal" resultType="int">
select count(*) from ${tableName}
<where>
<if test="conditions != null">
<foreach item="item" index="index" collection="conditions" separator=" and ">
<!--
${item.key} ${item.denotation} #{item.value}
不能用于 key != ''
-->
`${item.key}` ${item.denotation} #{item.value}
</foreach>
</if>
</where>
</select>
<!-- 查询实体存在的数量-->
<select id="getTotal_v1_2" resultType="int">
select count(*) from ${tableName}
<if test="conditionSql != null">${conditionSql}</if>
</select>
<!-- 统计一个字段所有值的和-->
<select id="countField" resultType="long">
select sum(${fieldName}) from ${tableName}
<where>
<if test="conditions != null">
<foreach item="item" index="index" collection="conditions" separator=" and ">
`${item.key}` ${item.denotation} #{item.value}
</foreach>
</if>
</where>
</select>
<!-- 自定义sql查询-->
<select id="selectSql" resultType="hashmap">
${sql}
</select>
</mapper>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="org.xnat.BaseDao">
<!-- 新增v1(以v开头的是版本的意思)-->
<insert id="insert">
insert into ${tableName}
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
${item.key}
</foreach>
values
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item.value}
</foreach>
</insert>
<!-- 新增v1_2
useGeneratedKeys="true" keyProperty="id"
-->
<insert id="insert_v1_2" parameterType="map">
insert into ${tableName}
<foreach item="item" index="index" collection="autoMaps" open="(" separator="," close=")">
${item.key}
</foreach>
values
<foreach item="item" index="index" collection="autoMaps" open="(" separator="," close=")">
#{item.value}
</foreach>
<selectKey resultType="int" keyProperty="autoKey" order="AFTER">
select LAST_INSERT_ID() as autoKey
</selectKey>
<!--
-->
</insert>
<!-- 查询select_v4-->
<select id="select_v4" resultType="hashmap">
select
<!-- 选择的字段 -->
<foreach collection="selectFields" item="item" separator=",">`${item}`</foreach>
from ${tableName}
<!-- 条件 -->
<where>
<if test="conditions != null">
<foreach collection="conditions" item="condition" separator=" and ">
<!--
${condition.key} ${condition.denotation} #{condition.value}
这种不能适应 denotation 为 "in" value 为 "(v1, v2)"
${condition.key} ${condition.denotation} ${condition.value}
这种不能适应 ${condition.value} 为字符串
-->
`${condition.key}` ${condition.denotation} #{condition.value}
</foreach>
</if>
</where>
<!-- 分组 list<String>-->
<if test="group != null">
group by
<foreach collection="group" item="item" separator=", ">
${item}
</foreach>
</if>
<!-- group 后的having 条件 -->
<if test="havingSql != null and group != null">
${havingSql}
</if>
<!-- 排序 -->
<if test="sort != null">
order by
<foreach collection="sort" item="item" separator=", ">
${item.key} ${item.value}
</foreach>
</if>
<!-- 分页 -->
<if test="page != null">
LIMIT #{page.start}, #{page.limit}
</if>
</select>
<!-- 查询select_v4_2-->
<select id="select_v4_2" resultType="hashmap">
select
<!-- 选择的字段 -->
<foreach collection="selectFields" item="item" separator=",">`${item}`</foreach>
from ${tableName}
<!-- 条件 -->
<if test="conditionSql != null">
${conditionSql}
</if>
<!-- 分组 list<String>-->
<if test="group != null">
group by
<foreach collection="group" item="item" separator=", ">
${item}
</foreach>
</if>
<!-- group 后的having 条件 -->
<if test="havingSql != null and group != null">
${havingSql}
</if>
<!-- 排序 -->
<if test="sort != null">
order by
<foreach collection="sort" item="item" separator=", ">
${item.key} ${item.value}
</foreach>
</if>
<!-- 分页 -->
<if test="page != null">
LIMIT #{page.start}, #{page.limit}
</if>
</select>
<!-- 删除_v3-->
<delete id="delete_v3">
delete from ${tableName}
<where>
<if test="conditions != null">
<foreach item="item" index="index" collection="conditions" separator=" and ">
${item.key}${item.denotation}#{item.value}
</foreach>
</if>
</where>
</delete>
<!-- 删除_v3-->
<delete id="delete_v3_2">
delete from ${tableName}
<if test="conditionSql != null">${conditionSql}</if>
</delete>
<!-- 更新_v2-->
<update id="update_v2">
update ${tableName}
<set>
<foreach item="item" index="index" collection="list" separator=",">
${item.key}=#{item.value}
</foreach>
</set>
<where>
<if test="conditions != null">
<foreach item="item" index="index" collection="conditions" separator=" and ">
${item.key} ${item.denotation} #{item.value}
</foreach>
</if>
</where>
</update>
<!-- 更新_v2-->
<update id="update_v2_2">
update ${tableName}
<set>
<foreach item="item" index="index" collection="list" separator=",">
${item.key}=#{item.value}
</foreach>
</set>
<if test="conditionSql != null">${conditionSql}</if>
</update>
<!-- 查询实体存在的数量-->
<select id="getTotal" resultType="int">
select count(*) from ${tableName}
<where>
<if test="conditions != null">
<foreach item="item" index="index" collection="conditions" separator=" and ">
<!--
${item.key} ${item.denotation} #{item.value}
不能用于 key != ''
-->
`${item.key}` ${item.denotation} #{item.value}
</foreach>
</if>
</where>
</select>
<!-- 查询实体存在的数量-->
<select id="getTotal_v1_2" resultType="int">
select count(*) from ${tableName}
<if test="conditionSql != null">${conditionSql}</if>
</select>
<!-- 统计一个字段所有值的和-->
<select id="countField" resultType="long">
select sum(${fieldName}) from ${tableName}
<where>
<if test="conditions != null">
<foreach item="item" index="index" collection="conditions" separator=" and ">
`${item.key}` ${item.denotation} #{item.value}
</foreach>
</if>
</where>
</select>
<!-- 自定义sql查询-->
<select id="selectSql" resultType="hashmap">
${sql}
</select>
</mapper>
相关文章推荐
- PreparedStatement 与 Statement 的区别 —— 理解动态SQL语句的执行机制
- 两个Repeater嵌套实现动态菜单(ado.net+sql和xml+Linq两种读取数据方式)
- 自制xml实现SQL动态参数配置
- SQL Server-聚焦深入理解动态SQL查询(三十二)
- SQL Server-聚焦深入理解动态SQL查询(三十二)
- ibatis 中xml动态sql
- 运用设计模式实现Sql语句动态转换
- Mybatis3.4.x技术内幕(十七):Mybatis之动态Sql设计原本(上)
- 理解性能的奥秘——应用程序中慢,SSMS中快(6)——SQL Server如何编译动态SQL
- mybatis使用注解替代xml配置,动态生成Sql
- 动态生成mybatis的xml文件,其中的sql语句生成
- Mybatis - SqlMapConfig.xml , 输入映射 ,输出映射 ,动态sql ,sql片段
- cxf restful 接口的多参数的通过反射动态生成sql的设计
- My Batis mapper.xml中 动态SQL中使用trim标签 if end的场景
- Mybatis3.4.x技术内幕(十八):Mybatis之动态Sql设计原本(下)
- ASP.NET+SQL Server利用XML实现动态菜单
- Mybatis学习(十二)mybatis理解动态sql及sql片段
- 一次动态sql查询订单数据的设计
- Mybatis_01_理解动态sql及sql片段
- (转)MyBatis框架的学习(四)——Mapper.xml文件中的输入和输出映射以及动态sql