ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 添加配置开启懒加载 ~~~ <settings> <setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/> </settings> ~~~ ## 所需类 ~~~ package com.like.dao; import com.like.domain.Account; import java.util.List; public interface IAccountDao { List<Account> findAll(); List<Account> findByUserId(Integer id); } ~~~ ~~~ package com.like.dao; import com.like.domain.User; import java.util.List; public interface IUserDao { List<User> findAll(); User findById(Integer id); } ~~~ ~~~ package com.like.domain; public class Account { private Integer id; private Integer uid; private Double money; private User user; @Override public String toString() { return "Account{" + "id=" + id + ", uid=" + uid + ", money=" + money + ", user=" + user + '}'; } 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) { this.money = money; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } } ~~~ ~~~ package com.like.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; @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", birthday=" + birthday + ", sex='" + sex + '\'' + ", address='" + address + '\'' + ", 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; } public List<Account> getAccounts() { return accounts; } public void setAccounts(List<Account> accounts) { this.accounts = accounts; } } ~~~ ## 一对一延迟加载 ~~~ <mapper namespace="com.like.dao.IUserDao"> <select id="findById" parameterType="int" resultType="com.like.domain.User"> select * from user where id = #{id} </select> </mapper> ~~~ ~~~ <mapper namespace="com.like.dao.IAccountDao"> <resultMap id="accountUserMap" type="com.like.domain.Account"> <id property="id" column="id"/> <result property="uid" column="uid"/> <result property="money" column="money"/> <association property="user" column="uid" //必须写,指定根据id查询用户时所需要的参数 javaType="com.like.domain.User" select="com.like.dao.IUserDao.findById"> //这里需要用到IUserDao根据ID查找的方法 </association> </resultMap> <select id="findAll" resultMap="accountUserMap"> select * from account; </select> </mapper> ~~~ ## 一对多延迟加载 ~~~ <select id="findByUserId" parameterType="int" resultType="com.like.domain.Account"> select * from account where uid = #{uid} </select> ~~~ ~~~ <resultMap id="userAccountMap" type="com.like.domain.User"> <id property="id" column="id"/> <result property="username" column="username"/> <result property="birthday" column="birthday"/> <result property="sex" column="sex"/> <result property="address" column="address"/> <collection property="accounts" column="id" //这里是id ofType="com.like.domain.Account" select="com.like.dao.IAccountDao.findByUserId"> //这里是根据用户的id查找账户的方法 <id property="id" column="aid"/> <result property="uid" column="uid"/> <result property="money" column="money"/> </collection> </resultMap> <select id="findAll" resultMap="userAccountMap"> select * from user </select> ~~~