0. MyBatis的注解实现复杂映射开发
实现复杂关系映射之前我们可以在映射文件中通过配置来实现,使用注解开发后,我们可以使用@Results注解,@Result注解,@One注解,@Many注解组合完成复杂关系的配置
1. 一对一查询
1.1 一对一查询的模型
一对一查询的需求:查询一个用户信息,与此同时查询出该用户对应的身份证信息
1.2 一对一查询的语句
创建数据库:
-- ---------------------------- -- 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;
对应的sql语句:
SELECT * FROM card; SELECT * FROM person WHERE id=#{id};
1.3 创建PersonMapper接口
public interface PersonMapper { //根据id查询 @Select("SELECT * FROM person WHERE id=#{id}") public abstract Person selectById(Integer id); }
1.4 使用注解配置Mapper
public interface CardMapper { //查询全部 @Select("SELECT * FROM card") @Results({ @Result(column = "id",property = "id"), @Result(column = "number",property = "number"), @Result( property = "p", // 被包含对象的变量名 javaType = Person.class, // 被包含对象的实际数据类型 column = "pid", // 根据查询出的card表中的pid字段来查询person表 /* one、@One 一对一固定写法 select属性:指定调用哪个接口中的哪个方法 */ one = @One(select = "com.yyl.one_to_one.PersonMapper.selectById") ) }) public abstract List<Card> selectAll(); }
1.5 测试类
public class Test01 { @Test public void selectAll() throws Exception{ //1.加载核心配置文件 InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml"); //2.获取SqlSession工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); //3.通过工厂对象获取SqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(true); //4.获取CardMapper接口的实现类对象 CardMapper mapper = sqlSession.getMapper(CardMapper.class); //5.调用实现类对象中的方法,接收结果 List<Card> list = mapper.selectAll(); //6.处理结果 for (Card card : list) { System.out.println(card); } //7.释放资源 sqlSession.close(); is.close(); } }
运行结果如下:
1.6 一对一配置总结