首頁 > 軟體

MyBatis圖文並茂講解註解開發一對一查詢

2022-07-06 10:00:19

MyBatis的註解實現複雜對映開發

實現複雜關係對映之前我們可以在對映檔案中通過設定來實現,使用註解開發後,我們可以使用@Results註解,@Result註解,@One註解,@Many註解組合完成複雜關係的設定

一對一查詢

一對一查詢的模型

一對一查詢的需求:查詢一個使用者資訊,與此同時查詢出該使用者對應的身份證資訊

一對一查詢的語句

建立資料庫:

-- ----------------------------
-- 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};

建立PersonMapper介面

public interface PersonMapper {
    //根據id查詢
    @Select("SELECT * FROM person WHERE id=#{id}")
    public abstract Person selectById(Integer id);
}

使用註解設定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();
}

測試類

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();
    }
}

執行結果如下:

一對一設定總結

註解說明
@Results封裝對映關係的父註解。
Result[] value():定義了 Result 陣列
@Result封裝對映關係的子註解。
column 屬性:查詢出的表中欄位名稱
property 屬性:實體物件中的屬性名稱
javaType 屬性:被包含物件的資料型別
one 屬性:一對一查詢固定屬性
@One一對一查詢的註解。
select 屬性:指定呼叫某個介面中的方法

到此這篇關於MyBatis圖文並茂講解註解開發一對一查詢的文章就介紹到這了,更多相關MyBatis一對一查詢內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com