MyBatis receives multiple parameters, one of which is a collection and the other is a string solution

Weekly Report 2020-09-14

Last week I wrote the code according to the detailed design. In the process, I encountered a problem about the MyBatis framework. When the abstract method in the interface has two parameters, and one of the parameters is a set and the other is a string, in this case, how to write the parameterType in the xml file to receive the parameters correctly. After searching on the Internet, I asked the seniors and finally solved the problem. That is to encapsulate them in HashMap, accept Map type parameters in parameterType, and then use Map key to retrieve data, and use dynamic SQL foreach tag Traverse the list.

interface

public List<XXXBean> getXXXBeanList(HashMap map);  

<select id="getXXXBeanList" parameterType="hashmap" resultType="XXBean">

  select 字段... from XXX where id=#{xxId} code = #{xxCode}  

</select>  

其中hashmap是mybatis自己配置好的直接使用就行。map中key的名字是那个就放在#{}中。
Concrete example

Encapsulate a list and a string into the map

List<String> list = new ArrayList<>();
//网址
list.add("https://www.baidu.com");
list.add("https://www.bilibili.com");
//网址类型
String siteTag = "https";

HashMap<String,Object> map = new HashMap<>();
//封装map,一会需要使用这里定义的key进行数据接收
map.put("list", list);
map.put("siteTag",siteTag)


Interface method

public List<SysWeb> getSysInfo(Map<String, Object>);

mapper.xml

The following xml code means that the query network type is https, but other websites except for the two websites of Baidu and bilibili.

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

Guess you like

Origin blog.csdn.net/weixin_43941676/article/details/108570499