模糊查询

UserMapper.xml

<select id="getUserbyName" parameterType="String" resultType="user">
    select * from mybatis.user where name like #{username}
</select>

UserMapper

//根据名字模糊匹配
public ArrayList<User> getUserbyName(String name);

UserDaoTest


@Test
public void testGetUserByName(){
    //第一步:获得sqlSession对象
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    //第二步:执行sql
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);

    ArrayList<User> userList = mapper.getUserbyName("%乔%");
    for (User user : userList) {
        System.out.println(user);
    }

    //第三步:关闭sqlSession
    sqlSession.close();
}
posted @ 2022-05-25 20:30  qiaoyizhi  阅读(11)  评论(0编辑  收藏  举报