最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis各種查詢接口使用詳解

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

一、查詢一個實體類對象

①創(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中常用的類型都設置了類型別名

例如: 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ù)對應一個map;若有多條數(shù)據(jù),就會產(chǎn)生多個map集合,此
    時可以將這些map放在一個list集合中獲取
     */
//    List<Map<String, Object>> getAllUserToMap();

方式二:

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

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

相關文章

最新評論

多伦县| 黑龙江省| 元氏县| 临城县| 娄底市| 辰溪县| 仁布县| 宣威市| 长岛县| 揭阳市| 调兵山市| 连南| 南充市| 沁水县| 大名县| 长岭县| 达拉特旗| 新和县| 大姚县| 六安市| 邯郸县| 咸阳市| 德江县| 句容市| 封丘县| 鲁山县| 紫金县| 河间市| 双柏县| 浙江省| 伊宁县| 沭阳县| 淮滨县| 大荔县| 固阳县| 甘南县| 仙桃市| 丹寨县| 云龙县| 阆中市| 尉犁县|