超级简单的教师学生管理系统的后台
需要:mysql数据库、tomcat、一堆杂七杂八的jar包、最主要的是CRUD的方法封装类 and Beanutil的封装类。
操作:建个数据库,然后先写一个模块框架,直接cv该模块按需求重写OR添加方法
学生模块
先自己建个简单的数据库,建议使用Navicat,毕竟这个草鸡简单的系统不需要太复杂的数据库,所以不存在数据库设计阶段,直接上手建个satms库,然后里面给学生表(简单粗暴)
(Navicat可以可视化建表,非常适合我们这些菜鸡)
1. 先做个数据库,把学生表建了
这里学生模块需要的字段:sno、sname、ssex、spassword、spic、sarea、sbirthday
emmmm…后面想到啥再加,问题不大
就先这样吧。。。然后自己去加点数据就行
2. 写学生的实体
由于是选的学生做的第一个模块搭建框架,所以还是要按规范老老实实的建包在包下放java类
学生的实体就按数据库字段封装就行了,还是注意命名规范,方便后面cv出其他模块就直接叫 Student.java
先把数据库的字段名都写下来(注意类型),建议和数据库的字段名称一样,最好都小写,不然后面容易写错名称。。。
package entity;
import java.sql.Timestamp;
public class Student {
private Integer sno;
private String sname;
private String ssex;
private String spassword;
private String spic;
private String sarea;
private Timestamp sbirthday;
}
然后直接快捷键生成geter、seter、方法重写…balabala
最后的效果就这:
Student.java
package entity;
import java.sql.Timestamp;
public class Student {
private Integer sno;
private String sname;
private String ssex;
private String spassword;
private String spic;
private String sarea;
private Timestamp sbirthday;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(Integer sno, String sname, String ssex, String spassword, String spic, String sarea,
Timestamp sbirthday) {
super();
this.sno = sno;
this.sname = sname;
this.ssex = ssex;
this.spassword = spassword;
this.spic = spic;
this.sarea = sarea;
this.sbirthday = sbirthday;
}
public Integer getSno() {
return sno;
}
public void setSno(Integer sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSsex() {
return ssex;
}
public void setSsex(String ssex) {
this.ssex = ssex;
}
public String getSpassword() {
return spassword;
}
public void setSpassword(String spassword) {
this.spassword = spassword;
}
public String getSpic() {
return spic;
}
public void setSpic(String spic) {
this.spic = spic;
}
public String getSarea() {
return sarea;
}
public void setSarea(String sarea) {
this.sarea = sarea;
}
public Timestamp getSbirthday() {
return sbirthday;
}
public void setSbirthday(Timestamp sbirthday) {
this.sbirthday = sbirthday;
}
@Override
public String toString() {
return "Student [sno=" + sno + ", sname=" + sname + ", ssex=" + ssex + ", spassword=" + spassword + ", spic="
+ spic + ", sarea=" + sarea + ", sbirthday=" + sbirthday + "]";
}
}
3. 写学生的Dao及其实现DaoImpl
和之前一样的,先建包:Dao、Dao.Impl
然后在这两个包下面分别建StudentDao类 and StudentDaoImpl类
StudentDao.java里需要写的就是声明要用到的CRUD方法,比如:查询全部、分页查询全部、根据主键查询、修改、增加…
反正你想加什么都可以,毕竟:可以,都可以,但得先给钱
注意StudentDao.java是一个接口,所以是public interface StudentDao{...}
- 先写全查,因为要拿的全部学生数据是一个集合,所以是
List <Student>
public List<Student> getAll();
后面写具体实现的时候肯定要抛出异常的,到时候编译器自己报错然后自动添加就行
StudentDao.java
package Dao;
import java.util.List;
import entity.Student;
import page.PageInfo;
public interface StudentDao {
/**
* 查询全部学生信息
* @return
*/
public List<Student> getAll();
/**
* 分页查询全部学生信息
* @param pageInfo
* @return
*/
public List<Student> getAllByPage(PageInfo pageInfo);
/**
* 按主键分页查询
* @param pageInfo
* @param cno
* @return
*/
public List<Student> getAllByIdAndPage(PageInfo pageInfo,String cno);
/**
* 按主键查询
* @param sno
* @return
*/
public Student getAllById(String sno);
/**
* 增加
* @param student
* @return
*/
public Integer add(Student student);
/**
* 更新
* @param student
* @return
*/
public Integer update(Student student);
}
StudentDaoImpl.java
直接 public class StudentDaoImpl implements StudentDao{...}
然后 add unimplements methods 自动添加方法,然后重写方法实现
package Dao.Impl;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import Dao.StudentDao;
import druid.JdbcUtils;
import entity.Student;
import page.PageInfo;
public class StudentDaoImpl implements StudentDao{
@Override
public List<Student> getAll() throws SQLException {
// TODO Auto-generated method stub
return JdbcUtils.queryListEntity("select * from student", Student.class);
}
@Override
public List<Student> getAllByPage(PageInfo pageInfo) throws SQLException {
// TODO Auto-generated method stub
int offset =pageInfo.getOfferset();
int pageSize = pageInfo.getPageSize();
int totalRecords = getCount();//算出总记录数 在这里保存
pageInfo.setTotalRecords(totalRecords);
return JdbcUtils.queryListEntity("select * from student limit ?,?", Student.class,new Object[]{offset,pageSize});
}
private int getCount() throws SQLException {
// TODO Auto-generated method stub
return JdbcUtils.findAllForInt("select count(*) from student");
}
@Override
public List<Student> getAllByIdAndPage(PageInfo pageInfo, String cno) throws SQLException {
// TODO Auto-generated method stub
int offset =pageInfo.getOfferset();
int pageSize = pageInfo.getPageSize();
int totalRecords = getCount();//算出总记录数 在这里保存
pageInfo.setTotalRecords(totalRecords);
return JdbcUtils.queryListEntity("select * from student where cno = ? limit ?,?", Student.class,new Object[]{cno,offset,pageSize});
}
@Override
public Student getAllById(String sno) throws SQLException {
// TODO Auto-generated method stub
return JdbcUtils.queryEntity("select * from student where sno = ?", Student.class, sno);
}
@Override
public Integer add(Student student) throws IllegalArgumentException, IllegalAccessException, SQLException {
// TODO Auto-generated method stub
Map<String, Object[]> map = JdbcUtils.insertEntity(student);
String sql = map.keySet().iterator().next();
Object[] params = map.get(sql);
return JdbcUtils.executeUpdate(sql, params);
}
@Override
public Integer update(Student student) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, SQLException {
// TODO Auto-generated method stub
Map<String, Object[]> map = JdbcUtils.updateEntity(student);
String sql = map.keySet().iterator().next();
Object[] params = map.get(sql);
return JdbcUtils.executeUpdate(sql, params);
}
}
4. 写学生的service及其实现serviceImpl
一样的,建包建类,service是interface
直接把dao里的cv过去就行
StudentService.java
package service;
import java.sql.SQLException;
import java.util.List;
import entity.Student;
import page.PageInfo;
public interface StudentService {
/**
* 查询全部学生信息
* @return
* @throws SQLException
*/
public List<Student> getAll() throws SQLException;
/**
* 分页查询全部学生信息
* @param pageInfo
* @return
* @throws SQLException
*/
public List<Student> getAllByPage(PageInfo pageInfo) throws SQLException;
/**
* 按主键分页查询
* @param pageInfo
* @param cno
* @return
* @throws SQLException
*/
public List<Student> getAllByIdAndPage(PageInfo pageInfo,String cno) throws SQLException;
/**
* 按主键查询
* @param sno
* @return
* @throws SQLException
*/
public Student getAllById(String sno) throws SQLException;
/**
* 增加
* @param student
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public Integer add(Student student) throws IllegalArgumentException, IllegalAccessException, SQLException;
/**
* 更新
* @param student
* @return
* @throws SQLException
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public Integer update(Student student) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, SQLException;
}
StudentServiceImpl.java
package service.impl;
import java.sql.SQLException;
import java.util.List;
import Dao.StudentDao;
import Dao.Impl.StudentDaoImpl;
import entity.Student;
import page.PageInfo;
import service.StudentService;
public class StudentServiceImpl implements StudentService{
StudentDao studentDao = new StudentDaoImpl();
@Override
public List<Student> getAll() throws SQLException {
// TODO Auto-generated method stub
List<Student> students = studentDao.getAll();
return students;
}
@Override
public List<Student> getAllByPage(PageInfo pageInfo) throws SQLException {
// TODO Auto-generated method stub
return studentDao.getAllByPage(pageInfo);
}
@Override
public List<Student> getAllByIdAndPage(PageInfo pageInfo, String cno) throws SQLException {
// TODO Auto-generated method stub
return studentDao.getAllByIdAndPage(pageInfo, cno);
}
@Override
public Student getAllById(String sno) throws SQLException {
// TODO Auto-generated method stub
return studentDao.getAllById(sno);
}
@Override
public Integer add(Student student) throws IllegalArgumentException, IllegalAccessException, SQLException {
// TODO Auto-generated method stub
return studentDao.add(student);
}
@Override
public Integer update(Student student) throws IllegalArgumentException, IllegalAccessException,
NoSuchFieldException, SecurityException, SQLException {
// TODO Auto-generated method stub
return studentDao.update(student);
}
}
5. 写学生的controller
package controller;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import core.ReturnObject;
import core.annotation.Controller;
import core.annotation.RequestMapping;
import entity.Student;
import page.PageInfo;
import service.StudentService;
import service.impl.StudentServiceImpl;
import utils.BeanUtils;
@Controller
@RequestMapping("/controller/student")
public class StudentController {
StudentService studentService = new StudentServiceImpl();
public ReturnObject getAll(HttpServletRequest request,HttpServletResponse response) throws SQLException {
System.out.println("查询全部学生:");
List<Student> students = studentService.getAll();
ReturnObject returnObject = new ReturnObject("students",students,false,null,"/back/student/list.jsp");
return returnObject;
}
@RequestMapping("/student")
public ReturnObject getAllById(HttpServletRequest request,HttpServletResponse response) throws SQLException {
System.out.println("按主键查询学生");
String sno = request.getParameter("sno");
String op = request.getParameter("op");
Boolean isAjax = Boolean.parseBoolean(Optional.ofNullable(request.getParameter("isAjax")).orElse("false"));
Student student = studentService.getAllById(sno);
ReturnObject returnObject = null;
op = Optional.ofNullable(op).orElse("");
if(op.equals("info")) {
returnObject = new ReturnObject("student",student,isAjax,null,"/front/student/info.jsp");
}else if(op.equals("update")){
returnObject = new ReturnObject("student",student,false,null,"/back/student/update.jsp");
}else{
request.setAttribute("reqOrRes", "res");
returnObject = new ReturnObject("msg","非法操作",false,null,"/error.jsp");
}
return returnObject;
}
@RequestMapping("/students/page") // restful 路径以名词形式命名 students
public ReturnObject page(HttpServletRequest request, HttpServletResponse response) throws SQLException {
System.out.println("分页查询全部学生");
//如果用户没有提供当前页的第一条编号,可以默认为0
Integer offset = Integer.parseInt(Optional.ofNullable(request.getParameter("offset")).orElse("0"));
Integer pageSize = Integer.parseInt(Optional.ofNullable(request.getParameter("pageSize")).orElse("3"));
//如果用户没有提供 isAjax这个参数 默认非ajax请求 要跳转页面显示数据
Boolean isAjax = Boolean.parseBoolean(Optional.ofNullable(
request.getParameter("isAjax")).orElse("false"));
PageInfo pageInfo = new PageInfo(offset,pageSize);
List<Student> students = studentService.getAllByPage(pageInfo);
ReturnObject returnObject = new ReturnObject("students",students,isAjax,null,"/back/student/list.jsp");
request.setAttribute("pageInfo", pageInfo);
return returnObject;
}
@RequestMapping("/studentsByCno/page") // restful 路径以名词形式命名 students
public ReturnObject getAllByIdAndPage(HttpServletRequest request, HttpServletResponse response) throws SQLException {
System.out.println("分页查询全部学生");
//如果用户没有提供当前页的第一条编号,可以默认为0
Integer offset = Integer.parseInt(Optional.ofNullable(request.getParameter("offset")).orElse("0"));
Integer pageSize = Integer.parseInt(Optional.ofNullable(request.getParameter("pageSize")).orElse("3"));
PageInfo pageInfo = new PageInfo(offset,pageSize);
String cno = Optional.ofNullable(request.getParameter("cno")).orElse("090101");
List<Student> students = studentService.getAllByIdAndPage(pageInfo,cno);
ReturnObject returnObject = new ReturnObject("students",students,false,null,"/back/student/list.jsp");
request.setAttribute("pageInfo", pageInfo);
return returnObject;
}
@RequestMapping("/student/post")
public ReturnObject add(HttpServletRequest request, HttpServletResponse response) throws IllegalArgumentException, IllegalAccessException, SQLException {
System.out.println("增加学生");
Student student = new Student();
BeanUtils.copyProperties(student, request.getParameterMap());
//如果用户没有提供当前页的第一条编号,可以默认为0
String msg = studentService.add(student)>0?"增加成功":"增加失败";
//为什么跳转的url 是分页查询的url,名 而不是 list.jsp 因为由查询再跳转到 list.jsp 能否获得最新的数据 显示
ReturnObject returnObject = new ReturnObject("msg",msg,false,null,"/controller/student/students/page");
return returnObject;
}
@RequestMapping("/student/put")
public ReturnObject update(HttpServletRequest request, HttpServletResponse response) throws IllegalArgumentException, IllegalAccessException, SQLException, NoSuchFieldException, SecurityException {
System.out.println("修改学生");
Student student = new Student();
BeanUtils.copyProperties(student, request.getParameterMap());
//如果用户没有提供当前页的第一条编号,可以默认为0
String msg = studentService.update(student)>0?"修改成功":"修改失败";
//为什么跳转的url 是分页查询的url,名 而不是 list.jsp 因为由查询再跳转到 list.jsp 能否获得最新的数据 显示
ReturnObject returnObject = new ReturnObject("msg",msg,false,null,"/controller/student/students/page");
return returnObject;
}
}
6. 完善jsp里的的杂七杂八
完善了jsp里的东西后就可以运行差错,然后挨着改了…
最后完成的效果图:
(删除还没加。。。懒得改了)