转载自:
http://www.cnblogs.com/lcwlovell/archive/2011/12/17/2291110.html
http://www.tuicool.com/articles/q2mui2
一、单个参数:
public List<XXBean> getXXBeanList(String xxCode);
<select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean">
select t.* from tableName t where t.id= #{id}
</select>
其中方法名和ID一致,#{}中的参数名与方法中的参数名一直, 我这里采用的是XXXBean是采用的短名字,
select 后的字段列表要和bean中的属性名一致, 如果不一致的可以用 as 来补充。
二、多参数:
1、第一种方案:dao层的函数方法
public List<XXXBean> getXXXBeanList(String xxId, String xxCode);
<select id="getXXXBeanList" resultType="XXBean">
select t.* from tableName where id = #{0} and name = #{1}
</select>
由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始
其中,#{0}代表接收的是dao层中的第一个参数,#{1}代表dao层中第二参数,更多参数一致往后加即可。
2、第二种方案:Map传多参数
ex1:
public List<XXXBean> getXXXBeanList(HashMap map);
<select id="getXXXBeanList" parameterType="hashmap" resultType="XXBean">
select 字段... from XXX where id=#{xxId} code = #{xxCode}
</select>
其中hashmap是mybatis自己配置好的直接使用就行。map中key的名字是那个就在#{}使用那个,map如何封装就不用了我说了吧。
ex2:
Dao层的函数方法
Public
User
selectUser(Map paramMap);
对应的Mapper.xml
<
select
id=
" selectUser"
resultMap=
"BaseResultMap"
>
select
*
from
user_user_t
where
user_name = #{userName,jdbcType=
VARCHAR
}
and
user_area=#{userArea,jdbcType=
VARCHAR
}
</
select
>
Service层调用
Private
User
xxxSelectUser(){
Map paramMap=new hashMap();
paramMap.put(“userName”,”对应具体的参数值”);
paramMap.put(“userArea”,”对应具体的参数值”);
User
user
=xxx. selectUser(paramMap);}
个人认为此方法不够直观,见到接口方法不能直接的知道要传的参数是什么。
ex3:
<!--
使用HashMap传递多个参数
parameterType 可以是别名或完全限定名 ,map->java.util.Map,这两个都是可以的
-->
<selectid=
"selectBlogByMap"
parameterType=
"map"
resultType=
"Blog"
>
SELECT t.ID, t.title, t.content
FROM blog t
WHERE t.title = #{h_title}
AND t.content =#{h_content}
</select>
/**
* 通过Map传递多个参数
*/
@Test
public
void
testSelectByMap() {
SqlSession session = sqlSessionFactory.openSession();
Map<String, Object> param=
new
HashMap<String, Object>();
param.put(
"h_title"
,
"oracle"
);
param.put(
"h_content"
,
"使用序列!"
);
Blog blog = (Blog)session.selectOne(
"cn.enjoylife.BlogMapper.selectBlogByMap"
,param);
session.close();
System.out.println(
"blog title:"
+blog.getTitle());
}
3、多参数传递之注解方式示:
ex1:
例子:
public AddrInfo getAddrInfo(@Param("corpId")int corpId, @Param("addrId")int addrId);
xml配置这样写:
<select id="getAddrInfo" resultMap="com.xxx.xxx.AddrInfo">
SELECT * FROM addr__info
where addr_id=#{addrId} and corp_id=#{corpId}
</select>
以前在<select>语句中要带parameterType的,现在可以不要这样写。
ex2:
Dao层的函数方法
Public
User
selectUser(@param(“userName”)Stringname,@param(“userArea”)String area);
对应的Mapper.xml
<
select
id=
" selectUser"
resultMap=
"BaseResultMap"
>
select
*
from
user_user_t
where
user_name = #{userName,jdbcType=
VARCHAR
}
and
user_area=#{userArea,jdbcType=
VARCHAR
}
</
select
>
四、使用JavaBean
<!-- 使用JavaBean传递多个参数 -->
<selectid=
"selectBlogByBean"
parameterType=
"Blog"
resultType=
"Blog"
>
SELECT t.ID, t.title, t.content
FROM blog t
WHERE t.title = #{title}
AND t.content =#{content}
</select>
/**
* 通过JavaBean传递多个参数
*/
@Test
public
void
testSelectByBean() {
SqlSession session = sqlSessionFactory.openSession();
Blog blog=
new
Blog();
blog.setTitle(
"oracle"
);
blog.setContent(
"使用序列!"
);
Blog newBlog = (Blog)session.selectOne(
"cn.enjoylife.BlogMapper.selectBlogByBean"
,blog);
session.close();
System.out.println(
"new Blog ID:"
+newBlog.getId());
}
五、List封装in:
public List<XXXBean> getXXXBeanList(List<String> list);
<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')
六、selectList()只能传递一个参数,但实际所需参数既要包含String类型,又要包含List类型时的处理方法: 五、List封装in:
将参数放入Map,再取出Map中的List遍历。如下:
List<String> list_3 = new ArrayList<String>();
Map<String, Object> map2 = new HashMap<String, Object>();
list.add("1");
list.add("2");
map.put("list", list); //网址id
map.put("siteTag", "0");//网址类型
public List<SysWeb> getSysInfo(Map<String, Object> map2) {
return getSqlSession().selectList("sysweb.getSysInfo", map2);
}
<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>