转载自:html
http://www.cnblogs.com/lcwlovell/archive/2011/12/17/2291110.htmljava
http://www.tuicool.com/articles/q2mui2web
1、单个参数:sql
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 来补充。
2、多参数:session
一、第一种方案:dao层的函数方法mybatis
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层中第二参数,更多参数一致日后加便可。
二、第二种方案:Map传多参数oracle
ex1:app
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);
ui
对应的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());
}
三、多参数传递之注解方式示:
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层的函数方法
PublicUserselectUser(@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
>
4、使用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());
}
5、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')
6、selectList()只能传递一个参数,但实际所需参数既要包含String类型,又要包含List类型时的处理方法: 5、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>