普通映射:
<!-- 使用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">
select
id,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;
}
@Override
public String toString() {
return "OrderUser [ "+super.toString()+",username=" + username + ", address=" + address + "]";
}
}
<!-- 查询orderUser对象 -->
<select id="queryOrderUser" resultType="OrderUser">
SELECT
o.id,
o.user_id userId,
o.number,
o.createtime,
o.note,
u.username,
u.address
FROM
`orders` o
LEFT 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 {
//订单编号 id
private Integer id;
//用户编号 user_id
private Integer userId;
//订单号 numer
private String number;
//下单时间 createTime
private Date createTime;
//备注 note
private 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()
*/
@Override
public 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">
SELECT
o.id,
o.user_id,
o.number,
o.createtime,
o.note
FROM
`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 {
//订单编号 id
private Integer id;
//用户编号 user_id
private Integer userId;
//订单号 numer
private String number;
//下单时间 createTime
private Date createTime;
//备注 note
private 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()
*/
@Override
public 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">
SELECT
o.id,
o.user_id,
o.number,
o.createtime,
o.note,
u.username uname,
u.address uaddr
FROM
`orders` o
LEFT 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
}
@Override
public 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">
SELECT
u.id,
u.username,
u.birthday,
u.sex,
u.address
FROM
`user` u
</select>
<!-- 通过用户编号查询订单 -->
<select id="queryOrderByUserId" parameterType="int" resultType="Order">
SELECT
o.id,
o.user_id userId,
o.number,
o.createtime,
o.note
FROM
`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
}
@Override
public 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">
SELECT
u.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 menu
WHERE 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种情况:
- 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list .
- 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array .
- 如果传入的参数是多个的时候,我们就需要把它们封装成一个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 e
where 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 e
where 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);
}