MyBatis传入多个参数 ,List集合解决方案

一、单个参数

1.1 java层代码

public List<XXBean> getXXBeanList(String xxCode); 

1.2 xml层代码

<select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean">
  select t.* from tableName t where t.id= #{id}  
select> 

二、多个参数

2.1 java层代码

public List<XXXBean> getXXXBeanList(@Param("id") String xxId, @Param("name") String xxCode);  

2.2 xml层代码

<select id="getXXXBeanList" resultType="XXBean">
  select t.* from tableName where id = #{0} and name = #{1}  
select> 

三、Map封装多参数

3.1 java层代码

public List<XXXBean> getXXXBeanList(HashMap map);

3.2 xml层代码

<select id="getXXXBeanList" parameterType="hashmap" resultType="XXBean">
  select * from XXX where id=#{xxId} code = #{xxCode}  
select> 

四、List封装in

4.1 java层代码

public List<XXXBean> getXXXBeanList(List<String> list);

4.2 xml层代码

<select id="getXXXBeanList" resultType="XXBean">
  select * from XXX where id in
  <foreach item="item" index="index" collection="list" open="(" separator="," close=")">  
    #{item}  
  foreach>  
select>  

五、String类型、List类型时的处理方法

4.1 java业务层代码

List<String> list_3 = new ArrayList<String>();
Map<String, Object> map = new HashMap<String, Object>();

list.add("1");
list.add("2");
map.put("list", list); //列表

map.put("tenantId", "tenantId");//租户ID

4.2 java层代码

public List<SysWeb> getSysInfo(Map<String, Object> map) {
  return getSqlSession().selectList("sysweb.getSysInfo", map);
}

4.3 xml层代码

<select id="getSysInfo" parameterType="java.util.Map" resultType="SysWeb">
		SELECT
			t.sysSiteId,
			t.siteName,
			t1.mzNum AS siteTagNum,
			t1.mzName AS siteTag,
			t.url,
			t.iconPath
		FROM
			TD_WEB_SYSSITE t
		LEFT JOIN TD_MZ_MZDY t1 ON t1.mzNum = t.siteTag
		AND t1.mzType = 10
		WHERE
			t.siteTag = #{siteTag } 
		AND t.sysSiteId NOT IN
   <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
       #{item}
   foreach>
 select>

你可能感兴趣的:(mybatis,mybatis,xml,java)