MyBatis注解式開發(fā)映射語(yǔ)句詳解
前言
MyBatis中也提供了注解式開發(fā)?式,采?注解可以減少Sql映射?件的配置。 當(dāng)然,使?注解式開發(fā)的話,sql語(yǔ)句是寫在java程序中的,這種?式也會(huì)給sql語(yǔ)句的維護(hù)帶來(lái)成本。
官?是這么說(shuō)的:
使?注解編寫復(fù)雜的SQL是這樣的:
@Update("<script> update table_name set grade='三年級(jí)'”+ " <if test=\ "name != null\"> , name = #{name} </if> ”+ " <if test=\ "sex != null\"> , sex = #{sex}</if>”+ " where num = #{num}</script>") void update(Student student);
原則:簡(jiǎn)單sql可以注解,復(fù)雜sql使?xml!使用注解式開發(fā)以后三兄弟之一的SqlMapper.xml文件就不需要了!
1. @Insert注解
二兄弟之一CarMapper接口,用來(lái)編寫方法
使用@Insert的注解方式,在注解上就可以寫上SQL語(yǔ)句,對(duì)于SQL語(yǔ)句當(dāng)中的變量就是pojo類Car對(duì)應(yīng)的變量名
package com.bjpowernode.mybatis.mapper; import com.bjpowernode.mybatis.pojo.Car; import org.apache.ibatis.annotations.Insert; public interface CarMapper { // 使用注解式開發(fā),插入數(shù)據(jù) @Insert("insert into t_car values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})") int insert(Car car); }
二兄弟之二CarMapperTest,用來(lái)測(cè)試
package com.bjpowernode.mybatis.test; import com.bjpowernode.mybatis.mapper.CarMapper; import com.bjpowernode.mybatis.pojo.Car; import com.bjpowernode.mybatis.utils.SqlSessionUtil; import org.apache.ibatis.session.SqlSession; import org.junit.Test; public class CarMapperTest { @Test public void testInsert(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); // 創(chuàng)建Car對(duì)象 Car car = new Car(null, "666", "豐田霸道", 32.0, "2023-1-9", "燃油車"); int count = mapper.insert(car); System.out.println(count); sqlSession.commit(); sqlSession.close(); } }
執(zhí)行結(jié)果:
2. @Delete注解
二兄弟之一CarMapper接口,用來(lái)編寫方法
package com.bjpowernode.mybatis.mapper; import com.bjpowernode.mybatis.pojo.Car; import org.apache.ibatis.annotations.Insert; public interface CarMapper { // 使用注解式開發(fā),刪除數(shù)據(jù) @Delete("delete from t_car where id = #{id}") int deleteById(Long id); }
二兄弟之二CarMapperTest,用來(lái)測(cè)試
package com.bjpowernode.mybatis.test; import com.bjpowernode.mybatis.mapper.CarMapper; import com.bjpowernode.mybatis.pojo.Car; import com.bjpowernode.mybatis.utils.SqlSessionUtil; import org.apache.ibatis.session.SqlSession; import org.junit.Test; public class CarMapperTest { @Test public void testDeleteById(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); int count = mapper.deleteById(40L); System.out.println(count); sqlSession.commit(); sqlSession.close(); } }
執(zhí)行結(jié)果:
3. @Update注解
二兄弟之一CarMapper接口,用來(lái)編寫方法
package com.bjpowernode.mybatis.mapper; import com.bjpowernode.mybatis.pojo.Car; import org.apache.ibatis.annotations.Insert; public interface CarMapper { // 使用注解式開發(fā),更新數(shù)據(jù) @Update("update t_car set car_num=#{carNum},brand=#{brand},guide_price=#{guidePrice},produce_time=#{produceTime},car_type=#{carType} where id = #{id}") int update(Car car); }
二兄弟之二CarMapperTest,用來(lái)測(cè)試
package com.bjpowernode.mybatis.test; import com.bjpowernode.mybatis.mapper.CarMapper; import com.bjpowernode.mybatis.pojo.Car; import com.bjpowernode.mybatis.utils.SqlSessionUtil; import org.apache.ibatis.session.SqlSession; import org.junit.Test; public class CarMapperTest { @Test public void testUpdate(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); // 創(chuàng)建Car對(duì)象,根據(jù)id進(jìn)行更新 Car car = new Car(34L, "666", "豐田霸道", 32.0, "2023-1-9", "燃油車"); int count = mapper.update(car); System.out.println(count); sqlSession.commit(); sqlSession.close(); } }
執(zhí)行結(jié)果:
4. @Select注解
二兄弟之一CarMapper接口,用來(lái)編寫方法
package com.bjpowernode.mybatis.mapper; import com.bjpowernode.mybatis.pojo.Car; import org.apache.ibatis.annotations.Insert; public interface CarMapper { // 使用注解式開發(fā),查詢數(shù)據(jù) @Select("select * from t_car where id = #{id}") Car selectById(Long id); }
二兄弟之二CarMapperTest,用來(lái)測(cè)試
package com.bjpowernode.mybatis.test; import com.bjpowernode.mybatis.mapper.CarMapper; import com.bjpowernode.mybatis.pojo.Car; import com.bjpowernode.mybatis.utils.SqlSessionUtil; import org.apache.ibatis.session.SqlSession; import org.junit.Test; public class CarMapperTest { @Test public void testSelectById(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); Car car = mapper.selectById(41L); System.out.println(car); sqlSession.close(); } }
執(zhí)行結(jié)果:
5. @Results注解
我們知道數(shù)據(jù)庫(kù)表中的字段和pojo類的屬性名有的是不一樣的,我們之所以能夠完整的查出數(shù)據(jù),是因?yàn)樵诤诵呐渲梦募ybatis-config.xml當(dāng)中配置了:?jiǎn)⒂民劮迕?動(dòng)映射
<!--啟?駝峰命名?動(dòng)映射--> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>
如果我們不啟用,不對(duì)應(yīng)的字段就是null,查詢的數(shù)據(jù)如下:
那還有什么辦法呢?還可以使用@Results注解!
注:從這里也能看出,使用注解的方式開發(fā),對(duì)于簡(jiǎn)單點(diǎn)的SQL還行,對(duì)于稍微復(fù)雜的查詢語(yǔ)句就太麻煩了!
package com.bjpowernode.mybatis.mapper; import com.bjpowernode.mybatis.pojo.Car; import org.apache.ibatis.annotations.*; public interface CarMapper { // 使用注解式開發(fā),查詢數(shù)據(jù) @Select("select * from t_car where id = #{id}") @Results({ @Result(property = "id",column = "id"), @Result(property = "carNum",column = "car_num"), @Result(property = "brand",column = "brand"), @Result(property = "guidePrice",column = "guide_price"), @Result(property = "produceTime",column = "produce_time"), @Result(property = "carType",column = "car_type"), }) Car selectById(Long id); }
這樣計(jì)算我們不啟用駝峰命名?動(dòng)映射,也能正常查詢數(shù)據(jù)
結(jié)語(yǔ):直到今天MyBatis的學(xué)習(xí)就完美撒花了,接下來(lái)就開始Spring的學(xué)習(xí),敬請(qǐng)期待!
到此這篇關(guān)于MyBatis注解式開發(fā)映射語(yǔ)句詳解的文章就介紹到這了,更多相關(guān)MyBatis注解式開發(fā)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springmvc實(shí)現(xiàn)跨服務(wù)器文件上傳功能
這篇文章主要為大家詳細(xì)介紹了springmvc實(shí)現(xiàn)跨服務(wù)器文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08詳解JVM類加載機(jī)制及類緩存問(wèn)題的處理方法
這篇文章主要給大家介紹了關(guān)于JVM類加載機(jī)制及類緩存問(wèn)題的處理方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01@RequestAttribute和@RequestParam注解的區(qū)別及說(shuō)明
這篇文章主要介紹了@RequestAttribute和@RequestParam注解的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05Java實(shí)現(xiàn)將文件或者文件夾壓縮成zip的詳細(xì)代碼
這篇文章主要介紹了Java實(shí)現(xiàn)將文件或者文件夾壓縮成zip的詳細(xì)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11JAVA中@ApiModel和@ApiModelProperty注解實(shí)戰(zhàn)代碼
這篇文章主要給大家介紹了關(guān)于JAVA中@ApiModel和@ApiModelProperty注解的相關(guān)資料,@ApiModel注解是用在接口相關(guān)的實(shí)體類上的注解,它主要是用來(lái)對(duì)使用該注解的接口相關(guān)的實(shí)體類添加額外的描述信息,常常和@ApiModelProperty注解配合使用,需要的朋友可以參考下2024-03-03SpringBoot自定義Redis實(shí)現(xiàn)緩存序列化詳解
Spring提供了一個(gè)RedisTemplate來(lái)進(jìn)行對(duì)Redis的操作,但是RedisTemplate默認(rèn)配置的是使用Java本機(jī)序列化。如果要對(duì)對(duì)象操作,就不是那么的方便。所以本文為大家介紹了另一種SpringBoot結(jié)合Redis實(shí)現(xiàn)序列化的方法,需要的可以參考一下2022-07-07java學(xué)生管理系統(tǒng)界面簡(jiǎn)單實(shí)現(xiàn)(全)
這篇文章主要為大家詳細(xì)介紹了java學(xué)生管理系統(tǒng)界面的簡(jiǎn)單實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01