MyBatis項(xiàng)目的創(chuàng)建和增刪查改操作詳解
1. 項(xiàng)目的創(chuàng)建
第一步還是和之前的 Spring 項(xiàng)目一樣
然后還需要添加以下依賴,Lombok 和 web 依賴也可以加上
之后需要進(jìn)行數(shù)據(jù)庫(kù)的配置,這里通過(guò) yml 來(lái)演示:
spring: application: name: mybatis-demo datasource: url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=false username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver
2. 中文亂碼問(wèn)題解決
yml 配置文件中,可能中文會(huì)出現(xiàn)亂碼,可以設(shè)置 idea 中的編碼方式來(lái)避免亂碼
為了避免之后創(chuàng)建的項(xiàng)目也出現(xiàn)亂碼,還需要更改一下新項(xiàng)目的設(shè)置
3. MyBatis 的簡(jiǎn)單操作
3.1. 查詢
可以把數(shù)據(jù)庫(kù)中的表的字段抽象為一個(gè)對(duì)象進(jìn)行描述
@Data public class UserInfo { private Integer id; private String username; private String password; private Integer age; private Integer gender; private String phone; private Integer deleteFlag; private Date createTime; private Date updateTime; }
然后定義接口進(jìn)行查詢
@Mapper public interface UserInfoMapper { //查詢所有的用戶信息 @Select("select * from user_info") List<UserInfo> selectAll(); }
在 Spring Boot 工程中,已經(jīng)自動(dòng)創(chuàng)建好了測(cè)試類,可以在這個(gè)類中進(jìn)行測(cè)試
@SpringBootTest class MybatisDemoApplicationTests { @Autowired private UserInfoMapper userInfoMapper; @Test void contextLoads() { System.out.println(userInfoMapper.selectAll()); } }
3.1.1. 駝峰轉(zhuǎn)化
表中的數(shù)據(jù)都被轉(zhuǎn)化為一個(gè)對(duì)象信息打印出來(lái)了,但是發(fā)現(xiàn)有幾個(gè)字段是沒有賦值的,只有和 java 對(duì)象屬性和數(shù)據(jù)庫(kù)字段一樣時(shí)才會(huì)進(jìn)行賦值
但是由于數(shù)據(jù)庫(kù)的命名規(guī)范和 java 的命名規(guī)范是不一樣的,java 中駝峰命名,如果想要保持一致的話,可以進(jìn)行別名的方式
@Select("select id, username, password, age, gender, phone, " + "delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from user_info") List<UserInfo> selectAll();
除了這種方式之外還可以通過(guò)注解來(lái)存儲(chǔ)映射:
一個(gè)@Results
注解中以數(shù)組的形式存儲(chǔ)@Result
注解
@Results({ @Result(column = "delete_flag", property = "deleteFlag"), @Result(column = "create_time", property = "createTime"), @Result(column = "update_time", property = "updateTime"), }) @Select("select id, username, password, age, gender, phone, delete_flag, create_time, update_time from user_info") List<UserInfo> selectAll();
和上面的方式不同的是,這里配置一次映射整個(gè)類都可以用
@Results(id = "BaseMap", value = { @Result(column = "delete_flag", property = "deleteFlag"), @Result(column = "create_time", property = "createTime"), @Result(column = "update_time", property = "updateTime"), })
這里指定好 id 之后,其他方法也就可以通過(guò)使用@ResultMap
注解使用到了:
@ResultMap("BaseMap") @Select("select id, username, password, age, gender, phone, delete_flag, create_time, update_time from user_info where id= #{aa}") UserInfo selectId(Integer id);
上面這些方法寫起來(lái)還是有些麻煩的,可以直接通過(guò)配置文件的方式,配置駝峰轉(zhuǎn)化
mybatis: configuration: map-underscore-to-camel-case: true #配置駝峰自動(dòng)轉(zhuǎn)換
這樣直接搞定,無(wú)論上面的別名還是注解,都不用寫
3.2. 單元測(cè)試
除了上面的測(cè)試方法之外,還可以使用 idea 自動(dòng)生成測(cè)試類
首先在需要測(cè)試的 Mapper 接口中右鍵之后點(diǎn) Generate,然后再點(diǎn) Test
然后就可以選擇要測(cè)試的方法,就會(huì)自動(dòng)生成單元測(cè)試類
生成的測(cè)試類需要加上 @SpringBootTest
注解
3.3. 打印日志
在 MyBatis 中可以借助日志來(lái)查看到 SQL 語(yǔ)句的執(zhí)行,執(zhí)行傳遞的參數(shù)和執(zhí)行結(jié)果,可以現(xiàn)在配置文件中進(jìn)行配置:
mybatis: configuration: # 配置打印 MyBatis日志 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
配置好之后多了下面的一些信息
通過(guò)這些信息也可以幫助我們?nèi)グl(fā)現(xiàn)問(wèn)題
3.4. 參數(shù)傳遞
如果說(shuō)想要查詢 id = 4 的用戶,那么就可以這樣寫
@Select("select * from user_info where id = 4") UserInfo selectId();
這時(shí)返回值就可以用一個(gè)對(duì)象來(lái)接收了,上面的方式中參數(shù)就寫的固定了,如果說(shuō)把傳入的 id 作為參數(shù)傳遞,就靈活很多
MyBatis 中是通過(guò)使用#{}
的方式進(jìn)行參數(shù)傳遞的:
@Select("select * from user_info where id = #{id}") UserInfo selectId(Integer id);
并且在一個(gè)參數(shù)的情況下,這兩個(gè)參數(shù)是可以不對(duì)應(yīng)的,但是如果是多個(gè)參數(shù),就需要相互對(duì)應(yīng)了
來(lái)看多個(gè)參數(shù)的情況:
@Select("select * from user_info where age = #{age} and gender = #{gender}") List<UserInfo> selectUserByAgeAndGender(Integer age,Integer gender);
由于是按照參數(shù)名稱匹配的,所以參數(shù)順序換一換也是沒問(wèn)題的,但是名稱一定要匹配,如果名稱匹配不上是會(huì)報(bào)錯(cuò)的
還有另一種寫法,還可以通過(guò) param1, param2 這樣的方式來(lái)匹配,不過(guò)還是推薦使用第一種寫法
還可以使用@Param
對(duì)參數(shù)進(jìn)行重命名:
@Select("select * from user_info where age = #{age} and gender = #{gender}") List<UserInfo> selectUserByAgeAndGender(@Param("gender") Integer gender, @Param("age") Integer age);
但是如果重命名的參數(shù)和 SQL 語(yǔ)句中的參數(shù)不匹配也是會(huì)報(bào)錯(cuò)的
還是推薦重命名之后的參數(shù)和 SQL 的參數(shù)都保持一致,也就是參數(shù)和注解中的名稱也要保持一致:
@Select("select * from user_info where age = #{age} and gender = #{gender}") List<UserInfo> selectUserByAgeAndGender(@Param("gender") Integer gender, @Param("age") Integer age);
3.5. 增加
插入的話,由于表中有多個(gè)字段,可以直接傳入一個(gè)對(duì)象,對(duì)象的屬性就代表這些字段
@Insert("insert into user_info(username,password,age,gender) " + "values (#{username},#{password},#{age},#{gender})") Integer insertUser(UserInfo userInfo);
然后測(cè)試一下
@Test void insertUser() { UserInfo userInfo = new UserInfo(); userInfo.setUsername("xxx"); userInfo.setPassword("111"); userInfo.setAge(19); userInfo.setGender(1); Integer integer = userInfoMapper.insertUser(userInfo); }
在 mysql 的日志中也是成功的更新了
如果說(shuō)要想上面那樣通過(guò)參數(shù)重命名的方式的話,來(lái)看一下結(jié)果:
這里發(fā)現(xiàn)是報(bào)錯(cuò)了,如果使用重命名,SQL 語(yǔ)句中的參數(shù)是需要通過(guò)對(duì)象來(lái)獲取屬性的
insert 語(yǔ)句默認(rèn)返回的是影響的行數(shù),如果想要獲取自增 id 的話需要在方法上再加上一個(gè)@Options
注解
@Test void insertUser() { UserInfo userInfo = new UserInfo(); userInfo.setUsername("xxx"); userInfo.setPassword("111"); userInfo.setAge(19); userInfo.setGender(1); Integer integer = userInfoMapper.insertUser(userInfo); System.out.println("影響行數(shù):" + integer + ",自增id:" + userInfo.getId()); }
方法的返回值還是影響行數(shù),也是成功拿到了自增 id
3.6. 刪除
刪除的返回值也是影響的行數(shù)
@Delete("delete from user_info where id = #{id}") Integer deleteId(Integer id);
3.7. 修改
修改數(shù)據(jù)也是影響的行數(shù)
@Update("update user_info set password = #{password} where id = #{id}") Integer update(Integer id,String password);
以上就是MyBatis項(xiàng)目的創(chuàng)建和增刪查改操作詳解的詳細(xì)內(nèi)容,更多關(guān)于MyBatis項(xiàng)目創(chuàng)建和增刪改查的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
MyBatis Plus實(shí)現(xiàn)一對(duì)多的查詢場(chǎng)景的三種方法
MyBatis Plus提供了多種簡(jiǎn)便的方式來(lái)進(jìn)行一對(duì)多子查詢,本文主要介紹了MyBatis Plus實(shí)現(xiàn)一對(duì)多的查詢場(chǎng)景的三種方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07IDEA中Directory創(chuàng)建多級(jí)目錄的實(shí)現(xiàn)
本文主要介紹了IDEA中Directory創(chuàng)建多級(jí)目錄的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Spring和IDEA不推薦使用@Autowired?注解原因解析
這篇文章主要為大家介紹了Spring和IDEA不推薦使用@Autowired?注解原因解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07IDEA搭建SpringBoot多模塊聚合工程過(guò)程詳解(多模塊聚合工程)
這篇文章主要是介紹一下,如何在IDEA開發(fā)工具下,搭建一個(gè)基于SpringBoot的多模塊聚合工程項(xiàng)目,本篇文章,將項(xiàng)目模塊細(xì)分為幾個(gè)子工程模塊,在文中給大家詳細(xì)介紹過(guò),對(duì)IDEA搭建SpringBoot多模塊聚合工程感興趣的朋友一起看看吧2022-04-04java項(xiàng)目實(shí)現(xiàn)圖片等比縮放
這篇文章主要為大家詳細(xì)介紹了java項(xiàng)目實(shí)現(xiàn)圖片等比縮放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Spring Transaction事務(wù)實(shí)現(xiàn)流程源碼解析
此文就Spring 事務(wù)實(shí)現(xiàn)流程進(jìn)行源碼解析,我們可以借此對(duì)Spring框架更多一層理解,下面以xml形式創(chuàng)建一個(gè)事務(wù)進(jìn)行分析2022-09-09JVM調(diào)優(yōu)OutOfMemoryError異常分析
這篇文章主要為大家介紹了JVM調(diào)優(yōu)OutOfMemoryError異常分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11深入理解happens-before和as-if-serial語(yǔ)義
本文大部分整理自《Java并發(fā)編程的藝術(shù)》,溫故而知新,加深對(duì)基礎(chǔ)的理解程度。下面可以和小編來(lái)一起學(xué)習(xí)下2019-05-05