一、单个参数
1. Java Mapper接口
public List<XXBean> getXXBeanList(@param("id")String id);
2. XML Mapper
<select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean"> select t.* from tableName t where t.id= #{id} </select>
二、多参数
方案1:
1. Java Mapper接口
public List<XXXBean> getXXXBeanList(String xxId, String xxCode);
2. XML Mapper
<select id="getXXXBeanList" resultType="XXBean"> select t.* from tableName where id = #{0} and name = #{1} </select>
注意:#{index},索引从0开始
方案2:基于注解(推荐)
1. Java Mapper接口
public List<XXXBean> getXXXBeanList(@Param("id")String id, @Param("code")String code);
2. XML Mapper
<select id="getXXXBeanList" resultType="XXBean"> select t.* from tableName where id = #{id} and name = #{code} </select>
三、Map封装多参数
1. Java Mapper接口
public List<XXXBean> getXXXBeanList(HashMap map);
2. XML Mapper
<select id="getXXXBeanList" parameterType="java.util.Map" resultType="XXBean"> select 字段... from XXX where id=#{xxId} code = #{xxCode} </select>
map中key可在#{}中直接使用
四、List封装in
1. Java Mapper接口
public List<XXXBean> getXXXBeanList(List<String> list);
2. XML Mapper
<select id="getXXXBeanList" resultType="XXBean"> select 字段... from XXX where id in <foreach collection="list" item="item" index="index" open="(" separator="," close=")"> #{item} </foreach> </select>
foreach 最后的效果是select 字段... from XXX where id in ('1','2','3','4')
五、接口方法只能传递一个参数,但实际所需参数既要包含String类型,又要包含List类型时的处理方法
1. Java代码
List<String> list = new ArrayList<String>(); Map<String, Object> map = new HashMap<String, Object>(); list.add("1"); list.add("2"); map.put("list", list); //网址id map.put("siteTag", "0");//网址类型
2. Java Mapper接口
public List<SysWeb> getSysInfo(Map<String, Object> map2);
3. XML Mapper
<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>