文章目录
1 MyBatis获取参数值的两种方式
MyBatis获取参数值的两种方式:${}和#{}
${}的本质就是字符串拼接
#{}的本质就是占位符赋值
${}使用字符串拼接的方式拼接sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单引号;
但是#{}使用占位符赋值的方式拼接sql,此时为字符串类型或日期类型的字段进行赋值时,可以自动添加单引号(尽量使用这一种)。
2 MyBatis获取参数值的五种情况
情况1: 单个字面量类型的参数(方法中有一个参数)
若mapper接口中的方法参数为单个的字面量类型
此时可以使用KaTeX parse error: Expected 'EOF', got '#' at position 4: {}和#̲{}以任意的名称获取参数的值,…{}需要手动加单引号
ParameterMapper接口:
public interface ParameterMapper {
/*
* 单个的字面量类型:
* 根据用户名查询用户信息
* */
User getUserByUsername(String username);
}
对应在ParameterMapper.xml中配置。
方式一
#{}的本质就是占位符赋值
<select id="getUserByUsername" resultType="User">
<!--使用#{},里面内容可以随便写,都是传进来的username的值-->
select * from t_user where username=#{username}
</select>在这里插入代码片
方式二
${}的本质就是字符串拼接
<select id="getUserByUsername" resultType="User">
<!--使用#{},里面内容可以随便写,都是传进来的username的值-->
<!-- select * from t_user where username=#{username}-->
<!--
select * from t_user where username = ${username}
如果使用这种方式,得到的sql语句是:
Preparing: select * from t_user where username = RUOYI
而其中username的值‘RUOYI’没有单引号,语句不正确,会报错。
因此要手动添加单引号-->
select * from t_user where username = '${username}'
</select>
测试类
@Test
public void testGetUserByUsername(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
User jay = mapper.getUserByUsername("jay");
System.out.println(jay);
}
情况2:多个字面量类型的参数
若mapper接口中的方法参数为多个时
此时MyBatis会自动将这些参数放在一个map集合中,以arg0,arg1…为键,以参数为值;
以 param1,param2…为键,以参数为值;
因此只需要通过KaTeX parse error: Expected 'EOF', got '#' at position 4: {}和#̲{}访问map集合的键就可以获…{}需要手动加单引号
ParameterMapper接口:
public interface ParameterMapper {
/*验证登录*/
User checkLogin(String username,String password);
/*
对应在ParameterMapper.xml中配置。
<!--User checkLogin(String username ,String password);-->
<select id="checkLogin" resultType="User">
<!--select * from t_user where username=#{arg0} and password=#{arg1}-->
select * from t_user where username='${param1} 'and password='${param2}'
</select>
测试类
@Test
public void testCheckLogin(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
User jay = mapper.checkLogin("jay", "123456");
System.out.println("jay = " + jay);
}
情况3:map集合类型的参数
若mapper接口中的方法需要的参数为多个时,此时可以手动创建map集合,将这些数据放在map中
只需要通过KaTeX parse error: Expected 'EOF', got '#' at position 4: {}和#̲{}访问map集合的键就可以获…{}需要手动加单引号
ParameterMapper接口:
public interface ParameterMapper {
/*验证登录*/
User checkLoginByMap(Map<String,Object> map);
}
对应在ParameterMapper.xml中配置。
<!-- User checkLoginByMap(Map<String,Object> map);-->
<select id="checkLoginByMap" resultType="User">
select * from t_user where username=#{username} and password=#{password}
</select>
测试类:
@Test
public void testCheckLoginByMap(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
Map<String ,Object> map=new HashMap<>();
map.put("username","jay");
map.put("password","123456");
User user = mapper.checkLoginByMap(map);
System.out.println(user);
}
情况4:实体类类型的参数
若mapper接口中的方法参数为实体类对象时 此时可以使用KaTeX parse error: Expected 'EOF', got '#' at position 4: {}和#̲{},通过访问实体类对象中的属…{}需要手动加单引号
ParameterMapper接口:
/*增加user*/
User insertUser(User user);
对应在ParameterMapper.xml中配置。
<!-- User insertUser(User user);-->
<select id="insertUser" resultType="User">
insert into t_user values (null,#{username},#{password},#{age},#{sex},#{email})
</select>
测试类
@Test
public void testInsertUser(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
User user = mapper.insertUser(new User(null, "张三", "123", 24, "男", "123@qq.com"));
System.out.println(user);
}
情况5: 使用@Param标识参数
可以通过@Param注解标识mapper接口中的方法参数
此时,会将这些参数放在map集合中,
以@Param注解的value属性值为键,以参数为值;
以 param1,param2…为键,以参数为值;
只需要通过KaTeX parse error: Expected 'EOF', got '#' at position 4: {}和#̲{}访问map集合的键就可以获…{}需要手动加单引号
ParameterMapper接口:
/*@Param*/
User getUserByParam(@Param("username") String username, @Param("password") String password);
对应在ParameterMapper.xml中配置。
<!--User getUserByParam();-->
<select id="getUserByParam" resultType="User">
select * from t_user where username=#{username} and password=#{password}
</select>
测试类:
@Test
public void testGetUserByParam(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
User jay = mapper.getUserByParam("jay","123456");
System.out.println("jay = " + jay);
}