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.sa