Good learning Java programmers to share mybatis route map
Good learning Java programmers to share mybatis route map, we want to help.
Mybatis 1-on-1 associated with implementation
- By way resultType
- By way of cascade attribute [way] resultType and resultMap
- By way of association association
- By fractional association of query
- By association of the double wrapper class associatively
- Association by way of nested definitions
Case: Query user information corresponding to the order
Sql statements to build the table
user table:
-- ---------------------------- -- Table structure for `user` -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `Username` varchar (32) NOT NULL COMMENT 'username' `Birthday` date DEFAULT NULL COMMENT 'birthday' `Sex` char (1) DEFAULT NULL COMMENT 'Sex', `address` varchar(256) DEFAULT NULL COMMENT '地址', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8;
-- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES ( '1', 'Wang Wu', '2019-08-30', '2', 'Hangzhou'); INSERT INTO `user` VALUES ( '10', 'John Doe', '2014-07-10', '1', 'Beijing'); INSERT INTO `user` VALUES ( '16', 'Xiaoming', '2019-08-15', '1', 'Zhengzhou'); INSERT INTO `user` VALUES ( '22', 'John Doe', '2019-08-08', '1', 'Zhengzhou'); INSERT INTO `user` VALUES ( '24', 'Chi Master', '2019-08-15', '1', 'Changsha'); INSERT INTO `user` VALUES ( '25', 'Wu Xiaoming', '2019-08-08', '1', 'Zhengzhou'); INSERT INTO `user` VALUES ( '26', 'Wang Wu', '2019-08-14', '2', 'Wuhan'); |
Orders table:
-- ---------------------------- -- Table structure for `orders` -- ---------------------------- DROP TABLE IF EXISTS `orders`; CREATE TABLE `orders` ( `id` int(11) NOT NULL AUTO_INCREMENT, `User_id` int (11) NOT NULL COMMENT 'single user id', `number` varchar(32) NOT NULL COMMENT '订单号', `createtime` datetime NOT NULL COMMENT '创建订单时间', `note` varchar(100) DEFAULT NULL COMMENT '备注', PRIMARY KEY (`id`), KEY `FK_orders_1` (`user_id`), CONSTRAINT `FK_orders_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
-- ---------------------------- -- Records of orders -- ---------------------------- INSERT INTO `orders` VALUES ('3', '1', '1000010', '2015-02-04 13:22:35', null); INSERT INTO `orders` VALUES ('4', '1', '1000011', '2015-02-03 13:22:41', null); INSERT INTO `orders` VALUES ('5', '10', '1000012', '2015-02-12 16:13:23', null); INSERT INTO `orders` VALUES ('6', '10', '1000013', '2015-08-30 10:11:56', null); INSERT INTO `orders` VALUES ('7', '16', '100014', '2019-08-16 10:12:54', null); |
实体类:
用户:User
public class User { //getter.. setter.. toString.. } |
订单:Orders
public class Orders { //getter.. setter.. toString.. } |
方式一:resultType方式
一个订单 对应 一个用户
对订单实体类进行扩展: 【只增加 对应的字段 ;例如案例中的 用户名称】
新建OrdersExtByColumn 的扩展类,继承 Orders, 并增加相应的字段
public class OrdersExtByColumn extends Orders { //getter.. setter.. toString.. |
OrdersMapper映射文件中,写对应的sql语句
<select id="queryOrdersByType" parameterType="int" resultType="OrdersExtByColumn"> |
编写测试程序:
public class TestAssociation { |
方式二 : 级联属性的方式
在orders类中, 增加 级联属性User user;
ResultType方式:
ResultMap方式:
方式三:association关联的方式
方式四:association的分步查询方式
分步查询:即多个sql查询 算出结果
此种方式,可以结合mybatis核心配置文件mybatisCfg.xml的配置项,产生延迟缓存;
mybatisCfg.xml 延迟加载相关设置项
方式五:包装类的双association的关联方式
新建包装类:
OrdersAndUser
public class OrdersAndUser { //getter.. setter.. } |
方式六:内嵌association级联
这种内嵌association会关联三张表;
需求简单变更为: 从某订单详情中,查询订单及对应 用户信息
追加一张订单详情表
-- ---------------------------- -- Table structure for `orderdetail` -- ---------------------------- DROP TABLE IF EXISTS `orderdetail`; CREATE TABLE `orderdetail` ( `id` int(11) NOT NULL AUTO_INCREMENT, `orders_id` int(11) NOT NULL COMMENT '订单id', `items_id` int(11) NOT NULL COMMENT '商品id', `items_num` int(11) DEFAULT NULL COMMENT '商品购买数量', PRIMARY KEY (`id`), KEY `FK_orderdetail_1` (`orders_id`), KEY `FK_orderdetail_2` (`items_id`), CONSTRAINT `FK_orderdetail_1` FOREIGN KEY (`orders_id`) REFERENCES `orders` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `FK_orderdetail_2` FOREIGN KEY (`items_id`) REFERENCES `items` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
-- ---------------------------- -- Records of orderdetail -- ---------------------------- INSERT INTO `orderdetail` VALUES ('1', '3', '1', '1'); INSERT INTO `orderdetail` VALUES ('2', '3', '2', '3'); INSERT INTO `orderdetail` VALUES ('3', '4', '3', '4'); INSERT INTO `orderdetail` VALUES ('4', '4', '2', '3'); |
新增OrderDeatil类 【关联Orders属性】
public class OrderDetail { //setter.. getter.. } |