多表查询和动态SQL
文章目录
- 多表查询
- 结果集映射
- resultmap构成元素
- 先在Mapper文件中,配置基本的sql语句
- 配置resultMap标签,映射不同的字段和属性名
- 动态SQL
- if标签
- choose\when\otherwise标签
- 标签
- 标签
- 标签
- 动态SQL支持标签
多表查询
结果集映射
resultmap是mybatis中最复杂的元素之一,它描述如何从结果集中加载对象,主要作用是定义映射规则、级联的更新、定制类型转化器。
resultmap构成元素
| constructor | idArg 、arg | 用于配置构造器方法 |
| id | 将结果集标记为id,以方便全局调用 | |
| result | 配置POJO到数据库列名映射关系 | |
| association | 级联使用 | 代表一对一关系 |
| collection | 级联使用 | 代表一对多关系 |
| discriminator | 级联使用 | 鉴别器 根据实际选择实例,可以通过特定条件确定结果集 |
association 和 collection 完成一对一和一对多以及多对多的高级映射
先在Mapper文件中,配置基本的sql语句
<!-- 查询所有的数据 --><!-- resultMap:填入配置的resultMap标签的id值 --><select id="queryBooks" resultMap="book">select b.*,bt.*, b.id bid, bt.id bt_id from book b, booktype bt where b.btype = bt.id;</select>配置resultMap标签,映射不同的字段和属性名
<!-- id:设置ResultMap的id --><resultMap id="book" type="com.lanou3g.mybatis.bean.Book"><!-- 定义主键 ,非常重要。如果是多个字段,则定义多个id --><!-- property:主键在pojo中的属性名 --><!-- column:主键在数据库中的列名 --><id column="bid" property="id" /><!-- resultMap最终还是要将结果映射到pojo上,type就是指定映射到哪一个pojo --><result column="bname" property="bname" /><result column="author" property="author" /><result column="author_gender" property="authorGender" /><result column="price" property="price" /><result column="description" property="description" /><association property="bookType" javaType="com.lanou3g.mybatis.bean.BookType"><id column="bt_id" property="id" /><result column="tname" property="tname" /></association></resultMap>动态SQL
if标签
使用if实现简单的条件判断。
<select id="findActiveBlogLike"resultType="Blog">SELECT * FROM BLOG WHERE state = ‘ACTIVE’<if test="title != null">AND title like #{title}</if><if test="author != null and author.name != null">AND author_name like #{author.name}</if> </select>如果我们的state字段也是动态拼接的,那这里就有问题了,比如我三个条件都没有时,拼出来的sql语句就是SELECT * FROM BLOG WHERE显然是无法执行的,这就要用到我们的where标签
choose 元素
choose\when\otherwise标签
有时我们不想应用到所有的条件语句,而只想从中择其一项。针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句。
使用示例:
<select id="findActiveBlogLike"resultType="Blog">SELECT * FROM BLOG WHERE state = ‘ACTIVE’<choose><when test="title != null">AND title like #{title}</when><when test="author != null and author.name != null">AND author_name like #{author.name}</when><otherwise>AND featured = 1</otherwise></choose> </select>标签
当我们拼接动态SQL时,如果一个查询条件都没有,那我们就不需要where子句,而如果有至少一个条件我们就需要where子句。这样,我们就需要做个判断,而mybatis里的标签就省去了我们自己做这个判断。 使用示例:
<select id="findActiveBlogLike"resultType="Blog">SELECT * FROM BLOG<where><if test="state != null">state = #{state}</if><if test="title != null">AND title like #{title}</if><if test="author != null and author.name != null">AND author_name like #{author.name}</if></where> </select>当一个查询条件都没有拼接时, mybatis会自动将where关键字和拼接多个条件之间的诸如AND、OR这些多余的关键字去掉
标签
set 元素会动态前置 SET 关键字,同时也会删掉无关的逗号(如:语句最后的逗号)
使用示例:
标签
trim标签可以
常用属性有:
prefix: 添加指定前缀
prefixOverrides: 删除指定前缀
suffixOverrides: 删除指定后缀
示例一:用标签实现标签功能
prefixOverrides的作用是移除字符串开头的内容中所有指定在 prefixOverrides 属性中的内容,并且插入 prefix 属性中指定的内容。
示例二: 用标签实现标签功能
<trim prefix="SET" suffixOverrides=",">... </trim>注意这里我们删去的是后缀值,同时添加了前缀值。
动态SQL支持标签
| if | 判断语句 | 单条件分支 |
| choose(when、otherwise) | 相当于 Java 中的 if else | 多条件分支 |
| trim(where、set) | 辅助元素 | 用于处理 SQL 拼接问题 |
| foreach | 循环语句 | 批量插入, 更新, 查询时经常用到 |
| bind | 创建一个变量, 并绑定到上下文中 | 用于兼容不同的数据库, 防止 SQL 注入等 |
总结
以上是生活随笔为你收集整理的多表查询和动态SQL的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 图解splay / splay模板 /
- 下一篇: mysql 查连接数,查看MySQL的连