与前面介绍的一对一相比 Mybatis】级联查询之一对一级联查询,在实际开发中接触最多的还是一对多的关联关系。
应用场景举例:用户与订单之间的关系为一对多,即一个用户可以有多个订单,而一个订单只属于一个用户。
创建Spring整合Mybatis工程 【Spring】Spring整合Mybatis案例
创建orders
表和user
表
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders` (
`id` int(11) NOT NULL,
`userid` int(11) DEFAULT NULL,
`orderinfo` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `userorder` (`userid`),
CONSTRAINT `userorder` FOREIGN KEY (`userid`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
public class Order {
private int id;
private int userid;
private String orderinfo;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getOrderinfo() {
return orderinfo;
}
public void setOrderinfo(String orderinfo) {
this.orderinfo = orderinfo;
}
@Override
public String toString() {
return "Order{" +
"id=" + id +
", userid=" + userid +
", orderinfo='" + orderinfo + '\'' +
'}';
}
}
public class User {
private int id;
private String name;
private int age;
private List<Order> orderList;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Order> getOrderList() {
return orderList;
}
public void setOrderList(List<Order> orderList) {
this.orderList = orderList;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", orderList=" + orderList +
'}';
}
}
@Repository("userMapper")
@Mapper
public interface UserMapper {
List<User> findUserOrdersById();
}
@Repository("orderMapper")
@Mapper
public interface OrderMapper {
List<Order> findOrdersByUserId(int id);
}
<configuration>
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
settings>
<mappers>
<mapper resource="UserMapper.xml"/>
<mapper resource="OrderMapper.xml"/>
mappers>
configuration>
<mapper namespace="com.lucas.mybatis.mapper.OrderMapper">
<select id="findOrdersByUserId" parameterType="integer" resultType="com.lucas.mybatis.model.Order">
select * from orders where userid=#{id}
select>
mapper>
<mapper namespace="com.lucas.mybatis.mapper.UserMapper">
<resultMap id="userAndOrders" type="com.lucas.mybatis.model.User">
<id property="id" column="id">id>
<result property="name" column="name">result>
<result property="age" column="age">result>
<collection property="orderList" ofType="com.lucas.mybatis.model.Order" column="id" select="com.lucas.mybatis.mapper.OrderMapper.findOrdersByUserId">collection>
resultMap>
<select id="findUserOrdersById" resultMap="userAndOrders">
select * from user
select>
mapper>
<mapper namespace="com.lucas.mybatis.mapper.UserMapper">
<resultMap id="userAndOrders" type="com.lucas.mybatis.model.User">
<id property="id" column="id">id>
<result property="name" column="name">result>
<result property="age" column="age">result>
<collection property="orderList" ofType="com.lucas.mybatis.model.Order" >
<id property="id" column="id">id>
<result property="orderinfo" column="orderinfo">result>
collection>
resultMap>
<select id="findUserOrdersById" resultMap="userAndOrders">
select user.*,orders.id,orders.orderinfo from user ,orders where `user`.id = orders.userid
select>
mapper>
public class App {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
List<User> users = userMapper.findUserOrdersById();
for (User user : users) {
System.out.println(user);
}
}
}