Mybatis一对多延迟加载实现代码解析

网友投稿 258 2022-11-16


Mybatis一对多延迟加载实现代码解析

需求:一个用户可以有多个账户,查询用户时实现账户的延迟加载

1、用户实体类

package com.yl.bean;

import java.io.Serializable;

import java.sql.Date;

import java.util.List;

/**

* 用户实体类

*/

public class User implements Serializable {

private Integer id;//id

private String username;//用户名

private Date birthday;//生日

private String sex;//性别

private String address;//地址

private List accountList;//用户所拥有的账户

public User() {

}

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;

}

public List getAccountList() {

return accountList;

}

public void setAccountList(List accountList) {

this.accountList = accountList;

}

@Override

public String toString() {

return "User{" +

"id=" + id +

", username='" + username + '\'' +

", birthday=" + birthday +

", sex='" + sex + '\'' +

", address='" + address + '\'' +

'}';

}

}

2、账户实体类

package com.yl.bean;

import java.io.Serializable;

/**

* 账户实体类

*/

public class Account implements Serializable {

private Integer id;//账户id

private Integer uid;//用户id

private Double money;//余额

private User user;//账户所属用户

public Account() {

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public Integer getUid() {

return uid;

}

public void setUid(Integer uid) {

this.uid = uid;

}

public Double getMoney() {

return money;

}

public void setMoney(Double money) {

thTgFHNaZHZXis.money = money;

}

public User getUser() {

return user;

}

public void setUser(User user) {

this.user = user;

}

@Override

public String toString() {

return "Account{" +

"id=" + id +

", uid=" + uid +

", money=" + money +

'}';

}

}

3、用户持久层接口

package com.yl.dao;

import com.yl.bean.User;

import java.util.List;

/**

* 用户持久层接口

*/

public interface IUserDao {

/**

* 查询所有用户

*/

List queryAll();

/**

* 根据id查询用户

*/

User queryById(int id);

}

4、账户持久层接口

package com.yl.dao;

import com.yl.bean.Account;

import com.yl.bean.User;

import java.util.List;

/**

* 账户持久层接口

*/

public interface IAccountDao http://{

/**

* 查询所有账户

*/

List queryAll();

/**

* 根据用户id查询账户

*/

List queryByUid(int uid);

}

5、全局配置文件

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

6、用户映射配置文件

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

SELECT * FROM USER

select * from user where id=#{id}

7、账户映射配置文件

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

SELECT * FROM account

select * from account where uid=#{uid}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot使用Netty实现远程调用的示例
下一篇:java的各种集合为什么不安全(List、Set、Map)以及代替方案
相关文章

 发表评论

暂时没有评论,来抢沙发吧~