記事ディレクトリ
MyBatisフレームワークの動的SQL
動的SQL、MyBatisが提供するさまざまなラベルを使用して、SQLステートメントの動的スプライシングを実現するための条件を判断します。ここでの条件付き判断で使用される式は、OGNL式です。一般的に使用される動的SQLタグは、<if>、<where>、<choose />、<foreach>などです。
MyBatisの動的SQLステートメントは、JSTLのステートメントと非常によく似ています。
動的SQLは主に、不確実なクエリ条件の問題を解決するために使用されます。プログラムの実行中、クエリはユーザーが送信したクエリ条件に従って実行されます。送信されたクエリ条件が異なり、実行されたSQLステートメントも異なります。考えられる各状況を1つずつリストし、すべての条件を整理して組み合わせると、多数のSQLステートメントが作成されます。この時点で、動的SQLを使用してこのような問題を解決できます。
環境への準備
1.新しいMavenプロジェクトを作成し、mybatis、mysqlドライバーの依存関係を追加します
2.エンティティクラスStudent、StudentDaoインターフェイス、StudentDao.xml、mybatis.xml、テストクラスを作成します
3.前のテーブルの生徒を使用します。
マッパーの動的SQLで、より大きい(>)、より小さい(=)、以下(<=)などのシンボルがある場合は、それらをエンティティシンボルに変換することをお勧めします。そうしないと、XMLで解析エラーが発生する可能性があります。
特に、小なり記号(<)はXMLに表示されてはなりません。そうしないと、マッパーファイルの解析中にエラーが発生します。
エンティティシンボルテーブル:
< | 未満 | &lt |
>> | 以上 | &gt |
> = | 以上以上 | &gt; = |
<= | 以下 | &lt; = |
動的SQLの場合
このラベルを実行するために、testの値がtrueの場合、そこに含まれるSQLフラグメントは、それが配置されているSQLステートメントにスプライスされます。
構文:<if test ="condition">sqlステートメントの一部</if>
インターフェース方法:
List<Student> selectStudentIf(Student student);
マッパーファイル:
<select id="selectStudentIf" resultType="com.bjpowernode.domain.Student">
select id,name,email,age from student
where 1=1
<if test="name != null and name !='' ">
and name = #{name}
</if>
<if test="age > 0 ">
and age > #{age}
</if>
</select>
試験方法:
@Test
public void testSelect() throws IOException {
Student param = new Student();
param.setName("李力");
param.setAge(18);
List<Student> studentList = studentDao.selectStudentIf(param);
studentList.forEach( stu -> System.out.println(stu));
}
動的SQLはどこにありますか
<if/>タグには厄介な場所があります。whereの後に1=1句を手動で追加する必要があります。なぜなら、whereの後のすべての<if />条件がfalseであり、whereの後に1 = 1句がない場合、SQLの残りの場所は1つだけであり、SQLはエラーを起こします。
したがって、whereの後に、これが発生しないように、常にtrueの句1=1を追加する必要があります。ただし、データ量が多い場合は、クエリの効率に大きく影響します。
<where />タグを使用すると、クエリ条件がある場合、where句が自動的に追加されます。クエリ条件がない場合、where句は追加されません。最初の<if/>タグのSQLフラグメントにはとが含まれていない可能性があることに注意してください。
ただし、とを書くのも良いです、システムは余分なとを削除します。ただし、<if/>内の他のSQLフラグメントのおよびを記述する必要があります。そうしないと、SQLステートメントが正しく連結されません。
構文:<where> other dynamic sql </ where>
インターフェース方法:
List<Student> selectStudentWhere(Student student);
マッパーファイル:
<select id="selectStudentWhere"
resultType="com.bjpowernode.domain.Student">
select id,name,email,age from student
<where>
<if test="name != null and name !='' ">
and name = #{name}
</if>
<if test="age > 0 ">
and age > #{age}
</if>
</where>
</select>
試験方法:
@Test
public void testSelectWhere() throws IOException {
Student param = new Student();
param.setName("李力");
param.setAge(18);
List<Student> studentList = studentDao.selectStudentWhere(param);
studentList.forEach( stu -> System.out.println(stu));
}
動的SQLのforeach
<foreach />タグは、配列とコレクションをトラバースするために使用されます。それを使用するときは、次の点に注意する必要があります。
- collectionは、トラバース、リスト、配列などのコレクションのタイプを表します。
- open、close、separatorは、トラバースされたコンテンツのSQLスプライシングです。
文法:
<foreach collection="集合类型" open="开始的字符" close="结束的字符"
item="集合中的成员" separator="集合成员之间的分隔符">
#{item 的值}
</foreach>
1.トラバースリスト<シンプルタイプ>
式のリストはlistで表され、そのサイズはlist.sizeで表されます。
要件:クエリの学生IDは1002,1005,1006です
インターフェース方法:
List<Student> selectStudentForList(List<Integer> idList);
マッパーファイル:
<select id="selectStudentForList"
resultType="com.bjpowernode.domain.Student">
select id,name,email,age from student
<if test="list !=null and list.size > 0 ">
where id in
<foreach collection="list" open="(" close=")"
item="stuid" separator=",">
#{stuid}
</foreach>
</if>
</select>
試験方法:
@Test
public void testSelectForList() {
List<Integer> list = new ArrayList<>();
list.add(1002);
list.add(1005);
list.add(1006);
List<Student> studentList = studentDao.selectStudentForList(list);
studentList.forEach( stu -> System.out.println(stu));
}
2.トラバースリスト<オブジェクトタイプ>
インターフェース方法:
List<Student> selectStudentForList2(List<Student> stuList);
マッパーファイル:
<select id="selectStudentForList2"
resultType="com.bjpowernode.domain.Student">
select id,name,email,age from student
<if test="list !=null and list.size > 0 ">
where id in
<foreach collection="list" open="(" close=")"
item="stuobject" separator=",">
#{stuobject.id}
</foreach>
</if>
</select>
試験方法:
@Test
public void testSelectForList2() {
List<Student> list = new ArrayList<>();
Student s1 = new Student();
s1.setId(1002);
list.add(s1);
s1 = new Student();
s1.setId(1005);
list.add(s1);
List<Student> studentList = studentDao.selectStudentForList2(list);
studentList.forEach( stu -> System.out.println(stu));
}
動的SQLのコードスニペット
<sql />タグは、他のSQLタグで再利用するSQLフラグメントを定義するために使用されます。他のタグでこのSQLフラグメントを使用するには、<include/>サブタグが必要です。<sql />タグはSQLステートメントの任意の部分を定義できるため、<include/>サブタグは動的SQLの任意の場所に配置できます。
インターフェース方法:
List<Student> selectStudentSqlFragment(List<Student> stuList);
マッパーファイル:
<!--创建 sql 片段 id:片段的自定义名称-->
<sql id="studentSql">
select id,name,email,age from student
</sql>
<select id="selectStudentSqlFragment"
resultType="com.bjpowernode.domain.Student">
<!-- 引用 sql 片段 -->
<include refid="studentSql"/>
<if test="list !=null and list.size > 0 ">
where id in
<foreach collection="list" open="(" close=")"
item="stuobject" separator=",">
#{stuobject.id}
</foreach>
</if>
</select>
試験方法:
@Test
public void testSelectSqlFragment() {
List<Student> list = new ArrayList<>();
Student s1 = new Student();
s1.setId(1002);
list.add(s1);
s1 = new Student();
s1.setId(1005);
list.add(s1);
List<Student> studentList = studentDao.selectStudentSqlFragment(list);
studentList.forEach( stu -> System.out.println(stu));
}