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

Mybatis獲取參數(shù)值和查詢功能的案例詳解

 更新時間:2023年03月21日 08:34:48   作者:烏云不要說話  
這篇文章主要介紹了Mybatis獲取參數(shù)值和查詢功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、MyBatis的增刪改查

1.1、新增

<!--int insertUser();-->
<insert id="insertUser">
    insert into t_user values(null,'admin','123456',23,'男')
</insert>

1.2、刪除

<!--int deleteUser();-->
<delete id="deleteUser">
    delete from t_user where id = 1
</delete>

1.3、修改

<!--int updateUser();-->
<update id="updateUser">
    update t_user set username='ybc',password='123' where id = 6
</update>

1.4、查詢一個實體類對象

<!--User getUserById();-->
<select id="getUserById" resultType="com.ssm.mybatis.bean.User">
    select * from t_user where id = 2
</select>

1.5、查詢list集合

<!--List<User> getUserList();-->
<select id="getUserList" resultType="com.ssm.mybatis.bean.User">
    select * from t_user
</select>

注意: 查詢的標簽select必須設置屬性resultType或resultMap,用于設置實體類和數(shù)據(jù)庫表的映射關系

resultType:自動映射,用于屬性名和表中字段名一致的情況

resultMap:自定義映射,用于一對多或多對一或字段名和屬性名不一致的情況

二、MyBatis獲取參數(shù)值的兩種方式

MyBatis獲取參數(shù)值的兩種方式:${}#{}

${} 的本質就是字符串拼接,#{} 的本質就是占位符賦值

${} 使用字符串拼接的方式拼接sql,若為字符串類型或日期類型的字段進行賦值時,需要手動加單 引號

 #{} 使用占位符賦值的方式拼接sql,此時為字符串類型或日期類型的字段進行賦值時, 可以自動添加單引號

2.1、單個字面量類型的參數(shù)

若 mapper 接口中的方法參數(shù)為單個的字面量類型

此時可以使用 ${}#{} 以任意的名稱獲取參數(shù)的值,注意 ${} 需要手動加單引號

    <!--User getUserByUsername(String username);-->
    <select id="getUserByUsername" resultType="User">
        <!--select * from t_user where username = '${username}'-->
        select * from t_user where username = #{username}
    </select>

2.2、多個字面量類型的參數(shù)

若mapper接口中的方法參數(shù)為多個時

此時MyBatis會自動將這些參數(shù)放在一個map集合中,以arg0,arg1...為鍵,以參數(shù)為值、以 param1,param2...為鍵,以參數(shù)為值;

因此只需要通過 ${}#{} 訪問map集合的鍵就可以獲取相 對應的值,注意 ${} 需要手動加單引號

    <!--User checkLogin(String username, String password);-->
    <select id="checkLogin" resultType="User">
        <!--select * from t_user where username = '${arg0}' and password = '${arg1}'-->
        <!--select * from t_user where username = #{param1} and password = #{param2}-->
        select * from t_user where username = #{arg0} and password = #{arg1}
 
    </select>

2.3、map集合類型的參數(shù)

若mapper接口中的方法需要的參數(shù)為多個時,此時可以手動創(chuàng)建map集合,將這些數(shù)據(jù)放在 map中

只需要通過 ${} #{} 訪問map集合的鍵就可以獲取相對應的值,注意 ${} 需要手動加單引號

    <!--User checkLoginByMap(Map<String ,Object> map);-->
    <select id="checkLoginByMap" resultType="User">
        <!--select * from t_user where username = '${username}' and password = '${password}'-->
        select * from t_user where username = #{username} and password = #{password}
    </select>

2.4、實體類類型的參數(shù)

若mapper接口中的方法參數(shù)為實體類對象時

此時可以使用 ${} #{} ,通過訪問實體類對象中的屬性名獲取屬性值,注意 ${} 需要手動加單引號

    <!--void insertUser(User user);-->
    <insert id="insertUser" >
        insert into t_user values(null,#{username},#{password},#{age},#{gender},#{email})
    </insert>

2.5、使用@Param標識參數(shù)

可以通過 @Param 注解標識mapper接口中的方法參數(shù)

此時,會將這些參數(shù)放在map集合中,以@Param注解的value屬性值為鍵,以參數(shù)為值;以 param1,param2...為鍵,以參數(shù)為值;

只需要通過 ${} #{} 訪問map集合的鍵就可以獲取相對應 的值, 注意 ${} 需要手動加單引號

    <!--User checkLoginByParam(@Param("username") String username, @Param("password") String password);-->
    <select id="checkLoginByParam" resultType="User">
        <!--select * from t_user where username = '${username}' and password = '${password}'-->
        select * from t_user where username = #{username} and password = #{password}
    </select>

三、MyBatis的各種查詢功能

3.1、查詢一個實體類對象

package com.ssm.mybatis.mapper;
 
import com.ssm.mybatis.pojo.User;
import org.apache.ibatis.annotations.Param;
 
import java.util.List;
 
/**
 * Author:wy
 * Date:2023/3/18
 */
 
public interface SelectMapper {
    /**
     * 根據(jù)用戶id查詢用戶信息
     * @param id
     * @return
     */
    User getUserById(@Param("id") int id);
}
<!--User getUserById(@Param("id") int id);-->
<select id="getUserById" resultType="User">
    select * from t_user where id = #{id}
</select>
    @Test
    public void testGetUserById() {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
 
        User user = mapper.getUserById(1);
        System.out.println(user);
    }

3.2、查詢一個list集合

    /**
     * 查詢所有用戶信息
     * @return
     */
    List<User> getUserList();
    <!--List<User> getUserList();-->
    <select id="getUserList" resultType="User">
        select * from t_user
    </select>
    @Test
    public void testGetUserList() {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
 
        List<User> userList = mapper.getUserList();
        for (User user : userList) {
            System.out.println(user);
        }
    }

當查詢的數(shù)據(jù)為多條時,不能使用實體類作為返回值,否則會拋出異常TooManyResultsException;

但是若查詢的數(shù)據(jù)只有一條,可以使用實體類或集合作為返回值

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

    /**
     * 查詢用戶的總記錄數(shù)
     * @return
     */
    Integer getCount();
     <!--Integer getCount();-->
    <!--
        在MyBatis中,對于Java中常用的類型都設置了類型別名
        例如: Integer: int, integer
        例如: int: _int, _integer
        例如: Map: map
              List: list
    -->
    <select id="getCount" resultType="Integer">
        select count(*) from t_user
    </select>
    @Test
    public void testGetCount() {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
 
        Integer count = mapper.getCount();
        System.out.println("用戶總數(shù)=" + count);
    }

3.4、查詢一條數(shù)據(jù)為map集合

    /**
     * 根據(jù)用戶id查詢用戶信息為map集合
     * @param id
     * @return
     */
    Map<String, Object> getUserByIdToMap(@Param("id") Integer id);
    <!--Map<String, Object> getUserByIdToMap(@Param("id") int id);-->
    <select id="getUserByIdToMap" resultType="map">
        select * from t_user where id = #{id}
    </select>
    @Test
    public void testGetUserByIdToMap() {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
 
        Map<String, Object> map = mapper.getUserByIdToMap(1);
        System.out.println(map);
        //結果:{password=123456, gender=男, id=1, age=21, email=123456@qq.com, username=張三}
    }

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

方式一

    /**
     * 查詢所有用戶信息為map集合
     * @return
     * 將表中的數(shù)據(jù)以map集合的方式查詢,一條數(shù)據(jù)對應一個map;若有多條數(shù)據(jù),就會產生多個map集合
       此時可以將這些map放在一個list集合中獲取
     */
    @MapKey("id")
    List<Map<String, Object>> getAllUserToMap();
    <!--Map<String, Object> getAllUserToMap();-->
    <select id="getAllUserToMap" resultType="map">
        select * from t_user
    </select>
    @Test
    public void testGetAllUserToMap() {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
 
        List<Map<String, Object>> allUserToMap = mapper.getAllUserToMap();
 
        for (Map<String, Object> map : allUserToMap) {
            System.out.println(map);
        }
    }

方式二

    /**
     * 查詢所有用戶信息為map集合
     * @return
     * 可以將每條數(shù)據(jù)轉換的map集合放在一個大的map中,
     * 但是必須要通過@Mapkey注解將查詢的某個字段的值作為大的map的鍵
     */
    @MapKey("id")
    Map<String, Object> getAllUserToMap();
    <!--Map<String, Object> getAllUserToMap();-->
    <select id="getAllUserToMap" resultType="map">
        select * from t_user
    </select>
    @Test
    public void testGetAllUserToMap() {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
 
        Map<String, Object> map = mapper.getAllUserToMap();
        System.out.println(map);
        //{ 
        // 1={password=123456, gender=男, id=1, age=21, email=123456@qq.com, username=張三}, 
        // 2={password=123456, gender=女, id=2, age=19, email=123456@qq.com, username=老六} 
        // }
    }

到此這篇關于Mybatis --- 獲取參數(shù)值和查詢功能的文章就介紹到這了,更多相關Mybatis獲取參數(shù)值內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • java使用POI實現(xiàn)html和word相互轉換

    java使用POI實現(xiàn)html和word相互轉換

    這篇文章主要為大家詳細介紹了java使用POI實現(xiàn)html和word的相互轉換,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Mybatis詳解在注解sql時報錯的解決方法

    Mybatis詳解在注解sql時報錯的解決方法

    MyBatis-Plus 是一個 Mybatis 增強版工具,在 MyBatis 上擴充了其他功能沒有改變其基本功能,為了簡化開發(fā)提交效率而存在,本篇文章帶你看看在注解sql時所報出的錯誤解決
    2022-03-03
  • IDEA 2020.1.2 安裝教程附破解教程詳解

    IDEA 2020.1.2 安裝教程附破解教程詳解

    這篇文章主要介紹了IDEA 2020.1.2 安裝教程附帶破解教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • 5個并發(fā)處理技巧代碼示例

    5個并發(fā)處理技巧代碼示例

    這篇文章主要介紹了5個并發(fā)處理技巧代碼示例,具有一定參考價值,需要的朋友可以了解下。
    2017-10-10
  • java集合——Java中的equals和hashCode方法詳解

    java集合——Java中的equals和hashCode方法詳解

    本篇文章詳細介紹了Java中的equals和hashCode方法詳解,Object 類是所有類的父類,非常具有實用價值,需要的朋友可以參考下。
    2016-10-10
  • mybatis/mybatis-plus模糊查詢語句特殊字符轉義攔截器的實現(xiàn)

    mybatis/mybatis-plus模糊查詢語句特殊字符轉義攔截器的實現(xiàn)

    在開發(fā)中,我們通常會遇到這樣的情況。用戶在錄入信息是錄入了‘%’,而在查詢時無法精確匹配‘%’。究其原因,‘%’是MySQL的關鍵字,如果我們想要精確匹配‘%’,那么需要對其進行轉義,本文就詳細的介紹一下
    2021-11-11
  • Mybatis注解sql時出現(xiàn)的一個錯誤及解決

    Mybatis注解sql時出現(xiàn)的一個錯誤及解決

    這篇文章主要介紹了Mybatis注解sql時出現(xiàn)的一個錯誤及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Springboot 整合 Java DL4J 打造文本摘要生成系統(tǒng)

    Springboot 整合 Java DL4J 打造文本摘要生成系統(tǒng)

    本文介紹了如何使用SpringBoot整合JavaDeeplearning4j構建文本摘要生成系統(tǒng),該系統(tǒng)能夠自動從長篇文本中提取關鍵信息,生成簡潔的摘要,幫助用戶快速了解文本的主要內容,技術實現(xiàn)包括使用LSTM神經網絡進行模型構建和訓練,并通過SpringBoot集成RESTfulAPI接口
    2024-11-11
  • Spring?中使用?Validation?注解校驗參數(shù)的方法

    Spring?中使用?Validation?注解校驗參數(shù)的方法

    本文介紹了如何在Spring中使用Validation注解進行參數(shù)校驗,包括引入依賴、簡單示例、常見校驗注解分類與說明、分組校驗和自定義校驗,通過這些方法,可以方便地對Controller、Service等層面的參數(shù)進行校驗,確保數(shù)據(jù)的合法性和一致性,感興趣的朋友跟隨小編一起看看吧
    2024-11-11
  • 解決import包時報 Java 程序包不存在的問題

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

    你是否也有過在import包時idea報錯說這個包不存在,可是這個包我們看得到確實存在的情況,不要慌,今天這篇文章帶你徹底告別這個問題
    2021-10-10

最新評論

青浦区| 黎平县| 云浮市| 云南省| 陕西省| 桐庐县| 博湖县| 浙江省| 易门县| 大同市| 许昌市| 蒲江县| 南开区| 陵川县| 黄骅市| 绥棱县| 浪卡子县| 米泉市| 昭通市| 秦安县| 昆山市| 胶州市| 崇阳县| 雷州市| 新沂市| 宜兰市| 柘荣县| 通州市| 东辽县| 盘山县| 板桥市| 沈丘县| 勃利县| 太湖县| 拉孜县| 北流市| 满城县| 江陵县| 古蔺县| 延安市| 泾川县|