溫馨提示×

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

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

Mybatis如何獲取參數(shù)值和查詢功能

發(fā)布時(shí)間:2023-03-21 10:18:48 來(lái)源:億速云 閱讀:119 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了Mybatis如何獲取參數(shù)值和查詢功能的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Mybatis如何獲取參數(shù)值和查詢功能文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

一、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、查詢一個(gè)實(shí)體類對(duì)象

<!--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>

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

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

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

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

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

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

${} 使用字符串拼接的方式拼接sql,若為字符串類型或日期類型的字段進(jìn)行賦值時(shí),需要手動(dòng)加單 引號(hào);

 #{} 使用占位符賦值的方式拼接sql,此時(shí)為字符串類型或日期類型的字段進(jìn)行賦值時(shí), 可以自動(dòng)添加單引號(hào)

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

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

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

    <!--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、多個(gè)字面量類型的參數(shù)

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

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

因此只需要通過(guò) ${}#{} 訪問(wèn)map集合的鍵就可以獲取相 對(duì)應(yīng)的值,注意 ${} 需要手動(dòng)加單引號(hào)

    <!--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ù)為多個(gè)時(shí),此時(shí)可以手動(dòng)創(chuàng)建map集合,將這些數(shù)據(jù)放在 map中

只需要通過(guò) ${} #{} 訪問(wèn)map集合的鍵就可以獲取相對(duì)應(yīng)的值,注意 ${} 需要手動(dòng)加單引號(hào)

    <!--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í)體類類型的參數(shù)

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

此時(shí)可以使用 ${} #{} ,通過(guò)訪問(wèn)實(shí)體類對(duì)象中的屬性名獲取屬性值,注意 ${} 需要手動(dòng)加單引號(hào)

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

2.5、使用@Param標(biāo)識(shí)參數(shù)

可以通過(guò) @Param 注解標(biāo)識(shí)mapper接口中的方法參數(shù)

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

只需要通過(guò) ${} #{} 訪問(wèn)map集合的鍵就可以獲取相對(duì)應(yīng) 的值, 注意 ${} 需要手動(dòng)加單引號(hào)

    <!--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、查詢一個(gè)實(shí)體類對(duì)象

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、查詢一個(gè)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);
        }
    }

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

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

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

    /**
     * 查詢用戶的總記錄數(shù)
     * @return
     */
    Integer getCount();
     <!--Integer getCount();-->
    <!--
        在MyBatis中,對(duì)于Java中常用的類型都設(shè)置了類型別名
        例如: 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);
        //結(jié)果:{password=123456, gender=男, id=1, age=21, email=123456@qq.com, username=張三}
    }

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

方式一

    /**
     * 查詢所有用戶信息為map集合
     * @return
     * 將表中的數(shù)據(jù)以map集合的方式查詢,一條數(shù)據(jù)對(duì)應(yīng)一個(gè)map;若有多條數(shù)據(jù),就會(huì)產(chǎn)生多個(gè)map集合
       此時(shí)可以將這些map放在一個(gè)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ù)轉(zhuǎn)換的map集合放在一個(gè)大的map中,
     * 但是必須要通過(guò)@Mapkey注解將查詢的某個(gè)字段的值作為大的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=老六} 
        // }
    }

關(guān)于“Mybatis如何獲取參數(shù)值和查詢功能”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Mybatis如何獲取參數(shù)值和查詢功能”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

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

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

AI