MyBatis注解式開發(fā)映射語句詳解
前言
MyBatis中也提供了注解式開發(fā)?式,采?注解可以減少Sql映射?件的配置。 當然,使?注解式開發(fā)的話,sql語句是寫在java程序中的,這種?式也會給sql語句的維護帶來成本。
官?是這么說的:

使?注解編寫復雜的SQL是這樣的:
@Update("<script> update table_name set grade='三年級'”+
" <if test=\ "name != null\"> , name = #{name} </if> ”+
" <if test=\ "sex != null\"> , sex = #{sex}</if>”+
" where num = #{num}</script>")
void update(Student student);原則:簡單sql可以注解,復雜sql使?xml!使用注解式開發(fā)以后三兄弟之一的SqlMapper.xml文件就不需要了!

1. @Insert注解
二兄弟之一CarMapper接口,用來編寫方法
使用@Insert的注解方式,在注解上就可以寫上SQL語句,對于SQL語句當中的變量就是pojo類Car對應的變量名
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,用來測試
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對象
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接口,用來編寫方法
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,用來測試
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接口,用來編寫方法
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,用來測試
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對象,根據(jù)id進行更新
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接口,用來編寫方法
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,用來測試
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ù)庫表中的字段和pojo類的屬性名有的是不一樣的,我們之所以能夠完整的查出數(shù)據(jù),是因為在核心配置文件mybatis-config.xml當中配置了:啟用駝峰命名?動映射
<!--啟?駝峰命名?動映射-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>如果我們不啟用,不對應的字段就是null,查詢的數(shù)據(jù)如下:

那還有什么辦法呢?還可以使用@Results注解!
注:從這里也能看出,使用注解的方式開發(fā),對于簡單點的SQL還行,對于稍微復雜的查詢語句就太麻煩了!
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);
}這樣計算我們不啟用駝峰命名?動映射,也能正常查詢數(shù)據(jù)

結(jié)語:直到今天MyBatis的學習就完美撒花了,接下來就開始Spring的學習,敬請期待!
到此這篇關(guān)于MyBatis注解式開發(fā)映射語句詳解的文章就介紹到這了,更多相關(guān)MyBatis注解式開發(fā)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
@RequestAttribute和@RequestParam注解的區(qū)別及說明
這篇文章主要介紹了@RequestAttribute和@RequestParam注解的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
Java實現(xiàn)將文件或者文件夾壓縮成zip的詳細代碼
這篇文章主要介紹了Java實現(xiàn)將文件或者文件夾壓縮成zip的詳細代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-11-11
JAVA中@ApiModel和@ApiModelProperty注解實戰(zhàn)代碼
這篇文章主要給大家介紹了關(guān)于JAVA中@ApiModel和@ApiModelProperty注解的相關(guān)資料,@ApiModel注解是用在接口相關(guān)的實體類上的注解,它主要是用來對使用該注解的接口相關(guān)的實體類添加額外的描述信息,常常和@ApiModelProperty注解配合使用,需要的朋友可以參考下2024-03-03
SpringBoot自定義Redis實現(xiàn)緩存序列化詳解
Spring提供了一個RedisTemplate來進行對Redis的操作,但是RedisTemplate默認配置的是使用Java本機序列化。如果要對對象操作,就不是那么的方便。所以本文為大家介紹了另一種SpringBoot結(jié)合Redis實現(xiàn)序列化的方法,需要的可以參考一下2022-07-07
java學生管理系統(tǒng)界面簡單實現(xiàn)(全)
這篇文章主要為大家詳細介紹了java學生管理系統(tǒng)界面的簡單實現(xiàn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01

