java實現刪除某條信息并刷新當前頁操作
我就廢話不多說了,大家還是直接看代碼吧~
//執行的是刪除信息的操作 String a=request.getParameter('name'); a = URLEncoder.encode(a, 'ISO-8859-1'); String name = URLDecoder.decode(a, 'UTF-8'); String num=request.getParameter('num'); System.out.println('name:'+name+'num:'+num); String sql='delete from person_info where name=? and num=?'; String sz[]={name,num}; JdbcUtils.update(sql, sz); //刷新操作 String sqls='select * from person_info'; ResultSet rs=JdbcUtils.select(sqls, null); ArrayList<Person_info> list=new ArrayList<Person_info>(); try { while(rs.next()){ Person_info pi=new Person_info(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6)); list.add(pi); } request.setAttribute('list', list); request.getRequestDispatcher('Personnel_definition.jsp').forward(request, response); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
補充知識:關于分頁時怎么實現當本頁面最后一條記錄被刪除時,自動向上一個頁面跳轉的實現(java實現)
##問題詳解
在做批量刪除時,發現若批量刪除整頁時,會自動跳到第一頁首頁,而不是返回刪除當前頁的上一頁,不符合產品要求且使界面交互不好,給用戶帶來糟糕體驗。
##思路詳解
在controller層傳參時要考慮到不僅要傳入需要刪除的id集合,同時傳入pageSize,pageNum以及總條數集合的查詢條件(如:本示例會傳入groupId(分組id)),在刪除成功后初始化當前頁,先根據查詢條件查詢出總條數數量,在pageSize不等于null或為0的情況下。算出余數[(pageSize*pageNum-count)%pageSize ].若余數為0,則當前頁等于pageNum-1;若余數不為0,則當前頁=pageNum.將結果當前頁傳給前臺即可。
##后臺代碼實現
#controller層#
@Api(description = '分組下的學生',value = '分組下的學生')@RestController@RequestMapping('studentGroup')public class StudentGroupController { @Autowired private RestStudentGroupService restStudentGroupService; @RequestMapping(value = 'deleteGroupStudent',method = RequestMethod.POST) @ApiOperation(value = '刪除分組中的學生',notes = '刪除分組中的學生') public ResponseObj deleteGroupStudent(@RequestParam(value = 'groupId',required = true)Long groupId, @RequestParam(value = 'ids',required = true)String ids, @RequestParam(value = 'pageSize',required = false)Integer pagesize, @RequestParam(value = 'pageNum',required = false)Integer pageNum){ return restStudentGroupService.deleteGroupStudent(groupId,ids,pagesize,pageNum); } }
#service層#
@FeignClient(value = ServiceName.VALUE)public interface RestStudentGroupService { @RequestMapping(value = '/school/cloud/student/deleteGroupStudent',method = RequestMethod.POST) public ResponseObj deleteGroupStudent(@RequestParam(value = 'groupId')Long groupId, @RequestParam(value = 'ids')String ids, @RequestParam(value = 'pageSize')Integer pagesize, @RequestParam(value = 'pageNum')Integer pageNum); }
#serviceImpl層#
@Servicepublic class RestStudentGroupServiceImpl implements RestStudentGroupService { @Autowired private DubboStudentGroupService dubboStudentGroupService ; @Override public ResponseObj deleteGroupStudent(Long groupId,String ids,Integer pageSize,Integer pageNum) { List<Long> idList = TextUtils.split(ids); if(groupId == null || idList== null || idList.size() == 0){ ResponseObj responseObj = ResponseObj.ERROR('參數錯誤'); responseObj.setSuccess(true); return responseObj; } ServiceResult<Long> serviceResult = dubboStudentGroupService .deleteCorpGroup(idList, groupId); if(!serviceResult.getSuccess()){ throw new RuntimeException('分組下學生查詢失敗'); } //應前端要求加此dto,封裝傳給前臺的當前頁屬性 CurrenPageDto currenPageDto=new CurrenPageDto(); //初始化當前頁 Integer currentPage = 1; //查出該分組id下的學生數量 ServiceResult<Long> itemCountLongs = dubboStudentGroupService.getTotalCount(groupId); Long itemCountLong= itemCountLongs.getResult(); Integer itemCount = itemCountLong!=null ? itemCountLong.intValue() : 0; //'查詢到學生數量:{},pageSize:{}', itemCount,pageSize; if(pageSize != null && pageSize != 0){ //算出余數 Integer temp = (pageNum*pageSize-itemCount)%pageSize; if(temp == 0){ //余數為0的話就pageNum-1 currentPage = (pageNum - 1) == 0 ? 1 : (pageNum -1) ; }else { //余數不為0則等于pageNum currentPage = pageNum; } currenPageDto.setPresentPage(currentPage); } ResponseObj responseObj = ResponseObj.SUCCESS(); responseObj.setData(currenPageDto); return responseObj; }}
#dubbo接口的service層#
①://刪除分組下的學生ServiceResult<Long> deleteCorpGroup(List<Long> idList,Long groupId);②://根據條件查詢對應的條目總數ServiceResult<Long> getTotalCount(Long groupId);
#dubbo接口的serviceImpl層#
①://刪除分組下的學生 @Override public ServiceResult<Long> deleteCorpGroup(List<Long> idList, Long groupId) { ServiceResult<Long> result = new ServiceResult<>(); try { studentGroupDao.deleteCorpGroup(idList, groupId); } catch (Exception e) { log.error('調用{}方法 異常', '[RestStudentGroupServiceImpl .deleteCorpGroup]'); log.error('方法使用參數:[idList:{},groupId:{}]', idList, groupId); log.error('異常信息:{}', e); result.setErrMessage('調用deleteCorpGroup方法異常,異常信息:' + e.getMessage()); } return result; }②://根據條件查詢對應的條目總數 @Override public ServiceResult<Long> getTotalCount(Long groupId) { ServiceResult<Long> result = new ServiceResult<>(); try { long count = studentGroupDao.getFindCorpGroupDirectoryCount(groupId); result.setResult(count); } catch (Exception e) { log.error('調用{}方法 異常', '[RestStudentGroupServiceImpl .getTotalCount]'); log.error('方法使用參數:[groupId:{}]', groupId); log.error('異常信息:{}', e); result.setErrMessage('調用getTotalCount方法異常,異常信息:' + e.getMessage()); } return result; }
#dubbo接口的dao層#
①://刪除分組下的學生 Long deleteCorpGroup(@Param(value = 'idList') List<Long> idList,@Param(value = 'groupId') Long groupId);②://根據條件查詢對應的條目總數Long getFindCorpGroupDirectoryCount(@Param(value = 'groupId') Long groupId);
#dubbo接口的sql#
①://刪除分組下的學生 <delete id='deleteCorpGroup'> delete from student_group where group_id = #{groupId} and id in <foreach collection='idList' index='index' separator=',' item='id' open='(' close=')'> #{id} </foreach> </delete>②://根據條件查詢對應的條目總數 <select resultType='long'> SELECT COUNT(1) FROM student_group where group_id = #{groupId} </select>
#Entity類(學生分組類)#(get,set函數省略)
public class StudentGroup implements java.io.Serializable { /** * */ private static final long serialVersionUID = 1L; /** * @描述: * @字段:id BIGINT(19) */ private Long StudentGroupId; /** * @描述: * @字段:group_id BIGINT(19) */ private Long groupId; /** * @描述: * @字段:id BIGINT(19) * 此id為學生表id */ private Long id; /** * @描述:創建時間 * @字段:create_time DATETIME(19) */ private java.util.Date createTime; * @描述:創建人用戶名 * @字段:create_user_name VARCHAR(30) */ private String createUserName; /** * @描述:創建人用戶ID * @字段:create_user_id BIGINT(19) */ private Long createUserId; /** * @描述:更新時間 * @字段:update_time DATETIME(19) */ private java.util.Date updateTime; * @描述:更新人用戶名 * @字段:update_user_name VARCHAR(30) */ private String updateUserName; /** * @描述:更新人用戶ID * @字段:update_user_id BIGINT(19) */ private Long updateUserId; }
#Entity類(學生類)#(get,set函數省略)
public class Student implements java.io.Serializable { /** * */ private static final long serialVersionUID = 1L; private Long id; private String name ; private Integer age; }
以上這篇java實現刪除某條信息并刷新當前頁操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章: