项目结构
第1种:在Java代码中添加sql通配符。
1、在UserMapper接口
定义方法
List<User> getUserLike(String value);
2、在UserMapper.xml
中,编写Sql语句
<select id="getUserLike" resultType="com.pojo.User">
select * from 2mybatis_first.user where name like #{value}
</select>
3、VagueQuery 测试类
@Test
public void getUserList(){
SqlSession session = MybatisUtils.getSqlSession(); //获取SqlSession连接
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> test=mapper.getUserLike("%李%");
for (User user : test) {
System.out.println(user);
}
session.close();
}
第2种:在sql语句中拼接通配符,会引起sql注入
<select id="getUserLike" resultType="com.pojo.User">
select * from 2mybatis_first.user where name like "%"#{value}"%"
</select>
故VagueQuery 测试类中的"%"可去掉