外链论坛

 找回密码
 立即注册
搜索
查看: 84|回复: 4

Java实现删除某条信息并返回当前页操作

[复制链接]

2558

主题

360

回帖

9911万

积分

论坛元老

Rank: 8Rank: 8

积分
99110920
发表于 2024-8-17 10:06:17 | 显示全部楼层 |阅读模式

我就废话不多说了,大众还是直接看代码吧

//执行的是删除信息的操作 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<erson_info> list=new ArrayList<erson_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 blocke.printStackTrace(); } }

image.png

弥补知识:关于分页时怎么实现当本页面最后一条记录被删除时,自动向上一个页面的实现(java实现)

问题详解

在做批量删除时,发掘若批量删除整页时,会自动跳到第1页首页,而不是返回删除当前页的上一页,不符合制品需求且使界面交互欠好,给用户带来糟糕体验。

思路详解

在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 { @Autowiredprivate 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层#

@Service public class RestStudentGroupServiceImpl implements RestStudentGroupService { @Autowired privateDubboStudentGroupService 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); LongitemCountLong= 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-1currentPage = (pageNum -1) == 0 ? 1 : (pageNum -1) ; }else { //余数不为0则等于pageNumcurrentPage = 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;` `}`| [](javascript:; "全选")[](javascript:; "复制java代码") <textarea style="margin: 0px; padding: 0px; outline: none; font: 16px / 24px tahoma, arial, 宋体;"></textarea> #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 id="getFindCorpGroupDirectoryCount" resultType="long"> SELECT COUNT(1) FROM student_groupwhere group_id = #{groupId} </select>

Entity类(学生分组类)#(get,set函数省略)

|

publicclassStudentGroupimplementsjava.io.Serializable {

/**

*

*/

privatestaticfinallongserialVersionUID = 1L;

/**

* @描述:

* @字段:id BIGINT(19)

*/

privateLong StudentGroupId;

/**

* @描述:

* @字段:group_id BIGINT(19)

*/

privateLong groupId;

/**

* @描述:

* @字段:id BIGINT(19)

* 此id为学生表id

*/

privateLong id;

/**

* @描述:创建时间

* @字段:create_time DATETIME(19)

*/

privatejava.util.Date createTime;

* @描述:创建人用户名

* @字段:create_user_name VARCHAR(``30``)

*/

privateString createUserName;

/**

* @描述:创建人用户ID

* @字段:create_user_id BIGINT(19)

*/

privateLong createUserId;

/**

* @描述:更新时间

* @字段:update_time DATETIME(19)

*/

privatejava.util.Date updateTime;

* @描述:更新人用户名

* @字段:update_user_name VARCHAR(``30``)

*/

privateString updateUserName;

/**

* @描述:更新人用户ID

* @字段:update_user_id BIGINT(19)

*/

privateLong updateUserId;

}

|

[](javascript:; "全选")[](javascript:; "复制java代码")

<textarea style="margin: 0px; padding: 0px; outline: none; font: 16px / 24px tahoma, arial, 宋体;"></textarea>

#Entity类(学生类)#(get,set函数省略) ```c `public` `clas` `Student` `implements` `java.io.Serializable {` `/**` `*` `*/` `private` `static` `final` `long` `serialVersionUID = 1L;` `private` `Long id;` `private` `String name ;` `private` `Integer age;`
回复

使用道具 举报

1

主题

1366

回帖

-3

积分

限制会员

积分
-3
发表于 2024-8-24 00:13:13 | 显示全部楼层
你的话语如春风拂面,让我心生暖意。
回复

使用道具 举报

0

主题

2万

回帖

1

积分

新手上路

Rank: 1

积分
1
发表于 2024-9-8 19:15:52 | 显示全部楼层
我完全同意你的看法,期待我们能深入探讨这个问题。
回复

使用道具 举报

2

主题

1314

回帖

-7

积分

限制会员

积分
-7
发表于 2024-9-9 22:22:04 | 显示全部楼层
你的见解真是独到,让我受益匪浅。
回复

使用道具 举报

0

主题

2万

回帖

1

积分

新手上路

Rank: 1

积分
1
发表于 3 天前 | 显示全部楼层
我深受你的启发,你的话语是我前进的动力。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|外链论坛 ( 非经营性网站 )|网站地图

GMT+8, 2024-9-17 06:08 , Processed in 0.087558 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.