一、单个参数:
XxDao.java
public List<XXBean> getXXBeanList(@param("id")String id);
XxMapper.xml
<select id="getXXBeanList" parameterType="java.lang.String" resultType="XXBean">
select t.* from tableName t where t.id= #{id}
</select>
二、多参数的情况:
1、多个入参
XxDao.java
public List<XXXBean> getXXXBeanList(String xxId, String xxCode);
XxMapper.xml
<select id="getXXXBeanList" resultType="XXBean">
select t.* from tableName where id = #{0} and name = #{1}
</select>
2、基于注解起名(推荐)
XxDao.java
public List<XXXBean> getXXXBeanList(@Param("id")String id, @Param("code")String code);
XxMapper.xml
<select id="getXXXBeanList" resultType="XXBean">
select t.* from tableName where id = #{id} and name = #{code}
</select>
3、 map入参
XxDao.java
public List<XXXBean> getXXXBeanList(HashMap map);
XxMapper.xml
<select id="getXXXBeanList" parameterType="hashmap" resultType="XXBean">
select 字段 from XXX where id=#{xxId} code = #{xxCode}
</select>
4、List封装in:
XxDao.java
public List<XXXBean> getXXXBeanList(List<String> list);
XxMapper.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>
foreach 最后的效果是select 字段… from XXX where id in (‘1’,’2’,’3’,’4’)
5、selectList()只能传递一个参数,但实际所需参数既要包含String类型,又要包含List类型时的处理方法。
将参数放入Map,再取出Map中的List遍历。
map的数据如下:
List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
Map<String, Object> map = new HashMap<String, Object>();
map.put("list", list); //网址id
map.put("siteTag", "0");//网址类型
XxDao.java
public List<SysWeb> getSysInfo(Map<String, Object> map);
XxMapper.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>
6、实体对象做入参:
实体类
User.java
private String userId;
private String userName;
getter and setter
XxDao.java
public List<User> getAll(User user);
XxMapper.xml
a).入参中必须有userId和userName的写法
<select id="getAll" parameterType="com.xx.yy.User" resultType="com.xx.yy.User">
select t.* from user t
where t.user_id= #{userId,jdbcType=VARCHAR} and t.user_name=#{userName,jdbcType=VARCHAR}
</select>
b).入参判断有userId和userName的写法
<select id="getAll" parameterType="com.xx.yy.User" resultType="com.xx.yy.User">
select t.* from user t
<where>
<if test="userId != null and userId != ''">
and t.user_id= #{userId,jdbcType=VARCHAR}
</if>
<if test="userId != null and userId != ''">
and t.user_name=#{userName,jdbcType=VARCHAR}
</if>
</where>
</select>
7、一个数组入参
Array:forech中的collection属性类型是array,
collection的值必须是:list,item的值可以随意
XxDao.java
List<Employees> getEmployeesArrayParams(String[] employeeIds);
XxMapper.xml
<select id="getEmployeesArrayParams" resultType="Employees">
select *
from EMPLOYEES e
where e.EMPLOYEE_ID in
<foreach collection="array" item="employeeId" index="index"
open="(" close=")" separator=",">
#{employeeId}
</foreach>
</select>
参考:
http://www.cnblogs.com/ningheshutong/p/5828854.html
http://blog.csdn.net/aya19880214/article/details/41961235