# Mybatis使用之参数传递
### 一:简介
主要记录Mybatis是如何映射传递的参数的。分四种来记录:1、java基本类型的传递、2、Java对象形式传递 3、多参数传递4、集合类型参数传递
### 二:具体方式
### 2.1 java基本类型
以整形为例、映射文件:
~~~
<select id="getAuthorById" parameterType="int" resultType="org.alien.mybatis.samples.model.Author">
SELECT *
FROM author
WHERE id = #{id}
</select>
~~~
映射方法:
~~~
Author getAuthorById(int id);
~~~
测试代码:
~~~
private AuthorMapper authorMapper;
public AuthorMapperTest() {
authorMapper = MybatisUtil.getSqlSession().getMapper(AuthorMapper.class);
}
@Test
public void testGetAuthorById() throws Exception {
Author author = authorMapper.getAuthorById(1);
Assert.assertNotNull(author);
}
~~~
### 2.2. java对象
以Author类为例、映射文件:
~~~
<select id="getAuthorWithValidate" parameterType="org.alien.mybatis.samples.model.Author"
resultType="org.alien.mybatis.samples.model.Author">
SELECT *
FROM author
WHERE username = #{username} AND password = #{password}
</select>
~~~
映射方法:
~~~
Author getAuthorWithValidate(Author author);
~~~
测试方法:
~~~
@Test
public void testGetAuthorWithValidate() throws Exception {
Author author = authorMapper.getAuthorWithValidate(new Author("star_year", "alien"));
Assert.assertNotNull(author);
}
~~~
### 2.3 多参数
多参数分为两种:1、将参数放在Map中。 2、使用Mybatis的注解@Param来指定。
Map类型映射文件:
~~~
<select id="getAuthorByMultiCondition" parameterType="hashMap" resultType="author">
SELECT *
FROM
(
SELECT
A.*,
ROWNUM RN
FROM (SELECT *
FROM author
WHERE username LIKE '%' || #{username} || '%') A
WHERE ROWNUM <= #{endRecord}
)
WHERE RN >= #{startRecord}
</select>
~~~
Map类型映射方法:
~~~
List<Author> getAuthorByMultiCondition(Map<String, Object> map);
~~~
Map类型测试方法:
~~~
@Test
public void testGetAuthorByMultiCondition() throws Exception {
Map<String, Object> map = new HashMap<>();
map.put("startRecord", 0);
map.put("endRecord", 10);
map.put("username", "star_year");
List<Author> authors = authorMapper.getAuthorByMultiCondition(map);
Assert.assertNotNull(authors);
}
~~~
@Param类型映射文件:
~~~
<select id="getAuthorByUsername" resultType="Author">
SELECT *
FROM author
WHERE username LIKE '%' || #{username} || '%' AND email LIKE '%' || #{email} || '%'
</select>
~~~
@Param类型映射方法:
~~~
List<Author> getAuthorByUsername(@Param("username") String username, @Param("email") String email);
~~~
@Param类型测试方法:
~~~
@Test
public void testGetAuthorByUsername() throws Exception {
List<Author> authorList = authorMapper.getAuthorByUsername("star_year", "46185");
Assert.assertNotNull(authorList);
}
~~~
### 2.4 集合类型参数
常用与根据一系列id来做一些操作。
以Author类为例、映射文件:
~~~
<select id="getAuthorByIdCollection" resultType="author">
select * from author where id in
<foreach collection="list" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
~~~
映射方法:
~~~
List<Author> getAuthorByIdCollection(List<Integer> idList);
~~~
测试方法:
~~~
@Test
public void testGetAuthorByIdCollection() throws Exception {
List<Integer> idList = new ArrayList<>();
idList.add(1);
idList.add(2);
List<Author> authorList = authorMapper.getAuthorByIdCollection(idList);
Assert.assertNotNull(authorList);
}
~~~
### 三:${paramName} 与 #{paramName}区别
使用#{parameterName}引用参数的时候,Mybatis会把这个参数认为是一个字符串,例如传入参数是“Smith”,那么在SQL(Select * from emp where name = #{employeeName})使用的时候就会转换为Select * from emp where name = 'Smith';同时在SQL(Select * from emp where name = ${employeeName})使用的时候就会转换为Select * from emp where name = Smith。
再次,从安全性上考虑,能使用#尽量使用#来传参,因为这样可以有效防止SQL注入的问题。
### 四:补充
更多内容:[Mybatis 目录](http://blog.csdn.net/crave_shy/article/details/45825599)
github地址:https://github.com/andyChenHuaYing/scattered-items/tree/master/items-mybatis
源码下载地址:http://download.csdn.net/detail/chenghuaying/8713311
Author 类:
~~~
public class Author {
private int id;
private String username;
private String password;
private String email;
private String bio;
private String favouriteSection;
//getter setter ...
}
~~~