mybatis传递多个参数 传参有几种方式

阅读(3228)
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
(2)interface
public interface TestMapper{
List&Map&String,Object&& getTest(Map&String,Object& map);
(3)
测试类:
public void test3(){
SimpleDateFormat sf=new SimpleDateFormat("yyyyMMddHH");
d1 = sf.parse("");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
d2 = sf.parse("");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
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.批量更新.
参考http://my.oschina.net/jsonavaj/blog/265112,如下:
oracle和mysql数据库的批量update在mybatis中配置不太一样:
oracle数据库:
&update id="batchUpdate"
parameterType="java.util.List"&
&foreach collection="list" item="item" index="index" open="begin" close="" separator=";"&
update test
test=${item.test}+1
where id = ${item.id}
&/foreach&
mysql数据库:
mysql数据库采用一下写法即可执行,但是数据库连接必须配置:&allowMultiQueries=true
例如:jdbc:mysql://192.168.1.236:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
&update id="batchUpdate"
parameterType="java.util.List"&
&foreach collection="list" item="item" index="index" open="" close="" separator=";"&
update test
test=${item.test}+1
where id = ${item.id}
&/foreach&
阅读排行榜架构师之mybatis-----传入传出多个参数,都是map或list,批量更新 - 对酒当歌,人生几何 - ITeye技术网站
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
(2)interface
public interface TestMapper{
List&Map&String,Object&& getTest(Map&String,Object& map);
(3)
测试类:
public void test3(){
SimpleDateFormat sf=new SimpleDateFormat("yyyyMMddHH");
d1 = sf.parse("");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
d2 = sf.parse("");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
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
int batchUpdate(List&Test& list);
&update id="batchUpdate" parameterType="java.util.List"&
&foreach collection="list" item="list" index="index" open="begin" close=";" separator=";"&
update Test
A= A + #{list.a}
where B = #{list.b}
&/foreach &
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);
浏览: 839594 次
来自: 深圳
java-lxm 写道好湿好湿好湿谢谢: )。
好湿好湿好湿mybatis循环遍历集合以及使用多个参数对象的sql配置 -
- ITeye技术网站
博客分类:
1、mybatis循环删除数据的sql
循环删除时,要求相应的Mapper方法传递的参数是一个集合(如List),在&delete&标签的参数中设置parameterType="java.util.List",书写sql语句时,使用&foreach&标签循环,其中collection表示Mapper中传递的参数名,item便是循环时压入栈顶的元素,open表示第一次加入的sql前缀,separator表示集合中每个元素之间的分隔符,colse表示循环结束时为sql语句添加的后缀。具体配置如下:
&delete id="deleteList" parameterType="java.util.List"&
DELETE FROM CITY WHERE 1 = 1
&foreach collection="list" item="item" open="AND CITY_NO IN(" separator="," close=")"&
#{item.cityNo}
&/foreach&
2、mybatis中Mapper方法传递多个参数对象
如果在Mapper中的方法中传递了多个参数对象,可以在service层进行封装(系统的流程为controller-&service-&mapper),将多个参数封装到map中,用key值进行区别,然后在mapper.xml中的配置如下:
&select id="findByPage" resultMap="listCityMap" parameterType="java.util.Map"&
SELECT CITY_NO, CITY_NAME, PROVINCE_NO FROM CITY WHERE 1 = 1
&if test="city.cityNo != null and city.cityNo != '' "&
AND CITY_NO = #{city.cityNo}
&if test="city.cityName != null and city.cityName != ''"&
AND CITY_NAME = #{city.cityName}
&if test="city.provinceNo != null and city.provinceNo != ''"&
AND PROVINCE_NO = #{city.provinceNo}
LIMIT #{page.startRowNum}, #{page.pageSize}
其中在Mapper.xml的&select&标签的参数中设置parameterType="java.util.Map",在service方法中,有page和city2个形参,故在service的方法中进行如下处理:
public List&City& findByPage(SimplePage page, City city) {
Map&String, Object& paramMap = new HashMap&String, Object&();
paramMap.put("page", page);
paramMap.put("city", city);
return cityMapper.findByPage(paramMap);
浏览 17009
yangyongByJava
浏览: 72515 次
来自: 上海
* @param strXml
遍历对象属性的时候为嘛不直接用反射查找对象属性,感觉你这样反射 ...
yangyongByJava 写道cfying 写道请问,可是 ...
好!http://git.oschina.net/shengz ...
cfying 写道请问,可是为什么会执行两边啊?QQ:3429 ...下次自动登录
现在的位置:
& 综合 & 正文
Mybatis 中在传参时,$ 和# 的区别
MyBatis中使用parameterType向SQL语句传参,parameterType后的类型可以是基本类型int,String,HashMap和java自定义类型。
在SQL中引用这些参数的时候,可以使用两种方式#{parameterName}或者${parameterName},
首先,我们说一下这两种引用参数时的区别,使用#{parameterName}引用参数的时候,Mybatis会把这个参数认为是一个字符串,例如传入参数是“Smith”,那么在SQL(Select * from emp where name = #{employeeName})使用的时候就会转换为Select * from emp where name = 'Smith';同时在SQL(Select
* from emp where name = ${employeeName})使用的时候就会转换为Select * from emp where name = Smith。
再次,从安全性上考虑,能使用#尽量使用#来传参,因为这样可以有效防止SQL注入的问题。
最后总结一下必须使用$引用参数的情况,我能想到的目前只有一种,那就是参数的int型的时候,必须使用$引用。
&&&&推荐文章:
【上篇】【下篇】}

我要回帖

更多关于 mybatis传入map参数 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信