对mybastis 的理解1--xml中的动态sql设计

<?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=",#{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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇