MyBatis-Plus 注解方式 和xml 方式进行一对多 多对一 查询;

注解方式

多对一

@Repository
public interface UserMapper extends BaseMapper<User> {
	//多对一
    @Results(value = {
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "age", column = "age"),
            @Result(property = "email", column = "email"),
            @Result(property = "dept", column = "deptid", one = @One(select = "com.atguigu.mybatisplus.mapper.DeptMapper.selectById")),
    })
    @Select("select id, name, age, email, deptid from t_user")
    List<UserVO> getUserDept();

    @Select("select id, name, age, email from t_user where deptid=#{deptid}")
    List<User> getUserByDeptId(@Param("deptid") int deptid);
}

一对多

public interface DeptMapper extends BaseMapper<Dept> {
	//一对多
    @Results(value = {
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "users", column = "id", many = @Many(select = "com.atguigu.mybatisplus.mapper.UserMapper.getUserByDeptId")),
    })
    @Select("select id, name from t_dept")
    List<DeptVO> getDeptUser();
}

@Data
public class DeptVO {

    private Integer id;

    private String name;

    private List<User> users;
}

@Data
public class UserVO {
    private int id;
    
    private String name;
    
    private int age;
    
    private String email;
    
    private Dept dept;
}

调用

@SpringBootTest
class MybatisplusApplicationTests {

    @Resource
    private IUserService userService;

    @Resource
    private IDeptService deptService;

    @Test
    void contextLoads() {
        List<UserVO> userDept = userService.getUserDept();
        userDept.forEach(System.out::println);

        List<DeptVO> deptUser = deptService.getDeptUser();
        deptUser.forEach(System.out::println);
    }

}

xml 方式

一对多

   <select id="oneVsMore" resultMap="more">
        SELECT
            *
        FROM
            computer_type AS ct
                JOIN component_peijian AS cp ON ct.type_id = cp.type_id
    </select>
<!--一对多关系-->
    <resultMap id="more" type="computerType" extends="ComputerTypeMap">
        <collection property="componentPeijianList" ofType="com.xiaoliang.computer.entity.ComponentPeijian" javaType="list">
                <result property="componentId" column="component_id" jdbcType="INTEGER"/>
                <result property="typeId" column="type_id" jdbcType="INTEGER"/>
                <result property="componentName" column="component_name" jdbcType="VARCHAR"/>
                <result property="capacity" column="capacity" jdbcType="VARCHAR"/>
                <result property="price" column="price" jdbcType="INTEGER"/>
        </collection>
    </resultMap>

多对一

 <select id="getAllTwo" resultMap="getTwoMap">
        SELECT
            cp.*,
            cy.typename
        FROM
            component_peijian AS cp
                JOIN computer_type AS cy ON cy.type_id = cp.type_id
    </select>
    
<!--        多对一 关系映射  继承多方的这个xml -->
    <resultMap id="getTwoMap" type="com.xiaoliang.computer.entity.ComponentPeijian" extends="ComponentPeijianMap">
<!--    
复杂的属性单独处理
这里解释下 association  和 collection 的使用场景
collection 应用场景为一对多关系,即实体里放集合。关联-association
association用于一对一、多对一场景使用。集合-collection
-->
        <association property="computerType" javaType="com.xiaoliang.computer.entity.ComputerType">
            <result property="typeId" column="type_id" jdbcType="INTEGER"/>
            <result property="typename" column="typename" jdbcType="VARCHAR"/>
        </association>
    </resultMap>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虚构的乌托邦

如果可以请打赏鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值