マッパーインターフェイスでのメソッド分析

原文:mybatisリバースエンジニアリングで生成されたサンプルクラスの使用_thedarkclouds blog-CSDNブログ_mybatisリバースエンジニアリングで生成されたサンプルクラスの使用 

1. マッパー インターフェイスのメソッドは、
マッパー インターフェイスの関数とメソッドを分析します。

メソッド 機能 説明
int countByExample(UserExample の例) thorws SQLException 条件によるカウント
int deleteByPrimaryKey(Integer id) thorws SQLException 主キーによる削除
int deleteByExample(UserExample の例) thorws SQLException 条件によるクエリ
String/Integer insert(User Example) thorws SQLExce ption 挿入データ(戻り値は ID)
User selectByPrimaryKey(Integer id) thorws SQLException 主キーによるクエリ
ListselectByExample(UserExample の例) thorws SQLException 条件によるクエリ
ListselectByExampleWithBLOGs(UserExample の例) thorws SQLException 条件によるクエリ (BLOB フィールドを含む)。データ テーブルのフィールド タイプがバイナリの場合にのみ生成されます。
int updateByPrimaryKey(User Record) thorws SQLException 主キーによる更新
int updateByPrimaryKeySelective(User Record) thorws SQLException 主キーによる更新 値が null でないフィールド
int updateByExample(User Record, UserExample example) thorws SQLException 条件による更新
int updateByExampleSelective(User Record, UserExample example) thorws SQLException 値が null ではないフィールドを条件
2によって更新します
。 mybatis のリバース エンジニアリングでのインスタンスの解析例では、インスタンスが生成され、インスタンスの対応例。example は、where
xxxExample example = new xxxExample(); 
Criteria criteria = new Example().createCriteria();の後ろの部分に条件を追加するために使用されます。 

メソッド 説明
example.setOrderByClause("フィールド名 ASC"); 昇順条件を追加、DESC は降順
example.setDistinct(false) 重複を削除、ブール型、true は重複しないレコードを選択します。
criteria.andXxxIsNull フィールド xxx が null であるという条件を追加します。criteria.andXxxIsNotNullフィールド xxx が
nullではないという条件
を追加します。 value) xxx フィールドが value より大きいという条件を追加しますcriteria.andXxxGreaterThanOrEqualTo(value) という条件を追加しますxxx フィールドが値基準以上であるという条件を追加します。andXxxLessThan(value) xxx フィールドが値基準未満であるという条件を追加します。andXxxLessThanOrEqualTo (value) xxx フィールドが値基準以下であるという条件を追加します。andXxxIn ( List<?>) List<? に xxx フィールドの値を追加します。>条件条件.andXxxNotIn(List<?>) xxx フィールドの値がリストにないことを追加します<? > 条件







criteria.andXxxLike("%"+value+"%") xxx フィールドの値が value であるファジー クエリ条件を追加します
criteria.andXxxNotLike("%"+value+"%") xxx フィールドの値が value でないファジー クエリ条件を追加しますvalue
criteria.andXxxBetween (value1,value2) xxx フィールドの値が value1 と value2 の間にあるという条件を追加します
criteria.andXxxNotBetween(value1,value2) xxx フィールドの値が value1 と value2 の間にないという条件を追加します
3.応用例
1.クエリ

①selectByPrimaryKey()

ユーザー user = XxxMapper.selectByPrimaryKey(100); // select * from user where id = 100
1に相当します
。 ② selectByExample() および selectByExampleWithBLOGs()

UserExample 例 = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("wyw");
criteria.andUsernameIsNull();
example.setOrderByClause("username asc,email desc");
List<?>list = XxxMapper.selectByExample(example);
//以下と同等: select * from user where username = 'wyw' and username is null order by username asc,email desc
注: iBator リバース エンジニアリングによって生成されたファイル XxxExample.java には、静的な内部クラス Criteria のメソッドは、SQL ステートメントの後にクエリ条件を定義します。

2. データを挿入する

①インサート()

ユーザー user = new User();
user.setId("dsfgsdfgdsfgds");
user.setユーザー名("管理者");
user.setPassword("admin")
user.setEmail("[email protected]");
XxxMapper.insert(ユーザー);
// 相当:user(ID,username,password,email) 値に挿入 ('dsfgsdfgdsfgds','admin','admin','[email protected]');
3.データを更新する

①updateByPrimaryKey()

ユーザー user =new User();
user.setId("dsfgsdfgdsfgds");
user.setユーザー名("wyw");
user.setPassword("wyw");
user.setEmail("[email protected]");
XxxMapper.updateByPrimaryKey(ユーザー);
// 相当:update user set username='wyw'、password='wyw'、email='[email protected]' where id='dsfgsdfgdsfgds'
②updateByPrimaryKeySelective()

ユーザー user = new User();
user.setId("dsfgsdfgdsfgds");
user.setPassword("wyw");
XxxMapper.updateByPrimaryKey(ユーザー);
// 相当:update user set password='wyw' where id='dsfgsdfgdsfgds'
③ updateByExample() と updateByExampleSelective()

UserExample 例 = new UserExample();
基準 criteria = example.createCriteria();
criteria.andUsernameEqualTo("admin");
ユーザー user = new User();
user.setPassword("wyw");
XxxMapper.updateByPrimaryKeySelective(user,example );
//以下と同等: update user set passwd='wyw' where username='admin'
updateByExample() は、null のフィールドを含むすべてのフィールドを更新します。更新するフィールドを更新するには、updateByExampleSelective() を使用することをお勧めします。

4. データの削除

①PrimaryKey()による削除

XxxMapper.deleteByPrimaryKey(1);  //相当于:delete from user where id=1
 
②deleteByExample()

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("admin");
XxxMapper.deleteByExample(example);
// 同等: ユーザーから削除 where username='admin'
5. クエリデータ量

①countByExample()

UserExample の例 = new UserExample();
基準 criteria = example.createCriteria();
criteria.andUsernameEqualTo("wyw");
int count = XxxMapper.countByExample(例);

———————————————
著作権表示: この記事は CSDN ブロガー「Wind Dance Songlin Jian」のオリジナル記事であり、CC 4.0 BY-SA 著作権契約に従い、オリジナルの情報源を添付してください転載用のリンクとこの声明。
元のリンク: https://blog.csdn.net/thedarkclouds/article/details/82384606

おすすめ

転載: blog.csdn.net/lan861698789/article/details/114884071