SSM15.1【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 public Date getBirthday() { 18 return birthday; 19 } 20 21 public void setBirthday(Date birthday) { 22 this.birthday = birthday; 23 } 24 25 public int getId() { 26 return id; 27 } 28 29 public void setId(int id) { 30 this.id = id; 31 } 32 33 public String getUsername() { 34 return username; 35 } 36 37 public void setUsername(String username) { 38 this.username = username; 39 } 40 41 public String getPassword() { 42 return password; 43 } 44 45 public void setPassword(String password) { 46 this.password = password; 47 } 48 49 //1:1 50 @Override 51 public String toString() { 52 return "User{" + 53 "id=" + id + 54 ", username='" + username + '\'' + 55 ", password='" + password + '\'' + 56 ", birthday=" + birthday + 57 '}'; 58 } 59 }
1 package com.haifei.domain; 2 3 import java.util.Date; 4 5 /** 6 * orders表 7 * 因为order是mysql数据库的关键字,所以建表时命名为orders 8 */ 9 public class Order { 10 11 private int id; //orders主键 12 private Date ordertime; //orders表中该字段数据类型设置为datatime 13 private double total; 14 // private int uid; //orders外键;但从面向对象思想上讲,在java中实现外键时实际应用中多采用相关对象的方式 15 16 17 //当前订单属于哪一个用户 18 private User user; 19 public User getUser() { 20 return user; 21 } 22 public void setUser(User user) { 23 this.user = user; 24 } 25 26 public int getId() { 27 return id; 28 } 29 30 public void setId(int id) { 31 this.id = id; 32 } 33 34 public Date getOrdertime() { 35 return ordertime; 36 } 37 38 public void setOrdertime(Date ordertime) { 39 this.ordertime = ordertime; 40 } 41 42 public double getTotal() { 43 return total; 44 } 45 46 public void setTotal(double total) { 47 this.total = total; 48 } 49 50 51 @Override 52 public String toString() { 53 return "Order{" + 54 "id=" + id + 55 ", ordertime=" + ordertime + 56 ", total=" + total + 57 ", user=" + user + 58 '}'; 59 } 60 }
1 package com.haifei.mapper; 2 3 import com.haifei.domain.Order; 4 5 import java.util.List; 6 7 public interface OrderMapper { 8 9 public List<Order> 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.OrderMapper"> 5 <resultMap id="orderMap" type="order"> 6 <!--手动指定 [sql语句执行结果的]数据库表的字段(column)与java实体类的属性(property) 的映射关系--> 7 <id column="oid" property="id" /> 8 <result column="ordertime" property="ordertime"/> 9 <result column="total" property="total"/> 10 11 <!--<result column="uid" property="user.id"/> 12 <result column="username" property="user.username"/> 13 <result column="password" property="user.password"/> 14 <result column="birthday" property="user.birthday"/>--> 15 <!--还可以这样设置--> 16 <association property="user" javaType="user"> 17 <!-- 18 property: 当前实体(order)中的属性名称(private User user) 19 javaType: 当前实体(order)中的属性的类型(User) 20 --> 21 <result column="uid" property="id"/> 22 <result column="username" property="username"/> 23 <result column="password" property="password"/> 24 <result column="birthday" property="birthday"/> 25 </association> 26 </resultMap> 27 28 <select id="findAll" resultMap="orderMap"> 29 SELECT *,o.id oid FROM orders o,user u WHERE o.uid=u.id 30 </select> 31 </mapper>
1 /** 2 * 一对一查询 3 * 场景:一个订单属于一个用户 4 * @throws IOException 5 */ 6 @Test 7 public void test1() throws IOException { 8 InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); 9 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); 10 SqlSession sqlSession = sqlSessionFactory.openSession(); 11 12 OrderMapper mapper = sqlSession.getMapper(OrderMapper.class); 13 List<Order> orderList = mapper.findAll(); 14 for (Order order : orderList) { 15 System.out.println(order); 16 } 17 /* 18 Order{id=1, ordertime=Thu Jan 01 08:00:02 CST 1970, total=3000.0, user=User{id=1, username='zhangsan', password='123', birthday=Sat Jul 24 20:00:29 CST 2021}} 19 Order{id=2, ordertime=Thu Jan 01 08:00:02 CST 1970, total=1000.0, user=User{id=2, username='lisi', password='345', birthday=Sat Jul 24 20:00:29 CST 2021}} 20 Order{id=3, ordertime=Thu Jan 01 08:00:02 CST 1970, total=344.0, user=User{id=1, username='zhangsan', password='123', birthday=Sat Jul 24 20:00:29 CST 2021}} 21 Order{id=4, ordertime=Thu Jan 01 08:00:02 CST 1970, total=455.0, user=User{id=2, username='lisi', password='345', birthday=Sat Jul 24 20:00:29 CST 2021}} 22 Order{id=5, ordertime=Thu Jan 01 08:00:02 CST 1970, total=100.0, user=User{id=3, username='tom', password='abc', birthday=Sat Jul 24 20:00:29 CST 2021}} 23 */ 24 25 sqlSession.close(); 26 }
【推荐】国内首个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 让容器管理更轻松!