普通映射:

<!-- 使用resultMap解决列名和属性名不一致的情况 --><!-- 配置一个resultMap映射列和属性 --><resultMap type="Order" id="orderMap"><!-- id:设置ResultMap的id --><!-- 定义主键 ,非常重要。如果是多个字段,则定义多个id --><!-- property:主键在pojo中的属性名 --><!-- column:主键在数据库中的列名 --><id column="id" property="id" /><!-- 映射其他普通列 --><result column="user_id" property="userId" /><result property="number" column="number" /><result property="createTime" column="createTime" /></resultMap><!-- 方法的返回值可以使用 --><select id="queryAll" resultMap="orderMap">selectid,user_id,number,createTime,note from orders</select>

使用封装后的pojo接收2张表数据:

/*** * TODO** 2018年10月18日下午7:32:47*/
public class OrderUser extends Order {private String username;private String address;/*** @return the username*/public String getUsername() {return username;}/*** @param username the username to set*/public void setUsername(String username) {this.username = username;}/*** @return the address*/public String getAddress() {return address;}/*** @param address the address to set*/public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "OrderUser [ "+super.toString()+",username=" + username + ", address=" + address + "]";}}
<!-- 查询orderUser对象 --><select id="queryOrderUser" resultType="OrderUser">SELECTo.id,o.user_id userId,o.number,o.createtime,o.note,u.username,u.addressFROM`orders` oLEFT JOIN `user` u ON o.user_id = u.id</select>

使用封装的对象传参:

/*** * TODO** 2018年10月18日下午6:40:29*/
public class QueryVo {private User user;/*** @return the user*/public User getUser() {return user;}/*** @param user the user to set*/public void setUser(User user) {this.user = user;}}
    <!-- 查询语句 --><select id="queryByQueryVo" parameterType="QueryVo" resultType="User">SELECT * FROM user WHERE username LIKE '%${user.username}%'</select>

关联映射 1对1(单表查询非连接)1:

import java.util.Date;/*** * TODO** 2018年10月18日下午7:02:58*/
public class Order {//订单编号   idprivate Integer id;//用户编号  user_idprivate Integer userId;//订单号   numerprivate String number;//下单时间 createTimeprivate Date createTime;//备注  noteprivate String note;//让order持有user的引用private User user;/*** @return the id*/public Integer getId() {return id;}/*** @param id the id to set*/public void setId(Integer id) {this.id = id;}/*** @return the userId*/public Integer getUserId() {return userId;}/*** @param userId the userId to set*/public void setUserId(Integer userId) {this.userId = userId;}/*** @return the number*/public String getNumber() {return number;}/*** @param number the number to set*/public void setNumber(String number) {this.number = number;}/*** @return the createTime*/public Date getCreateTime() {return createTime;}/*** @param createTime the createTime to set*/public void setCreateTime(Date createTime) {this.createTime = createTime;}/*** @return the note*/public String getNote() {return note;}/*** @param note the note to set*/public void setNote(String note) {this.note = note;}/*** @return the user*/public User getUser() {return user;}/*** @param user the user to set*/public void setUser(User user) {this.user = user;}/* (non-Javadoc)* @see java.lang.Object#toString()*/@Overridepublic String toString() {return "Order [id=" + id + ", userId=" + userId + ", number=" + number + ", createTime=" + createTime+ ", note=" + note + "]";}}
 <!-- 关联映射 1对1 --><resultMap type="Order" id="orderUserMap"><!-- order的主键 --><id column="id" property="id" /><!-- 映射其他普通列 --><result column="user_id" property="userId" /><result property="number" column="number" /><result property="createTime" column="createTime" /><!-- 这种配置类似与使用子查询查询,不用连接查询 --><!-- association :配置一对一属性 --><!-- property:Order里面的User属性名 --><!-- javaType:属性类型 --><!-- column:当前查询中查询的column指定的数据会作为子查询queryUserById的参数 --><!-- select:子查询的sql语句的ID --><association property="user" javaType="User"  column="user_id" select="queryUserById"/></resultMap><!-- 关联映射1对1 --><select id="queryOrderUserResultMap" resultMap="orderUserMap">SELECTo.id,o.user_id,o.number,o.createtime,o.noteFROM`orders` o</select><select id="queryUserById" resultType="User" parameterType="int">select * from user where id = #{id}</select>

关联映射 1对1(连接查询)2:

import java.util.Date;/*** * TODO** 2018年10月18日下午7:02:58*/
public class Order {//订单编号   idprivate Integer id;//用户编号  user_idprivate Integer userId;//订单号   numerprivate String number;//下单时间 createTimeprivate Date createTime;//备注  noteprivate String note;//让order持有user的引用private User user;/*** @return the id*/public Integer getId() {return id;}/*** @param id the id to set*/public void setId(Integer id) {this.id = id;}/*** @return the userId*/public Integer getUserId() {return userId;}/*** @param userId the userId to set*/public void setUserId(Integer userId) {this.userId = userId;}/*** @return the number*/public String getNumber() {return number;}/*** @param number the number to set*/public void setNumber(String number) {this.number = number;}/*** @return the createTime*/public Date getCreateTime() {return createTime;}/*** @param createTime the createTime to set*/public void setCreateTime(Date createTime) {this.createTime = createTime;}/*** @return the note*/public String getNote() {return note;}/*** @param note the note to set*/public void setNote(String note) {this.note = note;}/*** @return the user*/public User getUser() {return user;}/*** @param user the user to set*/public void setUser(User user) {this.user = user;}/* (non-Javadoc)* @see java.lang.Object#toString()*/@Overridepublic String toString() {return "Order [id=" + id + ", userId=" + userId + ", number=" + number + ", createTime=" + createTime+ ", note=" + note + "]";}}
 <!-- 关联映射 1对1 --><resultMap type="Order" id="orderUserMap"><!-- order的主键 --><id column="id" property="id" /><!-- 映射其他普通列 --><result column="user_id" property="userId" /><result property="number" column="number" /><result property="createTime" column="createTime" /><!-- association :配置一对一属性 --><!-- property:Order里面的User属性名 --><!-- javaType:属性类型 --><association property="user" javaType="User"><!-- 映射user的属性和列 --><id column="user_id" property="id"/><result column="uname" property="username"/><result column="uaddr" property="address"/></association></resultMap><!-- 关联映射1对1 --><select id="queryOrderUserResultMap" resultMap="orderUserMap">SELECTo.id,o.user_id,o.number,o.createtime,o.note,u.username uname,u.address uaddrFROM`orders` oLEFT JOIN `user` u ON o.user_id = u.id</select>

一对多 (单表查询非连接查询):

import java.util.Date;
import java.util.List;/*** * TODO** 2018年10月13日下午2:15:12*/
public class User {private int id;private String username;private Date birthday;private int sex;private String address;private String uuid;//使用集合让用户持有一组订单的引用private List<Order> orders;/*** @return the uuid*/public String getUuid() {return uuid;}/*** @param uuid the uuid to set*/public void setUuid(String uuid) {this.uuid = uuid;}/*** @return the id*/public int getId() {return id;}/*** @param id the id to set*/public void setId(int id) {this.id = id;}/*** @return the username*/public String getUsername() {return username;}/*** @param username the username to set*/public void setUsername(String username) {this.username = username;}/*** @return the birthday*/public Date getBirthday() {return birthday;}/*** @param birthday the birthday to set*/public void setBirthday(Date birthday) {this.birthday = birthday;}/*** @return the sex*/public int getSex() {return sex;}/*** @param sex the sex to set*/public void setSex(int sex) {this.sex = sex;}/*** @return the address*/public String getAddress() {return address;}/*** @param address the address to set*/public void setAddress(String address) {this.address = address;}/*** @return the orders*/public List<Order> getOrders() {return orders;}/*** @param orders the orders to set*/public void setOrders(List<Order> orders) {this.orders = orders;}/*** @param id* @param username* @param birthday* @param sex* @param address*/public User(int id, String username, Date birthday, int sex, String address) {super();this.id = id;this.username = username;this.birthday = birthday;this.sex = sex;this.address = address;}/*** */public User() {super();// TODO Auto-generated constructor stub}@Overridepublic String toString() {return "User [id=" + id + ", username=" + username + ", birthday=" + birthday + ", sex=" + sex + ", address="+ address + ", uuid=" + uuid + "]";}}
 <!-- 配置一个resultMap解决一对多个问题 --><resultMap type="User" id="userOrderMap"><!-- 配置用户主键 --><id column="id" property="id"/><!-- 配置普通属性 --><result column="username" property="username"/><result column="birthday" property="birthday"/><result column="sex" property="sex"/><result column="address" property="address"/><!-- 映射一对多关系(映射集合) --><!-- javaType:配置集合类型 --><!-- ofType:配置集合内部的数据类型 --><!-- column:当前查询中查询的column指定的数据会作为子查询queryUserById的参数 --><!-- select:子查询的sql语句的ID --><collection property="orders" javaType="list" column="id" ofType="Order" select="queryOrderByUserId"/></resultMap><!-- 查询所有的用户以及他们的订单 --><select id="queryAllUserAndOrder" resultMap="userOrderMap">SELECTu.id,u.username,u.birthday,u.sex,u.address
FROM`user` u</select><!-- 通过用户编号查询订单 --><select id="queryOrderByUserId" parameterType="int" resultType="Order">SELECTo.id,o.user_id userId,o.number,o.createtime,o.noteFROM`orders` o where o.user_id = #{id}</select>

一对多 (连接查询):

import java.util.Date;
import java.util.List;/*** * TODO** 2018年10月13日下午2:15:12*/
public class User {private int id;private String username;private Date birthday;private int sex;private String address;private String uuid;//使用集合让用户持有一组订单的引用private List<Order> orders;/*** @return the uuid*/public String getUuid() {return uuid;}/*** @param uuid the uuid to set*/public void setUuid(String uuid) {this.uuid = uuid;}/*** @return the id*/public int getId() {return id;}/*** @param id the id to set*/public void setId(int id) {this.id = id;}/*** @return the username*/public String getUsername() {return username;}/*** @param username the username to set*/public void setUsername(String username) {this.username = username;}/*** @return the birthday*/public Date getBirthday() {return birthday;}/*** @param birthday the birthday to set*/public void setBirthday(Date birthday) {this.birthday = birthday;}/*** @return the sex*/public int getSex() {return sex;}/*** @param sex the sex to set*/public void setSex(int sex) {this.sex = sex;}/*** @return the address*/public String getAddress() {return address;}/*** @param address the address to set*/public void setAddress(String address) {this.address = address;}/*** @return the orders*/public List<Order> getOrders() {return orders;}/*** @param orders the orders to set*/public void setOrders(List<Order> orders) {this.orders = orders;}/*** @param id* @param username* @param birthday* @param sex* @param address*/public User(int id, String username, Date birthday, int sex, String address) {super();this.id = id;this.username = username;this.birthday = birthday;this.sex = sex;this.address = address;}/*** */public User() {super();// TODO Auto-generated constructor stub}@Overridepublic String toString() {return "User [id=" + id + ", username=" + username + ", birthday=" + birthday + ", sex=" + sex + ", address="+ address + ", uuid=" + uuid + "]";}}
 <!-- 配置一个resultMap解决一对多个问题 --><resultMap type="User" id="userOrderMap"><!-- 配置用户主键 --><id column="id" property="id"/><!-- 配置普通属性 --><result column="username" property="username"/><result column="birthday" property="birthday"/><result column="sex" property="sex"/><result column="address" property="address"/><!-- 映射一对多关系(映射集合) --><!-- javaType:配置集合类型 --><!-- ofType:配置集合内部的数据类型 --><collection property="orders" javaType="list" ofType="Order"><!-- 映射主键 --><id column="oid" property="id"/><!-- 映射普通属性 --><result column="number" property="number"/><result column="createtime" property="createTime"/><result column="note" property="note"/></collection></resultMap><!-- 查询所有的用户以及他们的订单 --><select id="queryAllUserAndOrder" resultMap="userOrderMap">SELECTu.id,u.username,u.birthday,u.sex,u.address,o.id oid,o.number,o.createtime,o.note
FROM`user` u
LEFT JOIN `orders` o ON u.id = o.user_id;</select>

传参为一个集合+一个参数:

dao层接口为:

List<Menu> listMenueByIdAndLevel(@Param("ids") List<Integer> ids,@Param("level") Integer level);

对应xml:

</select><select id="listMenueByIdAndLevel" resultMap="BaseResultMap">SELECT *FROM menuWHERE menu_Id IN <foreach collection="ids" open="(" close=")" separator="," item="id">#{id}</foreach>AND menu_Level = #{level}</select>

这是用到是<foreach>标签:

属性

collection:

1. 传参只是一个集合:list;

2. 传参为一个数组:array;

3. 传参为一个map:map;

4. 传参为一个dao接口指定的值,比如上面的ids(这里我就是用的这种方法);

open:

表示语句开始

close:

表是语句结束

separator:

每次分隔时的符号

item:

遍历时获取元素的别名
-------------------------------------------------------------------------------------------------------------------------------

foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:

  1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list .
  2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array .
  3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.

===========================================================================

<!--List:forech中的collection属性类型是List,collection的值必须是:list,item的值可以随意,Dao接口中参数名字随意 --><select id="getEmployeesListParams" resultType="Employees">select *from EMPLOYEES ewhere e.EMPLOYEE_ID in<foreach collection="list" item="employeeId" index="index"open="(" close=")" separator=",">#{employeeId}</foreach></select><!--Array:forech中的collection属性类型是array,collection的值必须是:list,item的值可以随意,Dao接口中参数名字随意 --><select id="getEmployeesArrayParams" resultType="Employees">select *from EMPLOYEES ewhere e.EMPLOYEE_ID in<foreach collection="array" item="employeeId" index="index"open="(" close=")" separator=",">#{employeeId}</foreach></select><!--Map:不单单forech中的collection属性是map.key,其它所有属性都是map.key,比如下面的departmentId --><select id="getEmployeesMapParams" resultType="Employees">select *from EMPLOYEES e<where><if test="departmentId!=null and departmentId!=''">e.DEPARTMENT_ID=#{departmentId}</if><if test="employeeIdsArray!=null and employeeIdsArray.length!=0">AND e.EMPLOYEE_ID in<foreach collection="employeeIdsArray" item="employeeId"index="index" open="(" close=")" separator=",">#{employeeId}</foreach></if></where></select>
public interface EmployeesMapper { List<Employees> getEmployeesListParams(List<String> employeeIds);List<Employees> getEmployeesArrayParams(String[] employeeIds);List<Employees> getEmployeesMapParams(Map<String,Object> params);
}

mybatis多对一,一对一,多对多resultMap映射,pojo映射,传参集合,封装的对象传参相关推荐

  1. 数据库建表-- 一对多/多对一/一对一/多对多 关系

     转自: https://blog.csdn.net/zhongshan_c/article/details/8210196 数据库建表-- 一对多/多对一/一对一/多对多 关系 关联映射:一对多 ...

  2. MyBatis中resuleMap一对一和一对多属性字段映射

    处理属性和字段不一致 通过resultMap 当Java中实体类属性和数据库字段不一致的时候,我们可以通过resultMap进行映射,例如我们要查询一个user表中的用户的全部信息,在Java中属性是 ...

  3. Mybatis【一对多、多对一、多对多】知识要点

    Mybatis[多表连接] 我们在学习Hibernate的时候,如果表涉及到两张的话,那么我们是在映射文件中使用<set>..<many-to-one>等标签将其的映射属性关联 ...

  4. MyBatis之使用resultMap实现高级映射

    MyBatis之使用resultMap实现高级映射 2017/09/30 对于数据库中对表的增删改查操作,我们知道增删改都涉及的是单表,而只有查询操作既可以设计到单表操作又可以涉及到多表操作,所以对于 ...

  5. MyBatis一对多,多对一,多对多

    MyBatis中的一对多和对多一,多对多 主要就是resultMap中 association(关联) – 一个复杂的类型关联;许多结果将包成这种类型(多对一) 嵌套结果映射 – 关联本身可以是一个 ...

  6. layui表格显示后台的多表的级联查询(多对多,多对一)带mybatis级联查询源码,已解决

    在ssm和springboot项目中我们存在表与表之间一对多和多对多的情况,那么他们就要进行级联查询查询出相关联的数据,级联查询涉及到的问题就是在一个实体类中存在另一个实体类的对象(一对一)或者对象集 ...

  7. Mybatis连3表查询数据resultMap结果映射

    Mybatis连结3表查询数据resultMap结果映射 一.前言 Mybatis实现了sql与java代码的分离,达到了解耦合的目的,配置sql语句时有个resultType="" ...

  8. MyBatis 一对多、多对一的处理~

    目录 1.多对一处理 1.1.打个比方 1.2.实例环境搭建 1.3.目标:查询所有的学生信息以及对应的老师信息 方式一:查询嵌套 方式二:结果嵌套 2.一对多处理 2.1.目标:获取指定老师下的所有 ...

  9. MyBatis一对多与多对一

    一对多 项目结构目录 项目结构搭建 数据库文件 CREATE TABLE `teacher` (`id` INT(10) NOT NULL PRIMARY KEY,`name` VARCHAR(30) ...

最新文章

  1. 如何查看服务器有多少网站--免费工具
  2. mysql数据库技术方案,MySql数据库优化方案
  3. 用python简单处理图片(4):图像中的像素访问
  4. 超级马里奥代码_任天堂的源码泄露,揭示超级马里奥的前世之生
  5. mybatis06 增删改差 源码
  6. Reapter 中客户端控件和服务器端控件的选择
  7. java课程设计模拟画图_课程设计java画板模拟画图工具
  8. Java编码规范总结
  9. ubuntu安装无线网卡驱动(Ralink)
  10. mysql的数据库实例名是啥_数据库名、数据库实例名与数据库域名
  11. 网站制作流程详解(学做网站第一步)
  12. 如何定位到服务器CPU飙高的原因
  13. MIME sniffing攻击
  14. 微信小程序跳转微信小店
  15. rabbitmq和erlang版本对应关系
  16. Java 8: 元空间(Metaspace)
  17. 学习笔记 计算机组成原理_名词解释
  18. Android开发者选项——GPU呈现模式分析
  19. python清除输出内容_如何只清除python输出控制台中的最后一行?
  20. FTP文件传输协议原理详解(两种工作模式)

热门文章

  1. 创建一个新的extender
  2. 【Android开发艺术探索】RemoteViews
  3. 运用EL表达式进行复杂比较(在JSTL中调用函数)
  4. 图片上传预览 (URL.createObjectURL)
  5. ios隐藏导航栏底线条和导航、状态栏浙变色
  6. android的ant脚本build.xml自动生成模板
  7. UVA 297 Quadtrees
  8. 黑客如何让脚本定时执行
  9. Centos 7安装报错:Warning:dracut-initqueue timeout - starting timeout scripts
  10. 编写可靠Linux shell脚本的建议