mybatis入口参数类型

基本类型的单参数 即:parameterType使用string基本类型

类:
public List<XXBean> getXXBeanList(String id);  
xml:
<select id="getXXXBeanList" parameterType="string" resultType="xxx.xxx.XXBean">
  	select * from test where id= #{id}  
</select>

其中方法名和ID保持一致,#{}中的参数名与方法中的参数名一致

复杂类型的单参数 即:parameterType使用HashMap类型

类:
public List<XXBean> getXXBeanList(HashMap map);  
xml:
<select id="getXXXBeanList" parameterType="hashmap" resultType="xxx.xxx.XXBean">
  	select * from test where  id=#{xxId} code = #{xxCode}  
</select>

map的key就是#{}中的参数名称

注解方式的多参数

类:
public List<XXXBean> getXXXBeanList(@Param("id")String id, @Param("code")String code);  
xml:
<select id="getXXXBeanList" resultType="xxx.xxx.XXBean">
  	select * from test where id = #{id} and name = #{code}
</select>

List参数 即:select * from test id in ( List )

类:
public List<XXXBean> getXXXBeanList(List<String> list);  
xml:
<select id="getXXXBeanList" resultType="xxx.xxx.XXBean">
  select * from test where id in
  <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
    #{item}  
  </foreach>  
</select>

Map多类型参数 即:Map参数既包含String等类型,又包含List等非String类型

类:
public List<XXXBean> getXXXBeanList(Map<String, Object> map);  
xml:
<select id="getXXXBeanList" resultType="xxx.xxx.XXBean">
  select * from test where name=#{name} and id in
  <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
    #{item}  
  </foreach>  
</select>
模拟初始化数据:
List<String> list = new ArrayList<String>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "myname");
list.add("1");
list.add("2");
map.put("list", list);

猜你喜欢

转载自blog.csdn.net/weixin_44153121/article/details/88052406
今日推荐