mybatis 多表关联mapper文件写法操作

两张表SystemParam(系统参数表) Suit (主题)

SystemParam 与 Suit 是多对一

Suit 的higerSuit字段是Suit 的父及主题id 是多对一,需要自连接查询,因为重名所以父表sql字段加别名

mapper方法

Systemparam selectJoinSuit(String strparamcode);

Po类

public class Systemparam {
 //ManyToOne "主题"
 private Suit suitobj;
 private String strparamcode;
 private String strenable;
 private String strparamname;
 //suit表主键
 private String suit;
 private String strparamvalue;
} 

public class Suit {
 //ManyToOne
 private Suit suit;
 //主键
 private String strsuitcode;
 private String strsuitname;
 //父级id
 private String higersuit;
}

resultMap的写法

<resultMap id="BaseResultMap" type="net.transino.model.Systemparam" >
 <id column="strParamCode" property="strparamcode" jdbcType="VARCHAR" />
 <result column="strEnable" property="strenable" jdbcType="VARCHAR" />
 <result column="strParamName" property="strparamname" jdbcType="VARCHAR" />
 <result column="suit" property="suit" jdbcType="VARCHAR" />
</resultMap>

resultMap 使用extends 继承上级map

<resultMap id="ResultMapWithBLOBs" type="net.transino.model.Systemparam" extends="BaseResultMap" >
 <result column="strParamValue" property="strparamvalue" jdbcType="LONGVARCHAR" />
</resultMap>
<resultMap id="JoinsuitMap" type="net.transino.model.Systemparam" extends="ResultMapWithBLOBs" >
 <association property="suitobj" javaType="Suit">
 <id column="strSuitCode" property="strsuitcode" jdbcType="VARCHAR" />
 <result column="strSuitName" property="strsuitname" jdbcType="VARCHAR" />
 <result column="higerSuit" property="higersuit" jdbcType="VARCHAR" />
 <association property="suit" javaType="Suit">
 <id column="pstrSuitCode" property="strsuitcode" jdbcType="VARCHAR" />
 <result column="pstrSuitName" property="strsuitname" jdbcType="VARCHAR" />
 <result column="phigerSuit" property="higersuit" jdbcType="VARCHAR" />
 </association>
 </association>
</resultMap>

select写法

<select id="selectJoinSuit" resultMap="JoinsuitMap" parameterType="java.lang.String">
 select
 systempara0_.*,
 suit1_.*,
 suit2_.strSuitCode pstrSuitCode,
 suit2_.strSuitName pstrSuitName,
 suit2_.higerSuit phigerSuit
 from SystemParam systempara0_
 LEFT OUTER JOIN
 Suit suit1_
 ON systempara0_.suit=suit1_.strSuitCode
 LEFT OUTER JOIN
 Suit suit2_
 ON suit1_.higerSuit=suit2_.strSuitCode
 WHERE
 systempara0_.strParamCode=#{strparamcode,jdbcType=VARCHAR}
</select>

补充知识:Mybatis中resultMap标签实现多表查询(多个对象)

1 n+1

1 在teacher中添加List student,

public class Teacher {
 private int id;
 private String name;
 private List<Student> list;

2 在studentMapper.xml中添加通过tid查询

<select id="selByTid" resultType="Student" parameterType="int">
 select * from student where tid=#{0}
</select> 

3 在TeacherMapper.xml中添加查询全部

<resultMap type="Teacher" id="mymap1">
 <id column="id" property="id"/>
 <result column="name" property="name"/>
 <collection property="list" ofType="Student" select="com.bjsxt.mapper.StudentMapper.selByTid" column="id"></collection>
</resultMap>
<select id="selAll" resultMap="mymap1">
 select * from teacher
</select> 

其中collection是当属性为集合类型时使用的标签

2 多表联合

<resultMap type="Teacher" id="stumap1">
 <id column="tid" property="id"/>
 <result column="tname" property="name"/>
 <collection property="list" ofType="Student">
 <id column="sid" property="id"/>
 <result column="sname" property="name"/>
 <result column="age" property="age"/>
 <result column="tid" property="tid"/>
 <association property="teacher" select="com.bjsxt.mapper.TeacherMapper.selById" column="tid"></association>
 </collection>
 </resultMap>

 <select id="selAll1" resultMap="stumap1">
 select t.id tid,t.name tname,s.id sid,s.name sname,age,tid from teacher t left join student s on t.id=s.tid
 </select>

以上这篇mybatis 多表关联mapper文件写法操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Mybatis中传递多个参数的4种方法总结

    前言 现在大多项目都是使用Mybatis了,但也有些公司使用Hibernate.使用Mybatis最大的特性就是sql需要自己写,而写sql就需要传递多个参数.面对各种复杂的业务场景,传递参数也是一种学问. 下面给大家总结了以下几种多参数传递的方法. 方法1:顺序传参法 #{}里面的数字代表你传入参数的顺序. 这种方法不建议使用,sql层表达不直观,且一旦顺序调整容易出错. 方法2:@Param注解传参法 #{}里面的名称对应的是注解 @Param括号里面修饰的名称. 这种方法在参数不多的情况还

  • mybatis动态sql之Map参数的讲解

    mybatis 动态sql之Map参数 Mapper文件: <mapper namespace="com.cn.shoje.oa.modules.logistics.dao.PurcDao"> <select id="findAll" parameterType="Map" resultType="Purchase"> select * from prod_purchase where 1=1 <

  • Mybatis 动态表名+Map参数传递+批量操作详解

    需求: 之前项目一个变动,需要对3张mysql数据库表数据进行清洗,3张表表名不同,表结构完全相同,需要对这3张表进行相同的增.改.查动作,一开始比较紧急先对一张表进行操作,后来复制了3个一样的 service.dao.mapper等.后来对代码进行优化,研究了一下动态表名的处理. 1,查询操作: 查询操作只需要传入动态表名的时候,传递参数仍然是map mapper.xml内,需要使用statementType="STATEMENT",采用非预编译模式 mapper.xml内,动态表名

  • mybatis 多表关联mapper文件写法操作

    两张表SystemParam(系统参数表) Suit (主题) SystemParam 与 Suit 是多对一 Suit 的higerSuit字段是Suit 的父及主题id 是多对一,需要自连接查询,因为重名所以父表sql字段加别名 mapper方法 Systemparam selectJoinSuit(String strparamcode); Po类 public class Systemparam { //ManyToOne "主题" private Suit suitobj;

  • Mybatis增删改查mapper文件写法详解

      1. 插入 <mapper namespace="需要实现接口的全类名"> <insert id="需要实现的接口里的方法名" parameterType="方法参数类型,如果是对象要写全类名"> INSERT sql命令(命令里通过#{}获取对象属性) <!--注意属性名区分大小写 --> </insert> <mapper> EG: <mapper namespace=&q

  • mybatis主从表关联查询,返回对象带有集合属性解析

    目录 主从表关联查询,返回对象带有集合属性 VersionResult为接收返回数据对象 UpdateRecordEntity为从表数据 mapper.xml写法,这个是关键 sql查询语句 执行sql返回的数据 页面调取接口 mybatis关联查询(对象嵌套对象) 一种是用关联另一个resultMap的形式 一种联合查询(一对一)的实现 主从表关联查询,返回对象带有集合属性 昨天有同事让我帮着看一个问题,mybatis主从表联合查询,返回的对象封装集合属性.我先将出现的问题记录一下,然后再讲处

  • 使用AOP+反射实现自定义Mybatis多表关联查询

    目录 一.需求 二.核心代码 MapTo DoMap IDualMapper DualMapper DoMapAspect 三.使用方法 SysUser SysRole SysPermission SysUserService DoMapTests 测试数据 测试结果 一.需求 目前使用的ORM框架是Mybatis Plus,是Mybatis的增强框架,基础的CRUD的方法都集成了,开发起来很是方便.但是项目中总是需要多表关联查询. Mybatis的多表关联有两种 一.在Mapper中使用@Re

  • MyBatis找不到mapper文件的实现

    用的Idea,在写MyBatis时,测试发现有以下的报错信息 Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource dao/PersonMapper.xml 即找不到mapper文件 下面是我的目录结构 发现路径没问题啊,粘贴的相对路径啊 查阅资料后,发现idea对目录结构里的存放的文件类型有要求,mapper文件必须放入到resources目录里, 但后期mappe

  • Mybatis多表关联查询的实现(DEMO)

    概要 本节要实现的是多表关联查询的简单demo.场景是根据id查询某商品分类信息,并展示该分类下的商品列表. 一.Mysql测试数据 新建表Category(商品分类)和Product(商品),并插入几条测试数据. create table Category ( Id int not null auto_increment, Name varchar(80) null, constraint pk_category primary key (Id) ); INSERT INTO category

  • PostgresSql 多表关联删除语句的操作

    最近用PostgresSql数据库进行多表关联删除的操作,在写sql语句的时候遇到了问题: DELETE s.* FROM student s,classroom c WHERE s.cid = c.id AND s.sid = 1 DELETE FROM student s,classroom c WHERE s.cid = c.id AND s.sid = 1 上面两种写法操作后提示报错,下面是PostgresSql数据库对多表关联操作的正确用法,多张表之间用USING连接: DELETE

  • mybatis的使用-Mapper文件各种语法介绍

    一.查询 mybatis自定义查询条件,queryString.queryMap.limit,Mapper文件写法如下: <select id="getByQueryParam" parameterType="com.systom.base.BaseDaoQueryParam" resultMap="BaseResultMap"> SELECT * FROM user WHERE 1 = 1 <if test="par

  • mybatis 运行时加载自定义mapper文件方式

    mybatis 运行时加载自定义mapper文件 用mybatis一定要写mapper文件,这是使用mybatis的常识,但有时候应用需求,mapper文件中的节点需要动态生成,或者根据业务场景进行组装,那这个时候的SQL语句直接写在mapper文件显然不可取,又或者采用动态SQL完成,今天介绍一种方式,支行时加载自定义mapper配置文件. 我首先介绍一种mapper文件存在的写法,也是大家常用的,至于spring-mybatis配置方法,我这里就不列了: dao接口: package com

  • 如何自动生成Mybatis的Mapper文件详解

    前言 工作中使用mybatis时我们需要根据数据表字段创建pojo类.mapper文件以及dao类,并且需要配置它们之间的依赖关系,这样的工作很琐碎和重复,mybatis官方也发现了这个问题,因此给我们提供了mybatis generator工具来帮我们自动创建pojo类.mapper文件以及dao类并且会帮我们配置好它们的依赖关系. 实际上,最非常流行MyBatis-Plus中内置了代码生成器:采用代码或者 Maven 插件可快速生成 Mapper . Model . Service . Co

随机推荐