public List getXXBeanList(@param("id")String id);
- <select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean">
- select t.* from tableName t where t.id= #{id}
- select>
public List 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来指定哪一个
public List getXXXBeanList(String xxId, String xxCode);
- <select id="getXXXBeanList" resultType="XXBean">
- select t.* from tableName where id = #{0} and name = #{1}
- select>
由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始
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的名字是那个就在#{}使用那个
注意:这里的collection是你集合的字段名。
public List getXXXBeanList(List 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')
mybatis接受的参数分为:
• 基本类型
• 对象
• List
• 数组
• Map
List selectByUserIdList(@Param("userIds")String[] userIds);
注意:这里的collection是你集合的字段名。
- <select id="selectByRoleIdList" resultMap="user" >
- SELECT * FROM user
- <if test="userIds!= null">
- where user_id in
- <foreach collection="userIds" item="userId" index="index" open="(" separator="," close=")" >
- #{userId,jdbcType=INTEGER}
- foreach>
-
- if>
将参数放入Map,再取出Map中的List遍历。如下:
- List
list_3 = new ArrayList(); - list.add("1");
- list.add("2");
-
- Map
map2 = new HashMap(); - map.put("list", list); //网址id
- map.put("siteTag", "0");//网址类型
- <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>