# 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 ...
}
~~~