MyBatis动态SQL之增、删、改、查操作
2016-10-13 22:23
357 查看
MyBatis动态SQL之增、删、改、查操作
MyBatis动态SQL之增删改查操作概述
MyBatis动态SQL支持
动态SQL之查询
动态SQL之插入
动态SQL之删除
动态SQL之更新
概述
在实际的场景中,经常会遇到动态SQL的增、删、改、查问题,这里就必要说明何谓动态SQL,我们举一个实际的例子,比如,在一个web工程中,经常会有一个搜索框,并且在搜索之前通常会进行一个关键词的过滤,比如可以过滤的条件有:姓名、年龄等,这样的话当我们姓名和年龄都不选,则等价于下面的SQL语句select * from students; -- 不去限制姓名和年龄
当我们将年龄选择为>20时,相当于下面的SQL语句:
select * from students where age>20;
当我们同时选择条件姓名为:张三,年龄>20,则相当于下面的SQL语句:
select * from students where age>20 and name='张三';
当我们有很多的条件时,此时就需要我们去组合这些条件,并动态的生成一个可执行的SQL语句,这样就不是一个简单的SQL语句能够解决问题,那么我们该怎么办呢?在MyBatis中同样是支持这种动态SQL的写法,具体见下面的内容。
MyBatis动态SQL支持
[b]动态SQL之查询[/b]<?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="studentNameSpace"> <!-- resultMap标签将了工程entity实体类中的对象与数据库中的表对应起来 resultMap中的id属性是一个唯一的名字 子标签中的id属性用来指定主键 result标签用来指定其他的键,其中property属性是指实体中的字段,对应的 column属性表示的数据库中的响应的字段 --> <resultMap type="com.jpzhutech.entity.Student" id="studentMap"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="sal" column="sal"/> </resultMap> <!-- 动态查询SQL语句 --> <select id="findAll" parameterType="map" resultMap="studentMap"> select id , name , sal from students <where> <if test="pid!=null" > and id = #{pid} <!-- #{}和之前使用c3p0的时候写的?含义是相同的 --> </if> <if test="pname!=null" > and name = #{pname} </if> <if test="psal!=null" > and sal = #{psal} </if> </where> </select> </mapper>
[b]动态SQL之插入[/b]
<!-- 动态insert --> <!-- 定义两个sql片段,第一个对应字段名,id属性值任意并且唯一 --> <sql id="key"> <trim suffixOverrides=","> <if test="id!=null"> id, </if> <if test="name!=null"> name, </if> <if test="sal!=null"> sal, </if> </trim> </sql> <!-- 定义第二个sql片段,第二个对应?,key属性值任意并且唯一 --> <sql id="value"> <trim suffixOverrides=","> <if test="id!=null"> #{id}, </if> <if test="name!=null"> #{name}, </if> <if test="sal!=null"> #{sal}, </if> </trim> </sql> <!-- <include refid="key"/>和<include refid="value"/>表示引用上面sql片段 --> <insert id="insertStudent" parameterType="com.jpzhutech.entity.Student"> insert into students(<include refid="key"/>) values(<include refid="value"/>); </insert>
[b]动态SQL之删除[/b]
<!-- 动态删除操作 delete from students where id in(?,?,?);--> <delete id="deleteStudent"> delete from students where id in <!-- foreach用于迭代数组元素 open表示开始符号 close表示结束符号 seprator表示元素间的分割符 items表示迭代的数组 --> <foreach collection="array" open="(" close=")" separator="," item="ids"> #{ids} </foreach> </delete>
[b]动态SQL之更新[/b]
<!-- 动态更新SQL语句,update table_name set name=? , sal=? where id=?, 其中id不能更新,因为id为主键,这个动态更新该怎么写呢? set标签会自动判断后面是否加, --> <update id="updateStudent" parameterType="map" > update students <set> <if test="pname!=null"> name = #{pname}, </if> <if test="psal!=null"> sal = #{psal}, </if> </set> where id = #{pid} </update>
相关文章推荐
- MyBatis的动态SQL操作--插入
- mybatis实战之路,疯狂的数据库操作框架、动态sql的mybats之快速搭建基本环境
- MyBatis的getMapper()接口、resultMap标签、Alias别名、 尽量提取sql列、动态操作
- (4) hibernate增删查改+批量操作+类似Mybatis动态sql
- JAVAWEB开发之mybatis详解(一)——mybatis的入门(实现增删改查操作)、自定义别名、抽取代码块以及动态SQL的使用
- MyBatis的动态SQL操作--更新
- mybatis 动态操作Sql语句
- MyBatis的动态SQL操作
- JAVAWEB开发之mybatis详解(一)——mybatis的入门(实现增删改查操作)、自定义别名、抽取代码块以及动态SQL的使用
- 详解MyBatis的getMapper()接口、resultMap标签、Alias别名、 尽量提取sql列、动态操作
- MyBatis的动态SQL操作--删除
- mybatis实战之路,疯狂的数据库操作框架、动态sql实现CRUD及带条件的增CRUD
- MyBatis中的动态SQL例子
- MyBatis学习 之 三、动态SQL语句
- 关于mybatis 动态 sql 的一些陷阱:防止批量update,delete,select...
- MyBatis动态SQL
- MyBatis学习 之 四、动态SQL语句
- MyBatis动态SQL中trim标签的使用
- mybatis动态SQL语句