在我先前的基础上面添加批量删除功能。实现的效果如下
删除成功
通常情况下删除不应该真正删除,而是应该有一个标志flag,但flag=true表示状态可见,但flag=false表示状态不可见,为删除状态。便于日后数据库的维护和信息的查询。因此表结构添加一个flag字段
没有改变的代码这里就不写了,发生改变的代码贴出来
1、因为表结构发生变化。所以对应的Student.java和Student.hbm.xml发生改变
- package com.model;
-
- public class Student {
- String studentid;
- String name;
- String gender;
- String age;
- String flag;
-
- public String getStudentid() {
- return studentid;
- }
-
- public void setStudentid(String studentid) {
- this.studentid = studentid;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getGender() {
- return gender;
- }
-
- public void setGender(String gender) {
- this.gender = gender;
- }
-
- public String getAge() {
- return age;
- }
-
- public void setAge(String age) {
- this.age = age;
- }
-
- public String getFlag() {
- return flag;
- }
-
- public void setFlag(String flag) {
- this.flag = flag;
- }
-
-
-
- }
Student.hbm.xml的代码
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="com.model.Student" table="STUDENT">
- <id name="studentid" type="java.lang.String">
- <column name="STUDENTID" />
- <generator class="assigned" />
- </id>
- <property name="name" type="java.lang.String">
- <column name="NAME" />
- </property>
- <property name="gender" type="java.lang.String">
- <column name="GENDER" />
- </property>
- <property name="age" type="java.lang.String">
- <column name="AGE" />
- </property>
- <property name="flag" type="java.lang.String">
- <column name="FLAG" />
- </property>
- </class>
- </hibernate-mapping>
2、对应的Action。StudentAction
3、对应的接口StudentService
- package com.service;
-
- import java.util.List;
-
- import com.model.Student;
-
- public interface StudentService {
- public List getStudentList(String page,String rows) throws Exception;
- public int getStudentTotal() throws Exception;
- public void saveStudent(Student student)throws Exception;
- public String queryBy_unique(String table,String field ,String parameter) throws Exception;
- public void deleteStudent(String ids) throws Exception;
-
- }
4、对应的接口实现类,从43行开始的del()方法用循环的方式输出多个id,进行批量删除
- package com.serviceImpl;
-
- import java.util.List;
-
- import org.apache.log4j.Logger;
- import org.hibernate.Criteria;
- import org.hibernate.Query;
- import org.hibernate.SessionFactory;
- import org.hibernate.criterion.Restrictions;
-
- import com.model.Student;
- import com.service.StudentService;
-
- public class StudentServiceImpl implements StudentService {
- Logger log=Logger.getLogger(this.getClass());
- private SessionFactory sessionFactory;
-
-
- public List getStudentList(String page, String rows) {
-
-
- int currentpage = Integer.parseInt((page == null || page == "0") ? "1": page);
- int pagesize = Integer.parseInt((rows == null || rows == "0") ? "10": rows);
-
- List list = this.sessionFactory.getCurrentSession().createQuery("from Student where flag=‘true‘ order by studentid")
- .setFirstResult((currentpage - 1) * pagesize).setMaxResults(pagesize).list();
-
-
- return list;
- }
-
-
- public int getStudentTotal() throws Exception {
- return this.sessionFactory.getCurrentSession().find("from Student where flag=‘true‘").size();
- }
-
-
- public void saveStudent(Student student) throws Exception {
- student.setFlag("true");
- this.sessionFactory.getCurrentSession().save(student);
-
- }
-
-
- public String queryBy_unique(String table,String field ,String parameter) throws Exception {
- System.out.println("===============验证唯一性=========");
- String s="select * from "+table +" t where t."+field+"=‘"+parameter+"‘";
- System.out.println("SQL语句:"+s);
- Query query = this.sessionFactory.getCurrentSession().createSQLQuery(s);
-
- int n=query.list().size();
- if(n==0)
- {
- return "1";
- }
- return "0";
- }
-
-
- public void deleteStudent(String ids) throws Exception {
- log.info("删除学生信息");
- Student student=this.queryStudent(ids);
- student.setFlag("false");
- this.sessionFactory.getCurrentSession().update(student);
- }
-
-
- public Student queryStudent(String id) throws Exception{
- log.info("根据学生主键查询学生信息");
- Criteria criteria=this.sessionFactory.getCurrentSession().createCriteria(Student.class);
- criteria.add(Restrictions.eq("studentid", id));
- Student student=(Student) criteria.list().get(0);
- return student;
-
- }
-
- public SessionFactory getSessionFactory() {
- return sessionFactory;
- }
-
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
-
-
-
-
- }
5、对应的JSP页面index.jsp
- <%@ page language="java" pageEncoding="utf-8" isELIgnored="false"%>
- <%
- String path = request.getContextPath();
- %>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Easyui</title>
-
- <!-- 引入Jquery -->
- <script type="text/javascript" src="<%=path%>/js/easyui/jquery-1.8.0.min.js" charset="utf-8"></script>
- <!-- 引入Jquery_easyui -->
- <script type="text/javascript" src="<%=path%>/js/easyui/jquery.easyui.min.js" charset="utf-8"></script>
- <!-- 引入easyUi国际化--中文 -->
- <script type="text/javascript" src="<%=path%>/js/easyui/locale/easyui-lang-zh_CN.js" charset="utf-8"></script>
- <!-- 引入easyUi默认的CSS格式--蓝色 -->
- <link rel="stylesheet" type="text/css" href="<%=path%>/js/easyui/themes/default/easyui.css" />
- <!-- 引入easyUi小图标 -->
- <link rel="stylesheet" type="text/css" href="<%=path%>/js/easyui/themes/icon.css" />
-
- <!-- 引入对应的JS,切记一定要放在Jquery.js和Jquery_Easyui.js后面,因为里面需要调用他们,建议放在最后面 -->
- <script type="text/javascript" src="<%=path%>/index.js" charset="utf-8"></script>
-
- </head>
- <body>
- <h2>
- <b>easyui的DataGrid实例</b>
- </h2>
-
- <table id="mydatagrid">
- <thead>
- <tr>
- <th data-options="field:‘studentid‘,width:100,align:‘center‘">学生学号</th>
- <th data-options="field:‘name‘,width:100,align:‘center‘">姓名</th>
- <th data-options="field:‘gender‘,width:100,align:‘center‘">性别</th>
- <th data-options="field:‘age‘,width:100,align:‘center‘">年龄</th>
- </tr>
- </thead>
- </table>
- <!-- 显示添加按钮的Div -->
- <div id="easyui_toolbar" style="padding: 2px 0px 2px 15px; height: auto">
- <a href="#" id="easyui_add" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加学生信息</a>
- <a href="#" id="deltable" class="easyui-linkbutton" iconCls="icon-remove" plain="true">批量删除</a>
- </div>
-
- <!-- 添加学生信息的表单 -->
- <div id="addDlg" class="easyui-dialog" style="width: 580px; height: 350px; padding: 10px 20px" closed="true" buttons="#addDlgBtn">
- <form id="addForm" method="post">
- <table>
- <tr>
- <td>学生主键</td>
- <td>
- <input name="student.studentid" id="studentid" class="easyui-validatebox" required="true" missingMessage="学生主键不能为空">
- </td>
- <td>
- <!-- 存放提示重复信息的div -->
- <div id="xianshi1" style="float: left"></div>
- <div style="float: left"> </div>
- <div id="xianshi2" style="font-size: 14px; color: #FF0000; float: left"></div>
- </td>
- </tr>
- <tr>
- <td>姓名</td>
- <td>
- <input name="student.name" id="name" class="easyui-validatebox" required="true" missingMessage="姓名不能为空">
- </td>
- </tr>
- <tr>
- <td>性别</td>
- <td>
- <!-- 使用Easyui中的combobox -->
- <select class="easyui-combobox" style="width: 155px;" name="student.gender" id="gender" data-options="panelHeight:‘auto‘">
- <option value="男">男</option>
- <option value="女">女</option>
- </select>
- </td>
- </tr>
- <tr>
- <td>年龄</td>
- <td>
- <input name="student.age" id="age" class="easyui-validatebox">
- </td>
- </tr>
- </table>
- </form>
- </div>
- <!-- 保存学生信息的按钮,被Jquery设置,当没被调用的时候不显示 -->
- <div id="addDlgBtn">
- <a href="#" id="addSaveBooktimecode" class="easyui-linkbutton" iconCls="icon-ok" onclick="add_ok()">确认</a>
- <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$(‘#addDlg‘).dialog(‘close‘)">取消</a>
- </div>
-
- </body>
- </html>
6、对应的JavaScript页面。index.js
- var isClickOk=true;
- $(function() {
-
- $(‘#mydatagrid‘).datagrid({
- title : ‘datagrid实例‘,
- iconCls : ‘icon-ok‘,
- width : 600,
- pageSize : 5,
- pageList : [ 5, 10, 15, 20 ],
- nowrap : true,
- striped : true,
- collapsible : true,
- toolbar:"#easyui_toolbar",
- url:‘studentallInfo.action‘,
- loadMsg : ‘数据装载中......‘,
-
- fitColumns:true,
- sortName : ‘studentid‘,
- sortOrder : ‘asc‘,
- remoteSort : false,
- frozenColumns : [ [ {
- field : ‘ck‘,
- checkbox : true
- } ] ],
- pagination : true,
- rownumbers : true
- });
-
-
- $("#easyui_add").click(function() {
- $("#xianshi1").empty();
- $("#xianshi2").empty();
- $(‘#addDlg‘).dialog(‘open‘).dialog(‘setTitle‘, ‘添加学生信息‘);
- $(‘#addForm‘).form(‘clear‘);
- });
-
-
- $("#studentid").blur(function(){
- jQuery.ajax({
- type : "post",
- url : "studentverify.action?table=Student&field=studentid¶meter="+$(‘#studentid‘).val(),
- dataType:‘json‘,
- success : function(s){
- if($(‘#studentid‘).val()==""){
-
- }
- else if( s == "1" )
- { isClickOk=true;
- $("#xianshi1").empty();
- var txt1="<img src="+"‘imgs/agree_ok.gif‘"+"/>";
- $("#xianshi1").append(txt1);
- $("#xianshi2").empty();
- $("#xianshi2").append("未被使用");
- }
- else
- {
- $("#xianshi1").empty();
- isClickOk=false;
- var txt1="<img src="+"‘imgs/agree_no.gif‘"+"/>"
- $("#xianshi1").append(txt1);
- $("#xianshi2").empty();
- $("#xianshi2").append("已被使用");
- }
- }
- });
- });
-
-
- $(‘#deltable‘).click(function(){
- var array = $(‘#mydatagrid‘).datagrid(‘getSelections‘);
- var id2="";
- var num=array.length;
- for(var i=0; i<array.length; i++){
- if(i!=array.length-1){
- id2=id2+array[i].studentid+",";
- }else{
- id2=id2+array[i].studentid;
- }
- }
- var selected = $(‘#mydatagrid‘).datagrid(‘getSelected‘);
- if (array != "") {
- $.messager.defaults={ok:"确定",cancel:"取消"};
- $.messager.confirm(‘‘, ‘是否要删除该信息?‘, function(r){
- if (r){
- $.post("studentdel.action",
- { ids:id2,num:num},function(response){
- if(response=="-1"){
- $.messager.alert(‘操作提示‘,"删除失败",‘error‘);
- }else{
- $(‘#mydatagrid‘).datagrid({url:"studentallInfo.action"});
- $.messager.alert(‘操作提示‘,"删除成功",‘info‘);
- }
- });
- }
- });
- }else{
- $.messager.alert(‘‘,"请先选择要删除的信息!");
- }
-
- });
-
-
- });
-
- function add_ok(){
- $.messager.defaults={ok:"确定",cancel:"取消"};
- $.messager.confirm(‘Confirm‘, ‘您确定增加?‘, function(r){
- if (r){
- $(‘#addForm‘).form(‘submit‘,{
- url:"studentadd.action",
- onSubmit: function(){
- if(isClickOk==false){
- $.messager.alert(‘操作提示‘, ‘主键不能重复!‘,‘error‘);
- return false;
- }
- else if($(‘#addForm‘).form(‘validate‘)){
- $.messager.alert(‘操作提示‘, ‘添加信息成功!‘,‘info‘);
- return true;
- }else{
- $.messager.alert(‘操作提示‘, ‘信息填写不完整!‘,‘error‘);
- return false;
- }
- }
- });
- $(‘#mydatagrid‘).datagrid({url:‘studentallInfo.action‘});
- $(‘#addDlg‘).dialog(‘close‘);
- }
- });
- }
实例:SSH结合Easyui实现Datagrid的批量删除功能
原文:http://www.cnblogs.com/henuyuxiang/p/4282984.html