示例:测试多对多关联
根据数据模型,分别创建t_user、t_order、t_item表。
然后,输入以下测试数据。
程序清单:com.po.User.java
package com.po;
public class User {
private Integer userId;
private String userName;
private String userSex;
private String userAddress;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserSex() {
return userSex;
}
public void setUserSex(String userSex) {
this.userSex = userSex;
}
public String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}
@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName + ", userSex=" + userSex + ", userAddress="
+ userAddress + "]";
}
}
程序清单:com.po.Item.java
package com.po;
public class Item {
private Integer itemId;
private String itemName;
private double itemPrice;
public Integer getItemId() {
return itemId;
}
public void setItemId(Integer itemId) {
this.itemId = itemId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public double getItemPrice() {
return itemPrice;
}
public void setItemPrice(double itemPrice) {
this.itemPrice = itemPrice;
}
@Override
public String toString() {
return "Item [itemId=" + itemId + ", itemName=" + itemName + ", itemPrice=" + itemPrice + "]";
}
}
程序清单:com.po.Order.java
package com.po;
import java.util.List;
public class Order {
private Integer orderId;
private User user;
private List<Item> items;
public Integer getOrderId() {
return orderId;
}
public void setOrderId(Integer orderId) {
this.orderId = orderId;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}
程序清单:com.mapper.UserMapper.java
package com.mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import com.po.User;
public interface UserMapper {
@Select("select * from t_user where user_id=#{userId}")
@Results({@Result(id=true,column="user_id",property="userId"),
@Result(column="user_name",property="userName"),
@Result(column="user_sex",property="userSex"),
@Result(column="user_address",property="userAddress")})
User getUserById(Integer userId);
}
程序清单:com.mapper.ItemMapper.java
package com.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import com.po.Item;
public interface ItemMapper {
@Select("select * from t_item where item_id in(select item_id from t_order where order_id=#{orderId})")
@Results({@Result(id=true,column="item_id",property="itemId"),
@Result(column="item_name",property="itemName"),
@Result(column="item_price",property="itemPrice")})
List<Item> getItems(Integer orderId);
}
程序清单:com.mapper.OrderMapper.java
package com.mapper;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;
import com.po.Order;
public interface OrderMapper {
@Select("select * from t_order where order_id=#{orderId}")
@Results({@Result(id=true,column="order_id",property="orderId"),
@Result(column="user_id",property="user",
one=@One(select="com.mapper.UserMapper.getUserById",fetchType=FetchType.EAGER)),
@Result(column="item_id",property="items",
many=@Many(select="com.mapper.ItemMapper.getItems",fetchType=FetchType.LAZY))})
Order getOrderById(Integer orderId);
}
程序清单:com.dao.TestDao.java
package com.dao;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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 org.junit.Test;
import com.mapper.OrderMapper;
import com.po.Item;
import com.po.Order;
public class TestDao {
private SqlSession session=null;
//获取SqlSession对象
public SqlSession getSqlSession() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(is);
return sessionFactory.openSession();
}
@Test
public void getOrderById() throws IOException {
session=getSqlSession();
OrderMapper om=session.getMapper(OrderMapper.class);
Order order=om.getOrderById(1);
System.out.println(order.getUser());
List<Item> items=order.getItems();
for (Item item : items) {
System.out.println(item);
}
}
}
运行TestDao的getOrderById()方法,控制台显示如下: