MyBatis中使用配置文件与注解两种方式实现一对一、一对多查询

在MyBatis中,有两种方式实现SQl语句的配置,一种是使用同路径下xml文件配置,一种是在持久层接口的方法上添加注解。现在使用这两种方式实现一对一、一对多查询。

一、配置文件

1、一对一

这里使用user表与account表为例,一个账户对应一个用户,此时是一对一的关系。那么在查询账户时,可以将用户信息一并查询出来。首先需要做的就是在account实体类中添加User类型的属性user,设置set、get方法。代码如下

package com.runze.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

public class User implements Serializable {
    private int id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;

    private List<Account> accounts;

    public List<Account> getAccounts() {
        return accounts;
    }

    public void setAccounts(List<Account> accounts) {
        this.accounts = accounts;
    }

    public User() {
    }
    public User(int id, String username, Date birthday, String sex, String address) {
        this.id = id;
        this.username = username;
        this.birthday = birthday;
        this.sex = sex;
        this.address = address;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", birthday=" + birthday +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                ", accounts=" + accounts +
                '}';
    }
}

之后在同路径下同名的xml配置文件中的resultMap映射中添加association属性,配置User类的映射关系。代码如下

 <!--封装account与user的resultMap-->
    <resultMap id="resultUserMap" type="com.runze.domain.Account">
        <!--property属性值是pojo的属性名称,column是查询出来的属性名称-->
        <id property="id" column="aid"></id>
        <result property="uid" column="uid"></result>
        <result property="money" column="money"></result>
        <!--写User类的属性映射-->
        <association property="user" javaType="com.runze.domain.User">
            <id property="id" column="id"></id>
            <result property="username" column="username"></result>
            <result property="address" column="address"></result>
        </association>
    </resultMap>

如此就完成了在account信息中嵌入user信息,即为一个账户表对应一个用户表。查询时就可以将账户对用的用户信息查询出来。对用的sql语句如下

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

2、一对多

一对多与一对一很相识,区别是一对多在pojo类中的实体类是集合类型,而一对一在实体类中的是单个的。在配置文件上也略有差别,一对多进行配置的时候是collection属性。配置文件如下

	    <resultMap id="userMap" type="com.runze.domain.User">
        <!--先进行主键的映射-->
        <id property="id" column="id"></id>
        <!--进行其他属性的映射-->
        <result property="username" column="username"></result>
        <result property="birthday" column="birthday"></result>
        <result property="sex" column="sex"></result>
        <result property="address" column="address"></result>

        <collection property="accounts" ofType="com.runze.domain.Account">
            <id property="id" column="id"></id>
            <result property="uid" column="uid"></result>
            <result property="money" column="money"></result>
        </collection>
    </resultMap>

二、注解开发

一、一对一

使用注解进行开发时,与配置文件不同的是,并不是用一条sql语句进行连接查询得来的,而是使用一种叫做延迟加载的方式进行的。当进行多对一或者一对一(MyBatis中没有多对一的概念)查询时,不延迟加载。当进行一对多或者多对多查询时,会使用延迟加载,需要得到多的一方,才会进行查询,使用的是两条sql语句。一对一的注解如下:

@Select("select * from account")
    @Results(value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "uid",column = "uid"),
            @Result(property = "money",column = "money"),
            @Result(property = "user",column = "uid",one = @One(select = "com.runze.dao.UserDao.findById",fetchType = FetchType.EAGER))
    })

其中Result的one属性即为加载处于com.runze.dao.UserDao包下的findById函数进行二次查询,如果不需要延迟加载,fetchType设置为 FetchType.EAGER,如果需要延迟加载,fetchType设置为 FetchType.LAZY。

二、一对多

@Results(value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "accounts",column = "id",many = @Many(select = "com.runze.dao.AccountDao.findById",fetchType = FetchType.LAZY))
    })
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值