动态sql
动态 SQL 动态
SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同 条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个 列名的逗号。利用动态 SQL,可以彻底摆脱这种痛苦。 使用动态 SQL 并非一件易事,但借助可用于任何 SQL 映射语句中的强大的动态 SQL 语言,MyBatis 显 著地提升了这一特性的易用性。
-
if
用于进行条件判断, test 属性用于指定判断条件. 为了拼接条件, 在 SQL 语句后强行添加 1=1 的恒成立条件
<select id="sel" resultType="user"> select * from t_user where 1=1 <if test="username != null and username != ''"> and username=#{username} </if> <if test="password != null and password != ''"> and password=#{password} </if> </select>
-
where
用于管理 where 子句.
有如下功能:
-
如果没有条件, 不会生成 where 关键字
-
如果有条件, 会自动添加 where 关键字
-
如果第一个条件中有 and, 去除之
-
-
choose...when...otherwise
这是一套标签, 功能类似于 switch...case...
<select id="sel" resultType="user"> select * from t_user <where> <choose> <when test="username != null and username != ''"> and username = #{username} </when> <when test="password != null and password != ''"> and password = #{password} </when> <otherwise> and 1=1 </otherwise> </choose> </where> </select>
-
set
用于维护 update 语句中的 set 子句.
功能如下:
-
满足条件时, 会自动添加 set 关键字
-
去除 set 子句中多余的逗号
-
不满足条件时, 不会生成 set 关键字
int updUser(User user);
<update id="updUser" parameterType="user"> update t_user <set> id=#{id},<!-- 防止所有条件不成立时的语法错误 --> <if test="username != null and username != ''"> username=#{username}, </if> <if test="password != null and password != ''"> password=#{password}, </if> </set> where id=#{id} </update>
-
-
trim
用于在前后添加或删除一些内容
-
prefix, 在前面添加内容
-
prefixOverrides, 从前面去除内容
-
suffix, 向后面添加内容
-
suffixOverrides, 从后面去除内容
<update id="updUser" parameterType="user"> update t_user <!-- prefix: 前缀, 表示向前面添加内容 prefixOverrides: 从前面删除内容 suffix: 后缀, 表示向后面添加内容 suffixOverrides: 从后面删除内容 --> <trim prefix="set" prefixOverrides="user" suffix="hahaha" suffixOverrides=","> username=#{username}, </trim> where id=#{id} </update
-
-
bind
用于对数据进行再加工, 用于模糊查询
<select id="sel" resultType="user"> select * from t_user <where> <if test="username!=null and username!=''"> <bind name="username" value="'%' + username + '%'" /> and username like #{username} </if> </where> </select>
-
foreach
用于在 SQL 语句中遍历集合参数, 在 in 查询中使用
-
collection: 待遍历的集合
-
open: 设置开始符号
-
item: 迭代变量
-
separator: 项目分隔符
-
close: 设置结束符
List<User> selIn(@Param("list") List<Integer> list);
<select id="selIn" parameterType="list" resultType="user"> select * from t_user where id in <foreach collection="list" open="(" separator="," close=")" item="item"> #{item} </foreach> </select>
-
-
sql...include
sql用于提取 SQL 语句, include用于引用 SQL 语句
<sql id="mySql"> id, username, password </sql>
<select id="selIn" parameterType="list" resultType="user"> select <include refid="mySql" /> from t_user where id in <foreach collection="list" open="(" separator="," close=")" item="item"> #{item} </foreach> </select>
使用resultMap
resultMap用于自定义映射关系, 可以由程序员自主制定列名和属性名的映射关系. 一旦使用 resultMap, 表示不再采用自动映射机制.
<resultMap type="user" id="umap"> <!-- id用于映射主键 -->
<id column="id" property="id1" /> <!-- 非主键使用result映射 -->
<result column="username" property="username1" />
<result column="password" property="password1" />
</resultMap>
<select id="selAll" resultMap="umap"> select * from t_user </select>
resultMap 的关联方式实现多表查询(一对一|多对一)
-
在 StudentMapper.xml 中定义多表连接查询 SQL 语句, 一次性查到需要的所有数据, 包括对应班级 的信息.
-
通过resultMap标签定义映射关系, 并通过association标签指定对象属性的映射关系. 可以把 association标签看成一个resultMap标签使用. javaType 属性表示当前对象, 可以写全限定路径或 别名.
学生类中添加一个班级类型的属性 :
public class Student implements Serializable{ private int id; private String name; private int age; private String gender; private int cid; private Clazz cls; }
<resultMap type="student" id="smap">
<id property="id" column="sid" />
<result property="name" column="sname" />
<result property="age" column="age" />
<result property="gender" column="gender" />
<result property="cid" column="cid" />
<association property="cls" javaType="clazz">
<id property="id" column="cid" />
<result property="name" column="cname" />
<result property="room" column="room" />
</association>
</resultMap>
<select id="selAll" resultMap="smap">
select s.id sid, s.name sname,
s.age, s.gender, c.id cid, c.name cname, c.room from t_student s left
join t_class c on s.cid=c.id
</select>
resultMap 的关联方式实现多表查询(一对 多)
-
在 ClazzMapper.xml 中定义多表连接查询 SQL 语句, 一次性查到需要的所有数据, 包括对应学生的 信息.
-
通过resultMap定义映射关系, 并通过collection标签指定集合属性泛型的映射关系. 可以collection 标签看成一个resultMap标签使用. ofType 属性表示集合的泛型, 可以写全限定路径或别名.
班级类中添加一个List容器存储学生类型 的数据 :
public class Clazz implements Serializable{
private int id;
private String name;
private int roomNum;
private List<Student> stus;
}
html1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>基础语法</title>
</head>
<body>
<!--
标记语言中的注释:
-->
<!--
1.html 超文本标记语言
2.<!DOCTYPE html> html文件的版本类型声明
<!DOCTYPE html> ->html5的版本声明
一个html页面中必须在首行存在版本声明
3.<meta charset="UTF-8"> 告诉浏览器使用哪一种字符编码格式来解析我页面中的内容
保证一个文件的编码与解码格式保证统一不会出现乱码
4.html标签的分类:
单标签 |自闭和标签 : 在开始标签的最后结束 <meta charset="UTF-8"/>
双标签 |闭合标签 : 有开始有结束 <title>基础语法</title>
5.标签上可以添加属性
开始标签上空格添加属性
属性名 = '属性值'|"属性值"
作用: 更完善的展示标签的作用
6.标签的嵌套关系:
父标签可以嵌套子标签
-->
</body>
</html>
html2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>基础语法</title>
</head>
<body bgcolor="#f03725" background="images/xiaoxin.png">
<!--
常用的标签:
1.<head> 页面的头部设置
给浏览器看的一些设置信息
2.<body> 定义一些给用户看的内容
属性:
bgcolor 背景颜色
background 背景图片
颜色的设置:
1)颜色的英文单词
2)颜色的十六进制值 #+6个字符
3)rgb颜色三原色 rgb(0~255,0~255,0~255)
3.<title> 标题标签
用户能够看到
为与搜索引擎优化有好处
用户收藏为书签的时候,title的内容作为书签名
在一个html必须存在的标签
4.p 标签
段落标签
语义化作用
上下存在一块外边距(css样式),浏览器默认提供
前后换行
5.a标签
超链接标签
属性:
href : 定义连接地址
相对地址
绝对地址 http://www.baidu.com
必填属性
title 当鼠标悬停在内容上时候,显示的提示字
target 打开方式
_self : 原页面打开
_blank : 新页面打开
特点:
存在下划线
连接未访问: 蓝色
连接已访问: 紫色
行内元素
标签的分类:
元素: 标签+内容
行内元素 : 可以和其他元素同行展示
块元素 : 前后换行,独占一行
-->
这是p标签之前:
<p>这是一段文本</p>
这是p标签之后:
<a href="http://www.baidu.com" title="你百度一下,没有解决不了的问题" target="_blank">百度一下你就知道</a>
<a href="yjxxt_html01.html">html01</a>
</body>
</html>