Mybatis如何傳入多個(gè)參數(shù)(實(shí)體類型和基本類型)
Mybatis傳入多個(gè)參數(shù)
1.ProjectController類
Integer oldUserId = getUserIdByUserName(oldUserName); ? ? ? ?? ProjectAndUser projectAndUser = new ProjectAndUser(); projectAndUser.setProjectId(project.getId()); projectAndUser.setUserId(newUserId); projectAndUser.setAccessLevel(1); projectAndUserService.update(projectAndUser, oldUserId);
2.ProjectAndUserService接口
public interface ProjectAndUserService extends BaseService<ProjectAndUser> { ? ? public boolean update(ProjectAndUser projectAndUser, Integer oldUserId); }
3.ProjectAndUserServiceImpl實(shí)現(xiàn)類
@Service public class ProjectAndUserServiceImpl extends BaseServiceImpl<ProjectAndUser> implements ProjectAndUserService { ? ? @Autowired ? ? ProjectAndUserMapper projectAndUserMapper; ? ? @Override ? ? protected BaseMapper<ProjectAndUser> getMapper() { ? ? ? ? return projectAndUserMapper; ? ? } ?? ?public boolean update(ProjectAndUser projectAndUser, Integer oldUserId){ ? ? ? ? return projectAndUserMapper.update(projectAndUser, oldUserId); ? ? } }
4.ProjectAndUserMapper接口
通過使用@Param注解,實(shí)現(xiàn)傳入多個(gè)參數(shù)
@Mapper public interface ProjectAndUserMapper extends BaseMapper<ProjectAndUser> { ? ? public boolean update(@Param("projectAndUser") ProjectAndUser projectAndUser, @Param("oldUserId") Integer oldUserId); }
5.ProjectAndUserMapper.xml
因?yàn)閭魅氲膮?shù)包括實(shí)體類對(duì)象,因此使用實(shí)體類對(duì)象的屬性時(shí)需要采用projectAndUser.projectId的形式
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.taobao.rigel.rap.mapper.ProjectAndUserMapper"> ?? ?<resultMap id="ProjectAndUserMap" type="com.taobao.rigel.rap.model.ProjectAndUser"> ?? ??? ?<result column="project_id" property="projectId"/> ?? ??? ?<result column="user_id" property="userId"/> ?? ??? ?<result column="access_level" property="accessLevel"/> ?? ?</resultMap> ?? ?<update id="update"> ?? ??? ?update tb_project_and_user_new set user_id=#{projectAndUser.userId} where project_id=#{projectAndUser.projectId} and user_id=#{oldUserId} and access_level=#{projectAndUser.accessLevel} ?? ?</update> </mapper>
Mybatis傳入多個(gè)參數(shù)時(shí),如何處理
方式一
通過使用索引方式,來指定想傳入的參數(shù),#{index} 索引從0開始。
DAO接口
Mybatis配置
注意:
1.由于是多參數(shù)傳入,所以不需要對(duì)parameterType進(jìn)行配置。
2.由于使用索引方式,所以在DAO接口中不需要使用@Param注解來注明參數(shù)名
方式二
通過MyBatis的注解(@Param("paramName"))方式來注明參數(shù)
DAO接口
MyBatis配置
注意:
1.同樣由于是多參數(shù)傳入,所以不需要對(duì)parameterType進(jìn)行配置。
方式三
通過Map方式傳遞多個(gè)參數(shù),map中的key的名字就是對(duì)應(yīng)xml配置中#{}中使用的那個(gè)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot日志收集及鏈路追蹤實(shí)現(xiàn)示例
這篇文章主要為大家介紹了Spring Boot日志收集及鏈路追蹤實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Java項(xiàng)目Guava包?HashMultimap使用及注意事項(xiàng)
guava基本上可以說是java開發(fā)項(xiàng)目中,大概率會(huì)引入的包,今天介紹的主角是一個(gè)特殊的容器HashMultmap,可以簡(jiǎn)單的將它的數(shù)據(jù)結(jié)構(gòu)理解為Map<K,?Set<V>>,今天主要介紹下基礎(chǔ)的知識(shí)點(diǎn)?HashMultmap級(jí)使用,感興趣的朋友一起看看吧2022-05-05利用Spring Boot如何開發(fā)REST服務(wù)詳解
這篇文章主要給大家介紹了關(guān)于利用Spring Boot如何開發(fā)REST服務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12使用Java實(shí)現(xiàn)讀取手機(jī)文件名稱
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)讀取手機(jī)文件名稱,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03