mybatis延迟加载(Collection)

上篇讲了assocation,同样我们也可以在一对多关系配置的结点中配置延迟加载策略。 结点中也有 select 属性,column 属性。

  需求: 完成加载用户对象时,查询该用户所拥有的账户信息。 

 在 User 实体类中加入 List属性 

package com.henu.domain;

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

public class User implements Serializable {
    private Integer 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 Integer getId() {
        return id;
    }

    public void setId(Integer 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 + '\'' +
                '}';
    }
}

编写用户和账户持久层接口的方法

* @return
*/
List<User> findAll();
/**
* 根据用户 id 查询账户信息
* @param uid
* @return
*/
List<Account> findByUid(Integer uid);

编写用户持久层映射配置

<resultMap type="user" id="userMap">
<id column="id" property="id"></id>
<result column="username" property="username"/>
<result column="address" property="address"/>
<result column="sex" property="sex"/>
<result column="birthday" property="birthday"/>
<!-- collection 是用于建立一对多中集合属性的对应关系
ofType 用于指定集合元素的数据类型
select 是用于指定查询账户的唯一标识(账户的 dao 全限定类名加上方法名称)
column 是用于指定使用哪个字段的值作为条件查询
-->
<collection property="accounts" ofType="account"
select="com.henu.dao.AccountDao.findByUid"
column="id">
</collection>
</resultMap>
<!-- 配置查询所有操作 -->
<select id="findAll" resultMap="userMap">
select * from user
</select>
<collection>标签:
主要用于加载关联的集合对象
select 属性:
用于指定查询 account 列表的 sql 语句,所以填写的是该 sql 映射的 id
column 属性:
用于指定 select 属性的 sql 语句的参数来源,上面的参数来自于 user 的 id 列,所以就写成 id 这一
个字段名了

编写账户持久层映射配置

<!-- 根据用户 id 查询账户信息 -->
<select id="findByUid" resultType="account" parameterType="int">
    select * from account where uid = #{uid}
</select>

测试只加载用户信息

/**
*
* <p>Title: MybastisCRUDTest</p>
* <p>Description: 一对多的操作</p>
* <p>Company: http://www.itheima.com/ </p>
*/
public class UserTest {
  private InputStream in ;
  private SqlSessionFactory factory;
  private SqlSession session;
  private UserDao userDao;
  @Test
  public void testFindAll() {
    //6.执行操作
    List<User> users = userDao.findAll();
  }
  
  @Before
//在测试方法执行之前执行   public void init()throws Exception {     //1.读取配置文件     in = Resources.getResourceAsStream("SqlMapConfig.xml");     //2.创建构建者对象     SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();     //3.创建 SqlSession 工厂对象     factory = builder.build(in);     //4.创建 SqlSession 对象     session = factory.openSession();     //5.创建 Dao 的代理对象     userDao = session.getMapper(UserDao.class);   }   
  @After
//在测试方法执行完成之后执行
  public void destroy() throws Exception{
  session.commit();
  //7.释放资源
  session.close();
  in.close();
}
}

 

测试结果如下:

 

 

我们发现并没有加载 Account 账户信息。

 

posted on 2019-12-11 15:12  Hi,Bro  阅读(944)  评论(0编辑  收藏  举报