国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

MyBatis傳入多個參數

2019-11-11 00:04:58
字體:
來源:轉載
供稿:網友

一、單個參數:

public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList" parameterType="java.lang.String" resultType="xxxx.xx.XXBean">    select t.* from tableName t where t.xxCode= #{xxCode}     </select其中方法名和ID一致,#{}中的參數名與方法中的參數名一致,resultType="xxx.x.XXXBean" , 寫的是類全名

二、多參數:

   第一種方案    DAO層的函數方法

Public User selectUser(String name,String area)

對應的Mapper.xml 

<select id="selectUser" resultMap="BaseResultMap">      select  *  from user_user_t   where user_name = #{0} and user_area=#{1}  </select>其中,#{0}代表接收的是dao層中的第一個參數,#{1}代表dao層中第二參數,更多參數一致往后加即可。

 第二種方案  此方法采用Map傳多參數

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);}個人認為此方法不夠直觀,見到接口方法不能直接的知道要傳的參數是什么。

第三種方案

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>   個人覺得這種方法比較好,能讓開發者看到dao層方法就知道該傳什么樣的參數,比較直觀,個人推薦用此種方案。

三、List封裝in:

public List<XXXBean> getXXXBeanList(List<String> listTag);

<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') foreach元素的屬性主要有 item,index,collection,open,separator,close。  item表示集合中每一個元素進行迭代時的別名.index指 定一個名字,用于表示在迭代過程中,每次迭代到的位置.open表示該語句以什么開始,separator表示在每次進行迭代之間以什么符號作為分隔 符.close表示以什么結束.  四、selectList()只能傳遞一個參數,但實際所需參數既要包含String類型,又要包含List類型時的處理方法:

將參數放入Map,再取出Map中的List遍歷。如下

List<String> list_3 = new ArrayList<String>();Map<String, Object> map2 = new HashMap<String, Object>();list.add("1");list.add("2");map2.put("list", list); //網址idmap2.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>


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 绥滨县| 五华县| 江油市| 丹阳市| 桐梓县| 城固县| 民乐县| 凤台县| 福海县| 建水县| 洞口县| 白玉县| 买车| 盱眙县| 通江县| 张家界市| 泊头市| 和龙市| 建始县| 三都| 麻栗坡县| 大悟县| 红桥区| 西和县| 太湖县| 洪雅县| 深水埗区| 凤山县| 华池县| 高邑县| 集贤县| 磴口县| 饶河县| 霍州市| 乐东| 桂阳县| 右玉县| 大港区| 讷河市| 肥城市| 始兴县|