mybatise 条件查询

(一)单个参数 public List<XXBean> getXXBeanList(String xxCode);java

<select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean"> select 字段... from XXX where code = #{xxCode} </select>mybatis

其中方法名和ID一致,#{}中的参数名与方法中的参数名一直, 我这里采用的是XXXBean是采用的短名字, select 后的字段列表要和bean中的属性名一致, 若是不一致的能够用 as 来补充。code

(二)多参数 public List<XXXBean> getXXXBeanList(String xxId, String xxCode);索引

<select id="getXXXBeanList" resultType="XXBean"> select 字段... from XXX where id=#{0} code = #{1} </select>get

因为是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始 (三)Map封装多参数hash

public List<XXXBean> getXXXBeanList(HashMap map);it

<select id="getXXXBeanList" parameterType="hashmap" resultType="XXBean"> select 字段... from XXX where id=#{xxId} code = #{xxCode} </select>io

其中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>hashmap

foreach 最后的效果是select 字段... from XXX where id in ('1','2','3','4')配置

相关文章
相关标签/搜索