mybatis - 1+N 问题解决

mybatis - 1+N 问题解决

在这里插入图片描述

这是 mybatis 官网提供的解决 n+1 问题方案,关联的多结果集, 需要 mybatis 版本 在 3.2.3 以后 !!

下面照葫芦画瓢

数据库脚本

CREATE TABLE `t_account` (
  `id` int NOT NULL AUTO_INCREMENT,
  `user_name` varchar(255) DEFAULT NULL,
  `passwd` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

CREATE TABLE `t_account_detail` (
  `id` int NOT NULL AUTO_INCREMENT,
  `account_id` int DEFAULT NULL,
  `amount` int DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

INSERT INTO `t_account` (`id`, `user_name`, `passwd`) VALUES (1, 'test', 'test');
INSERT INTO `t_account` (`id`, `user_name`, `passwd`) VALUES (2, 'haha', 'haha');
INSERT INTO `t_account_detail` (`id`, `account_id`, `amount`) VALUES (1, 1, 10);
INSERT INTO `t_account_detail` (`id`, `account_id`, `amount`) VALUES (2, 2, 43);

model 文件

@Data
public class AccountModel implements Serializable {
    private Integer id;

    private String userName;

    private String passwd;

    private static final long serialVersionUID = 1L;
}

@Data
public class AccountDetailModel implements Serializable {
    private Integer id;

    private Integer accountId;

    private Integer amount;

    private static final long serialVersionUID = 1L;
}

@Data
public class AccountDto {


    private Integer id;

    private String userName;

    private String passwd;

    private AccountDetailModel accountDetailModel;
}

配置文件

// 其中 accounts,details 是任意命名的
<select id="getAccountList2" resultSets="accounts,details" resultMap="getAccountList2Map" 
																	statementType="CALLABLE">
    {call getCallAccountList()}
  </select>

  <resultMap id="getAccountList2Map" type="com.ddup.bootmybatis.AccountDto">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="user_name" jdbcType="VARCHAR" property="userName" />
    <result column="passwd" jdbcType="VARCHAR" property="passwd" />
    <association property="accountDetailModel" resultSet="details" 
                 javaType="com.ddup.bootmybatis.model.AccountDetailModel"
                 column="id" 
                 foreignColumn="account_id">         //t_account_detail 表中和t_account 关联的字段
      <id column="id" jdbcType="INTEGER" property="id"/>
      <result column="account_id" jdbcType="INTEGER" property="accountId"/>
      <result column="amount" jdbcType="INTEGER" property="amount"/>
    </association>
  </resultMap>

字段含义:

column当使用多个结果集时,该属性指定结果集中用于与 foreignColumn 匹配的列(多个列名以逗号隔开),以识别关系中的父类型与子类型。
foreignColumn指定外键对应的列名,指定的列将与父类型中 column 的给出的列进行匹配。
resultSet指定用于加载复杂类型的结果集名字。

存储过程

CREATE DEFINER=`root`@`%` PROCEDURE `getCallAccountList`()
BEGIN
	#Routine body goes here...
	select * from t_account;
 
	select * from t_account_detail;
END

测试用例

@Test
public void nPlusOneTest() throws JsonProcessingException {
    List<AccountDto> accountList2 = accountDao.getAccountList2();
    ObjectMapper mapper = new ObjectMapper();
    System.out.println(mapper.writeValueAsString(accountList2));
}

测试结果:

[
    {
        "id":1,
        "userName":"test",
        "passwd":"test",
        "accountDetailModel":{
            "id":1,
            "accountId":1,
            "amount":10
        }
    },
    {
        "id":2,
        "userName":"haha",
        "passwd":"haha",
        "accountDetailModel":{
            "id":2,
            "accountId":2,
            "amount":43
        }
    }
]

perfect , good luck !

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值