Mybatisのソースコードの読み方を段階的に教えます(1)、ソースコードの閲覧環境を構築します

ここでは mysql データベースを使用しますが、もちろん他のデータベースも使用できます。

1. データベースの構築

テーブル作成ステートメント:


DROP TABLE IF EXISTS `payment`;
CREATE TABLE `payment`  (
  `id` bigint(20) NOT NULL,
  `serial` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of payment
-- ----------------------------
INSERT INTO `payment` VALUES (1, '2323');
INSERT INTO `payment` VALUES (36, '121212');

SET FOREIGN_KEY_CHECKS = 1;

2.Mavenプロジェクトをビルドする

それでもダメならカタログを送ってください。(mavenプロジェクトはビルドできないので、現時点ではソースコードを見るのは不向きです。mybatisに慣れてからお話します)

2.1. プロジェクトの構造

プロジェクト構造図

2.2. pom ファイル

依存関係、ここではバージョン 3.5.0 を使用しています

	<dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.0</version>
        </dependency>
    </dependencies>

2.3. 関連コード

2.3.1 mybatis設定ファイル

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/db2019?serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/PaymentMapper.xml"/>
    </mappers>
</configuration>

2.3.2 エンティティファイル

package com.fxy.mybatis.entity;

public class Payment {
    
    
        private Long id;
        private String serial;

    public Long getId() {
    
    
        return id;
    }

    public void setId(Long id) {
    
    
        this.id = id;
    }

    public String getSerial() {
    
    
        return serial;
    }

    public void setSerial(String serial) {
    
    
        this.serial = serial;
    }

    @Override
    public String toString() {
    
    
        return "Payment{" +
                "id=" + id +
                ", serial='" + serial + '\'' +
                '}';
    }
}

2.3.3. マッパーインターフェースファイル

package com.fxy.mybatis.reposity;

import com.fxy.mybatis.entity.Payment;

public interface PaymentMapper {
    
    

    Payment getPayment(int id);
}

2.3.4、mapper.xml設定ファイル

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.fxy.mybatis.reposity.PaymentMapper">
    <select id="getPayment" resultType="com.fxy.mybatis.entity.Payment">
        select * from payment where id = #{id}
    </select>
</mapper>

2.3.5. メイン関数エントリファイル

package com.fxy.mybatis;

import com.fxy.mybatis.entity.Payment;
import com.fxy.mybatis.reposity.PaymentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class OrgionCodeRead {
    
    

    public static void main(String[] args) throws IOException {
    
    
        //1\读取配置文件
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //2、初始化mybatis,创建SqlSessionFactory类实例
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //3、创建Session实例
        SqlSession session = sqlSessionFactory.openSession();
        Payment payment = session.selectOne("com.fxy.mybatis.reposity.PaymentMapper.getPayment",36);
        PaymentMapper paymentMapper = session.getMapper(PaymentMapper.class);
        Payment payment1= paymentMapper.getPayment(1);
        System.out.println(payment.toString());
        System.out.println(payment1.toString());

    }
}

3. 実行結果、つまり簡単なデバッグ命令

ここに画像の説明を挿入します
構築した環境に問題がないことが確認できたので、次はデバッグとソースコードの読み込みを段階的に進めていきます。
注意事項:
1. mybatis の使い方をある程度理解してください
2. 設定ファイルはどのように取得され、どこで使用されるのかなど、目的を持ってソースコードを読んでください。マッパーインターフェイスとmapper.xmlはどのように関係していますか? インターフェイスのメソッドと XML の SQL はどのようにバインドされているのでしょうか? 入力パラメータと出力パラメータはどのように変換されますか? SQL ステートメント内の入力パラメーターはどこでパラメーターに置き換えられますか? マッパー インターフェイスの実装はまだ記述していませんが、インターフェイス内のメソッドはどのように呼び出されるでしょうか? 問題はたくさんあります。問題のあるソース コードを参照して、どのように解決したかを見つければ、より良い結果が得られます。そうでないと、読むのが退屈で目的もなく、多くの詳細が無視されてしまいます。
3. ソース コードを読むときは、マクロからミクロ、概要、ミクロからマクロに至るまで、mybatis のフレームワーク全体をある程度理解している必要があります。何度か読むとさらに理解が深まります。

次のセクションでは、mybatis のフレームワーク全体を紹介します。

おすすめ

転載: blog.csdn.net/u010445301/article/details/106711836
おすすめ