目录
一、表设计
二、一对一
三、多对一
四、一对多
五、多对多
一、表设计
一对一级联关系在现实生活中是十分常见的,例如一个大学生只有一个学号,一个学号只属于一个学生。同样,人与身份证也是一对一的级联关系。
一对多,例如一个用户可以有多个订单,而一个订单只属于一个用户。
多对多,实际应用中,由于多对多的关系比较复杂,会增加理解和关联的复杂度,所以应用较少。MyBatis 没有实现多对多级联,推荐通过两个一对多级联替换多对多级联,以降低关系的复杂度,简化程序。
例如,一个订单可以有多种商品,一种商品可以对应多个订单,订单与商品就是多对多的级联关系。可以使用一个中间表(订单记录表)将多对多级联转换成两个一对多的关系。
二、一对一
略
三、多对一
一个银行账户account只有一个user,一个user可以有多个账户account
要求:查询的数据中包含account所有的字段,再包含用户的名称和地址
实体类设计:(get,set,toString,构造器省略)
public class Account implements Serializable{private Integer id;private Integer uid;private Double money;
// 用户private User user;
}
mapper
public interface AccountMapper {
public List<Account> findAll();
}
sql(resultMap对数据进行封装)
<!--内连接查询--><select id="findAll" resultMap="accountMap">select a.*,u.username,u.address from account a,user u where a.uid = u.id</select>
<!--进行数据封装--><resultMap id="accountMap" type="com.qcbyjy.domain.Account"><result property="id" column="id" /><result property="uid" column="uid"/><result property="money" column="money"/><association property="user" javaType="com.qcbyjy.domain.User"><result property="username" column="username"/><result property="address" column="address"/></association></resultMap>
四、一对多
实体类(添加List<Account>)
public class User implements Serializable{
// 主键private Integer id;// 用户名private String username;// 生日private Date birthday;// 性别private String sex;// 地址private String address;
// 演示foreach标签private List<Integer> ids;// 演示一对多查询private List<Account> accounts;
}
sql
<!--一对多查询--><select id="findOneToMany" resultMap="userMap">select u.*,a.money from user u left join account a on u.id = a.uid</select>
<resultMap type="com.qcbyjy.domain.User" id="userMap"><result property="id" column="id"/><result property="username" column="username"/><result property="birthday" column="birthday"/><result property="sex" column="sex"/><result property="address" column="address"/><collection property="accounts" ofType="com.qcbyjy.domain.Account"><result property="money" column="money"/></collection></resultMap>
五、多对多
创建 order(订单),product(商品)和 order_details(订单和商品中间表),SQL 语句如下。
CREATE TABLE `order` (`oid` int(11) NOT NULL AUTO_INCREMENT,`ordernum` int(25) DEFAULT NULL,`userId` int(11) DEFAULT NULL,PRIMARY KEY (`oid`),KEY `userId` (`userId`),CONSTRAINT `order_ibfk_1` FOREIGN KEY (`userId`) REFERENCES `user` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;insert into `order`(`oid`,`ordernum`,`userId`) values (1,20200107,1),(2,20200806,2),(3,20206702,3),(4,20200645,1),(5,20200711,2),(6,20200811,2),(7,20201422,3),(8,20201688,4),(9,NULL,5);DROP TABLE IF EXISTS `orders_detail`;CREATE TABLE `orders_detail` (`id` int(11) NOT NULL AUTO_INCREMENT,`orderId` int(11) DEFAULT NULL,`productId` int(11) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;insert into `orders_detail`(`id`,`orderId`,`productId`) values (1,1,1),(2,1,2),(3,1,3),(4,2,3),(5,2,1),(6,3,2);DROP TABLE IF EXISTS `product`;CREATE TABLE `product` (`pid` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(25) DEFAULT NULL,`price` double DEFAULT NULL,PRIMARY KEY (`pid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;insert into `product`(`pid`,`name`,`price`) values (1,'Java教程',128),(2,'C语言教程',138),(3,'Python教程',132.35);
order(每个实体类都要有对应的list)
public class Order {private int oid;private int ordernum;private List<Product> products;/*省略setter和getter方法*/@Overridepublic String toString() {return "Order [id=" + oid + ", ordernum=" + ordernum + ", products=" + products + "]";}
}
Product
public class Product {private int pid;private String name;private Double price;// 多对多中的一个一对多private List<Order> orders;/*省略setter和getter方法*/@Overridepublic String toString() {return "Product [id=" + pid + ", name=" + name + ", price=" + price + "]";}
}
mapper
public interface OrderMapper {public List<Order> selectAllOrdersAndProducts();
}
sql(注意resultMap的编写)
<resultMap type="net.biancheng.po.Order" id="orderMap"><id property="oid" column="oid" /><result property="ordernum" column="ordernum" /><collection property="products"ofType="net.biancheng.po.Product"><id property="pid" column="pid" /><result property="name" column="name" /><result property="price" column="price" /></collection></resultMap><select id="selectAllOrdersAndProducts" parameterType="Integer"resultMap="orderMap">SELECT o.oid,o.`ordernum`,p.`pid`,p.`name`,p.`price` FROM`order` oINNER JOIN orders_detail od ON o.oid=od.`orderId`INNER JOINproduct pON p.pid = od.`productId`</select>
//SELECT o.oid,o.`ordernum`,p.`pid`,p.`name`,p.`price` FROM `order` o INNER JOIN orders_detail od ON o.oid=od.`orderId` INNER JOIN product p ON p.pid = od.`productId`