首页 > 其他分享 >Mybatis:通过自动映射解决一对一映射字段

Mybatis:通过自动映射解决一对一映射字段

时间:2022-12-18 17:35:12浏览次数:46  
标签:name 映射 一对一 grade autoMapping Mybatis cno id

像是下面这样的,如果真要一个个地去把 reulst 子标签写出来,有点不太现实,还好 mybatis 提供了 autoMapping,可以作用在 association 和 resultMap 标签上:

<resultMap id="mapOfQueryMyself" type="Student" autoMapping="true">
  <association property="college" columnPrefix="c_" javaType="College">
    <id property="cno" column="cno"/>
    <result property="name" column="name"/>
    <result property="intro" column="intro"/>
  </association>
  <association property="profession" columnPrefix="p_" javaType="Profession" autoMapping="true">
    <id property="pno" column="pno"/>
    <result property="name" column="name"/>
    <result property="type" column="type"/>
    <result property="eduSys" column="edu_sys"/>
    <result property="degree" column="degree"/>
    <result property="code" column="code"/>
  </association>
  <association property="grade" columnPrefix="g_" javaType="Grade">
    <id property="gno" column="gno"/>
    <result property="name" column="name"/>
    <result property="layer" column="layer"/>
    <result property="grade" column="grade"/>
  </association>
</resultMap>

用 autoMapping 之前,联合查询中,一对一关系需要给字段别名,最好用一种格式,比如 c_xxx,在 association 中添加 columnPrefix,然后再开启 autoMapping 就可以完美匹配查询过后的字段:

<resultMap id="mapOfQueryMyself" type="Student" autoMapping="true">
  <association property="college" columnPrefix="c_" javaType="College" autoMapping="true"/>
  <association property="profession" columnPrefix="p_" javaType="Profession" autoMapping="true"/>
  <association property="grade" columnPrefix="g_" javaType="Grade" autoMapping="true"/>
</resultMap>

<select id="queryMyself" resultMap="mapOfQueryMyself">
  SELECT s.*,
         c.cno     c_cno,
         c.name    c_name,
         p.pno     p_pno,
         p.name    p_name,
         p.type    p_type,
         p.edu_sys p_edu_sys,
         p.degree  p_degree,
         p.code    p_code,
         g.gno     g_gno,
         g.name    g_name,
         g.layer   g_layer,
         g.grade   g_grade
  FROM `students` as s
         JOIN `colleges` as c on c.cno = s.college_id
         JOIN `grades` as g on g.gno = s.grade_id
         JOIN `professions` as p on p.pno = s.profession_id
  WHERE s.sno = 1;
</select>

标签:name,映射,一对一,grade,autoMapping,Mybatis,cno,id
From: https://www.cnblogs.com/Enziandom/p/16990630.html

相关文章

  • 通过docker启动redis,存在端口映射和数据卷
    先将redis.conf文件进行创建,因为如果不先创建,在redis启动的时候,/etc/redis/中本没有文件,就会将redis.conf创建为一个文件夹mkdir-p/mydata/redis/conftouch/mydata/re......
  • Mybatis:解决实体类驼峰命名与数据库字段之间映射的问题
    数据库的命名规则都是_来隔开单词,Java中是驼峰命名法,所以导致实体类与数据库字段不一致,从而返回的结果有部分会被丢失。一、可以在mapper.xml中通过resultMap来解决:......
  • nginx配置conf(转发+文件映射)
     worker_processes1;events{worker_connections1024;}http{includemime.types;default_typeapplication/octet-stream;sendfileon;#tcp_nopushon;......
  • MyBatis核心配置文件详解
    目录environmentstransactionManagerDataSource引入jdbc.propertiestypeAliasessettings下划线转驼峰延迟加载MappersIDEA核心配置模板及解释environments可以配置多个......
  • Mybatis-Spring
    Mybatis-Spring版本关系图当前环境JDK8Mybatis-Spring2.1.0Mybatis3.5.11Log4j1.2.17lombok1.18.24SpringFramework5.3.20Dbcp2.9.0......
  • 基于Mybatis-Plus实现Geometry字段在PostGis空间数据库中的使用
    背景在OGC标准中,通常空间字段是由Geometry类型来表示。而一般编程语言中是没有这种数据类型的。以java为例,怎么操作这些数据,满足业务需求呢?跟着本文一起来学习吧。今天介绍......
  • RequestMappingHandlerMapping请求地址映射流程!
    上篇文章里,我们讲解了RequestMappingHandlerMapping请求地址映射的初始化流程,理解了@Controller和@RequestMapping是如何被加载到缓存中的。今天我们来进一步学习,在接收到......
  • 【MyBatis】MyBatis入门教程
    一、参考资料​​mybatis–MyBatis3|简介​​​​mybatis-spring官方文档​​​​【狂神说Java】Mybatis最新完整教程IDEA版通俗易懂_哔哩哔哩_bilibili​​​​GitHub......
  • Mybatis3
    Mybatis作用域和生命周期SqlSessionFactoryBuilder最佳作用域是方法作用域,一旦创建了SqlSessionFactory就不再需要它了。可以重用SqlSessionFactoryBuilder来创建多......
  • mybatis报错:can not find lambda cache for this property
    因为MP3.2+之后不会缓存实体类的父类字段信息,所以在使用泛型的Lambda表达式时会报错.{@codeMybatisPlusException:cannotfindlambdacacheforthisentity[com.cop......