0. MyBatis的注解实现复杂映射开发
实现复杂关系映射之前我们可以在映射文件中通过配置来实现,使用注解开发后,我们可以使用@Results注解,@Result注解,@One注解,@Many注解组合完成复杂关系的配置
1. 项目准备工作
目录结构
1.1 建表
-- ---------------------------- -- Table structure for card -- ---------------------------- DROP TABLE IF EXISTS `card`; CREATE TABLE `card` ( `id` int(11) NOT NULL AUTO_INCREMENT, `number` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, `pid` int(11) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE, INDEX `cp_fk`(`pid`) USING BTREE ) ENGINE = MyISAM AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of card -- ---------------------------- INSERT INTO `card` VALUES (1, '12345', 1); INSERT INTO `card` VALUES (2, '23456', 2); INSERT INTO `card` VALUES (3, '34567', 3); -- ---------------------------- -- Table structure for classes -- ---------------------------- DROP TABLE IF EXISTS `classes`; CREATE TABLE `classes` ( `id` int(11) NOT NULL AUTO_INCREMENT, `NAME` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = MyISAM AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of classes -- ---------------------------- INSERT INTO `classes` VALUES (1, '极地一班'); INSERT INTO `classes` VALUES (2, '极地二班'); -- ---------------------------- -- Table structure for course -- ---------------------------- DROP TABLE IF EXISTS `course`; CREATE TABLE `course` ( `id` int(11) NOT NULL AUTO_INCREMENT, `NAME` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = MyISAM AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of course -- ---------------------------- INSERT INTO `course` VALUES (1, '语文'); INSERT INTO `course` VALUES (2, '数学'); -- ---------------------------- -- Table structure for person -- ---------------------------- DROP TABLE IF EXISTS `person`; CREATE TABLE `person` ( `id` int(11) NOT NULL AUTO_INCREMENT, `NAME` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, `age` int(11) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = MyISAM AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of person -- ---------------------------- INSERT INTO `person` VALUES (1, '张三', 23); INSERT INTO `person` VALUES (2, '李四', 24); INSERT INTO `person` VALUES (3, '王五', 25); -- ---------------------------- -- Table structure for stu_cr -- ---------------------------- DROP TABLE IF EXISTS `stu_cr`; CREATE TABLE `stu_cr` ( `id` int(11) NOT NULL AUTO_INCREMENT, `sid` int(11) NULL DEFAULT NULL, `cid` int(11) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE, INDEX `sc_fk1`(`sid`) USING BTREE, INDEX `sc_fk2`(`cid`) USING BTREE ) ENGINE = MyISAM AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Fixed; -- ---------------------------- -- Records of stu_cr -- ---------------------------- INSERT INTO `stu_cr` VALUES (1, 1, 1); INSERT INTO `stu_cr` VALUES (2, 1, 2); INSERT INTO `stu_cr` VALUES (3, 2, 1); INSERT INTO `stu_cr` VALUES (4, 2, 2); -- ---------------------------- -- Table structure for student -- ---------------------------- DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `NAME` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, `age` int(11) NULL DEFAULT NULL, `cid` int(11) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE, INDEX `cs_fk`(`cid`) USING BTREE ) ENGINE = MyISAM AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of student -- ---------------------------- INSERT INTO `student` VALUES (1, '张三', 23, 1); INSERT INTO `student` VALUES (2, '李四', 24, 1); INSERT INTO `student` VALUES (3, '王五', 25, 2); INSERT INTO `student` VALUES (4, '赵六', 26, 2); SET FOREIGN_KEY_CHECKS = 1;
1.2 MybatisConfig.xml配置
<?xml version="1.0" encoding="UTF-8" ?> <!--MyBatis的DTD约束--> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!--configuration 核心根标签--> <configuration> <!--引入数据库连接的配置文件--> <properties resource="jdbc.properties"/> <!--配置LOG4J--> <settings> <setting name="logImpl" value="log4j"/> </settings> <!--起别名--> <typeAliases> <package name="com.yyl.bean"/> </typeAliases> <!--environments配置数据库环境,环境可以有多个。default属性指定使用的是哪个--> <environments default="mysql"> <!--environment配置数据库环境 id属性唯一标识--> <environment id="mysql"> <!-- transactionManager事务管理。 type属性,采用JDBC默认的事务--> <transactionManager type="JDBC"></transactionManager> <!-- dataSource数据源信息 type属性 连接池--> <dataSource type="POOLED"> <!-- property获取数据库连接的配置信息 --> <property name="driver" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <!--配置映射关系--> <mappers> <package name="com.yyl"/> </mappers> </configuration>
1.3 properties
jdbc
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/db2 username=root password=root
log4j
# Global logging configuration # ERROR WARN INFO DEBUG log4j.rootLogger=DEBUG, stdout # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
1.4 Bean
Card
package com.yyl.bean; public class Card { private Integer id; //主键id private String number; //身份证号 private Person p; //所属人的对象 public Card() { } public Card(Integer id, String number, Person p) { this.id = id; this.number = number; this.p = p; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public Person getP() { return p; } public void setP(Person p) { this.p = p; } @Override public String toString() { return "Card{" + "id=" + id + ", number='" + number + '\'' + ", p=" + p + '}'; } }
Classes
package com.yyl.bean; import java.util.List; public class Classes { private Integer id; //主键id private String name; //班级名称 private List<Student> students; //班级中所有学生对象 public Classes() { } public Classes(Integer id, String name, List<Student> students) { this.id = id; this.name = name; this.students = students; } 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 List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } @Override public String toString() { return "Classes{" + "id=" + id + ", name='" + name + '\'' + ", students=" + students + '}'; } }
Course
package com.yyl.bean; public class Course { private Integer id; //主键id private String name; //课程名称 public Course() { } public Course(Integer id, String name) { this.id = id; this.name = name; } 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; } @Override public String toString() { return "Course{" + "id=" + id + ", name='" + name + '\'' + '}'; } }