mybatis 一对一、一对多、多对一、多对多
本项目是 框架架构是 springboot+mybatis
添加maven依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot.version}</version>
</dependency>
在 application.properties 配置
#为实体对象所在的包,跟数据库表一一对应
mybatis.typeAliasesPackage=com.exer.demo.entity
#mapper文件的位置
mybatis.mapperLocations=classpath:mapper/*.xml
数据库
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` bigint(30) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(30) DEFAULT NULL,
`teacher_id` bigint(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', 'weww', '10', '1');
INSERT INTO `student` VALUES ('2', 'asdd', '12', '1');
-- ----------------------------
-- Table structure for teacher
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`id` bigint(30) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES ('1', 'tytt', '32');
INSERT INTO `teacher` VALUES ('2', 'ytyty', '24');
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL COMMENT '主键',
`name` varchar(64) NOT NULL COMMENT '姓名',
`birthday` date DEFAULT NULL COMMENT '生日',
`address` varchar(256) DEFAULT NULL COMMENT '地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', '小明', '2017-08-12', '北京市丰台区科怡路');
INSERT INTO `user` VALUES ('2', '小刚', '2017-08-12', '北京市海淀区');
-- ----------------------------
-- Table structure for user_device
-- ----------------------------
DROP TABLE IF EXISTS `user_device`;
CREATE TABLE `user_device` (
`id` int(11) NOT NULL COMMENT '主键',
`user_device_name` varchar(64) NOT NULL COMMENT '设备名称',
`user_id` int(11) DEFAULT NULL COMMENT '用户ID',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user_device
-- ----------------------------
INSERT INTO `user_device` VALUES ('1', '安卓', '1');
INSERT INTO `user_device` VALUES ('2', 'IOS', '1');