mybatis parametertype 多个参数
2017-11-01 00:24
681 查看
一、单个参数:
二、多参数:
方案1
方案2(推荐)基于注解
三、Map封装多参数:
四、List封装in:
五、selectList()只能传递一个参数,但实际所需参数既要包含String类型,又要包含List类型时的处理方法:
将参数放入Map,再取出Map中的List遍历。如下:
转载网址:http://blog.csdn.net/lixld/article/details/77980443
public List<XXBean> getXXBeanList(@param("id")String id); <select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean"> select t.* from tableName t where t.id= #{id} </select> 其中方法名和ID一致,#{}中的参数名与方法中的参数名一致, 这里采用的是@Param这个参数,实际上@Param这个最后会被Mabatis封装为map类型的。 select 后的字段列表要和bean中的属性名一致, 如果不一致的可以用 as 来补充。
二、多参数:
方案1
public List<XXXBean> getXXXBeanList(String xxId, String xxCode); <select id="getXXXBeanList" resultType="XXBean">不需要写parameterType参数 select t.* from tableName where id = #{0} and name = #{1} </select> 由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始
方案2(推荐)基于注解
public List<XXXBean> getXXXBeanList(@Param("id")String id, @Param("code")String code); <select id="getXXXBeanList" resultType="XXBean"> select t.* from tableName where id = #{id} and name = #{code} </select> 由于是多参数那么就不能使用parameterType, 这里用@Param来指定哪一个
三、Map封装多参数:
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如何封装就不用了我说了吧。
四、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')
五、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");
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>
转载网址:http://blog.csdn.net/lixld/article/details/77980443
相关文章推荐
- MyBatis中传入参数parameterType类型详解
- MyBatis的传入参数parameterType类型
- MyBatis 传入参数之parameterType
- MyBatis的传入参数parameterType类型
- mybatis中关于传入参数parameterType
- MyBatis的传入参数parameterType类型
- MyBatis之传入参数parameterType
- mybatis 之 parameterType="HashMap"参数包含list
- mybatis parametertype 多个参数
- MyBatis传入参数与parameterType
- MyBatis 之parameterType属性—传入参数
- MyBatis传入参数与parameterType不同类型使用
- MyBatis的传入参数parameterType类型
- MyBatis的传入参数parameterType类型
- mybati之parameterType传递多个参数
- mybatis 之 parameterType="list"1
- template non-type parameter 非类型参数
- mybatis if 动态生成SQL的一个注意点 parameterType="map"
- 在MyBatis的select、insert、update、delete这些元素中都提到了parameterType这个属性。MyBatis现在可以使用的parameterType有基本数据类型和Ja
- mybatis 之 parameterType="List" 2