当前位置: 首页 > news >正文

一级a做爰片免费网站黄济南网站建设哪家便宜

一级a做爰片免费网站黄,济南网站建设哪家便宜,织梦做的网站为什么显示404,做木皮的网站一对一映射 预置条件 在某网络购物系统中,一个用户只能拥有一个购物车,用户与购物车的关系可以设计为一对一关系 数据库表结构(唯一外键关联) 创建两个实体类和映射接口 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 List<Order> 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 version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mapper.UserMapper"><select id="findUserAndCartByUserId" resultType="com.mybatis.entity.User">select*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 version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mapper.UserMapper"> <resultMap id="userMap" type="org.example.demo.User"><id property="userId" column="user_id"/><result property="userName" column="user_name"/><result property="password" column="password"/><result property="cart.cartId" column="cart_id"/><result property="cart.price" column="price"/></resultMap>   <select id="findUserAndCartByUserId" resultMap="userMap">select*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 id="userMap" type="org.example.demo.User"><id property="userId" column="user_id"/><result property="userName" column="user_name"/><result property="password" column="password"/><association property="cart"><id property="cartId" column="cart_id"/><result property="price" column="price"/></association></resultMap><select id="selectUserById" resultMap="userMap">select * from t_user where user_id = #{userId};</select>
association元素的嵌套查询

association元素的嵌套查询常用的属性如下

select:另一个查询映射的statement id,MyBatis会额外执行这个查询获取嵌套对象

column:列名,将主查询中列的结果作为嵌套查询的参数,column=“{prop1=col1,prop2=col2}”,其中prop1和prop2将作为嵌套查询的参数

fetchType:数据加载方式,可选值为lazy和eager,分别为延迟加载和积极加载,会覆盖全局的lazyLoadingEnable配置

    <resultMap id="userMap" type="org.example.demo.User"><id property="userId" column="user_id"/><result property="userName" column="user_name"/><result property="password" column="password"/><association property="cart" column="user_id" select="findCartbyUserId"fetchType="lazy"/></resultMap><resultMap id="cartMap" type="org.example.demo.Cart"><id property="cartId" column="cart_id"/><result property="price" column="price"/></resultMap><select id="findUserAndCartByUserId" resultMap="userMap">select * from t_suer u where user_id = #{user_id};</select><select id="findCartbyUserId" resultMap="cartMap">select * from t_cart c where c.user_id = #{user_id};</select>

嵌套查询会多执行SQL,当查询N条数据时,就会出现N+1次查询问题

为了解决N+1次查询问题,可以设置延迟加载策略,将association元素的fetchType属性设置为lazy 

设置为延迟加载的对象,默认情况下,当调用该对象的equals、hashCode、toString、clone方法时,就会加载该对象的全部数据

对比四种方式

前面的三种方式都属于“关联的嵌套结果映射“,即通过一次SQL查询根据表或指定的属性映射到不同的对象中

最后一种方式属于“关联的嵌套查询”,利用简单的SQL语句,通过多次查询得到想要的结果,可实现延迟加载效果

 

 

 

http://www.hkea.cn/news/29967/

相关文章:

  • 昭通网站制作aso优化技巧
  • 制作网站时怎样做滚动字幕新网站多久会被百度收录
  • 余姚物流做网站微信指数是搜索量吗
  • 怎样做网站轮播今日国内重大新闻事件
  • 想给大学做网站百度网盘搜索神器
  • jsp网站开发论文官方app下载安装
  • 关于机场建设的网站今日疫情最新情况
  • 网站域名注册服务商google浏览器官方
  • 通过网站开发工具怎么改自动跳网站百度指数有哪些功能
  • 可以发锚文本的网站百度搜索官方网站
  • 东莞网站建设企慕简述如何优化网站的方法
  • 可以做网站的公司seo外包
  • 自己怎么做网站视频赚钱5g网络优化培训
  • 数据库修改网站管理员密码seo网站有优化培训吗
  • 福田做商城网站建设找哪家公司好抖音怎么运营和引流
  • 厘米售卡站怎么做网站禁止搜索引擎收录的方法
  • 网站首页滚动图片怎么做谷歌搜索关键词排名
  • 嵩县网站开发友情链接获取的途径有哪些
  • 国家企业信息公示网(广东)海南快速seo排名优化
  • 高端网站设计 上海徐州seo排名公司
  • 泰安网站建设公司排名石家庄最新消息
  • 域名只做邮箱没网站要备案吗常见的网络推广方式包括
  • 昆山建设局网站360搜索首页
  • 正常做网站多少钱无锡网站制作无锡做网站
  • php做网站csdn网站seo公司哪家好
  • 今日头条建站工具何鹏seo
  • wordpress 培训模板优化落实疫情防控新十条
  • 关于做外汇现货的网站太原整站优化排名外包
  • 星悦做任务网站是新网站百度收录
  • 十大营销网站seo关键词查询工具