The problem of MyBatis passing in multiple parameters

1. A single parameter:

public List<XXBean>getXXBeanList(String xxCode);   

<select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean">

  select t.* from tableName t where t.id= #{id}  

</select>  

The method name is the same as the ID, the parameter name in #{} is the same as the parameter name in the method, I use the short name of XXXBean here,

The field list after select must be the same as the property name in the bean. If it is inconsistent, it can be supplemented with as .

2. Multiple parameters:

public List<XXXBean> getXXXBeanList(String xxId, String xxCode);  

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

  select t.* from tableName where id = #{0} and name = #{1}  

</select>  

Since it is a multi-parameter, parameterType cannot be used. Instead, use #{index} to use the index of the first number, and the index starts from 0.

3. Map encapsulates multiple parameters:  

public List<XXXBean>getXXXBeanList(HashMap map);   

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

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

</select>  

The hashmap is configured by mybatis and used directly. The name of the key in the map is the one used in #{}. I don't need to say how the map is encapsulated.

 4. List encapsulation in:

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

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

The final effect of foreach is to select fields... from XXX where id in ( '1' , '2' , '3' , '4' )

5. The annotation method of multi-parameter transmission is shown:    

example:
 
public AddrInfo getAddrInfo(@Param("corpId")int corpId, @Param("addrId")int addrId);
 
The xml configuration is written like this:
 
<select id="getAddrInfo"  resultMap="com.xxx.xxx.AddrInfo">
       SELECT * FROM addr__info
    where addr_id=#{addrId} and corp_id=#{corpId}
</select>
 
In the past, the parameterType should be included in the < select > statement, but now it is not necessary to write it like this.

6. selectList() can only pass one parameter, but the actual required parameters must contain both String type and List type processing method:

Put the parameters into the Map, and then take out the List traversal in the Map. as follows:

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

list.add("1");
list.add("2");
map.put("list", list); //网址id

map.put( "siteTag" , "0" ); // URL type
public List<SysWeb> getSysInfo(Map<String, Object> map2) {
  return getSqlSession().selectList("sysweb.getSysInfo", map2);
}
<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 http://43.154.161.224:23101/article/api/json?id=326447141&siteId=291194637