MyBaits参数传递
单个参数多个参数 注解开发
注解完成增删改查
提示 简单举例
编写接口方法编写测试方法
MyBaits参数传递MyBatis接口方法中可以接收各种各样的参数,MyBatis底层对于这些参数进行不同的封装处理方式
单个参数MyBatis提供了 ParamNameResolver类来进行参数封装
POJO类型:直接使用.属性名 和 参数占位符名称 一致
Map集合:直接使用,键名 和 参数占位符名称 一致
Collection:封装为Map集合,可以使用@Param注解,替换Map集合中默认的arg键名
map.put(“arg0”,collection集合)
map.put(“collection,collection集合”)
List:封装为Map集合,可以使用@Param注解,替换Map集合中默认的arg键名
map.put(“arg0”,list集合)
map.put(“collection”,list集合)
map.put(“list”,list集合)
Array:封装为Map集合,可以使用@Param注解,替换Map集合中默认的arg键名
map.put(“arg0”,数组)
map.put(“array”,数组)
其他类型:直接使用
//封装为Map集合,可以使用@Param注解,替换Map集合中默认的arg键名
map.put("arg0",参数值1)
map.put("param1",参数值1)
map.put("param2",参数值2)
map.put("agr1",参数值2)
//-----------------@Param("username")
map.put("username",参数值1)
map.put("param1",参数值1)
map.put("param2",参数值2)
map.put("agr1",参数值2)
注解开发
注解完成增删改查
使用注解开发会比配置文件开发更加方便
@Select
@Insert
@Update
@Delete
提示注解完成简单功能配置文件完成复杂功能 简单举例
@Select("select * from tb_user where id = #{id}")
public User selectById(int id);
编写接口方法
编写BrandMapper.java
@Select("select * from tb_brand where id = #{id}")
Brand selectById02(int id);
编写测试方法
编写MyBatisTest.java
@Test
public void testSelectById02() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
Brand brand = brandMapper.selectById02(1);
System.out.println(brand);
sqlSession.close();
}



