Mybatis学习三(动态sql语句)
动态sql语句主要为以下语句
1、动态SQL:if 语句
2、动态SQL:if+where 语句
3、动态SQL:if+set 语句
4、动态SQL:choose(when,otherwise) 语句
5、动态SQL:trim 语句
6、动态SQL: SQL 片段
7、动态SQL: foreach 语句
之前的几篇学习博客我用mybatis对一张表进行的CRUD操作,但是 SQL 语句都比较简单,如果有比较复杂的 SQL 语句,经常需要拼接,而拼接 SQL稍微不注意就容易出错此时我们可以用mybatis 动态SQL,通过 if, choose, when, otherwise, trim, where, set, foreach等标签来解决这个问题,这几个标签可组合成非常灵活的SQL语句,用起来也是很舒服
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
4 <!-- namespace:表示名称空间。现在的目的是区分id的. -->
5 <mapper namespace="com.zhiyou100.zhl.dao.UsersDao">
6 <sql id="detail">
7 id,name,age,sex,day
8 </sql>
9
10 <select id="selByWhere" resultType="com.zhiyou100.zhl.bean.Users">
11 select * from users
12 <where>
13 <if test="name!=null">
14 and name=#{name}
15 </if>
16 <if test="sex!=null and sex!=''">
17 and sex=#{sex}
18 </if>
19 </where>
20 </select>
21
22 <update id="updateWhere">
23 update users
24 <set>
25 <if test="name!=null">
26 name=#{name},
27 </if>
28 <if test="sex!=null">
29 sex=#{sex},
30 </if>
31 <if test="age>0">
32 age=#{age},
33 </if>
34 <if test="day!=null">
35 day=#{day}
36 </if>
37 </set>
38 where id=#{id}
39 </update>
40
41 <select id="selByWhere2" resultType="com.zhiyou100.zhl.bean.Users">
42 select
43 <include refid="detail"/>
44 from users
45 <where>
46 <choose>
47 <when test="name!=null and name!=''">
48 name like concat('%',#{name},'%')
49 </when>
50 <when test="sex!=null and sex!=''">
51 sex=#{sex}
52 </when>
53 <otherwise>
54 age>=#{age}
55 </otherwise>
56 </choose>
57 </where>
58 </select>
59
60 <select id="selByWhere3" resultType="com.zhiyou100.zhl.bean.Users">
61 select
62 <include refid="detail"/>
63 from users
64 <trim prefix="where" prefixOverrides="and | or">
65 <if test="name!=null and name!=''">
66 and name=#{name}
67 </if>
68 <if test="sex!=null and sex!=''">
69 and sex=#{sex}
70 </if>
71 <if test="age>0">
72 and age=#{age}
73 </if>
74 <if test="day!=null and day!=''">
75 and day=#{day}
76 </if>
77 </trim>
78 </select>
79
80 <update id="updateWhere2">
81 update users
82 <trim prefix="set" suffixOverrides=",">
83 <if test="name!=null">
84 name=#{name},
85 </if>
86 <if test="sex!=null">
87 sex=#{sex},
88 </if>
89 <if test="age>0">
90 age=#{age},
91 </if>
92 <if test="day!=null">
93 day=#{day}
94 </if>
95 </trim>
96 where id=#{id}
97 </update>
98
99 <delete id="deleteByIds">
100 delete from users
101 <where>
102 <foreach collection="ids" open="id in(" close=")" separator="," item="id">
103 #{id}
104 </foreach>
105 </where>
106 </delete>
107
108
109 <delete id="deleteByIds2">
110 delete from users where id in
111 <foreach collection="ids" open="(" close=")" separator="," item="id">
112 #{id}
113 </foreach>
114 </delete>
115
116
117 </mapper>
Mybatis学习三(动态sql语句)的更多相关文章
- mybatis 学习五 动态SQL语句
3.1 selectKey 标签 在insert语句中,在Oracle经常使用序列.在MySQL中使用函数来自动生成插入表的主键,而且需要方法能返回这个生成主键.使用myBatis的selectKey ...
- Mybatis映射文件动态SQL语句-01
因为在很多业务逻辑复杂的项目中,往往不是简单的sql语句就能查询出来自己想要的数据,所有mybatis引入了动态sql语句, UserMapper.xml <?xml version=" ...
- mybatis学习之动态sql
mybatis的动态sql语句很强大,在mapper映射文件中使用简单的标签即可实现该效果,下面一个个记录: 1.select查询 简单的select类似如下: <select id=" ...
- mybatis中的动态SQL语句
有时候,静态的SQL语句并不能满足应用程序的需求.我们可以根据一些条件,来动态地构建 SQL语句. 例如,在Web应用程序中,有可能有一些搜索界面,需要输入一个或多个选项,然后根据这些已选择的条件去执 ...
- MyBatis之五:动态sql语句
在mybatis 3 或以上的版本提供了4类标签,分别是:if,choose(when,otherwise),rim(where,set),foreach.接下来将分别介绍这几种标签的具体用法,映射x ...
- mybatis学习 十 动态 SQL
1. 根据方法传入的参数不同执行不同的 SQL 命令.称为动态 SQL, MyBatis 中动态 SQL 就是在 mapper.xml 中添加逻辑判断等. 2. <if>标签 <s ...
- Mybatis学习笔记-动态SQL
概念 根据不同环境生成不同SQL语句,摆脱SQL语句拼接的烦恼[doge] 本质:SQL语句的拼接 环境搭建 搭建数据库 CREATE TABLE `blog`( `id` VARCHAR(50) N ...
- 阶段3 1.Mybatis_08.动态SQL_01.mybatis中的动态sql语句-if标签
创建新的工程 复制到新建的项目里面 pom.xml依赖部分复制过来 dao中整理代码 只保留四个查询 映射文件也只保留四个查询方法 增加一个根据条件查询的方法. 由于用了别名,所以parpameter ...
- Mybatis映射文件动态SQL语句-02
foreach UserMapper.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYP ...
- MyBatis学习 之 三、动态SQL语句
目录(?)[-] 三动态SQL语句 selectKey 标签 if标签 if where 的条件判断 if set 的更新语句 if trim代替whereset标签 trim代替set choose ...
随机推荐
- Avalonia发布MacOS运行程序
1 打开xxx.csproj项目文件,添加Dotnet.Bundle包: <PackageReference Include="Dotnet.Bundle" Version= ...
- VS2010插件NuGet
下载地址 NuGet Package Manager - Visual Studio Marketplace NuGet包地址 NuGet Gallery | Home
- 自己写个网盘系列:③ 开源这个网盘编码,手把手教你windows linux 直接部署,docker本地打包部署网盘应用
系列①②已经完成了这个项目的页面和项目的全部编码,前后端分离,这个文章将向你展示运维小伙伴如何部署到windows服务器,linux服务器,docker部署,一学就会,快来看看吧! 说明:这个系列准备 ...
- Scala编译原理
1 package com.atguigu.chapter01; 2 /** 3 * 4 */ 5 //main 方法名 6 //小括号表示参数列表 7 // 参数声明方式: java -> 类 ...
- 共筑使能千行百业的数字底座 | HDC 2022松湖对话顺利召开
11月5日,华为开发者大会2022松湖对话在东莞松山湖凯悦酒店召开,开放原子开源基金会秘书长冯冠霖.华为终端BG软件部总裁龚体.深圳国家金融科技测评中心董事长钟剑.鸿湖万联(江苏)科技发展有限公司董 ...
- openGauss Sqlines 使用指导
openGauss Sqlines 使用指导 Sqlines 简介 Sqlines 是一款开源软件,支持多种数据库之间的 SQL 语句语法的的转换,openGauss 将此工具修改适配,新增了 ope ...
- HDC2021技术分论坛:分布式调试、调优能力解决方案
作者:yangjianwei 华为高级工程师 HarmonyOS致力于提供1+8+N智慧全场景解决方案,打造设备流转.多端协同的分布式体验,实现一次开发.多端部署,让分布式应用的开发更加简单. 针对分 ...
- 一文带你详细了解HarmonyOS折叠屏设计规范!
原文:https://mp.weixin.qq.com/s/G25IbfcX2Bq9s1IDPCELGw,点击链接查看更多技术内容. 随着新一代折叠屏手机HUAWEI Mate Xs 2发布,Harm ...
- k8s之存储卷local PV
一.简介 local能够作为PV使用的本地存储卷. local卷插件用于将本地存储设备(如磁盘.分区或目录) 配置为卷. hostPath卷在Pod被重建后可能被调试至其它节点而无法再次使用此前的数据 ...
- c# 深克隆与浅克隆
前言 我们都知道memberwiseclone 会将浅克隆. 什么是浅克隆?如何深克隆呢? 正文 public class good{ private good(){ oneclass=new cla ...