(五)mybatis之一对一关系
一、需求分析
- 需求:查询订单信息关联查询用户信息
- 分析:一条订单只能由一个消费者来下单,也就是说从订单的角度来说与消费者是一对一的关系。
二、建数据库表和实体对象
- 其中订单表中的字段user_id对应着消费者表的主键user_id,是一对一关系。
Customer.java
package com.shyroke.entity; public class Customer { private int id; private String name; private String sex; private String birthday; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } @Override public String toString() { return "Customer [id=" + id + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + "]"; } }
Order.java
package com.shyroke.entity; public class Order { private int orderId; private String Number; private String createTime; private String note; private Customer customer; public int getOrderId() { return orderId; } public void setOrderId(int orderId) { this.orderId = orderId; } public String getNumber() { return Number; } public void setNumber(String number) { Number = number; } public String getCreateTime() { return createTime; } public void setCreateTime(String createTime) { this.createTime = createTime; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } @Override public String toString() { return "Order [orderId=" + orderId + ", Number=" + Number + ", createTime=" + createTime + ", note=" + note + ", customer=" + customer + "]"; } }
- 在Orders类中创建一个customer属性,将关联查询到的信息映射到Customer的属性中。
三、编写OrderMapper.java接口和OrderMapper.xml配置文件
OrderMappe.java
package com.shyroke.mapper; import java.util.List; import com.shyroke.entity.Order; public interface OrderMapper { public List<Order> getOrderCustomList(); }
OrderMappe.xml
<?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.shyroke.mapper.OrderMapper"> <resultMap type="com.shyroke.entity.Order" id="orderMap"> <id column="order_id" property="orderId" /> <result column="order_number" property="Number" /> <result column="order_createTime" property="createTime" /> <result column="order_note" property="note" /> <association property="customer" javaType="com.shyroke.entity.Customer"> <id column="user_id" property="id" /> <result column="user_name" property="name" /> <result column="user_sex" property="sex" /> <result column="user_birthday" property="birthday" /> </association> </resultMap> <select id="getOrderCustomList" resultMap="orderMap"> SELECT orders.*, customer.user_name, customer.user_sex, customer.user_birthday FROM orders, customer WHERE orders.user_id = customer.user_id; </select> </mapper>
-
association 标签中的property指的是成员变量名,而javaType指的是该成员变量对应的类。
四、注册到总配置文件中
mybatis-config.xml
<?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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <!-- 配置数据库连接信息 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC" /> <property name="username" value="root" /> <property name="password" value="" /> </dataSource> </environment> </environments> <mappers> <mapper class="com.shyroke.mapper.OrderMapper"/> </mappers> </configuration>
五、测试
App.java
package com.shyrolk.firstMybatis; import java.awt.image.ImageProducer; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.swing.plaf.synth.SynthSeparatorUI; 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 com.shyroke.entity.Order; import com.shyroke.mapper.OrderMapper; /** * Hello world! * */ public class App { public static void main( String[] args ) throws IOException { String resource="resource/mybatis-config.xml"; InputStream inputStream=Resources.getResourceAsStream(resource); SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(inputStream); SqlSession session=sessionFactory.openSession(); OrderMapper orderMapper=session.getMapper(OrderMapper.class); List<Order> orderList=orderMapper.getOrderCustomList(); System.out.println(orderList); } }
结果: