【Mybatis】 多表查询&懒加载

mybatis多表查询(一对一)

查询语句:

SELECT
account.*,
user.username,
user.address
FROM
account,
USER
WHERE account.uid = user.id

方法一

可以使用继承的思想,新建一个类,继承要查询的类对应的表,并在其中编写另一个表的字段,这个思想也比较常用与企业开发

public class AccountUser extends Account implements Serializable {
    private String username;
    private String address;

注意输出时无法自动输出所有字段值,需要手动调用,如

AccountUser.AccouontId.sout

方法二

使用association可以连接两个单表,适用于一对一查询

Java类中必须将另一个表对应类封装到当前表对应类中,注意要为user配置getter\setter\toString方法

public class Account implements Serializable {
    private Integer id;
    private Integer uid;
    private Double money;
    User user;

此时类字段不再与表一一对应,需要配置resultMap

<resultMap id="accMap" type="com.xq.pojo.Account">
        <id property="id" column="id"/>
        <result column="uid" property="uid"/>
        <result column="money" property="money"/>
        <association property="user" javaType="com.xq.pojo.User">
            <id property="id" column="id"/>
            <result property="name" column="username"/>
            <result property="address" column="address"/>
        </association>
</resultMap>

复习:其中column对应数据库表列名,property对应类中名称

在association标签中,property为主类中多出的那个对应另一个表的字段,javaType为该字段的全限定名

懒加载:

当不需要查询时不会加载相应数据

1.在相应mapper中配置:(使用association)
    <resultMap id="resMap" type="com.xq.pojo.Account">
        <id column="id" property="id"/>
        <result column="uid" property="uid"/>
        <result column="money" property="money"/>
        <!--
select: 填写我们要调用的 select 映射的 id
column : 填写我们要传递给 select 映射的参数(必须要写) 就是查
询出来的account的uid属性
fetchType="lazy" 配置懒加载,只在当前方法里面有效,如果想要所
有的方法都进行懒加载,需要进行全局懒加载的配置
-->

        <association property="user" select="com.xq.dao.UserDao.findById" column="uid" fetchType="lazy">
        </association>
    </resultMap>

2.在核心配置文件中配置:(lazyLoadingEnabled true)
<settings>
        <setting name="lazyLoadingEnabled" value="true"/>
</settings>

调用以及结果:

UserDao:

<mapper namespace="com.xq.dao.UserDao">
    <select id="findById" resultType="com.xq.pojo.User" parameterType="int">
        select * from user where id =#{id};
    </select>
</mapper>

Java调用:

 @Test
    public void t(){
        List<Account> all = accountDao.findAll();
//        all.forEach(System.out::println);
        //注意这里不能输出account,否则全部访问的情况下必须全部加载
    }

一对多

区别:一个user可能对应多个accont,因此每个user增加一个list字段保存对应account


public class newUser {
    private String name;
    private int id;
    private String sex;
    private String address;
    private Date birthday;
    List<Account> accounts;

mapper配置:同上,这里用collection表示多对多的关系,ofType为account对应返回的泛型类型,即集合元素的数据类型

<resultMap id="resMap" type="com.xq.pojo.newUser">
        <id column="id" property="id"/>
        <result property="name" column="username"/>
        <result property="address" column="address"/>
        <result property="sex" column="sex"/>
        <result property="birthday" column="birthday"/>
        <collection property="accounts" ofType="com.xq.pojo.Account">
            <id column="id" property="id"/>
            <result property="money" column="money"/>
        </collection>
    </resultMap>
<select id="findAll" resultMap="resMap">
        select user.*,account.`MONEY`,account.`ID` from account ,user  where ACCOUNT.uid=USER.id
</select>

直接调用即可

@Test
    public void t1(){
        List<newUser> all = newUserDao.findAll();
        for (newUser newUser : all) {
            System.out.println(newUser);
        }
    }

懒加载

与上文类似,这里为collection

<mapper namespace="com.xq.dao.UserDao">
    <resultMap id="resMap" type="com.xq.pojo.User">
        <id column="id" property="id"/>
        <result property="username" column="username"/>
        <result property="birthday" column="birthday"/>
        <result property="sex" column="sex"/>
        <result property="address" column="address"/>
        <collection property="accounts" select="com.xq.dao.AccountDao.findAccountById" column="id"/>
    </resultMap>
    <select id="findAll" resultMap="resMap">
        select * from user
    </select>
</mapper>

<mapper namespace="com.xq.dao.AccountDao">

    <select id="findAccountById" parameterType="int" resultType="com.xq.pojo.Account">
        select * from account where uid=#{id}
    </select>
</mapper>

多对多

与一对多类似,将要查询的两个表对应的类按关系组合即可

在这里插入图片描述

public class Role {
private Integer roleId;
private String roleName;
private String roleDesc;
//多对多的关系映射:一个角色可以赋予多个用户
private List<User> users;
}

查询sql时再将user_role_id表加入即可

sql语句

select u.*,r.id as rid,r.role_name,r.role_desc from role
r
left outer join user_role ur on r.id = ur.rid
left outer join user u on u.id = ur.uid
private List<User> users;

查询sql时再将user_role_id表加入即可

sql语句

select u.*,r.id as rid,r.role_name,r.role_desc from role
left outer join user_role ur on r.id = ur.rid
left outer join user u on u.id = ur.uid
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值