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

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

MyBatis傳入多個參數

2019-11-10 23:55:07
字體:
來源:轉載
供稿:網友

一、單個參數:

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>


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 北海市| 长汀县| 梁河县| 正蓝旗| 岳池县| 江口县| 西林县| 达拉特旗| 犍为县| 鄂托克前旗| 玉龙| 岢岚县| 鄂伦春自治旗| 虞城县| 云霄县| 新民市| 玉溪市| 洪江市| 张家港市| 泰顺县| 黔西县| 南陵县| 兰考县| 新野县| 黄陵县| 大石桥市| 十堰市| 改则县| 馆陶县| 杭州市| 托里县| 宁南县| 桐柏县| 靖宇县| 永春县| 通城县| 舒兰市| 克什克腾旗| 乐至县| 鹤庆县| 兖州市|