MyBatis-Plus(新しいウィンドウが開きます)(略してMP)は、MyBatis(新しいウィンドウが開きます)の拡張ツールで、MyBatisをベースに、変更を加えずに拡張のみを行い、開発の簡素化と効率化を目的として生まれました。公式ウェブサイトの関連コンテンツをご覧になりたい場合は、公式ウェブサイトのアドレス: https://baomidou.com/
特徴
: 非侵入型: 機能強化のみで変更はありません。導入しても既存のプロジェクトには影響しません。シルクのようにスムーズです。
低損失: 基本的な CRUD は起動時に自動的に挿入され、基本的にパフォーマンスの損失はなく、直接オブジェクト指向の操作が行われます。
強力な CRUD 操作: 組み込みの一般的なマッパーと一般的なサービス。フォーム上のほとんどの CRUD 操作はわずかな構成で実現でき、さまざまな使用ニーズを満たす強力な条件付きコンストラクターもあります。
Lambda フォーム呼び出しをサポート: Lambda 式と同様に、さまざまなタイプのクエリ ストリップを記述するのに便利で、フィールドのタイプミスを心配する必要はありません
主キーの自動生成をサポート: 最大 4 つの主キー戦略 (コンテンツ分散型固有 ID ジェネレーター - シーケンス) をサポートし、主キーの問題を完全に解決するために自由に構成できます。
ActiveRecord モードのサポート: ActiveRecord フォーム呼び出しをサポートします。エンティティ クラスは Model クラスを継承するだけで強力な CRUD 操作を実行できます。
カスタムのグローバル ユニバーサル操作をサポート: グローバル ユニバーサル メソッド インジェクションをサポート (一度書いたらどこでも使用可能)
組み込みのコード ジェネレーター: コードまたは Maven プラグインを使用して、マッパー、モデル、サービス、コントローラー、およびレイヤー コードを迅速に生成します。テンプレート エンジンをサポートし、多くのカスタム構成が利用可能です。
組み込みのページング プラグイン: 基本的な MyBatis 物理ページング、開発者は特定の操作を気にする必要はありませんプラグインの設定後、ページングの作成は通常のリスト クエリと同等です
ページング プラグインはさまざまなデータベースをサポートします。MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer およびその他のデータベースをサポートします。
組み込みのパフォーマンス分析プラグイン: SQL ステートメントとその実行時間を出力できます。遅いクエリを迅速に特定するために、開発およびテスト中にこの機能を有効にすることをお勧めします。
組み込みのグローバル インターセプト プラグイン: テーブル全体の削除および更新操作のインテリジェントな分析とブロックを提供します。また、誤操作を防ぐためにインターセプト ルールをカスタマイズすることもできます。
MyBatis-Plus の開始
Maven プロジェクトの作成
以下の図に示す手順で Maven プロジェクト構造を作成します。
最初の一歩:
ステップ2:
「次へ」をクリックします
最終的なプロジェクト構造:
データベースを準備します。
依存関係の追加
1. 指定した依存関係を pom.xml に追加します。
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.11</version>
</plugin>
< /プラグイン>
</ビルド>
設定ファイル yml を書き込む
2. プロパティ設定ファイルとして .yml を使用することに慣れているため、プロパティ ファイルを変更し、関連する SQL 設定を書き込みます。
アプリケーション.yml
サーバー:
ポート: 8888
春:
devtools:
再起動:
有効: true
追加パス: "src/main/java"
除外: "static/**"
データソース:
ユーザー名: "root"
パスワード: "h123456"
ドライバークラス名: "com.mysql .cj.jdbc.Driver"
url: "jdbc:mysql://localhost:3306/db_school?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false"
入門サンプルの作成とテスト
1. StudentMapper.java インターフェイスを作成します。
パッケージ com.etime.day15mybatisplus.mapper;
インポートcom.baomidou.mybatisplus.core.mapper.BaseMapper;
com.etime.day15mybatisplus.pojo.Student をインポートします。
org.springframework.stereotype.Repository をインポートします。
//StudentMapper クラスに導入
@Repository
//BaseMapper<T> のジェネリック型と Student クラスを覚えておく Student
パブリック インターフェイス StudentMapper extends BaseMapper<Student> { }
2. StudentService.java インターフェースを作成する
パッケージcom.etime.day15mybatisplus.service;
com.etime.day15mybatisplus.pojo.Student をインポートします。
java.util.Listをインポートします。
public Interface StudentService { //すべての学生情報を取得 List<Student> getAllStudent(); }
3. StudentService.javaインタフェースの実装クラスStudentServiceImpl.javaを作成します。
パッケージ com.etime.day15mybatisplus.service.impl;
com.etime.day15mybatisplus.mapper.StudentMapper をインポートします。
com.etime.day15mybatisplus.pojo.Student をインポートします。
com.etime.day15mybatisplus.service.StudentService をインポートします。
org.springframework.beans.factory.annotation.Autowired をインポートします。
org.springframework.stereotype.Service をインポートします。
java.util.Listをインポートします。
// 注入サービス
@Service
public class StudentServiceImplimplemented StudentService {
// @Autowired アノテーションを介して StudentMapper インターフェイスを
@Autowired
private StudentMapper StudentMapper に挿入します。
@Override
public List<Student> getAllStudent() { //すべての学生情報を取得 List<Student> list =studentMapper.selectList(null); return list; } }
4. StudentController.java クラス制御層を作成します。
パッケージcom.etime.day15mybatisplus.controller;
com.etime.day15mybatisplus.pojo.Student をインポートします。
com.etime.day15mybatisplus.service.StudentService をインポートします。
org.springframework.beans.factory.annotation.Autowired をインポートします。
org.springframework.web.bind.annotation.GetMapping をインポートします。
org.springframework.web.bind.annotation.RestController をインポートします。
java.util.Listをインポートします。
// RestController アノテーションを使用して、コントローラーに加えて @ResponseBody アノテーションを含めます
@RestController
public class StudentController {
// アノテーション @Autowired を使用して、インターフェイス StudentService を
@Autowired
private StudentService StudentService に挿入します。
//クエリ送信アノテーションを使用します @GetMapping メソッド
//すべての学生情報をクエリ
@GetMapping("/student")
public List<Student> getAllStudent(){ returnstudentService.getAllStudent(); } }
5. マッパー ファイル スキャンをアプリケーション起動クラスに追加します。
パッケージcom.etime.day15mybatisplus;
org.mybatis.spring.annotation.MapperScan をインポートします。
org.springframework.boot.SpringApplication をインポートします。
org.springframework.boot.autoconfigure.SpringBootApplication をインポートします。
//インジェクトマッパーファイルスキャン
@MapperScan("com.etime.day15mybatisplus.mapper")
@SpringBootApplication
public class Day15mybatisPlusApplication {
public static void main(String[] args) { SpringApplication.run(Day15mybatisPlusApplication.class, args); }
}
6. 実行結果:
テストには Apifox インターフェイス テスト ツールを使用します (必要に応じて、この記事を読んでダウンロード、インストール、使用できます: https://blog.csdn.net/m0_56245143/article/details/130270652?spm=1001.2014.3001.5501)
MyBatis-Plus は SQL ログを出力します
。application.xml ファイルで、ログ出力を設定します。
mybatis-plus:
configuration:
log-impl:org.apache.ibatis.logging.stdout.StdOutImpl
上記を再度テストし、アイデア コンソールを表示します。
MyBatis-Plus によってエンティティ クラス属性に使用されるアノテーション構成。
変更された学生クラス オブジェクト: Student.java
パッケージ com.etime.day15mybatisplus.pojo;
com.baomidou.mybatisplus.annotation.TableField をインポートします。
com.baomidou.mybatisplus.annotation.TableId をインポートします。
com.baomidou.mybatisplus.annotation.TableName をインポートします。
インポート lombok.AllArgsConstructor;
lombok.Data をインポートします。
インポート lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
@Data
//@TableName("tb_student") //データベース テーブルが変更された場合は、@TableName アノテーションを使用します
public class Student { //別の状況は、エンティティ クラスの属性フィールド名がエンティティ クラスの属性フィールド名と一致しない場合です。データベース テーブル 同じ、 // 主キーには @TableId("") の注釈を付け、データベース テーブルの主キーと同じ名前を付ける必要があることに注意してください @TableId("sid") private int stuId; //主キーが異なることを除いて、他の属性は @TableField(" ") アノテーションを使用します @TableField("sname") private String stuName;
@TableField("sgender")
プライベート文字列 stuGender;
@TableField("sage")
private int stuAge;
@TableField("semail")
private String stuEmail;
@TableField("sphoto")
プライベート文字列 stuPhoto;
}
操作結果:
MyBatis-Plusは、CRUD(追加・削除・修正・確認機能の実装)を実装し、
新たな学生情報を追加します。
1. StudentService.java インターフェイスに新しい Student メソッドを追加します
//学生情報を追加
int addStudent(Student Student);
2. StudentService インターフェースの StudentServiceImpl 実装クラスにオーバーライドされた新しいメソッドを記述します
//学生情報を追加
@Override
public int addStudent(Student Student) { returnstudentMapper.insert(student); } 3. StudentController.java クラスに学生情報を追加するメソッドを記述します
// @PostMapping アノテーション送信メソッドを使用して学生情報を追加
@PostMapping("/student")
public int addStudent(Student Student){ returnstudentService.addStudent(student); } 4. 実行結果:
Apifox ツールを使用してテストします。
学生情報の変更
1. StudentService.javaインタフェースに学生情報変更メソッドを記述する
//学生情報を変更
int updateStudent(Student Student);
2. StudentService インターフェースの StudentServiceImpl 実装クラスにオーバーライド学生更新メソッドを記述します
//学生情報を変更
@Override
public int updateStudent(Student Student) { //学生 ID に基づいて学生情報を変更 returnstudentMapper.updateById(student); } 3. StudentController.java クラスに学生情報を変更するメソッドを記述します
// @PutMapping アノテーションを使用して学生情報を送信する方法を変更します
@PutMapping("/student")
public int updateStudent(Student Student){ returnstudentService.updateStudent(student); } 4. 実行結果:
学生IDに基づいた学生情報の削除
1. StudentService.javaインターフェースに学生IDに基づいて学生情報を削除するメソッドを記述する
//学生IDに基づいて学生情報を削除
int deleteStudent(int sid);
2. StudentServiceインターフェースのStudentServiceImpl実装クラスに学生IDに基づいて学生情報を削除するメソッドを記述する
//学生IDに基づいて学生情報を削除
@Override
public int deleteStudent(int sid) { returnstudentMapper.deleteById(sid); }
3. 学生IDに基づいて学生情報を削除するコントロールクラスStudentController.javaを記述します。
//学生IDに基づいて学生情報を削除し、@DeleteMappingアノテーションを使用して送信します
@DeleteMapping("/student/{sid}")
public int deleteStudent(@PathVariable("sid") int sid){ returnstudentService.deleteStudent(sid ) ; } 4. 実行結果:
ページング クエリは、
MyBatis-Plus の組み込みページング プラグインを使用します。構成を完了するには、関連するコンテンツを構成し、構成クラス MybatisPlusConfig.java を作成する必要があります。
プラグイン構成 MybatisPlusConfig.java
パッケージcom.etime.day15mybatisplus.util;
com.baomidou.mybatisplus.annotation.DbType をインポートします。
com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor をインポートします。
インポート com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
org.springframework.context.annotation.Bean をインポートします。
org.springframework.context.annotation.Configuration をインポートします。
@Configuration //解析関連の設定ファイルを挿入
public class MybatisPlusConfig { @Bean //現在のクラス オブジェクトを public MybatisPlusInterceptor paginationInterceptor(){ MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); //データベースを MySQL として指定 interceptor.addInnerInterceptor(new PaginationInnerInterceptor (DbType .MYSQL)); インターセプタを返します; } }
1. StudentService.java インターフェイス クラスですべてのデータをクエリし、ページングを実行するメソッドを作成します。
//無条件クエリ データ ページング
Page<Student> getStudentByPage(int current,int size);
2. StudentServiceImpl.java の StudentService インターフェイス実装クラスで、すべてのデータをクエリし、ページングを実行するメソッドを記述します。
//無条件のデータ クエリとページング (つまり、すべてのデータのクエリとページング)
@Override
public Page<Student> getStudentByPage(int current, int size) { Page<Student> page = new Page<>(current,size); return StudentMapper.selectPage(page,null); } 3. コントロール層 StudentContrroller.java クラスに実装メソッドを記述する
// 無条件のクエリ データ ページングを使用し、@GetMapping を使用してデータを送信および挿入します @GetMapping
("/student/page")
public Page<Student> getStudentByPage(int current,int size){ returnstudentService.getStudentByPage(current,size); 4.実行結果:
条件付きクエリとページング
MyBatis-Plus の組み込みクエリは、さまざまな条件付きクエリの作成に役立ちます
以下は、一般的に使用されるメソッドの基本的な表です。
1. StudentService.java インターフェイスに条件付きクエリとページング メソッドを作成します。
//条件付きクエリとページング
Page<Student> getStudentBySname(String sname,int courent,int size);
2. StudentService インターフェイスの StudentServiceImpl 実装クラスに条件付きクエリとページングのメソッドを記述します
//条件付きクエリとページング
@Override
public Page<Student> getStudentBySname(String sname, int courrent, int size) { QueryWrapper<Student> Wrapper = new QueryWrapper<>(); //ラッパー オブジェクトに条件を追加します Wrapper.like ( "sname",sname); Page<Student> page = new Page<>(courent,size); returnstudentMapper.selectPage(page,wrapper); } 3. StudentController.java コントロール クラスに条件付きクエリとページングを追加します。
//条件付きクエリとページングは @GetMapping を使用してデータを送信し、データを挿入します @GetMapping
("student/like")
public Page<Student> getStudentBySname(String sname,int current,int size){ returnstudentService.getStudentBySname(sname,current , size); } 4. 実行結果:
フロントエンドとバックエンドの個別開発と統合
前回の準備では基本的なコードに問題はなかったので、上記が完了したらフロントエンドとバックエンドを個別に開発する方法を使用します。問題はないので、Vue3 フロントエンドを使用して HTML ページのテストを作成します。
生徒の情報をページに表示します。
クロスドメインの問題を解決する
解決すべき最初の問題は、アイデアと VScode 開発ツールによるクロスドメインの問題です。
図に示すように、アノテーション @CrossOrigin をコントローラー層に追加して、クロスドメインの問題を解決します。
VScode でstudentInfo.html ファイルを作成します。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-互換" content="IE=edge">
<メタ名="viewport" content="width=device-width,Initial-scale=1.0">
<title>ドキュメント</title>
<script src="js/axios.min.js"></script>
<script src= 「js/vue.global.js"></script>
</head>
<body>
<div id="app">
<div>
<table>
<tr>
<td>名前</td>
<td>性別</td>
<td>年齢</td>
<td>メール</td>
<td>アバター</td>
<td>操作</td>
</tr>
<tr v-for="学生のstu">
<td>{
{stu.sname}}</td>
<td>{
{stu.sgenger}}</ td>
<td>{
{stu.sage}}</td>
<td>{
{stu.semail}}</td>
<td>
{
{stu.sphoto}}
</td>
</tr>
</テーブル>
<div>
<a href="javascript:void(0)" @click="getStudentByPage(1)">首页</a>
<a href="javascript:void(0)" @click="getStudentByPage(prevPage)">上一页</a>
{
{current}}/{
{pages}}
<a href="javascript:void(0) )" @click="getStudentByPage(nextPage)">次页</a>
<a href="javascript:void(0)" @click="getStudentByPage(pages)">尾页</a>
</div >
</div>
</div>
<script>
const app =Vue.createApp({ data(){ return{ 学生:"", current:1, size:5, ページ:""、 前のページ:""、
nextPage:"",
sname:""
}
}、
methods:{ getStudentByPage(page){ let content = new URLSearchParams(); content.append("現在の",ページ); content.append("サイズ",this.size); axios({ url:"http://localhost:8888/student/page", method:"get", params:content }).then(resp =>{ console.log(resp) これ。
this.pages = resp.data.pages;
this.current = resp.data.current;
if(this.current ==1){ this.prevPage = 1; }else{ this.prevPage = this.current -1; if (this.current == this.pages){ this.nextPage = this.pages; }else{ this.nextPage = this.current + 1; } }); } }、 作成された(){
this.getStudentByPage(1);
}
});
app.mount("#app");
</script>
</body>
</html>