SSM15.2【Mybatis:Mybatis的多表操作之一对多】
1 package com.haifei.domain; 2 3 import java.util.Date; 4 import java.util.List; 5 6 /** 7 * user表 8 */ 9 public class User { 10 11 private int id; 12 private String username; 13 private String password; 14 private Date birthday; 15 16 //描述的是当前用户存在哪些订单 17 private List<Order> orderList; 18 public List<Order> getOrderList() { 19 return orderList; 20 } 21 public void setOrderList(List<Order> orderList) { 22 this.orderList = orderList; 23 } 24 25 26 public Date getBirthday() { 27 return birthday; 28 } 29 30 public void setBirthday(Date birthday) { 31 this.birthday = birthday; 32 } 33 34 public int getId() { 35 return id; 36 } 37 38 public void setId(int id) { 39 this.id = id; 40 } 41 42 public String getUsername() { 43 return username; 44 } 45 46 public void setUsername(String username) { 47 this.username = username; 48 } 49 50 public String getPassword() { 51 return password; 52 } 53 54 public void setPassword(String password) { 55 this.password = password; 56 } 57 58 59 //1:n 60 @Override 61 public String toString() { 62 return "User{" + 63 "id=" + id + 64 ", username='" + username + '\'' + 65 ", password='" + password + '\'' + 66 ", birthday=" + birthday + 67 ", orderList=" + orderList + 68 '}'; 69 } 70 71 }
1 package com.haifei.mapper; 2 3 import com.haifei.domain.User; 4 5 import java.util.List; 6 7 public interface UserMapper { 8 9 public List<User> findAll(); 10 11 }
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 4 <mapper namespace="com.haifei.mapper.UserMapper"> 5 6 <resultMap id="userMap" type="user"> 7 <!--user的信息--> 8 <id column="uid" property="id"/> 9 <result column="username" property="username"/> 10 <result column="password" property="password"/> 11 <result column="birthday" property="birthday"/> 12 <!--user内部的orderList信息--> 13 <collection property="orderList" ofType="order"> 14 <!--配置集合信息,property:集合名称,ofType:当前集合中的数据类型--> 15 <!--封装order的数据--> 16 <id column="oid" property="id"/> 17 <result column="ordertime" property="ordertime"/> 18 <result column="total" property="total"/> 19 </collection> 20 </resultMap> 21 22 <select id="findAll" resultMap="userMap"> 23 SELECT *,o.id oid FROM user u,orders o WHERE u.id=o.uid 24 </select> 25 26 27 </mapper>
1 /** 2 * 一对多查询 3 * 场景:一个用户有多个订单 4 * @throws IOException 5 */ 6 @Test 7 public void test2() throws IOException { 8 InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); 9 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); 10 SqlSession sqlSession = sqlSessionFactory.openSession(); 11 12 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 13 List<User> userList = mapper.findAll(); 14 for (User user : userList) { 15 System.out.println(user); 16 } 17 /* 18 User{id=1, username='zhangsan', password='123', birthday=Sat Jul 24 20:00:29 CST 2021, orderList=[Order{id=1, ordertime=Thu Jan 01 08:00:02 CST 1970, total=3000.0, user=null}, Order{id=3, ordertime=Thu Jan 01 08:00:02 CST 1970, total=344.0, user=null}]} 19 User{id=2, username='lisi', password='345', birthday=Sat Jul 24 20:00:29 CST 2021, orderList=[Order{id=2, ordertime=Thu Jan 01 08:00:02 CST 1970, total=1000.0, user=null}, Order{id=4, ordertime=Thu Jan 01 08:00:02 CST 1970, total=455.0, user=null}]} 20 User{id=3, username='tom', password='abc', birthday=Sat Jul 24 20:00:29 CST 2021, orderList=[Order{id=5, ordertime=Thu Jan 01 08:00:02 CST 1970, total=100.0, user=null}]} 21 */ 22 23 sqlSession.close(); 24 }
【推荐】国内首个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 让容器管理更轻松!