文章标题

一、单个参数:
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值