MyBatis注解式開(kāi)發(fā)映射語(yǔ)句詳解
前言
MyBatis中也提供了注解式開(kāi)發(fā)?式,采?注解可以減少Sql映射?件的配置。 當(dāng)然,使?注解式開(kāi)發(fā)的話,sql語(yǔ)句是寫(xiě)在java程序中的,這種?式也會(huì)給sql語(yǔ)句的維護(hù)帶來(lái)成本。
官?是這么說(shuō)的:

使?注解編寫(xiě)復(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!使用注解式開(kāi)發(fā)以后三兄弟之一的SqlMapper.xml文件就不需要了!

1. @Insert注解
二兄弟之一CarMapper接口,用來(lái)編寫(xiě)方法
使用@Insert的注解方式,在注解上就可以寫(xiě)上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 {
// 使用注解式開(kāi)發(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)編寫(xiě)方法
package com.bjpowernode.mybatis.mapper;
import com.bjpowernode.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Insert;
public interface CarMapper {
// 使用注解式開(kāi)發(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)編寫(xiě)方法
package com.bjpowernode.mybatis.mapper;
import com.bjpowernode.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Insert;
public interface CarMapper {
// 使用注解式開(kāi)發(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)編寫(xiě)方法
package com.bjpowernode.mybatis.mapper;
import com.bjpowernode.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Insert;
public interface CarMapper {
// 使用注解式開(kāi)發(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注解!
注:從這里也能看出,使用注解的方式開(kāi)發(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 {
// 使用注解式開(kāi)發(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)就開(kāi)始Spring的學(xué)習(xí),敬請(qǐng)期待!
到此這篇關(guān)于MyBatis注解式開(kāi)發(fā)映射語(yǔ)句詳解的文章就介紹到這了,更多相關(guān)MyBatis注解式開(kāi)發(fā)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis-Plus中AutoGenerator的使用案例
AutoGenerator是MyBatis-Plus的代碼生成器,通過(guò)?AutoGenerator?可以快速生成?Pojo、Mapper、?Mapper?XML、Service、Controller?等各個(gè)模塊的代碼,這篇文章主要介紹了MyBatis-Plus中AutoGenerator的詳細(xì)使用案例,需要的朋友可以參考下2023-05-05
Spring學(xué)習(xí)筆記之bean的基礎(chǔ)知識(shí)
ean在Spring和SpringMVC中無(wú)所不在,將這個(gè)概念內(nèi)化很重要,所以下面這篇文章主要給大家介紹了關(guān)于Spring學(xué)習(xí)筆記之bean基礎(chǔ)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳解,需要的朋友可以參考下。2017-12-12
Java 爬蟲(chóng)數(shù)據(jù)異步加載如何解決
這篇文章主要介紹了Java 爬蟲(chóng)遇上數(shù)據(jù)異步加載,試試這兩種辦法!問(wèn)題如何解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
詳解Spring簡(jiǎn)單容器中的Bean基本加載過(guò)程
本篇將對(duì)定義在 XMl 文件中的 bean,從靜態(tài)的的定義到變成可以使用的對(duì)象的過(guò)程,即 bean 的加載和獲取的過(guò)程進(jìn)行一個(gè)整體的了解2017-05-05
詳解SpringSecurity中的Authentication信息與登錄流程
這篇文章主要介紹了SpringSecurity中的Authentication信息與登錄流程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Java并發(fā)系列之AbstractQueuedSynchronizer源碼分析(條件隊(duì)列)
這篇文章主要為大家詳細(xì)介紹了Java并發(fā)系列之AbstractQueuedSynchronizer源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
SpringBoot注冊(cè)Filter的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了SpringBoot注冊(cè)Filter的兩種實(shí)現(xiàn)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

