首先看一下学生管理系统的体系(我这个系统做的比较简单) 主要应用到了 MVC模式 JDBC 前端用到了 HTML5 CSS JS等
后台数据处理主要分为:controller层 repository层 和service层 下面将每一层的代码进行展示->>>
首先在entity包内进行类的创建 这个系统只实现了对 学生的 CRUD(增删改查) 所以只需要创建一个学生类(Student):
package com.xcl.entity;
public class Student {
private Integer id;
private String name;
private String password;
private Double money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
public Student(Integer id, String name, String password, Double money) {
this.id = id;
this.name = name;
this.password = password;
this.money = money;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", money=" + money +
'}';
}
}
学生类写完之后 由于这个系统简单不需要进行业务逻辑处理 所以我选择去写 service层
首先写一个接口 在接口中定义 CRUD 的方法:
package com.xcl.service;
import com.xcl.entity.Student;
import java.util.List;
public interface StudentService {
public List<Student> findAll();
public void deleteById(Integer id);
public void update(Integer id,String name,String password,Double money);
public Student findById(Integer id);
public void add(String name,String password,Double money);
}
再写一个接口的实现类 实现接口中的 CRUD 方法:
package com.xcl.service.impl;
import com.xcl.entity.Student;
import com.xcl.repository.StudentRepository;
import com.xcl.repository.impl.StudentRepositoryImpl;
import com.xcl.service.StudentService;
import java.util.List;
public class StudentServiceImpl implements StudentService {
private StudentRepository studentRepository = new StudentRepositoryImpl();
@Override
public List<Student> findAll() {
return studentRepository.findAll();
}
@Override
public void deleteById(Integer id) {
studentRepository.deleteById(id);
}
@Override
public void update(Integer id, String name, String password, Double money) {
studentRepository.update(id, name, password, money);
}
@Override
public Student findById(Integer id) {
return studentRepository.findById(id);
}
@Override
public void add(String name, String password, Double money) {
studentRepository.add(name, password, money);
}
}
写到这里service层就算是写完了
下面我们进行 repository 和数据库操作方面的代码编写 这里会用到 JDBC 数据库连接 所以提前写一个 JDBC 连接数据库的工具类:
package com.xcl.Tools;
import java.sql.*;
public class JDBCTools {
public static Connection getConnection(){
Connection connection = null;
try {
//1.加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
String url = "jdbc:mysql://localhost:3306/user?serverTimezone=UTC";
String user = "root";
String password = "root";
connection = DriverManager.getConnection(url,user,password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void release(Connection connection, Statement statement, ResultSet resultSet){
try {
if(connection!=null){
connection.close();
}
if(statement!=null){
statement.close();
}
if(resultSet!=null){
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
下面是 repository 层代码的编写 还是老样子 先写一个接口 接口里面写好需要实现的 CRUD 方法:
package com.xcl.repository;
import com.xcl.entity.Student;
import java.util.List;
public interface StudentRepository {
public List<Student> findAll();
public void deleteById(Integer id);
public void update(Integer id,String name,String password,Double money);
public Student findById(Integer id);
public void add(String name,String password,Double money);
}
再写一个实现类 实现接口里面定义的方法:
package com.xcl.repository.impl;
import com.xcl.Tools.JDBCTools;
import com.xcl.entity.Student;
import com.xcl.repository.StudentRepository;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StudentRepositoryImpl implements StudentRepository {
/**
* 查询
*
* @return
*/
@Override
public List<Student> findAll() {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
List<Student> list = new ArrayList<>();
try {
connection = JDBCTools.getConnection();
String sql = "select * from stu";
statement = connection.prepareStatement(sql);
resultSet = statement.executeQuery();
Student student = null;
while (resultSet.next()) {
int id = resultSet.getInt(1);
String name = resultSet.getString(2);
String password = resultSet.getString(3);
Double money = resultSet.getDouble(4);
student = new Student(id, name, password, money);
list.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, statement, resultSet);
}
return list;
}
/**
*
* 删除
*
* @param id
*/
@Override
public void deleteById(Integer id) {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = JDBCTools.getConnection();
String sql = "delete from stu where id = ?";
statement = connection.prepareStatement(sql);
statement.setInt(1, id);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, statement, null);
}
}
/**
*
* 修改
*
* @param id
* @param name
* @param password
* @param money
*/
@Override
public void update(Integer id, String name, String password, Double money) {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = JDBCTools.getConnection();
String sql = "update stu set name =?,password=?,money=? where id=?";
statement = connection.prepareStatement(sql);
statement.setString(1, name);
statement.setString(2, password);
statement.setDouble(3, money);
statement.setInt(4, id);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, statement, null);
}
}
/**
* @param id
* @return
*/
@Override
public Student findById(Integer id) {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
Student student = null;
try {
connection = JDBCTools.getConnection();
String sql = "select * from stu where id=?";
statement = connection.prepareStatement(sql);
System.out.println(sql);
statement.setInt(1, id);
resultSet = statement.executeQuery();
if (resultSet.next()) {
id = resultSet.getInt(1);
String name = resultSet.getString(2);
String password = resultSet.getString(3);
Double money = resultSet.getDouble(4);
student = new Student(id, name, password, money);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, statement, resultSet);
}
return student;
}
/**
* 添加
*
*
* @param name
* @param password
* @param money
*/
@Override
public void add(String name, String password, Double money) {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = JDBCTools.getConnection();
String sql = "insert into stu (name,password,money) values (?,?,?)";
statement = connection.prepareStatement(sql);
statement.setString(1, name);
statement.setString(2, password);
statement.setDouble(3, money);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, statement, null);
}
}
}
接下来就是要编写前后端交互的代码 也就是 controller 层的编写
package com.xcl.controller;
import com.xcl.service.StudentService;
import com.xcl.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;
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
private StudentService studentService = new StudentServiceImpl();
private final static String USERNAME = "admin";
private final static String PASSWORD = "123";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String method = req.getParameter("method");
if (method == null) method = "findAll";
switch (method) {
case "findAll":
req.setAttribute("list", studentService.findAll());
req.getRequestDispatcher("index.jsp").forward(req, resp);
break;
case "deleteById":
String idStr = req.getParameter("id");
Integer id = Integer.parseInt(idStr);
studentService.deleteById(id);
resp.sendRedirect("/student");
break;
case "findById":
idStr = req.getParameter("id");
id = Integer.parseInt(idStr);
req.setAttribute("student", studentService.findById(id));
req.getRequestDispatcher("update.jsp").forward(req, resp);
break;
case "add":
String name = req.getParameter("name");
String password = req.getParameter("password");
String money1 = req.getParameter("money");
Double money = Double.parseDouble(money1);
studentService.add( name, password, money);
resp.sendRedirect("/student");
break;
case "update":
idStr = req.getParameter("id");
id = Integer.parseInt(idStr);
name = req.getParameter("name");
password = req.getParameter("password");
money1 = req.getParameter("money");
money = Double.parseDouble(money1);
studentService.update(id, name, password, money);
resp.sendRedirect("/student");
break;
case "login":
String username = req.getParameter("username");
password = req.getParameter("password");
if (username.equals(USERNAME) && password.equals(PASSWORD)) {
req.getSession().setAttribute("name", username);
resp.sendRedirect("/student");
} else {
req.getRequestDispatcher("index.html").forward(req, resp);
}
break;
case "logout":
req.getSession().invalidate();
req.getRequestDispatcher("index.html").forward(req, resp);
break;
}
}
}