架构师之mybatis-----传入传出多个参数,都是map或list,批量更新

1.前言.
如题.
2.代码.
(1)mapper.xml.


<select id="getTest" resultType="java.util.HashMap" parameterType="java.util.HashMap" >
select count(1) as c1,userid as c2 from test where insertime <![CDATA[>=]]> #{beginTime,jdbcType=TIMESTAMP} and insertime <![CDATA[<]]> #{endTime,jdbcType=TIMESTAMP} group by userid
</select>



(2)interface

public interface TestMapper{

List<Map<String,Object>> getTest(Map<String,Object> map);
}



(3)
测试类:

@Test
public void test3(){
SimpleDateFormat sf=new SimpleDateFormat("yyyyMMddHH");
Date d1 = null;
try {
d1 = sf.parse("2014061100");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Date d2 = null;
try {
d2 = sf.parse("2014121100");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//new
Map map=new HashMap<String, Object>();
map.put("beginTime", d1);
map.put("endTime", d2);
List list=testMapper.getTest(map);
System.out.println(list.size());
}




2.批量更新.
大部分传list就可以了,传map也可以,但map也要解析成list,可以自行研究map,这里介绍通用的list传值方法:
(1)mapper

public int batchUpdate(List<Test> list);


(2)xml

<update id="batchUpdate" parameterType="java.util.List">
<foreach collection="list" item="list" index="index" open="begin" close=";end;" separator=";">
update Test
<set>
A= A + #{list.a}
</set>
where B = #{list.b}
</foreach >
</update>

(3)测试类

public void testBatchUpdate(){
List<Test > item=new ArrayList<Test>();
for(int i=0;i<10;i++){
Test Test=new Test();
Test.setA(i+10);
Test.setB("kkk");
item.add(Test);
}
int count= TestMapper.batchUpdate(item);
System.out.println("jieguo:"+ count);
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值