欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis各種查詢接口使用詳解

 更新時間:2022年11月09日 10:49:34   作者:學(xué)習(xí)使我快樂T  
這篇文章主要介紹了Mybatis各種查詢接口使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧

一、查詢一個實體類對象

①創(chuàng)建SelectMapper接口

若sql語句查詢的結(jié)果為多條時,一定不能以實現(xiàn)類類型作為方法的返回值

否則會拋出異常TooManyResultsException

若sql語句查詢的結(jié)果為1條時,此時可以使用實體類類型或list集合類型作為方法的返回值

    /**
     * 根據(jù)id查詢用戶信息
     * @param id
     * @return
     */
    User getUserById(@Param("id") Integer id);

②創(chuàng)建SelectMapper配置文件

<!--    User getUserById(@Param("id") Integer id);-->
    <select id="getUserById" resultType="User">
        select * from t_user where id = #{id}
    </select>

③創(chuàng)建測試類

    @Test
    public void testGetUserById(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        User user = mapper.getUserById(2);
        System.out.println(user);
        sqlSession.close();
    }

二、查詢一個list集合

①創(chuàng)建SelectMapper接口

    /**
     * 查詢所有的用戶信息
     * @return
     */
    List<User> getAllUser();

②創(chuàng)建SelectMapper配置文件

<!--    List<User> getAllUser();-->
    <select id="getAllUser" resultType="User">
        select * from t_user
    </select>

③創(chuàng)建測試類

    @Test
    public void testGetAllUser(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        List<User> allUser = mapper.getAllUser();
        allUser.forEach(System.out::println);
        sqlSession.close();
    }

三、查詢單個數(shù)據(jù)

①創(chuàng)建SelectMapper接口

    /**
     * 查詢用戶的總記錄數(shù)
     * @return
     */
    Integer getCount();

②創(chuàng)建SelectMapper配置文件

在MyBatis中,對于Java中常用的類型都設(shè)置了類型別名

例如: java.lang.Integer -> int/integer

例如: int -> _int/_integer

例如: Map -> map,

例如: List -> list

<!--    Integer getCount();-->
<!--    <select id="getCount" resultType="java.lang.Integer">-->
    <select id="getCount" resultType="Integer">
        select count(*) from t_user
    </select>

③創(chuàng)建測試類

    @Test
    public void testGetCount(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        Integer count = mapper.getCount();
        System.out.println(count);
        sqlSession.close();
    }

四、查詢一個數(shù)據(jù)為map集合

①創(chuàng)建SelectMapper接口

    /**
     * 根據(jù)用戶id查詢用戶信息為map集合
     * @param id
     * @return
     */
    Map<String, Object> getUserToMap(@Param("id") int id);

②創(chuàng)建SelectMapper配置文件

    <!--Map<String, Object> getUserToMap(@Param("id") int id);-->
    <!--結(jié)果: {password=123456, sex=男 , id=1, age=23, username=admin}-->
    <select id="getUserToMap" resultType="map">
        select * from t_user where id = #{id}
    </select>

③創(chuàng)建測試類

    @Test
    public void testGetUserToMap(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        Map<String, Object> userToMap = mapper.getUserToMap(2);
        System.out.println(userToMap);
        sqlSession.close();
    }

五、查詢多條數(shù)據(jù)為map集合

①創(chuàng)建SelectMapper接口

方式一:

    /**
     * 查詢所有用戶信息為map集合
     * @return
     * 將表中的數(shù)據(jù)以map集合的方式查詢,一條數(shù)據(jù)對應(yīng)一個map;若有多條數(shù)據(jù),就會產(chǎn)生多個map集合,此
    時可以將這些map放在一個list集合中獲取
     */
//    List<Map<String, Object>> getAllUserToMap();

方式二:

    /**
     * 查詢所有用戶信息為map集合
     * @return
     * 將表中的數(shù)據(jù)以map集合的方式查詢,一條數(shù)據(jù)對應(yīng)一個map;若有多條數(shù)據(jù),就會產(chǎn)生多個map集合,并
    且最終要以一個map的方式返回數(shù)據(jù),此時需要通過@MapKey注解設(shè)置map集合的鍵,值是每條數(shù)據(jù)所對應(yīng)的
    map集合
     */
    @MapKey("id")
    Map<String, Object> getAllUserToMap();

②創(chuàng)建SelectMapper配置文件

    <!--Map<String, Object> getAllUserToMap();-->
    <select id="getAllUserToMap" resultType="map">
        select * from t_user
    </select>

③創(chuàng)建測試類

    @Test
    public void testGetAllUserToMap(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
//        List<Map<String, Object>> allUserToMap = mapper.getAllUserToMap();
//        allUserToMap.forEach(System.out::println);
        Map<String, Object> allUserToMap = mapper.getAllUserToMap();
        System.out.println(allUserToMap);
        sqlSession.close();
    }

到此這篇關(guān)于Mybatis各種查詢接口使用詳解的文章就介紹到這了,更多相關(guān)Mybatis查詢接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot+fileUpload獲取文件上傳進(jìn)度

    SpringBoot+fileUpload獲取文件上傳進(jìn)度

    這篇文章主要為大家詳細(xì)介紹了SpringBoot+fileUpload獲取文件上傳進(jìn)度,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Java關(guān)鍵字instanceof用法及實現(xiàn)策略

    Java關(guān)鍵字instanceof用法及實現(xiàn)策略

    instanceof 運(yùn)算符是用來在運(yùn)行時判斷對象是否是指定類及其父類的一個實例。這篇文章主要介紹了Java關(guān)鍵字instanceof用法解析,需要的朋友可以參考下
    2020-08-08
  • spring單元測試下模擬rabbitmq的實現(xiàn)

    spring單元測試下模擬rabbitmq的實現(xiàn)

    這篇文章主要介紹了spring單元測試下模擬rabbitmq的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Java解決程序包不存在的問題解決

    Java解決程序包不存在的問題解決

    在Java編程中,我們可以使用Maven或Gradle等構(gòu)建工具來管理依賴庫,本文主要介紹了Java解決程序包不存在的問題解決,具有一定的參考價值,感興趣的可以了解 一下
    2023-12-12
  • Java處理多API請求的方法詳解

    Java處理多API請求的方法詳解

    Java?中的并發(fā)是指語言并行運(yùn)行多個線程的能力,允許同時執(zhí)行多個任務(wù),
    2023-10-10
  • SpringBoot無法連接redis的解決方案

    SpringBoot無法連接redis的解決方案

    這篇文章主要介紹了SpringBoot無法連接redis的解決方案,文中通過代碼示例給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-08-08
  • springboot啟動不加載bootstrap.yml文件的問題

    springboot啟動不加載bootstrap.yml文件的問題

    這篇文章主要介紹了springboot啟動不加載bootstrap.yml文件的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java Web Listener實現(xiàn)事件監(jiān)聽與處理

    Java Web Listener實現(xiàn)事件監(jiān)聽與處理

    Java Web開發(fā)中的Listener是一種事件機(jī)制,通過監(jiān)聽Web應(yīng)用程序的事件,實現(xiàn)對事件的處理,從而實現(xiàn)更加靈活和高效的應(yīng)用程序開發(fā)。Listener能夠監(jiān)聽的事件包括應(yīng)用程序啟動和關(guān)閉、Session創(chuàng)建和銷毀、請求和響應(yīng)對象的創(chuàng)建和銷毀等
    2023-04-04
  • JAVA線程同步實例教程

    JAVA線程同步實例教程

    這篇文章主要介紹了JAVA線程同步實例教程,在Java程序設(shè)計中有著非常廣泛的應(yīng)用,需要的朋友可以參考下
    2014-08-08
  • 基于傳遞list類型的參數(shù)的問題

    基于傳遞list類型的參數(shù)的問題

    這篇文章主要介紹了基于傳遞list類型的參數(shù)的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11

最新評論