溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MyBatis注解式開發(fā)映射語句怎么使用

發(fā)布時(shí)間:2023-02-24 14:26:39 來源:億速云 閱讀:127 作者:iii 欄目:開發(fā)技術(shù)

今天小編給大家分享一下MyBatis注解式開發(fā)映射語句怎么使用的相關(guān)知識點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

前言

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

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

MyBatis注解式開發(fā)映射語句怎么使用

1. @Insert注解

二兄弟之一CarMapper接口,用來編寫方法

使用@Insert的注解方式,在注解上就可以寫上SQL語句,對于SQL語句當(dāng)中的變量就是pojo類Car對應(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,用來測試

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é)果:

MyBatis注解式開發(fā)映射語句怎么使用

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é)果:

MyBatis注解式開發(fā)映射語句怎么使用

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進(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é)果:

MyBatis注解式開發(fā)映射語句怎么使用

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é)果:

MyBatis注解式開發(fā)映射語句怎么使用

5. @Results注解

我們知道數(shù)據(jù)庫表中的字段和pojo類的屬性名有的是不一樣的,我們之所以能夠完整的查出數(shù)據(jù),是因?yàn)樵诤诵呐渲梦募ybatis-config.xml當(dāng)中配置了:啟用駝峰命名?動映射

    <!--啟?駝峰命名?動映射-->
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

如果我們不啟用,不對應(yīng)的字段就是null,查詢的數(shù)據(jù)如下:

MyBatis注解式開發(fā)映射語句怎么使用

那還有什么辦法呢?還可以使用@Results注解!

注:從這里也能看出,使用注解的方式開發(fā),對于簡單點(diǎn)的SQL還行,對于稍微復(fù)雜的查詢語句就太麻煩了!

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ì)算我們不啟用駝峰命名?動映射,也能正常查詢數(shù)據(jù)

MyBatis注解式開發(fā)映射語句怎么使用

以上就是“MyBatis注解式開發(fā)映射語句怎么使用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI