MyBatis获取参数的两种方式
单个字面量类型的参数多个字面量类型的参数Map集合类型的参数实体类类型的参数使用@Param标识参数 MyBatis的各种查询功能
查询一个实体类对象查询一个List集合查询单个数据查询一条数据为Map集合查询多条数据为Map集合
MyBatis获取参数的两种方式MyBatis获取参数值的两种方式:${}和#{}。
${}的本质就是字符串拼接,#{}的本质就是占位符赋值。
${}使用字符串拼接的方式拼接sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单引号。
#{}使用占位符赋值的方式拼接sql,此时为字符串类型或日期类型的字段进行赋值时,可以自动添加单引号。
单个字面量类型的参数#{}和${}都可以使用任意的名称来获取参数值,需要注意的是${}需要手动添加单引号。
Mapper接口:
User getUserByUserName(String username);
xml配置:
select * from t_user where username = '${username}';
单元测试:
@Test
public void testGetUserByUserName(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession("mybatis-config.xml");
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
User user = mapper.getUserByUserName("桑奇");
System.out.println(user);
}
多个字面量类型的参数
当mapper接口中的方法参数为多个时,此时MyBatis会自动将这些参数放在一个map集合中,以arg0,arg1…为键,以参数为值;以param1,param2,…为键,以参数为值。
因此只需要通过${}和#{}访问map集合的键就可以获取相应的值,${}需要手动加单引号。
mapper接口
User getLogin(String username,String password);
xml配置:
单元测试:
@Test
public void testLogin(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession("mybatis-config.xml");
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
User admin = mapper.getLogin("admin", "123456");
System.out.println(admin);
}
Map集合类型的参数
若mapper接口中的方法需要的参数为多个时,可以手动创建Map集合,将这些数据放在Map中,只需要通过#{}和${}访问Map集合的键就可以获取相对应的值,${}需要手动加单引号。
mapper接口
User checkLogin(Map map);
xml配置:
单元测试:
@Test
public void testCheckLogin(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession("mybatis-config.xml");
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
Map map = new HashMap<>();
map.put("username","桑奇");
map.put("password","sangqi168");
User user = mapper.checkLogin(map);
System.out.println(user);
}
实体类类型的参数
若mapper接口中的方法参数为实体类对象时,可以使用${}和#{},通过访问实体类对象中的属性名获取属性值,${}需要手动添加单引号。
mapper接口:
int insertUser(User user);
xml配置:
insert into t_user values(null,#{username},#{password},#{age},#{sex},#{email});
单元测试:
@Test
public void testInsertUser(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession("mybatis-config.xml");
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
mapper.insertUser(new User(null,"桑奇","sangqi168",1,"男","sangqi@wz.com"));
}
使用@Param标识参数
可以通过@Param注解标识mapper接口中的方法参数。
此时会将这些参数放在map集合中,以@Param注解的value属性值为键,以参数为值;以param1,param2,…为键,以参数为值。通过访问Map集合中的键就可以获取相对应的值,${}需要手动添加单引号。
mapper接口
User checkLoginByParam(@Param("username") String username,@Param("password") String password);
xml配置:
单元测试:
@Test
public void testCheckLoginByParam(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession("mybatis-config.xml");
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
User user = mapper.checkLoginByParam("桑奇", "sangqi168");
System.out.println(user);
}
MyBatis的各种查询功能
查询一个实体类对象
mapper接口
User getUserById(@Param("id") Integer id);
xml配置:
单元测试:
@Test
public void testGetUserById(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession("mybatis-config.xml");
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
User user = mapper.getUserById(5);
System.out.println(user);
}
查询一个List集合
mapper接口
List getUserList();
xml配置
单元测试:
@Test
public void testGetUserList(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession("mybatis-config.xml");
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
List userList = mapper.getUserList();
userList.forEach(System.out::println);
}
查询单个数据
mapper接口:
int getCount();
xml配置:
单元测试
@Test
public void testGetCount(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession("mybatis-config.xml");
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
int count = mapper.getCount();
System.out.println(count);
}
查询一条数据为Map集合
mapper接口
Map getUserMapById(@Param("id") Integer id);
xml配置:
单元测试:
@Test
public void testGetUserMapById(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession("mybatis-config.xml");
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
Map map = mapper.getUserMapById(5);
System.out.println(map);
}
查询多条数据为Map集合
方式一:
mapper接口
List
xml配置
单元测试
@Test
public void testGetAllUserToMap(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession("mybatis-config.xml");
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
List
方式二:
mapper接口
@MapKey("id")
Map getUserMap();
xml配置
单元测试
@Test
public void testGetUserMap(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession("mybatis-config.xml");
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
Map map = mapper.getUserMap();
System.out.println(map);
}



