运维开发网

Mybatis

运维开发网 https://www.qedev.com 2020-01-24 10:21 出处:网络 作者:wx5d123804636f7
一对一,一对多,多对多一对一的关系映射创建表和相关数据CREATETABLEhouseinfo(houseidint(11)NOTNULLAUTO_INCREMENT,housedescvarchar(32)DEFAULTNULL,typeidint(11)DEFAULTNULL,monthlyrentdecimal(10,2)DEFAULTNULL,publishdatedatetimeDEFA

一对一,一对多,多对多

一对一的关系映射

创建表和相关数据

CREATE TABLE houseinfo (

houseid int(11) NOT NULL AUTO_INCREMENT,

housedesc varchar(32) DEFAULT NULL,

typeid int(11) DEFAULT NULL,

monthlyrent decimal(10,2) DEFAULT NULL,

publishdate datetime DEFAULT NULL,

PRIMARY KEY (houseid)

) ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=utf8mb4;


> insert  into `houseinfo`(`houseid`,`housedesc`,`typeid`,`monthlyrent`,`publishdate`) values (1,'领包入住靠地铁站',1,'1000.00','2019-12-11 14:00:03'),(2,'环境优雅',2,'3200.00','2019-12-11 14:00:03'),(3,'豪华装修',3,'5000.00','2019-12-11 14:00:03'),(4,'1',1,'5.00','2019-12-20 20:46:03'),(5,'a',3,'123.00','2019-12-20 20:47:20'),(6,'1',1,'5.00','2019-12-20 21:04:42'),(7,'??????',1,'1800.00','2019-12-23 16:27:42'),(8,'111',1,'1215.00','2019-12-23 16:28:03'),(9,'???',1,'1500.00','2019-12-23 16:28:17'),(10,'11111???',1,'123.00','2019-12-23 18:30:58'),(11,'??',1,'123.00','2019-12-23 18:33:22'),(12,'1',3,'1.00','2019-12-31 19:23:25'),(13,'2',2,'2.00','2019-12-31 19:46:43'),(14,'a\'a\'a',1,'11.00','2020-01-01 14:23:34'),(15,'20',1,'20.00','2020-01-01 06:44:59'),(16,'30',1,'30.00','2020-01-01 06:45:25'),(17,'30',1,'30.00','2020-01-01 06:47:16'),(18,'???',1,'520.00','2020-01-01 07:15:42'),(19,'???',1,'520.00','2020-01-01 07:25:28'),(20,'???',1,'520.00','2020-01-01 07:30:23'),(21,'da',1,'0.00','2020-01-01 07:30:38'),(22,'a',1,'0.00','2020-01-01 07:33:19'),(23,'???',1,'520.00','2020-01-01 15:33:39'),(24,'???',1,'520.00','2020-01-01 07:34:15'),(25,'???',1,'520.00','2020-01-01 07:34:18'),(26,'???',1,'520.00','2020-01-01 07:34:19'),(27,'???',1,'520.00','2020-01-01 07:34:20'),(28,'???',1,'520.00','2020-01-01 07:34:20'),(29,'???',1,'520.00','2020-01-01 07:34:20'),(30,NULL,NULL,'0.00','2020-01-01 07:36:45'),(31,NULL,NULL,'0.00','2020-01-01 07:37:04'),(32,'????',1,'500.00','2020-01-01 07:39:53'),(33,'1',1,'500.00','2020-01-01 07:40:00'),(34,'a',1,'0.00','2020-01-01 07:40:14'),(35,'a',1,'0.00','2020-01-01 07:40:43'),(36,'2',1,'3.00','2020-01-01 07:41:22'),(37,'6',1,'3.00','2020-01-01 07:41:39'),(38,'6',1,'3.00','2020-01-01 07:41:52'),(39,'6',1,'3.00','2020-01-01 07:41:52'),(40,'6',1,'3.00','2020-01-01 07:41:52'),(41,'6',1,'3.00','2020-01-01 07:41:52'),(42,'6',1,'3.00','2020-01-01 07:41:53'),(43,'??',1,'5.00','2020-01-01 07:42:10'),(44,'a',1,'3.00','2020-01-01 07:42:28'),(45,'a',1,'3.00','2020-01-01 07:42:59'),(46,'?',1,'20.00','2020-01-01 07:43:23'),(47,'???',1,'20.00','2020-01-01 07:43:57'),(48,'???',1,'20.00','2020-01-01 07:48:27');

从表
>CREATE TABLE `housetype` (
  `typeid` int(11) NOT NULL AUTO_INCREMENT,
  `typename` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`typeid`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;

insert into housetype(typeid,typename) values (1,'一室一厅'),(2,'两室一厅'),(3,'三室一厅');

Mybatis

Mybatis

Mybatis

Mybatis

一对多的关系映射

示例:用户和账户

一个用户可以有多个账户

  一个账户只能属于一个用户(多个账户也可以属于同一个用户)

步骤:

1、建立两张表:用户表 帐户表

      让用户表和账户表之前具备一对多的关系:需要使用外键在帐户表上添加

  2、建立两个实体类:用户实体类和账户实体类

      让用户和账户的实体类能体现出来一对多的关系

  3、配置两个配置文件

      用户的配置文件

      账户的配置文件

  4、实现配置:

      当我们查询用户的时候,可以同时得到用户下所包含的信息

      当我们查询账户时,我们可以同时得到账户所属的用户信息

创建表与相关数据

CREATE TABLE account (

ID int(11) NOT NULL COMMENT '编号',

UID int(11) DEFAULT NULL COMMENT '用户编号',

MONEY double DEFAULT NULL COMMENT '金额'

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into account(ID,UID,MONEY) values (1,46,1000),(2,45,1000),(3,46,200);

CREATE TABLE user (

id int(11) DEFAULT NULL,

username varchar(255) DEFAULT NULL,

birthday date DEFAULT NULL,

sex varchar(255) DEFAULT NULL,

address varchar(255) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

insert into user(id,username,birthday,sex,address) values (1,'1','2020-01-03','男','上海'),(41,'zoick','2020-01-03','女','南京'),(42,'lei','2020-01-03','男','南京'),(43,'abdha','2020-01-03','男','上海'),(44,'zzzk','2020-01-03','男','石家庄'),(45,'大号','2020-01-03','男','北京'),(46,'彭于晏','2020-01-03','男','上海'),(47,'李浩','2020-01-03','男','上海'),(48,'吴彦祖','2020-01-03','女','天津');

查询的sql语句

select * from user u left outer join account a on u.id = a.uid

left join(左连接)是left outer join的简写,返回左表中所有记录和右表中连接字段相等的记录,即返回的记录数和左表的记录数一样

Mybatis

0

精彩评论

暂无评论...
验证码 换一张
取 消