SSM框架下模糊查询

    技术2024-11-03  12

    PageInfo

    import java.util.List; public class PageInfo<T> { private List<T> list; private int totalPage; private int size; private int totalCount; 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 getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } }

    UserController

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

    UserDao

    import com.bean.User; import org.apache.ibatis.annotations.Param; import java.util.List; public interface UserDao { List<User> findAll(@Param("start") int start,@Param("username") String username); }

    UserService

    @Override public PageInfo<User> findAll(int currentPage,String username) { PageInfo<User> pageInfo = new PageInfo<>(); pageInfo.setSize(5); int to = userDao.getTotalCount(username); pageInfo.setTotalCount(to); int tp = (int)Math.ceil(to/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.findAll(start,username); pageInfo.setList(userList); return pageInfo; }

    IUserService

    import com.bean.PageInfo; import com.bean.User; import java.util.List; public interface IUserService { PageInfo<User> findAll(int currentPage,String username); }

    UserMapping

    <select id="findUserByUserName" parameterType="String" resultType="user"> select * from tb_user where username=#{username} </select>

    JSP界面

    <form action="${pageContext.request.contextPath}/user/findAll.do?type=1" 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>
    Processed: 0.086, SQL: 9