企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 代码 ~~~ package com.like.dao; import com.like.domain.Role; import java.util.List; public interface IRoleDao { List<Role> findAll(); } ~~~ ~~~ package com.like.dao; import com.like.domain.User; import java.util.List; public interface IUserDao { List<User> findAll(); } ~~~ ~~~ package com.like.domain; import java.io.Serializable; import java.util.List; public class Role implements Serializable { private Integer id; private String roleName; private String roleDesc; // 多对多关系映射 private List<User> users; @Override public String toString() { return "Role{" + "id=" + id + ", roleName='" + roleName + '\'' + ", roleDesc='" + roleDesc + '\'' + ", users=" + users + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } public String getRoleDesc() { return roleDesc; } public void setRoleDesc(String roleDesc) { this.roleDesc = roleDesc; } public List<User> getUsers() { return users; } public void setUsers(List<User> users) { this.users = users; } } ~~~ ~~~ 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<Role> roles; @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", birthday=" + birthday + ", sex='" + sex + '\'' + ", address='" + address + '\'' + ", roles=" + roles + '}'; } 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<Role> getRoles() { return roles; } public void setRoles(List<Role> roles) { this.roles = roles; } } ~~~ ~~~ <mapper namespace="com.like.dao.IRoleDao"> <resultMap id="roleMap" type="com.like.domain.Role"> <id property="id" column="id"/> <result property="roleName" column="role_name"/> <result property="roleDesc" column="role_desc"/> <collection property="users" ofType="com.like.domain.User"> <id property="id" column="uid"/> <result property="username" column="username"/> <result property="sex" column="sex"/> <result property="address" column="address"/> <result property="birthday" column="birthday"/> </collection> </resultMap> <select id="findAll" resultMap="roleMap"> select * from role inner join user_role on role.id = user_role.rid inner join user on user_role.uid = user.id </select> </mapper> ~~~ ~~~ <mapper namespace="com.like.dao.IUserDao"> <resultMap id="userMap" type="com.like.domain.User"> <id property="id" column="id"/> <result property="username" column="username"/> <result property="sex" column="sex"/> <result property="address" column="address"/> <result property="birthday" column="birthday"/> <collection property="roles" ofType="com.like.domain.Role"> <id property="id" column="rid"/> <result property="roleName" column="role_name"/> <result property="roleDesc" column="role_desc"/> </collection> </resultMap> <select id="findAll" resultMap="userMap"> select * from user inner join user_role on user.id = user_role.uid inner join role on user_role.rid = role.id </select> </mapper> ~~~ ~~~ IRoleDao mapper = session.getMapper(IRoleDao.class); List<Role> roles = mapper.findAll(); for (Role role : roles) { System.out.println(role); } IUserDao mapper1 = session.getMapper(IUserDao.class); List<User> users = mapper1.findAll(); for (User user : users) { System.out.println(user); } ~~~