mybatis中一個標籤中使用兩個update更新兩個表_超全MyBatis動態SQL詳解!( 看完SQL爽多了)...
技術標籤:mybatis中一個標籤中使用兩個update更新兩個表mybatis動態sqlmybatis動態sql標籤有哪些mybatis按匹配度排序
MyBatis 令人喜歡的一大特性就是動態 SQL。 在使用 JDBC 的過程中, 根據條件進行 SQL 的拼接是很麻煩且很容易出錯的。 MyBatis 動態 SQL 的出現, 解決了這個麻煩。
MyBatis通過 OGNL 來進行動態 SQL 的使用的。目前, 動態 SQL 支援以下幾種標籤:
1 資料準備
為了後面的演示, 建立了一個 Maven 專案 mybatis-dynamic, 建立了對應的資料庫和表
DROP TABLE IF EXISTS `student`;CREATE TABLE `student` ( `student_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '編號', `name` varchar(20) DEFAULT NULL COMMENT '姓名', `phone` varchar(20) DEFAULT NULL COMMENT '電話', `email` varchar(50) DEFAULT NULL COMMENT '郵箱', `sex` tinyint(4) DEFAULT NULL COMMENT '性別', `locked` tinyint(4) DEFAULT NULL COMMENT '狀態(0:正常,1:鎖定)', `gmt_created` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '存入資料庫的時間', `gmt_modified` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改的時間', `delete` int(11) DEFAULT NULL, PRIMARY KEY (`student_id`)) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='學生表';
對應的專案結構
2 if 標籤
if 標籤是我們最常使用的。 在查詢、刪除、更新的時候很可能會使用到。 必須結合 test 屬性聯合使用。
2.1 在 WHERE 條件中使用 if 標籤
這是常見的一種現象, 我們在進行按條件查詢的時候, 可能會有多種情況。
2.1.1 查詢條件
根據輸入的學生資訊進行條件檢索
- 當只輸入使用者名稱時, 使用使用者名稱進行模糊檢索;
- 當只輸入性別時, 使用性別進行完全匹配
- 當用戶名和性別都存在時, 用這兩個條件進行查詢匹配查詢
2.1.2 動態 SQL
介面函式
/** * 根據輸入的學生資訊進行條件檢索 * 1. 當只輸入使用者名稱時, 使用使用者名稱進行模糊檢索; * 2. 當只輸入郵箱時, 使用性別進行完全匹配 * 3. 當用戶名和性別都存在時, 用這兩個條件進行查詢匹配的用 * @param student * @return */ List selectByStudentSelective(Student student);
對應的動態 SQL
select from student where 1=1 and name like concat('%', #{name}, '%') and sex=#{sex}
在此 SQL 語句中, where 1=1 是多條件拼接時的小技巧, 後面的條件查詢就可以都用 and 了。
同時, 我們添加了 if 標籤來處理動態 SQL
and name like concat('%', #{name}, '%') and sex=#{sex}
此 if 標籤的 test 屬性值是一個符合 OGNL 的表示式, 表示式可以是 true 或 false。 如果表示式返回的是數值, 則0為 false, 非 0 為 true;
2.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.2 在 UPDATE 更新列中使用 if 標籤
有時候我們不希望更新所有的欄位, 只更新有變化的欄位。
2.2.1 更新條件
只更新有變化的欄位, 空值不更新。
2.2.1 動態 SQL
介面方法
/** * 更新非空屬性 */ int updateByPrimaryKeySelective(Student record);
對應的 SQL
update student `name` = #{name,jdbcType=VARCHAR}, phone = #{phone,jdbcType=VARCHAR}, email = #{email,jdbcType=VARCHAR}, sex = #{sex,jdbcType=TINYINT}, locked = #{locked,jdbcType=TINYINT}, gmt_created = #{gmtCreated,jdbcType=TIMESTAMP}, gmt_modified = #{gmtModified,jdbcType=TIMESTAMP}, where student_id = #{studentId,jdbcType=INTEGER}
2.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("明明"); student.setPhone("13838438888"); System.out.println(studentMapper.updateByPrimaryKeySelective(student)); sqlSession.commit(); sqlSession.close(); }
結果如下
2.3 在 INSERT 動態插入中使用 if 標籤
我們插入資料庫中的一條記錄, 不是每一個欄位都有值的, 而是動態變化的。 在這時候使用 if 標籤, 可幫我們解決這個問題。
2.3.1 插入條件
只有非空屬性才插入。
2.3.2 動態SQL
介面方法
/** * 非空欄位才進行插入 */ int insertSelective(Student record);
對應的SQL
insert into student