一. 一对一关系
1. 一对一的实体类
2. 一对一映射配置
二. 一对多关系
1. 一对多的实体类
2. 一对多映射配置
二. 一对一和一对多关系例子
1. 新建一个名为AssociationCollection的Java工程, 拷入相关jar包
2. 新建User.java
package com.lywgames.domain;
import java.util.Date;
public class User {
private Integer id;
private String name;
private String sex;
private Date birthday;
private String address;
public User() { }
public User(String name, String sex, Date birthday, String address) {
this.name = name;
this.sex = sex;
this.birthday = birthday;
this.address = address;
}
public User(Integer id, String name, String sex, Date birthday, String address) {
this.id = id;
this.name = name;
this.sex = sex;
this.birthday = birthday;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer 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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + ", address=" + address
+ "]";
}
}
3. 新建Order.java
package com.lywgames.domain;
import java.io.Serializable;
import java.sql.Timestamp;
public class Order implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private Integer uid;
private String number;
private Timestamp createtime;
public Order() { }
public Order(Integer uid, String number, Timestamp createtime) {
this.uid = uid;
this.number = number;
this.createtime = createtime;
}
public Order(Integer id, Integer uid, String number, Timestamp createtime) {
this.id = id;
this.uid = uid;
this.number = number;
this.createtime = createtime;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Timestamp getCreatetime() {
return createtime;
}
public void setCreatetime(Timestamp createtime) {
this.createtime = createtime;
}
@Override
public String toString() {
return "Order [id=" + id + ", number=" + number + ", createtime=" + createtime + "]";
}
}
4. 新建OrderDetail.java
package com.lywgames.domain;
import java.io.Serializable;
/**
* 订单详情
* 一个订单对应一个用户
*/
public class OrderDetail implements Serializable {
private static final long serialVersionUID = 1L;
private User user;
private Order order;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
@Override
public String toString() {
return "OrderDetail [\r\n\t" + user + ",\r\n\t" + order + "\r\n]";
}
}
5. 新建UserOrderList.java
package com.lywgames.domain;
import java.io.Serializable;
import java.util.List;
/**
* 用户订单列表
* 一个用户对应多个订单
*/
public class UserOrderList implements Serializable {
private static final long serialVersionUID = 1L;
private User user;
private List<Order> orderList;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<Order> getOrderList() {
return orderList;
}
public void setOrderList(List<Order> orderList) {
this.orderList = orderList;
}
@Override
public String toString() {
String ol = "";
for (Order order : orderList) {
ol += ",\r\n\t" + order;
}
return "UserOrderList [\r\n\t" + user + ol + "\r\n]";
}
}
6. 新建UserMapper.java
package com.lywgames.dao;
import com.lywgames.domain.OrderDetail;
import com.lywgames.domain.UserOrderList;
public interface UserMapper {
public OrderDetail selectOrderDetailByOrderId(Integer id);
public UserOrderList selectUserOrderListByUserId(Integer id);
}
7. 在和UserMapper.java接口同一目录下新建UserMapper.xml映射文件
7. 在和UserMapper.java接口同一目录下新建UserMapper.xml映射文件
9. 在src目录下新建SqlMapConfig.xml数据库配置文件
10. 新建Test.java
package com.lywgames;
import java.io.InputStream;
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.lywgames.dao.UserMapper;
import com.lywgames.domain.OrderDetail;
import com.lywgames.domain.UserOrderList;
public class Test {
public static void main(String[] args) {
SqlSession sqlSession = null;
try {
// 指定全局配置文件
String resource = "SqlMapConfig.xml";
// 读取配置文件
InputStream inputStream = Resources.getResourceAsStream(resource);
// 构建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 获取sqlSession
sqlSession = sqlSessionFactory.openSession();
UserMapper um = sqlSession.getMapper(UserMapper.class);
OrderDetail od = um.selectOrderDetailByOrderId(1);
System.out.println("----------一对一----------");
System.out.println(od);
UserOrderList uol = um.selectUserOrderListByUserId(4);
System.out.println("----------一对多----------");
System.out.println(uol);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(sqlSession != null) {
// 释放资源
sqlSession.close();
}
}
}
}
11. 查看user表数据
12. 查看order表数据
13. 运行项目