mybatis一对多

1.一对多:两次 查询

<?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.shangsheng.springbootservlet.user.dao.UserMapper" >
<resultMap id="BaseResultMap" type="com.shangsheng.springbootservlet.user.entity.User" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="per_id" property="per_id" jdbcType="INTEGER"/>

</resultMap>

<select id="getUserList" resultMap="getList" parameterType="int">
select id,name,password,per_id from user where id=#{id}
</select>

<resultMap id="getList" type="com.shangsheng.springbootservlet.user.entity.User">
<collection property="persons" column="id" ofType="com.shangsheng.springbootservlet.user.entity.Person" select="getAll"/>
</resultMap>

<select id="getAll" resultMap="personResultMap">
select person_id,sex,mark from person where person_id=#{person_id}
</select>

<resultMap id="personResultMap" type="com.shangsheng.springbootservlet.user.entity.Person">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="sex" property="sex" jdbcType="VARCHAR" />
<result column="mark" property="mark" jdbcType="BIGINT" />
</resultMap>
②,一次查询:

<sql id="Base_Column_List" >
id, name, password
</sql>

<sql id="good_Column_List">
person_id,sex,mark
</sql>

<resultMap id="personResult" type="com.shangsheng.springbootservlet.user.entity.User" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<collection property="personList" ofType="com.shangsheng.springbootservlet.user.entity.Person">
<result column="person_id" property="personId" jdbcType="INTEGER" />
<result column="sex" property="sex" jdbcType="VARCHAR" />
<result column="mark" property="mark" jdbcType="BIGINT" />
</collection>
</resultMap>

<select id="getAll" resultMap="personResult" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />,
<include refid="good_Column_List" />
from user join person on person_id=id
and id = #{id,jdbcType=INTEGER}
</select>

转载于:https://www.cnblogs.com/shanshen/p/9646056.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值