ssm框架下模糊查找

环境说明

  1. mysql 5.7版本
  2. mysql中创建sx的数据库,在sx数据库中有一张tb_user的表
  3. tb_user的表中有id,username,password 三个字段
  4. Tomcat 9.0版本
  5. maven 3.6.3

mybatis中模糊查询sql语句

select * from table_name where username like concat('%',#{username},'%') 

Controller

@RequestMapping("/findUser.do")
public ModelAndView  findUser(@RequestParam(defaultValue = "1") int currentPage,String username){
    PageInfo<User> pageInfo=userService.findUser(currentPage,username);
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("pageInfo", pageInfo);
    modelAndView.setViewName("user-list");
    return modelAndView;

}

bean

package allone8.bean;

import java.util.List;

public class PageInfo<T> {
    private List<T> list;
    private int totalPage;
    private int size;
    private int totalCiunt;
    private int currentPage;

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getTotalCiunt() {
        return totalCiunt;
    }

    public void setTotalCiunt(int totalCiunt) {
        this.totalCiunt = totalCiunt;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
}

dao

List<User> findUser(@Param("start") int start,@Param("username") String username);

service

@Override
public PageInfo<User> findUser(int currentPage, String username) {
    PageInfo<User> pageInfo = new PageInfo<>();
    pageInfo.setSize(5);
    int tc = userDao.getTotalCount();
    pageInfo.setTotalPage(tc);
    int tp = (int) Math.ceil(tc / 5.0);
    pageInfo.setTotalPage(tp);
    if (currentPage < 1) {
        pageInfo.setCurrentPage(1);
    } else if (currentPage > tp) {
        pageInfo.setCurrentPage(tp);
    } else {
        pageInfo.setCurrentPage(currentPage);
    }
    int start = (pageInfo.getCurrentPage() - 1) * 5;
    List<User> userList = userDao.findUser(start,username);
    pageInfo.setList(userList);
    return pageInfo;
}

mapper

<select id="findUser" resultType="user" >
    select * from tb_user where username like concat('%',#{username},'%') limit #{start},5
</select>

jsp

<form action="${pageContext.request.contextPath}/user/findUser.do"
      method="post">
    <div class="col-md-4 data1">
        <input type="text" class="form-control" name="username"
               placeholder="username" value="">
    </div>
    <button type="submit" class="btn bg-maroon">搜索</button>
</form>

你可能感兴趣的:(Java,SSM框架)