日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

mybatis注解动态sql_超全MyBatis动态SQL详解

發布時間:2024/9/19 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mybatis注解动态sql_超全MyBatis动态SQL详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

總結

以上是生活随笔為你收集整理的mybatis注解动态sql_超全MyBatis动态SQL详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。