• layui实现(学生)数据的增删改查


    创建的数据库/表:

    在这里插入图片描述
    在这里插入图片描述

    运行效果展示:

    查询

    在这里插入图片描述

    添加:

    点击添加按钮:
    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述
    添加成功!
    在这里插入图片描述

    修改:

    点击编辑按钮:
    在这里插入图片描述
    修改信息数据,点击提交:
    在这里插入图片描述
    修改数据成功!
    在这里插入图片描述

    删除:

    点击删除按钮:
    在这里插入图片描述
    点击确定,删除成功!
    在这里插入图片描述

    分页:

    在这里插入图片描述

    由于数据有限,选择5条/页,进行分页:
    在这里插入图片描述
    在这里插入图片描述

    批量删除:

    首先勾选想要删除的行,点击批量删除:
    在这里插入图片描述
    删除成功:
    在这里插入图片描述

    控制台输出:

    在这里插入图片描述

    在这里插入图片描述

    操作后的数据库/表:

    在这里插入图片描述

    项目代码整体布局:

    在这里插入图片描述
    在这里插入图片描述

    代码部分:

    创建数据库:

    #创建数据库
    create database 70806_db
    default character set utf8mb4 #设置字符集
    default collate utf8mb4_general_ci #设置排序规则 
    
    • 1
    • 2
    • 3
    • 4

    创建数据表:

    #创建班级表
    create table classInfo
    (
    	classId int primary key auto_increment,
    	className varchar(20)
    );
    
    select * from classInfo;
    
    insert into classInfo
    (className)
    values
    ('AAA01'),
    ('AAA02');
    
    #创建学生表
    create table studentInfo
    (
    	studentId int primary key auto_increment,
    	name varchar(20) not null,
    	sex char(1) not null,
    	birthday date,
    	province varchar(20) default '河南',
    	classId int,
    	foreign key (classId)
    	references classInfo(classId)
    );
    
    select * from studentInfo;
    
    insert into studentInfo
    (name,sex,birthday,province,classId)
    values
    ('张三','男','2002-01-01','湖北',1),
    ('李四','女','2003-01-05','河南',2),
    ('王五','男','2010-03-01','湖北',1),
    ('赵六','男','2009-01-08','河南',2),
    ('孙琪','女','2001-09-01','湖北',1);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    util:

    BaseDAO:

    package com.util;
    
    import java.sql.*;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class BaseDAO {
    
    	//四大金刚
    	//驱动类
    	private static final String DRIVER="com.mysql.cj.jdbc.Driver";
    	//连接地址
    	private static final String URL="jdbc:mysql://localhost:3306/70806_db?useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
    	//用户名
    	private static final String USER="root";
    	//密码
    	private static final String PASSWORD="123456";
    
    
    
    	//获取连接
    	public static Connection getConnection(){
    
    		Connection con = null;
    
    		try{
    			//加载驱动类
    			Class.forName(DRIVER);
    			//获取连接
    			con = DriverManager.getConnection(URL,USER,PASSWORD);
    			
    		}catch(Exception ex){
    			ex.printStackTrace();
    		}
    
    		return con;
    	}
    
    	//关闭数据库对象
    	public static void closeAll(Connection con,Statement st,ResultSet rs){
    		
    		if(rs!=null){
    			try{
    				rs.close();
    			}catch(Exception ex){
    				ex.printStackTrace();
    			}
    			
    		}
    
    		if(st!=null){
    
    			try{
    				st.close();
    			}catch(Exception ex){
    				ex.printStackTrace();
    			}
    			
    		}
    
    		if(con!=null){
    			try{
    				con.close();
    			}catch(Exception ex){
    				ex.printStackTrace();
    			}
    			
    		}
    
    	}
    
    
    	//通用设置参数方法
    	public static void setParams(PreparedStatement pst,Object[] params){
    
    		if(params==null){
    			return;
    		}
    
    		for(int i=0;i> executeQuery(String sql,Object[] params) {
    
    		List> rows = new ArrayList<>();
    
    		Connection con = null;
    		PreparedStatement pst = null;
    		ResultSet rs = null;
    
    		try{
    			//获取连接	
    			con = getConnection();			
    			//获取命令对象
    			pst = con.prepareStatement(sql);
    			//设置参数
    			setParams(pst,params);
    			//执行查询
    			rs = pst.executeQuery();
    
    			//通过rs获取结果集的结构信息
    			ResultSetMetaData rsmd =  rs.getMetaData();
    			//获取结果集的列数
    			int colCount = rsmd.getColumnCount();
    
    			//遍历查询结果,并封装到List中
    			while(rs.next()){
    				//用Map存储当前行的各个列数据
    				Map map = new HashMap<>();
    				//循环获取每一列的信息
    				for(int i=1;i<=colCount;i++){
    					//获取列名(使用rsmd)
    					String colName = rsmd.getColumnLabel(i);
    					//获取列值(使用rs)
    					Object colVal = rs.getObject(i);
    					//将当前列存储到map中
    					map.put(colName,colVal);								
    				}
    				
    				//将遍历的当前行的数据存储到List中
    				rows.add(map);
    							
    			}
    
    
    		}catch(Exception ex){
    			ex.printStackTrace();
    		}finally{
    			closeAll(con,pst,rs);
    		}
    		
    		return rows;
    
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204

    entity:

    Student :

    package com.entity;
    
    import java.util.Date;
    
    public class Student {
        private Integer studentId;
        private String name;
        private String sex;
        private Date birthday;
        private String province;
        private Integer classId;
    
        public Student() {
        }
    
        public Student(Integer studentId, String name, String sex, Date birthday, String province, Integer classId) {
            this.studentId = studentId;
            this.name = name;
            this.sex = sex;
            this.birthday = birthday;
            this.province = province;
            this.classId = classId;
        }
    
        public Student(String name, String sex, Date birthday, String province, Integer classId) {
            this.name = name;
            this.sex = sex;
            this.birthday = birthday;
            this.province = province;
            this.classId = classId;
        }
    
        public Integer getStudentId() {
            return studentId;
        }
    
        public void setStudentId(Integer studentId) {
            this.studentId = studentId;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
        public Date getBirthday() {
            return birthday;
        }
    
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    
        public String getProvince() {
            return province;
        }
    
        public void setProvince(String province) {
            this.province = province;
        }
    
        public Integer getClassId() {
            return classId;
        }
    
        public void setClassId(Integer classId) {
            this.classId = classId;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "studentId=" + studentId +
                    ", name='" + name + '\'' +
                    ", sex='" + sex + '\'' +
                    ", birthday=" + birthday +
                    ", province='" + province + '\'' +
                    ", classId=" + classId +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93

    ClassInfo:

    package com.entity;
    
    public class ClassInfo {
        private Integer classId;
        private String className;
    
        public ClassInfo() {
        }
    
        public ClassInfo(String className) {
            this.className = className;
        }
    
        public ClassInfo(Integer classId, String className) {
            this.classId = classId;
            this.className = className;
        }
    
        public Integer getClassId() {
            return classId;
        }
    
        public void setClassId(Integer classId) {
            this.classId = classId;
        }
    
        public String getClassName() {
            return className;
        }
    
        public void setClassName(String className) {
            this.className = className;
        }
    
        @Override
        public String toString() {
            return "ClassInfo{" +
                    "classId=" + classId +
                    ", className='" + className + '\'' +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    PageData:

    package com.entity;
    
    public class PageData {
        //当前页码
        private Integer pageNo;
        //每页记录数
        private Integer pageSize;
        //总记录数
        private Integer totalCount;
        //总页数
        private Integer totalPage;
        //当前页记录
        private Object data;
    
        public PageData() {
        }
    
        public Integer getPageNo() {
            return pageNo;
        }
    
        public void setPageNo(Integer pageNo) {
            this.pageNo = pageNo;
        }
    
        public Integer getPageSize() {
            return pageSize;
        }
    
        public void setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
        }
    
        public Integer getTotalCount() {
            return totalCount;
        }
    
        public void setTotalCount(Integer totalCount) {
            this.totalCount = totalCount;
        }
    
        public Integer getTotalPage() {
            return totalPage;
        }
    
        public void setTotalPage(Integer totalPage) {
            this.totalPage = totalPage;
        }
    
        public Object getData() {
            return data;
        }
    
        public void setData(Object data) {
            this.data = data;
        }
    
        @Override
        public String toString() {
            return "PageData{" +
                    "pageNo=" + pageNo +
                    ", pageSize=" + pageSize +
                    ", totalCount=" + totalCount +
                    ", totalPage=" + totalPage +
                    ", data=" + data +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    CommonResult:

    package com.entity;
    
    public class CommonResult {
        //编码:0成功,1失败
        private Integer code;
        //提示消息
        private String msg;
        //记录数
        private Integer count;
        //记录数据
        private Object data;
    
        public CommonResult() {
        }
    
        public CommonResult(Integer code, String msg, Integer count, Object data) {
            this.code = code;
            this.msg = msg;
            this.count = count;
            this.data = data;
        }
    
        //返回查询结果的成功信息:
        public static CommonResult success(Integer count,Object data){
            return new CommonResult(0,"success",count,data);
        }
    
        //返回增删改的成功信息
        public static CommonResult success(){
            return new CommonResult(0,"success",null,null);
        }
    
        //返回失败信息
        public static CommonResult fail(){
            return new CommonResult(1,"fail",null,null);
        }
    
    
        public Integer getCode() {
            return code;
        }
    
        public void setCode(Integer code) {
            this.code = code;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    
        public Integer getCount() {
            return count;
        }
    
        public void setCount(Integer count) {
            this.count = count;
        }
    
        public Object getData() {
            return data;
        }
    
        public void setData(Object data) {
            this.data = data;
        }
    
        @Override
        public String toString() {
            return "CommonResult{" +
                    "code=" + code +
                    ", msg='" + msg + '\'' +
                    ", count=" + count +
                    ", data=" + data +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    dao:

    impl:

    StudentDAOImpl:
    package com.dao.impl;
    
    import com.dao.IStudentDAO;
    import com.entity.PageData;
    import com.entity.Student;
    import com.util.BaseDAO;
    
    import java.util.List;
    import java.util.Map;
    
    public class StudentDAOImpl implements IStudentDAO {
        @Override
        public int delete(String ids) {
            System.out.println("=======StudentDAOImpl delete===============");
            String sql = "delete from studentInfo where studentId in ("+ids +") ";
            return BaseDAO.executeUpdate(sql,null);
        }
    
        @Override
        public List> listAll() {
            System.out.println("=======StudentDAOImpl listAll===============");
            String sql = "select s.studentId,s.name,s.sex,s.birthday,s.province," +
                    "     c.classId,c.className " +
                    "     from studentInfo s " +
                    "     join classInfo c " +
                    "     on s.classId = c.classId ";
            System.out.println(sql);
            return BaseDAO.executeQuery(sql,null);
        }
    
        @Override
        public int insert(Student student) {
            String sql="insert into studentInfo "+
                    "   (name,sex,birthday,province,classId)"+
                    "   values"+
                    "   (?,?,?,?,?)";
            Object[] params={
                    student.getName(),
                    student.getSex(),
                    student.getBirthday(),
                    student.getProvince(),
                    student.getClassId()
            };
            return BaseDAO.executeUpdate(sql,params);
        }
    
        @Override
        public int update(Student student) {
            String sql = "update studentInfo" +
                    "     set name = ?," +
                    "         sex = ?," +
                    "         birthday = ?," +
                    "         province  = ?," +
                    "         classId = ? " +
                    "     where studentId = ? ";
            Object[] params = {
                    student.getName(),
                    student.getSex(),
                    student.getBirthday(),
                    student.getProvince(),
                    student.getClassId(),
                    student.getStudentId()
            };
            return BaseDAO.executeUpdate(sql,params);
        }
    
        @Override
        public PageData queryByPage(Integer pageNo, Integer pageSize) {
            //获取总记录数
            String totalSql = "select count(*) from studentInfo ";
            //总记录数
            Integer totalCount = BaseDAO.getTotal(totalSql,null);
            //总页数
            Integer totalPage = totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
    
            //分页查询
            String pageSql = "select s.studentId,s.name,s.sex,s.birthday,s.province," +
                    "     c.classId,c.className " +
                    "     from studentInfo s " +
                    "     join classInfo c " +
                    "     on s.classId = c.classId " +
                    "     order by s.studentId " +
                    "     limit ?,? ";
            int start = (pageNo-1)*pageSize;
            Object[] params = {start,pageSize};
            List> rows = BaseDAO.executeQuery(pageSql,params);
    
            //封装分页数据对象
            PageData pd = new PageData();
            pd.setPageNo(pageNo);
            pd.setPageSize(pageSize);
            pd.setTotalPage(totalPage);
            pd.setTotalCount(totalCount);
            pd.setData(rows);
    
            return pd;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    ClassInfoDAOImpl:
    package com.dao.impl;
    
    import com.dao.IClassInfoDAO;
    import com.util.BaseDAO;
    
    import java.util.List;
    import java.util.Map;
    
    public class ClassInfoDAOImpl implements IClassInfoDAO {
        @Override
        public List> listAll() {
            String sql="select classId,className from classInfo";
            return BaseDAO.executeQuery(sql,null);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    IStudentDAO:

    package com.dao;
    
    import com.entity.PageData;
    import com.entity.Student;
    
    import java.util.List;
    import java.util.Map;
    
    public interface IStudentDAO {
        List> listAll();
        //插入方法
        int insert(Student student);
        //修改
        int update(Student student);
    
        PageData queryByPage(Integer pageNo, Integer pageSize);
    
        //删除:1     2,3,4 delete from student where studentId in ()
        int delete(String ids);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    IClassInfoDAO:

    package com.dao;
    
    import java.util.List;
    import java.util.Map;
    
    public interface IClassInfoDAO {
        List> listAll();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    service:

    impl:

    StudentServiceImpl:
    package com.service.impl;
    
    import com.dao.IStudentDAO;
    import com.dao.impl.StudentDAOImpl;
    import com.entity.PageData;
    import com.entity.Student;
    import com.service.IStudentService;
    import com.util.BaseDAO;
    
    import java.util.List;
    import java.util.Map;
    
    public class StudentServiceImpl implements IStudentService {
        @Override
        public int delete(String ids) {
            return studentDAO.delete(ids);
        }
    
        private IStudentDAO studentDAO=new StudentDAOImpl();
    
        @Override
        public int update(Student student) {
            return studentDAO.update(student);
        }
    
        @Override
        public PageData queryByPage(Integer pageNo, Integer pageSize) {
            return studentDAO.queryByPage(pageNo,pageSize);
        }
    
        @Override
        public int add(Student student) {
            return studentDAO.insert(student);
        }
    
        @Override
        public List> listAll() {
            return studentDAO.listAll();
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    ClassInfoServiceImpl:
    package com.service.impl;
    
    import com.dao.IClassInfoDAO;
    import com.dao.impl.ClassInfoDAOImpl;
    import com.service.IClassInfoService;
    
    import java.util.List;
    import java.util.Map;
    
    public class ClassInfoServiceImpl implements IClassInfoService {
        private IClassInfoDAO classInfoDAO=new ClassInfoDAOImpl();
        @Override
        public List> listAll() {
            return classInfoDAO.listAll();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    IStudentService:

    package com.service;
    
    import com.entity.PageData;
    import com.entity.Student;
    
    import java.util.List;
    import java.util.Map;
    
    public interface IStudentService {
        List> listAll();
    
        //添加学生
        int add(Student student);
    
        int update(Student student);
    
        PageData queryByPage(Integer pageNo, Integer pageSize);
    
        //删除:单个删除和批量删除
        int delete(String ids);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    IClassInfoService:

    package com.service;
    
    import java.util.List;
    import java.util.Map;
    
    public interface IClassInfoService {
        List> listAll();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    servlet:

    StudentServlet:

    package com.servlet;
    
    import com.alibaba.fastjson.JSONObject;
    import com.entity.CommonResult;
    import com.entity.PageData;
    import com.entity.Student;
    import com.mysql.cj.util.StringUtils;
    import com.service.IStudentService;
    import com.service.impl.StudentServiceImpl;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    @WebServlet(urlPatterns = "/StudentServlet/*")
    public class StudentServlet extends HttpServlet {
        private IStudentService studentService = new StudentServiceImpl();
    
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //获取请求方法
            String uri = req.getRequestURI();
            String process = uri.substring(uri.lastIndexOf("/")+1);
    
            System.out.println("proccess:"+process);
    
    //        //设置响应编码
    //        resp.setContentType("text/html;charset=utf-8");
    
            //定义处理之后返回的数据对象
            Object data = null;
    
            switch (process){
                case "query":
                    data = this.query(req,resp);
                    break;
                case "queryByPage":
                    data = this.queryByPage(req,resp);
                    break;
                case "queryForLayUI":
                    data = this.queryForLayUI(req,resp);
                    break;
                case "add":
                    data=this.add(req,resp);
                    break;
                case "update":
                    data = this.update(req,resp);
                    break;
                case "del":
                    data = this.del(req,resp);
                    break;
            }
    
            System.out.println("data:"+data);
            //将数据转为json字符串
            String jsonStr = JSONObject.toJSONStringWithDateFormat(data,"yyyy-MM-dd");
            System.out.println(jsonStr);
            PrintWriter out = resp.getWriter(); //获取输出流对象
            out.println(jsonStr); //输出json字符串给浏览器
            out.flush(); //清空缓存
            out.close();//关闭流对象
        }
    
        //根据id删除
        private Object del(HttpServletRequest req, HttpServletResponse resp) {
            String ids = req.getParameter("ids");
            int count = studentService.delete(ids);
            if (count>0){
                return CommonResult.success();
            }else{
                return CommonResult.fail();
            }
        }
    
    
        private Object queryForLayUI(HttpServletRequest req, HttpServletResponse resp) {
            //获取前端传入的分页参数
            String pageNo = req.getParameter("page");
            String pageSize = req.getParameter("limit");
            //判断参数是否有效
            Integer pn = StringUtils.isNullOrEmpty(pageNo)?1:Integer.parseInt(pageNo);
            Integer ps = StringUtils.isNullOrEmpty(pageSize)?3:Integer.parseInt(pageSize);
    
            PageData pd =  studentService.queryByPage(pn,ps);
    
            return new CommonResult(0,"success",pd.getTotalCount(),pd.getData());
        }
    
        private Object queryByPage(HttpServletRequest req, HttpServletResponse resp) {
            //获取前端传入的分页参数
            String pageNo = req.getParameter("pageNo");
            String pageSize = req.getParameter("pageSize");
            //判断参数是否有效
            Integer pn = StringUtils.isNullOrEmpty(pageNo)?1:Integer.parseInt(pageNo);
            Integer ps = StringUtils.isNullOrEmpty(pageSize)?3:Integer.parseInt(pageSize);
    
            return studentService.queryByPage(pn,ps);
        }
    
        private Object update(HttpServletRequest req, HttpServletResponse resp) {
            System.out.println("111");
            //获取传入的数据
            Integer studentId = Integer.parseInt(req.getParameter("studentId"));
            String name = req.getParameter("name");
            String sex = req.getParameter("sex");
            Date birthday = java.sql.Date.valueOf(req.getParameter("birthday"));
            String province = req.getParameter("province");
            Integer classId = Integer.parseInt(req.getParameter("classId"));
    
            //封装实体对象
            Student student = new Student(
                    studentId,name,sex,birthday,province,classId
            );
    
            //调用业务层保存数据
            int count = studentService.update(student);
            System.out.println("count:"+count);
    
            //定义结果数据
            Map res = new HashMap<>();
            //添加成功
            if(count==1){
                //code为0说明处理成功
                res.put("code",0);
            }else{
                //code为1处理失败
                res.put("code",1);
            }
            System.out.println("res:"+res);
            return res;
        }
    
        //添加
        private Object add(HttpServletRequest req, HttpServletResponse resp) {
            //获取传入的数据
            String name=req.getParameter("name");
            String sex=req.getParameter("sex");
            Date birthday=java.sql.Date.valueOf(req.getParameter("birthday"));
            String province=req.getParameter("province");
            Integer classId=Integer.parseInt(req.getParameter("classId"));
    
            //封装实体对象
            Student student=new Student(
                    name,sex,birthday,province,classId
            );
    
            //调用业务层保存数据
            int count= studentService.add(student);
    
            //定义数据结果
            Map res=new HashMap<>();
            //添加成功
            if(count==1){
                //code为0说明处理成功
                res.put("code",0);
            }else{
                //code为1处理失败x`
                res.put("code",1);
            }
            return res;
    
        }
    
        /**
         * 执行查询
         * @param req
         * @param resp
         * @return
         */
        private Object query(HttpServletRequest req, HttpServletResponse resp) {
            System.out.println("=====StudentServlet query=======");
            return studentService.listAll();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182

    ClassInfoServlet:

    package com.servlet;
    
    import com.alibaba.fastjson.JSONObject;
    import com.entity.CommonResult;
    import com.service.IClassInfoService;
    import com.service.impl.ClassInfoServiceImpl;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.List;
    import java.util.Map;
    
    @WebServlet("/ClassInfoServlet/*")
    public class ClassInfoServlet extends HttpServlet {
        //业务层对象
        private IClassInfoService classInfoService=new ClassInfoServiceImpl();
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String uri=req.getRequestURI();
            String process=uri.substring(uri.lastIndexOf("/")+1);
            //定义结果对象
            Object date=null;
            switch (process){
                case "query":
                    date=query(req,resp);
                    break;
            }
    
            //输出json字符串
            String jsonStr= JSONObject.toJSONString(date);
            PrintWriter out=resp.getWriter();
            out.println(jsonStr);
            out.flush();
            out.close();
    
        }
    
        //查询
        private Object query(HttpServletRequest req, HttpServletResponse resp) {
            List> clsList = classInfoService.listAll();
            return CommonResult.success(clsList.size(), clsList);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    AjaxServlet :

    package com.servlet;
    
    import com.alibaba.fastjson.JSONArray;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    @WebServlet(urlPatterns = "/AjaxServlet")
    public class AjaxServlet extends HttpServlet {
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String method = req.getMethod();
            System.out.println("method:"+method);
            String name = req.getParameter("name");
            String sex = req.getParameter("sex");
            System.out.println(name+" "+sex);
    
            //String locaDateTime = LocalDateTime.now().toString();
            Map map1 = new HashMap();
            map1.put("id",1001);
            map1.put("name","张三");
            map1.put("sex","男");
    
            Map map2 = new HashMap<>();
            map2.put("id",1002);
            map2.put("name","李四");
            map2.put("sex","女");
    
            List> stuList = new ArrayList<>();
            stuList.add(map1);
            stuList.add(map2);
    
            //String jsonStr = "{\"name\":\"张三\",\"sex\":\"男\"}";
    
            //将java对象转为json字符串:jackson,Gson,fastjson
            System.out.println(stuList);
            //将数据转为json字符串
            String jsonStr =  JSONArray.toJSONString(stuList); //实体对象,map
            System.out.println(jsonStr);
    
    
            resp.setContentType("text/html;charset=utf-8");
            //获取输出流对象
            PrintWriter out = resp.getWriter();
            out.println(jsonStr);
            out.flush();
            out.close();
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    filter:

    CharacterEncodingFilter:

    package com.filter;
    
    import javax.servlet.*;
    import javax.servlet.annotation.WebFilter;
    import java.io.IOException;
    
    @WebFilter("/*")
    public class CharacterEncodingFilter implements Filter {
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            //设置编码
            servletRequest.setCharacterEncoding("utf-8");
            servletResponse.setContentType("text/html;charset=utf-8");
            //向后传递请求
            filterChain.doFilter(servletRequest,servletResponse);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    web:

    lay6.jsp:

    <%--
      Created by IntelliJ IDEA.
      User: 33154
      Date: 2022/8/9
      Time: 8:52
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
    
        
        
        开始使用 layui
        
        <%--    --%>
        
        
    
    
    
    
    
    <%----%>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
  • 相关阅读:
    基于Java+JSP+MySQL基于SSM的在线投票系统-计算机毕业设计
    移动web响应式布局
    ARM64 MMU 映射
    LeetCode二叉树系列——235.二叉搜索树的最近公共祖先
    利用云服务器搭配宝塔面板解禁网易云
    Mybatis、hibernate常用sql
    基于JSP网上书城的设计与实现
    python 深度学习 解决遇到的报错问题5
    第一章 作业【数据库原理】
    浮点数计算精度问题decimal.js
  • 原文地址:https://blog.csdn.net/Liu_wen_wen/article/details/126310329