Home MyBatis如何传入多个参数
Post
Cancel

MyBatis如何传入多个参数

单个参数

java

1
public List<Test> getTestList(String id);

xml

1
2
3
<select id="getTestList" parameterType="java.lang.String" resultType="com.test.Test">
  select t.* from test t where t.id=#{id}
</select>

多个参数

使用索引

java

1
public List<Test> getTestList(String id, String name);

xml

1
2
3
<select id="getTestList" resultType="com.test.Test">
  select t.* from test t where t.id=#{0} and t.name=#{1} 
</select>

使用Map封装

java

1
public List<Test> getTestList(HashMap map);

xml

1
2
3
4
<select id="getTestList" parameterType="hashmap" resultType="com.test.Test">
  select t.* from test t where t.id=#{id} and t.name=#{name} 
   <!-- #{}中的变量名要和map中的key对应 -->
</select>

使用注解

java

1
public List<Test> getTestList(@Param("id") int id, @Param("name") int name);

xml

1
2
3
<select id="getTestList" resultMap="com.test.Test">
   select t.* from test t where t.id=#{id} and t.name=#{name}
</select>
This post is licensed under CC BY 4.0 by the author.