SQLMapper接口
- public interface SQLMapper {
-
- /*
- 根据用户名 来模糊查询 用户信息
- */
- // 设置一个参数 有一个固定的访问方式
- List
getUserByList(@Param("username") String username); -
- }
接口对应的映射文件
- "1.0" encoding="UTF-8" ?>
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-
-
"com.atguigu.mybatis.mapper.SQLMapper"> -
-
-
-
-
- select * from t_user where username like '%#{username}%'
-
-
-
-
建立测试类
- public class SQLMapperTest {
-
-
- @Test
- public void testGetUserByLike(){
-
-
- //获取 sqlSession 对象
- SqlSession sqlSession = SqlSessionUtils.getSqlSession();
- // 获取一个SQLMapper 对象
- SQLMapper mapper = sqlSession.getMapper(SQLMapper.class);
- // 根据a来调用,返回值是一个list集合
- List
list = mapper.getUserByLike("a"); - System.out.println(list);
-
-
- }
- }
三种模糊拼接方式
select * from t_user where username like '%${username}%'
select * from t_user where username like concat('%',#{username},'%')
select * from t_user where username like "%" #{username} "%"