SSM16.5【Mybatis:通过注解实现Mybatis的复杂映射开发之多对多查询】
1 package com.haifei.domain; 2 3 public class Role { 4 5 private int id; 6 private String roleName; 7 private String roleDesc; 8 9 public int getId() { 10 return id; 11 } 12 13 public void setId(int id) { 14 this.id = id; 15 } 16 17 public String getRoleName() { 18 return roleName; 19 } 20 21 public void setRoleName(String roleName) { 22 this.roleName = roleName; 23 } 24 25 public String getRoleDesc() { 26 return roleDesc; 27 } 28 29 public void setRoleDesc(String roleDesc) { 30 this.roleDesc = roleDesc; 31 } 32 33 @Override 34 public String toString() { 35 return "Role{" + 36 "id=" + id + 37 ", roleName='" + roleName + '\'' + 38 ", roleDesc='" + roleDesc + '\'' + 39 '}'; 40 } 41 }
1 package com.haifei.domain; 2 3 import java.util.Date; 4 import java.util.List; 5 6 public class User { 7 8 private int id; 9 private String username; 10 private String password; 11 private Date birthday; 12 13 //描述的是当前用户具有的订单 14 private List<Order> orderList; 15 public List<Order> getOrderList() { 16 return orderList; 17 } 18 public void setOrderList(List<Order> orderList) { 19 this.orderList = orderList; 20 } 21 22 //描述的是当前用户具备哪些角色 23 private List<Role> roleList; 24 public List<Role> getRoleList() { 25 return roleList; 26 } 27 public void setRoleList(List<Role> roleList) { 28 this.roleList = roleList; 29 } 30 31 public Date getBirthday() { 32 return birthday; 33 } 34 35 public void setBirthday(Date birthday) { 36 this.birthday = birthday; 37 } 38 39 public int getId() { 40 return id; 41 } 42 43 public void setId(int id) { 44 this.id = id; 45 } 46 47 public String getUsername() { 48 return username; 49 } 50 51 public void setUsername(String username) { 52 this.username = username; 53 } 54 55 public String getPassword() { 56 return password; 57 } 58 59 public void setPassword(String password) { 60 this.password = password; 61 } 62 63 //1:1 64 /* @Override 65 public String toString() { 66 return "User{" + 67 "id=" + id + 68 ", username='" + username + '\'' + 69 ", password='" + password + '\'' + 70 ", birthday=" + birthday + 71 '}'; 72 }*/ 73 //1:n 74 /*@Override 75 public String toString() { 76 return "User{" + 77 "id=" + id + 78 ", username='" + username + '\'' + 79 ", password='" + password + '\'' + 80 ", birthday=" + birthday + 81 ", orderList=" + orderList + 82 '}'; 83 }*/ 84 //m:n 85 @Override 86 public String toString() { 87 return "User{" + 88 "id=" + id + 89 ", username='" + username + '\'' + 90 ", password='" + password + '\'' + 91 ", birthday=" + birthday + 92 ", orderList=" + orderList + 93 ", roleList=" + roleList + 94 '}'; 95 } 96 }
1 UserMapper 2 3 + 4 5 @Select("select * from user") 6 @Results({ 7 @Result(column = "id", property = "id", id=true), 8 @Result(column = "username",property = "username"), 9 @Result(column = "password",property = "password"), 10 @Result( 11 property = "roleList", 12 column = "id", 13 javaType = List.class, 14 many = @Many(select = "com.haifei.mapper.RoleMapper.findByUid") 15 ) 16 }) 17 public List<User> findUserAndRoleAll();
1 package com.haifei.mapper; 2 3 import com.haifei.domain.Role; 4 import org.apache.ibatis.annotations.Select; 5 6 import java.util.List; 7 8 public interface RoleMapper { 9 10 @Select("select * from sys_user_role ur,sys_role r where ur.roleId=r.id and ur.userId=#{uid}") 11 public List<Role> findByUid(int id); 12 13 }
1 package com.haifei.test; 2 3 import com.haifei.domain.User; 4 import com.haifei.mapper.UserMapper; 5 import org.apache.ibatis.io.Resources; 6 import org.apache.ibatis.session.SqlSession; 7 import org.apache.ibatis.session.SqlSessionFactory; 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 9 import org.junit.Before; 10 import org.junit.Test; 11 12 import java.io.IOException; 13 import java.io.InputStream; 14 import java.util.List; 15 16 public class MybatisTest4 { 17 18 private UserMapper mapper; 19 20 @Before 21 public void before() throws IOException { 22 InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); 23 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); 24 SqlSession sqlSession = sqlSessionFactory.openSession(true); //true-->事务自动提交 25 mapper = sqlSession.getMapper(UserMapper.class); 26 } 27 28 /** 29 * 多对多查询 30 */ 31 @Test 32 public void testFindUserAndOrderAll(){ 33 List<User> userList = mapper.findUserAndRoleAll(); 34 for (User user : userList) { 35 System.out.println(user); 36 } 37 /* 38 User{id=1, username='zhangsan', password='123', birthday=Sat Jul 24 20:00:29 CST 2021, orderList=null, roleList=[Role{id=1, roleName='院长', roleDesc='负责全面工作'}, Role{id=2, roleName='研究员', roleDesc='课程研发工作'}]} 39 User{id=2, username='lisi', password='345', birthday=Sat Jul 24 20:00:29 CST 2021, orderList=null, roleList=[Role{id=2, roleName='研究员', roleDesc='课程研发工作'}, Role{id=3, roleName='讲师', roleDesc='授课工作'}]} 40 User{id=3, username='tom', password='abc', birthday=Sat Jul 24 20:00:29 CST 2021, orderList=null, roleList=[]} 41 User{id=4, username='lucy', password='def', birthday=Sat Jul 24 20:00:29 CST 2021, orderList=null, roleList=[]} 42 User{id=5, username='sam', password='sam', birthday=Sat Jul 24 20:00:29 CST 2021, orderList=null, roleList=[]} 43 User{id=9, username='11', password='11', birthday=Sat Jul 24 20:00:29 CST 2021, orderList=null, roleList=[]} 44 */ 45 } 46 47 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!