↑ 點選上面 “時代Java”關注我們, 關注新技術,學習新知識!
MyBatis if 标簽
if 标簽是我們最常使用的。在查詢、删除、更新的時候很可能會使用到。必須結合 test 屬性聯合使用。
1 在 WHERE 條件中使用 if 标簽
這是常見的一種現象, 我們在進行按條件查詢的時候, 可能會有多種情況。
1.1 查詢條件
根據輸入的學生資訊進行條件檢索
- 當隻輸入使用者名時, 使用使用者名進行模糊檢索;
- 當隻輸入性别時, 使用性别進行完全比對
- 當使用者名和性别都存在時, 用這兩個條件進行查詢比對查詢
1.2 動态 SQL
接口函數
/** * 根據輸入的學生資訊進行條件檢索 * 1. 當隻輸入使用者名時, 使用使用者名進行模糊檢索; * 2. 當隻輸入郵箱時, 使用性别進行完全比對 * 3. 當使用者名和性别都存在時, 用這兩個條件進行查詢比對的用 * @param student * @return */ ListselectByStudentSelective(Student student);對應的動态 SQL <select id="selectByStudentSelective" resultMap="BaseResultMap" parameterType="com.homejim.mybatis.entity.Student"> select "Base_Column_List" /> from student where 1=1 <if test="name != null and name !=''"> and name like concat('%', #{name}, '%') if> <if test="sex != null"> and sex=#{sex} if> select>
在此 SQL 語句中, where 1=1 是多條件拼接時的小技巧, 後面的條件查詢就可以都用 and 了。
同時, 我們添加了 if 标簽來處理動态 SQL
<if test="name != null and name !=''"> and name like concat('%', #{name}, '%') if> <if test="sex != null"> and sex=#{sex} if>
此 if 标簽的 test 屬性值是一個符合 OGNL 的表達式, 表達式可以是 true 或 false。如果表達式傳回的是數值, 則0為 false, 非 0 為 true;
1.3 測試
@Test public void selectByStudent() { SqlSession sqlSession = null; sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); Student search = new Student(); search.setName("明"); System.out.println("隻有名字時的查詢"); List studentsByName = studentMapper.selectByStudentSelective(search); for (int i = 0; i < studentsByName.size(); i++) { System.out.println(ToStringBuilder.reflectionToString(studentsByName.get(i), ToStringStyle.MULTI_LINE_STYLE)); } search.setName(null); search.setSex((byte) 1); System.out.println("隻有性别時的查詢"); List studentsBySex = studentMapper.selectByStudentSelective(search); for (int i = 0; i < studentsBySex.size(); i++) { System.out.println(ToStringBuilder.reflectionToString(studentsBySex.get(i), ToStringStyle.MULTI_LINE_STYLE)); } System.out.println("姓名和性别同時存在的查詢"); search.setName("明"); List studentsByNameAndSex = studentMapper.selectByStudentSelective(search); for (int i = 0; i < studentsByNameAndSex.size(); i++) { System.out.println(ToStringBuilder.reflectionToString(studentsByNameAndSex.get(i), ToStringStyle.MULTI_LINE_STYLE)); } sqlSession.commit(); sqlSession.close(); }
隻有名字時的查詢, 發送的語句和結果

查詢的條件隻發送了
where 1=1 and name like concat('%', ?, '%')
隻有性别時的查詢, 發送的語句和結果
查詢的條件隻發送了
where 1=1 and sex=?
姓名和性别同時存在的查詢, 發送的語句和結果
查詢條件
where 1=1 and name like concat('%', ?, '%') and sex=?
2 在 UPDATE 更新列中使用 if 标簽
有時候我們不希望更新所有的字段, 隻更新有變化的字段。
2.1 更新條件
隻更新有變化的字段, 空值不更新。
2.1 動态 SQL
接口方法
/** * 更新非空屬性 */ int updateByPrimaryKeySelective(Student record);對應的 SQL "updateByPrimaryKeySelective" parameterType= update student <set> <if test="name != null"> `name` = #{name,jdbcType=VARCHAR}, if> <if test="phone != null"> phone = #{phone,jdbcType=VARCHAR}, if> <if test="email != null"> email = #{email,jdbcType=VARCHAR}, if> <if test="sex != null"> sex = #{sex,jdbcType=TINYINT}, if> <if test="locked != null"> locked = #{locked,jdbcType=TINYINT}, if> <if test="gmtCreated != null"> gmt_created = #{gmtCreated,jdbcType=TIMESTAMP}, if> <if test="gmtModified != null"> gmt_modified = #{gmtModified,jdbcType=TIMESTAMP}, if> set> where student_id = #{studentId,jdbcType=INTEGER}
2.3 測試
@Test public void updateByStudentSelective() { SqlSession sqlSession = null; sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); Student student = new Student(); student.setStudentId(1); student.setName("時代Java"); student.setPhone("13000000000"); System.out.println(studentMapper.updateByPrimaryKeySelective(student)); sqlSession.commit(); sqlSession.close(); }
結果如下
3 在 INSERT 動态插入中使用 if 标簽
我們插入資料庫中的一條記錄, 不是每一個字段都有值的, 而是動态變化的。在這時候使用 if 标簽, 可幫我們解決這個問題。
3.1 插入條件
隻有非空屬性才插入。
3.2 動态SQL
接口方法
/** * 非空字段才進行插入 */ int insertSelective(Student record);對應的SQL"insertSelective" parameterType= insert into student "(" suffix= <if test="studentId != null"> student_id, if> <if test="name != null"> `name`, if> <if test="phone != null"> phone, if> <if test="email != null"> email, if> <if test="sex != null"> sex, if> <if test="locked != null"> locked, if> <if test="gmtCreated != null"> gmt_created, if> <if test="gmtModified != null"> gmt_modified, if> "values (" suffix= <if test="studentId != null"> #{studentId,jdbcType=INTEGER}, if> <if test="name != null"> #{name,jdbcType=VARCHAR}, if> <if test="phone != null"> #{phone,jdbcType=VARCHAR}, if> <if test="email != null"> #{email,jdbcType=VARCHAR}, if> <if test="sex != null"> #{sex,jdbcType=TINYINT}, if> <if test="locked != null"> #{locked,jdbcType=TINYINT}, if> <if test="gmtCreated != null"> #{gmtCreated,jdbcType=TIMESTAMP}, if> <if test="gmtModified != null"> #{gmtModified,jdbcType=TIMESTAMP}, if>
這個 SQL 大家應該很熟悉,畢竟是自動生成的。
3.3 測試
@Test public void insertByStudentSelective() { SqlSession sqlSession = null; sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); Student student = new Student(); student.setName("nowjava"); student.setPhone("13000000000"); student.setEmail("[email protected]"); student.setLocked((byte) 0); System.out.println(studentMapper.insertSelective(student)); sqlSession.commit(); sqlSession.close(); }
SQL 中, 隻有非空的字段才進行了插入。
--
知識分享,時代前行!
~~ 時代Java
還有更多好文章……
請檢視曆史文章和官網,
↓有分享,有收獲~