框架学习笔记之Mybatis(二)
一、动态sql
通过mybatis提供的标签,实现sql语句的拼接。
1.where
<select id="findUserList" parameterType="user" resultType="user">
select * from user
<!--使用where可以自动处理第一个and-->
<where>
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
</if>
</where>
</select>
2.foreach
场景:当传入参数为一个数组或者集合时,mybatis提供了foreach标签解析
<if test="ids!=null">
<!-- 使用foreach遍历传入的ids
collection:输入对象中的集合属性
item:每次遍历的生成的对象名
open:开始遍历时拼接的串
close:结束遍历时拼接的串
separator:遍历的两个对象中需要拼接的串
-->
<!-- 实现以下拼接
AND(id=1 or id=2 or id=3)
-->
<foreach collection="ids" item="id" open="AND(" close=")" separator="or">
id=#{id}
</foreach>
</if>
3.sql片段
当sql语句重复使用时,我们可以对sql进行抽取,使用时直接引用,提升代码的复用性。
<!-- 传递pojo综合查询用户信息 -->
<select id="findUserList" parameterType="user" resultType="user">
select * from user
<where>
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
</if>
</where>
</select>
对where条件中的sql进行抽取
<sql id="query_user_where">
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
</if>
</sql>
引用sql
<select id="findUserList" parameterType="user" resultType="user">
select * from user
<where>
<include refid="query_user_where"/>
</where>
</select>
二、关联查询(重点)
resultType和resultMap
resultType指定输出结果类型,可以是基本类型,也可以 是pojo对象。
mapper.xml文件
<!-- 根据id查询用户信息 -->
<select id="findUserById" parameterType="int" resultType="user">
select * from user where id = #{id}
</select>
resultMap
resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功.
如果sql查询字段名和pojo的属性名不一致,可以通过resultMap将字段名和属性名作一个对应关系 ,resultMap实质上还需要将查询结果映射到pojo对象中。resultMap可以实现将查询结果映射为复杂类型的pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。
<!-- 配置resultmap type:为映射结果类型 -->
<resultMap type="orders" id="user_orders_resultmap">
<!-- 配置要映射的订单信息 -->
<!-- id为要查询对象的唯一标识(orders的主键 ) 如果有多个唯一表示都都配置上即可 column:唯一标识的咧 property:为结果类型的属性 -->
<id column="id" property="id" />
<result column="user_id" property="userId" />
<result column="number" property="number" />
<result column="createtime" property="createtime" />
<result column="note" property="note" />
<!-- 配置要映射的关联用户的信息 -->
<!-- assocation用来映射关联查询单个对象的信息 property:要将关联的信息映射到Orders中的那个属性 -->
<association property="user" javaType="cn.mycookies.mybatis.demo.po.User">
<result column="user_id" property="id" />
<result column="username" property="username" />
<result column="sex" property="sex" />
<result column="address" property="address" />
</association>
</resultMap>
1.一对一查询
场景:查询所有订单信息,关联查询下单用户信息。
注意:因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询。如果从用户信息出发查询用户下的订单信息则为一对多查询,因为一个用户可以下多个订单
way1:使用resultType定义返回值结果类型 返回结果为Orders的po类 类中有User属性
mapper.xml配置
<select id="findOrdersListResultMap" resultType="Orders">
SELECT
orders.*,
user.username,
user.address
FROM
orders, user
WHERE orders.user_id = user.id
</select>
way2:使用resultMap定义返回 结果类型
<!--定义resultMap-->
<resultMap type="orders" id="user_orders_resultmap">
<!--配置映射的订单信息-->
<!-- id为要查询对象的唯一标识(orders的主键 ) 如果有多个唯一表示都都配置上即可
column:唯一标识的咧 property:为结果类型的属性 -->
<id column="id" property="id" />
<result column="user_id" property="userId" />
<result column="number" property="number" />
<result column="createtime" property="createtime" />
<result column="note" property="note" />
<!-- 配置要映射的关联用户的信息 -->
<!-- assocation用来映射关联查询单个对象的信息 property:要将关联的信息映射到Orders中的那个属性 -->
<association property="user" javaType="cn.mycookies.mybatis.demo.po.User">
<result column="user_id" property="id" />
<result column="username" property="username" />
<result column="sex" property="sex" />
<result column="address" property="address" />
</association>
</resultMap>
<!-- resultMap映射结果查询 -->
<select id="findOrdersUserResultMap" resultMap="user_orders_resultmap">
SELECT orders.*
,User.username,User.sex,User.address
from orders ,user
where
orders.user_id = user.id
</select>
2.一对多查询
案例:查询所有订单信息一级订单下的订单明细
订单与订单明细是一对多的关系
只能用resultMap实现
<!-- 一对多查询 订单与订单明细 -->
<resultMap type="orders" id="OrdersAndOrderDetailResultMap"
extends="user_orders_resultmap">
<!-- 因为出现了重复 我们可以使用restulemap的继承 -->
<!-- 订单信息 <id column="id" property="id"/> <result column="user_id" property="userid"/>
<result column="number" property="number"/> <result column="createtime" property="createtime"/>
<result column="note" property="note"/> 用户信息 <association property="user"
javaType="user"> <result column="user_id" property="id"/> <result column="username"
property="username"/> <result column="sex" property="sex"/> <result column="address"
property="address"/> </association> -->
<!-- 订单明细信息 property:po类中对应的属性名 ofType映射到list集合属性中的po类型 -->
<collection property="orderdetailsList" ofType="Orderdetail">
<id column="ordertaile_id" property="id" />
<result column="items_id" property="itemsId" />
<result column="items_num" property="itemsNum" />
<result column="orders_id" property="ordersId" />
</collection>
</resultMap>
<select id="findOrdersAndOrderDetailsresultMap" resultMap="OrdersAndOrderDetailResultMap">
SELECT orders.* ,
User.username,
User.sex,
User.address,
orderdetail.id
ordertaile_id ,
orderdetail.items_id,
orderdetail.items_num,
orderdetail.orders_id
from orders ,user,orderdetail
where orders.user_id
= user.id and orderdetail.orders_id=orders.id
</select>
3.多对多的查询
案例:查询用户购买的商品信息。
需要查询用户信息,关联查询订单和订单明细,通过订单明细关联查询商品信息
<!--用户与items的 多对多映射关系 -->
<resultMap type="User" id="userAndItemsResultMap">
<!-- 用户信息 -->
<id column="user_id" property="id" />
<result column="username" property="username" />
<result column="sex" property="sex" />
<result column="address" property="address" />
<!-- 用户所购买的订单信息 -->
<collection property="ordersList" ofType="orders">
<id column="id" property="id" />
<result column="user_id" property="userId" />
<result column="number" property="number" />
<result column="createtime" property="createtime" />
<result column="note" property="note" />
<!-- 订单明细 -->
<collection property="orderdetailsList" ofType="orderdetail">
<id column="orderdetail_id" property="id" />
<result column="items_id" property="itemsId" />
<result column="items_num" property="itemsNum" />
<result column="orders_id" property="ordersId" />
<!-- 商品明细 一个订单包括一个item -->
<association property="items" javaType="Items">
<id column="items_id" property="id" />
<result column="items_name" property="name" />
<result column="items_detail" property="detail" />
<result column="items_price" property="price" />
</association>
</collection>
</collection>
</resultMap>
<select id="findUserandItemsResultMap" resultMap="userAndItemsResultMap">
SELECT
orders.*,
User.username,
User.sex,
User.address,
orderdetail.id
ordertaile_id ,
orderdetail.items_id,
orderdetail.items_num,
orderdetail.orders_id,
items.price items_price,
items.detail
items_detail,
items.name items_name
from orders ,user,orderdetail,items
where orders.user_id = user.id and
orderdetail.orders_id=orders.id and
orderdetail.items_id=items.id
</select>
总结:
resulttype:将查询的结果按照sql列名pojo属性名一致性映射到pojo中。
使用场景:常见一些明细记录的展示,比如用户购买商品明细,将关联查询信息全部展示在页面上时,此时可直接使用resultType将每一条记录映射到pojo中,在前端页面遍历list即可。
resultMap:使用association和collection完成一对一和一对多高级映射(对结果有特殊要求时使用)
association:将关联查询信息映射到一个pojo对象中
使用场景:为了方便查询关联信息可以使用association将关联订单信息映射到用户对象的pojo属性中,比如,查询订单的用户信息。
collection:将关联查询信息映射到一个list集合中
使用场景:为了方便查询遍历关联信息可以使用collection将关联信息映射到一个集合中,比如,查询用户权限范围模块以及模块下的菜单,可以使用collection将模块映射到模块list中,将菜单列表映射到模块对象的list属性中,这样做的目的是方便对查询结果集进行遍历查询。
框架学习笔记之Mybatis(二)的更多相关文章
- 框架学习笔记之Mybatis(一)
一.简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单 ...
- Yii框架学习笔记(二)将html前端模板整合到框架中
选择Yii 2.0版本框架的7个理由 http://blog.chedushi.com/archives/8988 刚接触Yii谈一下对Yii框架的看法和感受 http://bbs.csdn.net/ ...
- Spring框架学习笔记(8)——spring boot+mybatis plus+mysql项目环境搭建
之前写的那篇Spring框架学习笔记(5)--Spring Boot创建与使用,发现有多小细节没有提及,,正好现在又学习了mybatis plus这款框架,打算重新整理一遍,并将细节说清楚 1.通过I ...
- SSM框架学习笔记_第1章_SpringIOC概述
第1章 SpringIOC概述 Spring是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器框架. 1.1 控制反转IOC IOC(inversion of controller)是一种概念 ...
- phalcon(费尔康)框架学习笔记
phalcon(费尔康)框架学习笔记 http://www.qixing318.com/article/phalcon-framework-to-study-notes.html 目录结构 pha ...
- JavaSE中Map框架学习笔记
前言:最近几天都在生病,退烧之后身体虚弱.头疼.在床上躺了几天,什么事情都干不了.接下来这段时间,要好好加快进度才好. 前面用了三篇文章的篇幅学习了Collection框架的相关内容,而Map框架相对 ...
- JavaSE中线程与并行API框架学习笔记1——线程是什么?
前言:虽然工作了三年,但是几乎没有使用到多线程之类的内容.这其实是工作与学习的矛盾.我们在公司上班,很多时候都只是在处理业务代码,很少接触底层技术. 可是你不可能一辈子都写业务代码,而且跳槽之后新单位 ...
- spring mvc 及NUI前端框架学习笔记
spring mvc 及NUI前端框架学习笔记 页面传值 一.同一页面 直接通过$J.getbyName("id").setValue(id); Set值即可 二.跳转页面(bus ...
- Spring框架学习笔记(5)——Spring Boot创建与使用
Spring Boot可以更为方便地搭建一个Web系统,之后服务器上部署也较为方便 创建Spring boot项目 1. 使用IDEA创建项目 2. 修改groupid和artifact 3. 一路n ...
随机推荐
- [SDOI2010] 魔法猪学院
Description 给定e和边权,求有多少条不同的道路能从1到n使得边权之和的和小于e Solution A*裸题 娘的要是SPFA再把dis写成to就剁手 // By YoungNeal #in ...
- 【原创】Linux服务器集群通过SSH无密码登录
SSH 无密码授权访问slave集群机器 1. 安装SSH,所有集群机器,都要安装SSH环境介绍: Master : CNT06BIG01 192.168.3.61 SLAVE 1: CNT06BI ...
- MySQL数据库学习一 数据库概述
1.1 数据库管理技术的发展阶段 人工管理阶段,文件系统阶段,数据库系统阶段. 1.2 数据库系统阶段涉及的概念 数据库(Database DB):是指长期保存在计算机的存储设备上,按照一定的规则组织 ...
- Android_内部文件读取
我们这里做一个安卓的简易的文件读取程序之记住密码 首先我们先明确安卓的存储路径, 所有安装至手机的应用都会在 data/data 目录下生成一个安卓文件夹(包名),这个文件夹就是安卓存储的路径 在运行 ...
- USB Audio设计与实现
1 前言 本文将基于STM32F4 Discovery板,从零开始设计并实现一个USB Audio的例子. 2 设计构思 所谓的USB AUDIO就是制作一个盒子,这个盒子可以通过USB连接到PC,P ...
- Oracle安装11.2.0.4.180116补丁及如何检查数据库安装补丁
最近做了一个安装11.2.0.4.180116补丁的实验,突然想起之前和同事讨论的一个问题:如何检查数据库安装补丁的版本,之前搜到的是去查dba_registry_history,有的说在操作系统中执 ...
- android中activity.this跟getApplicationContext的区别
转载: http://www.myexception.cn/android/1968332.html android中activity.this和getApplicationContext的区别 在a ...
- [BZOJ 1079][SCOI 2008]着色方案
1079: [SCOI2008]着色方案 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2237 Solved: 1361[Submit][Stat ...
- Java基础学习笔记二十三 Java核心语法之反射
类加载器 类的加载 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,链接,初始化三步来实现对这个类进行初始化. 加载就是指将class文件读入内存,并为之创建一个Class对象.任 ...
- JavaScript(第二十二天)【动态加载js和css】
学习要点: 1.元素位置 2.动态脚本 3.动态样式 本章主要讲解上一章剩余的获取位置的DOM方法.动态加载脚本和样式. 一.元素位置 上一章已经通过几组属性可以获取元素所需的位置,那么这节课补充 ...