模糊查询
useController
@RequestMapping ( "/findAll.do" )
public ModelAndView findAll ( @RequestParam ( defaultValue = "1" ) int currentPage, String username, @RequestParam ( defaultValue = "0" ) int type, HttpSession session) {
if ( type== 1 ) {
session. setAttribute ( "searchname" , username) ;
} else {
username= ( String) session. getAttribute ( "searchname" ) ;
}
PageInfo< User> pageInfo = userService. findAll ( currentPage, username) ;
ModelAndView modelAndView = new ModelAndView ( ) ;
modelAndView. addObject ( "pageInfo" , pageInfo) ;
modelAndView. setViewName ( "user-list" ) ;
return modelAndView;
}
userService
@Override
public PageInfo< User> findAll ( int currentPage, String username) {
PageInfo< User> pageInfo= new PageInfo < > ( ) ;
pageInfo. setSize ( 5 ) ;
int tc= userDao. getTotalCount ( username) ;
pageInfo. setTotalCount ( 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. findAll ( start, username) ;
pageInfo. setList ( userList) ;
return pageInfo;
}
mapper
< select id= "getTotalCount" resultType= "int" >
select count ( * ) from tb_user
< if test= "username!=null and username!=''" >
WHERE username LIKE concat ( "%" , #{ username} , "%" )
< / if >
< / select>
批量删除
user-list.jsp
function deleteAll ( ) {
var checkedNum= $ ( "input[name='ids']:checked" ) . length;
alert ( checkedNum) ;
if ( checkedNum== 0 ) {
alert ( "请至少选择一个进行删除!!!" ) ;
return ;
}
if ( confirm ( "确认要删除这些用户吗?" ) ) {
var userList= new Array ( ) ;
$ ( "input[name='ids']:checked" ) . each (
function ( ) {
userList. push ( $ ( this ) . val ( ) )
}
) ;
alert ( userList) ;
$. ajax ( {
type: "post" ,
url: "${pageContext.request.contextPath}/user/deleteAll.do" ,
data: { userList: userList. toString ( ) } ,
success: function ( ) {
alert ( "删除成功" ) ;
location. reload ( ) ;
} ,
error: function ( ) {
alert ( "删除失败" ) ;
}
} ) ;
}
useController
@RequestMapping ( "deleteAll.do" )
public String deleteAll ( String userList) {
String[ ] strs = userList. split ( "," ) ;
List< Integer> ids= new ArrayList < > ( ) ;
for ( String s: strs) {
ids. add ( Integer. parseInt ( s) ) ;
}
userService. deleteAll ( ids) ;
return "redirect:findAll.do" ;
}
userDao
void deleteAll ( @Param ( "ids" ) List< Integer> ids) ;
mapper
< delete id= "deleteAll" parameterType= "list" >
delete from tb_user where id in
< foreach collection= "ids" item= "id" open= "(" close= ")" separator= "," >
#{ id}
< / foreach>
< / delete >