10. Mybatis多表查詢 一對一查詢
阿新 • • 發佈:2021-08-20
一對一查詢
我們這裡做一個 使用者表對訂單表,即 一對一查詢的需求:查詢一個訂單,與此同時查詢出該訂單所屬的使用者
我們準備工作 建表、Bean、核心配置檔案、對映檔案 這裡省略,
執行命令:
然後我們Bean:
package com.bihu.Bean; import java.util.Date; public class User { private int id; private String username; private String password; private Date birthday; public User() { }Userpublic Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() {return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() {return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", birthday=" + birthday + '}'; } }
package com.bihu.Bean; import java.util.Date; public class Order { private int id; private Date ordertime; private double total; //當前訂單屬於哪一個使用者 private User user; public int getId() { return id; } public void setId(int id) { this.id = id; } public Date getOrdertime() { return ordertime; } public void setOrdertime(Date ordertime) { this.ordertime = ordertime; } public double getTotal() { return total; } public void setTotal(double total) { this.total = total; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String toString() { return "Order{" + "id=" + id + ", ordertime=" + ordertime + ", total=" + total + ", user=" + user + '}'; } }Order //重點是哪個user ,查詢user表的資料是嵌進去的
mybetis核心配置檔案:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="datasourceInfo.properties"></properties> <typeAliases> <typeAlias type="com.bihu.Bean.User" alias="User"></typeAlias> <typeAlias type="com.bihu.Bean.Order" alias="Order"></typeAlias> <typeAlias type="java.util.List" alias="List"></typeAlias> </typeAliases> <!-- 外掛 --> <plugins> <plugin interceptor="com.github.pagehelper.PageHelper"> <property name="dialect" value="mysql"/> </plugin> </plugins> <environments default="dev"> <environment id="dev"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/bihu/mapper/UserMapper.xml"></mapper> <mapper resource="com/bihu/mapper/OrderMapper.xml"></mapper> </mappers> </configuration>mybatis 核心配置檔案
⭐⭐⭐ 重點是對映檔案這!!!
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.bihu.Dao.OrderMapper"> <resultMap id="orderMap" type="Order"> <!--Order已設定別名--> <id column="oid" property="id"></id> <result column="ordertime" property="ordertime"></result> <result column="total" property="total"></result> <!-- 下面開始是User的封裝--> <result column="uid" property="user.id"></result> <result column="username" property="user.username"></result> <result column="password" property="user.password"></result> <result column="birthday" property="user.birthday"></result> </resultMap> <select id="findAll" resultMap="orderMap"> <!--這的返回結果更改為上面自定義的結果集resultMap 因為裡面有User物件--> SELECT *,o.id oid FROM orders o,user u WHERE o.uid=u.id; </select> </mapper>OrderMapper.xml
重點看註釋即可
然後這裡測試
注意測試裡面的是Order物件,對映的也是Order檔案
package com.bihu.Service; import com.bihu.Bean.Order; import com.bihu.Bean.User; import com.bihu.Dao.OrderMapper; import com.bihu.Dao.UserMapper; import com.github.pagehelper.PageHelper; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; import java.util.List; public class UserService { public static void main(String[] args) throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = build.openSession(); OrderMapper mapper = sqlSession.getMapper(OrderMapper.class); List<Order> all = mapper.findAll(); for (Order item : all) { System.out.println(item); } sqlSession.close(); } }Service 測試
執行:
本文來自部落格園,作者:Bi-Hu,轉載請註明原文連結:https://www.cnblogs.com/bi-hu/p/15166043.html