MyBatis one to one 二次查询

MyBatis使用注解式@One和@Many实现一对一、一对多关联查询 在MyBatis 3提供了新的基于注解的配置,下面将使用MyBatis的注解式@One和@Many实现一对一、一对多关联查询。 (1)创建SpringBoot项目,项目结构如下图: (2)添加pom.xml配置信息 在pom.xml配置文件中添加MyBatis、MySQL的JDBC数据库驱动。 <!-- MyBatis与SpringBoot整合依赖 --> <dependency> <groupId>org.mybatis.spring.boot&. 阅读详情

接one to one

二次查询的映射文件
PersonDao.xml

<?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="com.hsj.dao.PersonDao">


    <select id="getPersons" resultMap="resultMap_Person">
        select * from t_person
    </select>

    <resultMap type="person" id="resultMap_Person">
        <id column="p_id" property="id"/>
        <result column="p_name" property="name"/>
        <!-- 
        column="p_id":二次查询时传递的值对应的字段
         -->
        <association property="card" column="p_id" javaType="card" select="getCardById">

        </association>
    </resultMap>

    <select id="getCardById" parameterType="int" resultMap="resultMap_card">
        select * from t_card where person_id=#{id}
    </select>

    <resultMap type="card" id="resultMap_card">
        <id column="c_id" property="id"/>
        <result column="c_cardno" property="cardNo"/>
        <result column="c_address" property="address"/>
    </resultMap>


    <!-- 根据人的id值查询人对象 -->
    <select id="getPersonById"  parameterType="int" resultMap="resultMap_Person">
        select * from t_person p inner join t_card c on p.p_id=c.person_id and p_id=#{id}
    </select>
</mapper>

CardDao.xml

<?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="com.hsj.dao.CardDao">


    <select id="getCards" resultMap="resultMap_Card">
        select * from t_card 
    </select>

    <resultMap type="card" id="resultMap_Card">
        <id column="c_id" property="id"/>
        <result column="c_cardno" property="cardNo"/>
        <result column="c_address" property="address"/>
        <association property="person" column="person_id" javaType="person" select="getPersonById">

        </association>
    </resultMap>

    <select id="getPersonById" parameterType="int" resultMap="resultMap_Person">
        select * from t_person where p_id=#{id}
    </select>

    <resultMap type="person" id="resultMap_Person">
        <id column="p_id" property="id"/>
        <result column="p_name" property="name"/>
    </resultMap>


    <!-- 根据id查询卡对象 -->
    <select id="getCardById" parameterType="int" resultMap="resultMap_Card">
        select * from t_card where c_id=#{id}
    </select>
</mapper>

测试类接one to one

Mybatis巧用@One注解一个SQL联合查询语句实现一对一查询 阅读详情

相关推荐

Jmeter压测报告

账户分析压测报告 一、测试内容 本次测试是针对账户分析进行的压力测试,在账户分析查询接口中,只对用户资产查询接口进行压力测试,其中不包含用户签名校验功能。 二、测试方法 本次采用apache的开源测试工具jmeter,采用本地动态拼装请求数据并通过http协议GET方式发送每日盈亏查询请求。并采用1000个用户账号测试访问情况。...

qq_31108731的博客 6624

mybatis 常用注解用法简介

@Insert 写法:@Insert(“SQL语句”) 使用在接口方法上 @Update 写法:@Update(“SQL语句”) 使用在接口方法上 @Delete 写法:@Delete(“SQL语句”) 使用在接口方法上 @Select 写法:@Select(“SQL语句”) 使用在接口方法上 @Result 写法:@Result(column = “oid”,property = “id”) 使用在@Results注解中,定义列名和变量名的映射关系,代替了<id>标签和<result&g

Kaysen的博客 887

关于token无痛刷新的两个方案

目录方案一方案二 方案一 思路: 后端需要返回一个token过期的时间,在请求发起前拦截每个请求,判断token的有效时间是否已经过期,如果已过期,则暂时将请求挂起,先刷新token后再继续请求。 前期思考: 返回过期时间是为了判断是否过期的,但是如果服务器时间和系统时间不一致呢? 如果同时发起多个请求时怎么办? 实现: 因为是在请求前进行拦截,所以这里用axios的axios.interceptors.request.use()方法 首先,关于服务器时间和系统时间不一致的问题,我的解决方法是:

Yugoup的博客 2715

java 入门测试代码(spring boot 14) MyBatis一对一映射注解@One的使用及理解

前言 本着更深入地使用注解来开发,慢慢地发现脱离了XML的MyBatis好多好多的坑。我觉得很大的原因还是来自于官方文档的不完善,以及使用注解开发持久层的人并不多见,导致网络上的相关讨论过于贫瘠并重复化,毕竟对于复杂查询的支持,我认为还没有在XML里写一条SQL方便呢~ 所以这篇文章用于补充一语带过的官方文档,记录下自己趟过的坑。 正文 一对一映射 注解化一对一映射其实就是将XML中的R...

yangzm的专栏 1942

一个测试工程师走进一家酒吧……

一个测试工程师走进一家酒吧,要了一杯啤酒;一个测试工程师走进一家酒吧,要了一杯咖啡;一个测试工程师走进一家酒吧,要了 0.7 杯啤酒;一个测试工程师走进一家酒吧,要了-1 杯啤酒;一个测试...

字节跳动技术团队官方博客 4884

Power Automate实战:用SharePoint REST API批量处理列表数据(附JSON解析技巧)

本文深入探讨了如何利用Power Automate调用SharePoint REST API实现列表数据的批量自动化处理。文章详细解析了API的协同工作原理、高效的数据获取与筛选技巧,并重点分享了JSON数据解析的核心难点与实战解决方案,旨在帮助企业用户构建稳定可靠的数据处理流程,提升运营效率。

coffee的博客 769

Mybatis注解开发的@Results和@One注解

//在orderMapper里: @Results({ @Result(property = “id”, column = “id”), @Result(property = “orderTime”, column = “orderTime”), @Result(property = “total”, column = “total”), @Result(property = “user”, column = “uid”, javaType = User.class, one = @One(select =

weixin_45657091的博客 1836

【数据集】中国陆地生态系统通量观测网 ChinaFLUX

【数据集】中国陆地生态系统通量观测网 ChinaFLUX

WW、forever的博客 186

Mybatis 多表关联查询(2) one-to-many关系

Mybatis 多表关联查询(2) one-to-many关系本文中未说明的都在多表关联查询(1)中有说明。基础文件 在mybatis- config.xml文件加入两个映射文件 <mapper resource="com/adoken/mapper/ClassesMapper.xml"/> <mapper resource="com/adoken/mapper/StudentMapper.xml"

mqweishijiunuli的博客 1752

从零手搓大模型(一)文本如何变成 LLM 的输入

本文介绍了从原始文本到LLM输入向量的完整处理流程。主要内容包括: 文本预处理:通过正则表达式将原始文本分割成token列表,处理标点和空格。 词表构建:创建token到ID的映射字典(vocabulary),这是文本数字化的关键步骤。 简单tokenizer实现:封装了编码(encode)和解码(decode)功能,完成文本与token ID的相互转换。 处理流程:原始文本 → 分词 → token ID → 嵌入表示 → 最终输入向量。 数据形状变化:从字符串逐步转换为(batch_size, cont

echo的博客 998

【OpenFOAM】-olaFlow-算例4- irreg45degTank

不规则波浪模拟

ou_no的博客 2117

Mybatis基于注解的one-to-one, one-to-many联合查询,传参及传多个参数的办法

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34055787的博客 2507

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

Mybatis使用注解方式多表one-to-one对应查询

https://www.jianshu.com/p/f913511bdf6d

weixin_44254400的博客 890

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

MyBatis嵌套查询解析

摘要:本文详细解析MyBatis处理关联关系的两种方式:嵌套查询(Nested Select)和嵌套结果(Nested Result)。通过用户-订单案例,对比说明association和collection标签在一对多、多对一关系中的应用。重点分析两种方式的实现原理、性能差异及N+1问题,指出嵌套查询适合小数据量场景但存在性能隐患,而嵌套结果通过JOIN查询能有效解决性能问题。最后给出根据数据量选择方案、延迟加载等实用建议,帮助开发者优化MyBatis关联查询。

ywl470812087的博客 11万+

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

MyBatis查询报错:Expected one result (or null) to be returned by selectOne(), but found: 2

MyBatis在查询数据时,查询涉及到多张表,且查询的对象存在集合的属性时,如下查询会报错:Expected one result (or null) to be returned by selectOne(), but found: 2 <resultMap id="classesMap" type="cn.jam.entity.Classes"> <id column="c.id" property="id"/> <result column="c.name

qq_33954947的博客 4977

Mybatis 多表关联查询(1) one-to-one关系

Mybatis 多表关联查询(1) one-to-one关系推荐有mysql基础和java基础的伙伴阅读。 需要的jar包为 mysql-jdbc.jar(mysql数据包) mybatis-3.2.1.jar(mybatis核心包)基本文件 MybatisUtil.java (工具类) package com.adoken.util;import org.apache.ibatis.io.R

mqweishijiunuli的博客 1312

【持久化框架MyBatis3四】MyBatis3一对一关联查询

  当两个实体具有1对1的对应关系时,可以使用One-To-One的进行映射关联查询   One-To-One示例数据 以学生表Student和地址信息表为例,每个学生都有都有1个唯一的地址(现实中,这种对应关系是不合适的,因为人和地址是多对一的关系),这里只是演示目的   学生表   CREATE TABLE STUDENTS ( STUD_ID INT(11) N...

bit1129的博客 192

a complete example of using One to Many and One to One associations using MyBatis annotations

Dear Readers, Below is a complete example of using One to Many and One to One associations using MyBatis annotations. Example illustrates how we realized is-a and has-a relation (Candidate is a Pers

Schen6 Online Life 9479

mybatis many to one

@Select("select * from box where box_manager_id=#{mid}" ) @Results({ @Result(property = "id",column = "id"), @Result(property = "no",column = "_no"), @Resul...

shhao918的博客 728

mybatis框架的连表查询one2one

mybatis 的连表查询one2one 1.可以通过主从表之间的关联查询通过主键两次 在resultMap映射中的association标签<association property="info" column="user_id"javaType="UserInfo" 再通过select属性得到从表对应映射的mapper的getUserInfoByUserId方法sele...

weixin_30521649的博客 113

MyBatis one to one 映射

Card类 public class Card { private int id; private String cardNo; private String address; private Person person; .... Person类 public class Person { private int id; pr...

amazing_Code的博客 1728

mybatis plus分页查询报错Expected one result (or null) to be returned by selectOne(), but found: 10

mybatis plus分页查询报错Expected one result (or null) to be returned by selectOne(), but found: 10

HumorChen的博客 886

One2One关联表查询

One2One关联表查询   &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"&gt; &lt;mapper name

shuizhongyue_的专栏 257

Mybatis One-to-many mapping

We can map one-to-many types of results to a collection of objects using the &lt;collection&gt; element.The JavaBeans for Course and Tutor are as follows: public class Course {  private Integer cour...

程序员备忘录 716
上一篇: MyBatis one to one 映射
下一篇: Mybatis 动态sql
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值