MyBatis动态SQL之增、删、改、查操作
来源:互联网 发布:linux下新建用户 编辑:程序博客网 时间:2024/05/03 09:56
MyBatis动态SQL之增、删、改、查操作
- MyBatis动态SQL之增删改查操作
- 概述
- MyBatis动态SQL支持
- 动态SQL之查询
- 动态SQL之插入
- 动态SQL之删除
- 动态SQL之更新
- MyBatis动态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支持
动态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="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>
动态SQL之插入
<!-- 动态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>
动态SQL之删除
<!-- 动态删除操作 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>
动态SQL之更新
<!-- 动态更新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>
0 0
- MyBatis动态SQL之增、删、改、查操作
- MyBatis动态SQL之增、删、改、查操作
- SQL增、删、改、查操作语句
- Mybatis之增删改查操作
- JAVAWEB开发之mybatis详解(一)——mybatis的入门(实现增删改查操作)、自定义别名、抽取代码块以及动态SQL的使用
- JAVAWEB开发之mybatis详解(一)——mybatis的入门(实现增删改查操作)、自定义别名、抽取代码块以及动态SQL的使用
- SQL 语法之“增”、“删”、“改”、“查”
- SQL 之“增”、“删”、“改”、“查”
- mybatis增、删、改、查
- mybatis 增删改查操作
- Mybatis增删查改操作
- MyBatis实战教程之四增删改查操作
- 【Mybatis】Mybatis增删改查操作
- mybatis之增删改查
- MyBatis之增删改查
- Mybatis之增删改查
- Sql的基本操作之增删查改操作
- hibernate之基本操作(增,删,改,查)
- Qt通过句柄操作外部窗口
- 开启新纪元-python
- [ExtJs6]combox&&timefield&&checkbox
- Ubuntu环境下MySQl的安装
- python爬虫---post抓取查询数据简单说明
- MyBatis动态SQL之增、删、改、查操作
- Win10下pip的安装
- Ubuntu 安装 opencv-nonfree
- HDU 2413 & POJ 3343 Against Mammoths【最大匹配+二分】
- Convolution Neural Network (CNN) 原理与实现
- javascript入门笔记6-内置对象
- linux命令--ethtool
- java中基本数据类型和包装类型
- java