这篇文章将为大家详细讲解有关使用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<Account> 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<Account> getAccountList() { return accountList; } public void setAccountList(List<Account> 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) { this.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<User> 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 { /** * 查询所有账户 */ List<Account> queryAll(); /** * 根据用户id查询账户 */ List<Account> queryByUid(int uid); }
5、全局配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!--开启延迟加载--> <setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/> </settings> <!--配置别名--> <typeAliases> <package name="com.yl.bean"/> </typeAliases> <!--配置mybatis环境--> <environments default="mysql"> <!--配置mysql环境--> <environment id="mysql"> <!--事务类型--> <transactionManager type="JDBC"></transactionManager> <!--数据源--> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&serverTimezone=GMT%2B8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!--指定映射配置文件位置--> <mappers> <mapper resource="IUserDao.xml"></mapper> <mapper resource="IAccountDao.xml"></mapper> </mappers> </configuration>
6、用户映射配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.yl.dao.IUserDao"> <!--配置用户实体类和数据库的对应信息--> <resultMap id="userMap" type="com.yl.bean.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> <!--根据id查询账户--> <collection property="accountList" ofType="com.yl.bean.Account" select="com.yl.dao.IAccountDao.queryByUid" column="id"></collection> </resultMap> <!--查询所有用户--> <select id="queryAll" resultMap="userMap"> SELECT * FROM USER </select> <!--根据id查询用户--> <select id="queryById" resultType="user" parameterType="int"> select * from user where id=#{id} </select> </mapper>
7、账户映射配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.yl.dao.IAccountDao"> <!--配置Account属性对应数据库信息--> <resultMap id="accountUserMap" type="account"> <id property="id" column="id"></id> <result property="uid" column="uid"></result> <result property="money" column="money"></result> <!--通过id查询用户--> <association property="user" column="uid" javaType="user" select="com.yl.dao.IUserDao.queryById"></association> </resultMap> <!--查询所有账户--> <select id="queryAll" resultMap="accountUserMap"> SELECT * FROM account </select> <!--根据用户id查询账户--> <select id="queryByUid" resultType="account" parameterType="int"> select * from account where uid=#{uid} </select> </mapper>
关于使用Mybatis实现一对多延迟加载就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。