0%

MyBatis获取参数值方式

1
2
3
4
5
6
MyBatis获取参数值的两种方式:${}与#{}

${}的本质就是字符串拼接,#{}的本质就是占位符赋值

${}使用字符串拼接的方式拼接sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单引号;
但是#{}使用占位符赋值的方式拼接sql,此时为字符串类型或日期类型的字段进行赋值时,可以自动添加单引号

情况一:mapper接口方法参数为单个字面量类型

1
2
3
4
5
6
7
@Test
public void testMybatis() throws IOException {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper parameterMapper = sqlSession.getMapper(ParameterMapper.class);
List<User> result = parameterMapper.getUserByUsername("张三");
result.forEach(user -> System.out.println(user));
}
1
2
3
4
<!--User getUserByUsername(String username);-->
<select id="getUserByUsername" resultType="User">
select * from t_user where username = #{username}
</select>

情况二:mapper接口方法参数为多个

  • 使用arg0,arg1,…… 或 param1,param2,…… 为键,参数为值
  • 只需通过#{}或${}以键的方式访问值即可
1
2
3
4
5
6
7
@Test
public void testMybatis() throws IOException {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper parameterMapper = sqlSession.getMapper(ParameterMapper.class);
List<User> result = parameterMapper.checkLogin("张三", "123456");
result.forEach(user -> System.out.println(user));
}
1
2
3
4
<!--User checkLogin(String username, String password)-->
<select id="checkLogin" resultType="User">
select * from t_user where username = #{arg0} and password = #{arg1}
</select>

情况三:mapper接口方法参数有多个,可以手动放在一个map中存储

  • 只需通过#{}或${}以键的方式访问值即可
1
2
3
4
5
6
7
8
9
10
@Test
public void testMybatis() throws IOException {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper parameterMapper = sqlSession.getMapper(ParameterMapper.class);
Map<String, Object> mp = new HashMap<>();
mp.put("username", "张三");
mp.put("password", "123456");
List<User> result = parameterMapper.checkLoginByMap(mp);
result.forEach(user -> System.out.println(user));
}
1
2
3
4
<!--List<User> checkLoginByMap(Map<String, Object> mp)-->
<select id="checkLoginByMap" resultType="User">
select * from t_user where username = #{username} and password = #{password}
</select>

情况四:参数是一个实体类类型

  • 直接通过属性名访问属性值
1
2
3
4
5
6
7
8
@Test
public void testMybatis() throws IOException {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper parameterMapper = sqlSession.getMapper(ParameterMapper.class);
User user = new User("Alice", "666666", 22, "女", "666@163.com");
int result = parameterMapper.insertUser(user);
System.out.println(result);
}
1
2
3
4
<!--int insertUser(User user)-->
<insert id="insertUser">
insert into t_user values(null, #{username}, #{password}, #{age}, #{sex}, #{email})
</insert>

情况五:通过@param命名参数

  • 以@param注解的值 或 param1,param2,……为键,以参数为值
1
2
3
4
5
6
7
@Test
public void testMybatis() throws IOException {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper parameterMapper = sqlSession.getMapper(ParameterMapper.class);
List<User> result = parameterMapper.checkLoginByParam("Alice", "666666");
result.forEach(User -> System.out.println(User));
}
1
2
3
4
<!--List<User> checkLoginByParam(@Param("username") String username, @Param("password") String password)-->
<select id="checkLoginByParam" resultType="User">
select * from t_user where username = #{username} and password = #{password}
</select>

建议整合为两种情况

  • 参数是实体类对象
  • 否则全都用@param
------ THEEND ------

欢迎关注我的其它发布渠道