做短袖的网站,那个网站做推广比较好,深圳网站建设便宜信科网络,徐州市中心做网站的公司招聘一对一映射
预置条件
在某网络购物系统中#xff0c;一个用户只能拥有一个购物车#xff0c;用户与购物车的关系可以设计为一对一关系 数据库表结构#xff08;唯一外键关联#xff09; 创建两个实体类和映射接口 package org.example.demo;import lombok.Data;import … 一对一映射
预置条件
在某网络购物系统中一个用户只能拥有一个购物车用户与购物车的关系可以设计为一对一关系 数据库表结构唯一外键关联 创建两个实体类和映射接口 package org.example.demo;import lombok.Data;import java.util.List;Data
public class User {private Integer userId;private String userName;private String password;private Cart cart;private ListOrder orderList;
}
package org.example.demo;import lombok.Data;Data
public class Cart {private Integer cartId;private double price;
}
实现根据用户id查询出所有用户信息包括该用户的购物车信息
package org.example.mapper;import org.example.demo.User;import java.util.List;public interface UserMapper {User findUserAndCartByUserId(Integer userId);
} MyBatis中处理一对一关联关系的方法有四种
使用自动映射处理一对一关系
使用自动映射处理一对一关系也就是通过别名自动将值匹配到对应的字段上
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.example.mapper.UserMapperselect idfindUserAndCartByUserId resultTypecom.mybatis.entity.Userselect*from t_user uinner join t_cart c on u.user_id c.user_idwhere u.user_id #{userId};/select
/mapper
复杂的属性映射时可以多层嵌套比如将cart表中的cart_id字段映射到Cart.id属性上
使用resultMap配置一对一映射
使用这种方式同自动映射方式相似之处为Cart中的属性配置部分使用了”Cart.”前缀
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.example.mapper.UserMapper resultMap iduserMap typeorg.example.demo.Userid propertyuserId columnuser_id/result propertyuserName columnuser_name/result propertypassword columnpassword/result propertycart.cartId columncart_id/result propertycart.price columnprice//resultMap select idfindUserAndCartByUserId resultMapuserMapselect*from t_user uinner join t_cart c on u.user_id c.user_idwhere u.user_id #{userId};/select
/mapper
使用association元素配置一对一映射
association元素用于和一个复杂的类型进行关联
association元素包含以下属性
property对应实体类中的属性名必填项
javaType属性对应的java类型可选项
resultMap可以直接使用现有的resultMap而不需要在这里配置可选项 resultMap iduserMap typeorg.example.demo.Userid propertyuserId columnuser_id/result propertyuserName columnuser_name/result propertypassword columnpassword/association propertycartid propertycartId columncart_id/result propertyprice columnprice//association/resultMapselect idselectUserById resultMapuserMapselect * from t_user where user_id #{userId};/select
association元素的嵌套查询
association元素的嵌套查询常用的属性如下
select另一个查询映射的statement idMyBatis会额外执行这个查询获取嵌套对象
column列名将主查询中列的结果作为嵌套查询的参数column“{prop1col1,prop2col2}”其中prop1和prop2将作为嵌套查询的参数
fetchType数据加载方式可选值为lazy和eager分别为延迟加载和积极加载会覆盖全局的lazyLoadingEnable配置 resultMap iduserMap typeorg.example.demo.Userid propertyuserId columnuser_id/result propertyuserName columnuser_name/result propertypassword columnpassword/association propertycart columnuser_id selectfindCartbyUserIdfetchTypelazy//resultMapresultMap idcartMap typeorg.example.demo.Cartid propertycartId columncart_id/result propertyprice columnprice//resultMapselect idfindUserAndCartByUserId resultMapuserMapselect * from t_suer u where user_id #{user_id};/selectselect idfindCartbyUserId resultMapcartMapselect * from t_cart c where c.user_id #{user_id};/select
嵌套查询会多执行SQL当查询N条数据时就会出现N1次查询问题
为了解决N1次查询问题可以设置延迟加载策略将association元素的fetchType属性设置为lazy
设置为延迟加载的对象默认情况下当调用该对象的equals、hashCode、toString、clone方法时就会加载该对象的全部数据
对比四种方式
前面的三种方式都属于“关联的嵌套结果映射“,即通过一次SQL查询根据表或指定的属性映射到不同的对象中
最后一种方式属于“关联的嵌套查询”利用简单的SQL语句通过多次查询得到想要的结果可实现延迟加载效果