SSM15.2【Mybatis:Mybatis的多表操作之一對多】
阿新 • • 發佈:2021-07-25
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 }