Mybatis傳入多個參數

轉載自: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>
相關文章
相關標籤/搜索