一、單個(gè)參數(shù):
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一致,#{}中的參數(shù)名與方法中的參數(shù)名一致,resultType="xxx.x.XXXBean" , 寫的是類全名二、多參數(shù):
第一種方案 DAO層的函數(shù)方法
Public User selectUser(String name,String area)對(duì)應(yīng)的Mapper.xml
<select id="selectUser" resultMap="BaseResultMap"> select * from user_user_t where user_name = #{0} and user_area=#{1} </select>其中,#{0}代表接收的是dao層中的第一個(gè)參數(shù),#{1}代表dao層中第二參數(shù),更多參數(shù)一致往后加即可。第二種方案 此方法采用Map傳多參數(shù)
Dao層的函數(shù)方法
Public User selectUser(Map paramMap);
對(duì)應(yīng)的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層調(diào)用PRivate User xxxSelectUser(){ Map paramMap=new hashMap(); paramMap.put(“userName”,”對(duì)應(yīng)具體的參數(shù)值”); paramMap.put(“userArea”,”對(duì)應(yīng)具體的參數(shù)值”); User user=xxx. selectUser(paramMap);}個(gè)人認(rèn)為此方法不夠直觀,見到接口方法不能直接的知道要傳的參數(shù)是什么。第三種方案
Dao層的函數(shù)方法
Public User selectUser(@param(“userName”)Stringname,@param(“userArea”)String area);
對(duì)應(yīng)的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> 個(gè)人覺得這種方法比較好,能讓開發(fā)者看到dao層方法就知道該傳什么樣的參數(shù),比較直觀,個(gè)人推薦用此種方案。三、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表示集合中每一個(gè)元素進(jìn)行迭代時(shí)的別名.index指 定一個(gè)名字,用于表示在迭代過程中,每次迭代到的位置.open表示該語句以什么開始,separator表示在每次進(jìn)行迭代之間以什么符號(hào)作為分隔 符.close表示以什么結(jié)束. 四、selectList()只能傳遞一個(gè)參數(shù),但實(shí)際所需參數(shù)既要包含String類型,又要包含List類型時(shí)的處理方法:將參數(shù)放入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); //網(wǎng)址idmap2.put("siteTag", "0");//網(wǎng)址類型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>
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注