学生管理系统
简介
练习:完成学生信息的增删查改(根据id),项目进行了三层架构进行创建。
pojo层,dao层,service层,utils层,程序入口;
1.pojo层
实体层 数据库在项目中的类
Entity层是实体层,也就是所谓的model,也称为pojo层,是数据库在项目中的类,该文件包含实体类的属性和对应属性的set、get方法;
pojo内的student类,主要是与数据库中相应的表进行对应,字段类型需一一对应。
package student.pojo;
/**
* @description: TODO
* @date 2023/7/15 9:14
*/
public class Student {
private int id;
private String name;
private String className;
private String score;
public Student() {
}
public Student(int id, String name, String className, String score) {
this.id = id;
this.name = name;
this.className = className;
this.score = score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
@Override
public String toString() {
return
"学号:" + id +"\t" +
" 姓名:" + name + '\t' +
"班级:" + className + '\t' +
"成绩:" + score + '\t' ;
}
}
2.dao层
持久层 主要与数据库进行交互
DAO层=mapper层,现在用Mybatis逆向工程生成的mapper层,其实就是dao层。DAO层会调用entity层,DAO中会定义实际使用到的方法,比如增删改查。DAO 层的数据源和数据库连接的参数都是在配置文件中进行配置的,配置文件一般在同层的XML文件夹中。数据持久化操作就是指,把数据放到持久化的介质中,同时提供增删改查操作。
package student.dao;
import student.pojo.Student;
public interface StudentDao {
/**
* 根据id查询信息
* @param id
*/
Student findById(int id);
/**
* 新增学生信息
* @param student
* @return
*/
void addStudent(Student student);
/**
* 根据id删除信息
* @param id
*/
void deleteStudent(int id);
/**
* 更新学生信息
* @param student
*/
void updateStudent(Student student);
}
实现类impl
dao接口中方法的具体实现
package student.dao.imp;
import student.dao.StudentDao;
import student.pojo.Student;
import student.utils.OperationUtils;
/**
* @description: TODO
* @date 2023/7/15 10:33
*/
public class StudentDaoImp implements StudentDao {
@Override
public Student findById(int id) {
Student student = OperationUtils.findById(id);
return student;
}
@Override
public void addStudent(Student student) {
OperationUtils.addStudent(student);
}
@Override
public void deleteStudent(int id) {
OperationUtils.deleteStudent(id);
}
@Override
public void updateStudent(Student student) {
OperationUtils.updateStudent(student);
}
}
3.service层
业务层 控制业务
Service层主要负责业务模块的逻辑应用设计。先设计放接口的类,再创建实现的类,然后在配置文件中进行配置其实现的关联。service层调用dao层接口,接收dao层返回的数据,完成项目的基本功能设计。
封装Service层的业务逻辑有利于业务逻辑的独立性和重复利用性。
public interface StudentService {
Student findById(int id);
void addStudent(Student student);
void deleteStudent(int id);
void updateStudent(Student student);
}
package student.service.imp;
import student.dao.StudentDao;
import student.dao.imp.StudentDaoImp;
import student.pojo.Student;
import student.service.StudentService;
/**
* @description: TODO
* @date 2023/7/15 10:44
*/
public class StudentServiceImp implements StudentService {
StudentDao studentDaoImp = new StudentDaoImp();
@Override
public Student findById(int id) {
Student student = studentDaoImp.findById(id);
return student;
}
@Override
public void addStudent(Student student) {
studentDaoImp.addStudent(student);
}
@Override
public void deleteStudent(int id) {
studentDaoImp.deleteStudent(id);
}
@Override
public void updateStudent(Student student) {
studentDaoImp.updateStudent(student);
}
}
4.utils工具类
完成数据库连接,具体的增删查改方法等,提高代码的复用率。
4.1数据库连接工具
这里使用jbdc,properties进行数据库连接(具体jdbc知识之前的博客有)
driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/student_db?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
username = root
password = 123456
这里将关闭和连接分成两个方法,方便调用释放资源
package student.utils;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
/**
* @description: TODO
* @date 2023/7/14 20:07
*/
public class JdbcUtils {
private static String url;
private static String driver;
private static String username;
private static String password;
static {
Properties properties = new Properties();
try {
properties.load(new FileInputStream("src\\db.properties"));
url = properties.getProperty("url");
driver = properties.getProperty("driver");
username = properties.getProperty("username");
password = properties.getProperty("password");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 建立数据库连接
* @return
*/
public static Connection getConnection() {
try {
return DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 关闭数据库连接,释放资源
*/
public static void close(Statement statement, ResultSet resultSet,Connection connection){
try {
if (statement != null){
statement.close();
}
if (resultSet != null){
resultSet.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
4.2 OperationUtils具体的增删查改方法
package student.utils;
import student.pojo.Student;
import java.sql.*;
/**
* @description: TODO
* @date 2023/7/15 9:13
* 数据库的增删查改
*/
public class OperationUtils {
private Student student = new Student();
/**
* 查询
* @param id
* @return 根据id查找学生信息
*/
public static Student findById(int id){
Student student = null;
try {
Connection connection = JdbcUtils.getConnection();
String sql = "select * from student where id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, id);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
id = resultSet.getInt(1);
String name = resultSet.getString(2);
String classname = resultSet.getString(3);
String score = resultSet.getString(4);
student = new Student(id, name, classname, score);
//JdbcUtils.close(statement,resultSet,connection);
}
} catch (SQLException e) {
e.printStackTrace();
}
return student;
}
/**
* 添加信息
* @return
*/
public static void addStudent(Student student){
Connection connection = JdbcUtils.getConnection();
String sql = "insert into student values(?,?,?,?)";
try {
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, student.getId());
statement.setString(2, student.getName());
statement.setString(3, student.getClassName());
statement.setString(4, student.getScore());
int i = statement.executeUpdate();
if (i>0){
System.out.println("数据添加成功!");
}
JdbcUtils.close(statement,null,connection);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 根据id删除学生信息
* @param id
*/
public static void deleteStudent(int id){
Connection connection = JdbcUtils.getConnection();
String sql = "delete from student where id = ?";
try {
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,id);
int i = statement.executeUpdate();
if (i>0){
System.out.println("删除成功!");
}else {
System.out.println("删除失败");
}
JdbcUtils.close(statement, null, connection);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 更新
* @param student
*/
public static void updateStudent(Student student){
Connection connection = JdbcUtils.getConnection();
String sql = "update student set name = ?,className = ?,score =? where id = ?";
try {
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, student.getName());
statement.setString(2, student.getClassName());
statement.setString(3, student.getScore());
statement.setInt(4,student.getId());
int i = statement.executeUpdate();
if (i>0) {
System.out.println("更新成功");
}
JdbcUtils.close(statement,null,connection);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
4.3 Menu主菜单
switch语句进行选择
package student.utils;
import student.dao.StudentDao;
import student.dao.imp.*;
import student.pojo.Student;
import java.util.Scanner;
/**
* @description: TODO
* @date 2023/7/15 10:49
*/
@SuppressWarnings("all")
public class Menu {
private static Scanner scanner = new Scanner(System.in);
public static void menuStudent() {
while (true) {
OperationMenu.start();
int i = scanner.nextInt();
switch (i) {
case 1:
OperationMenu.find();
break;
case 2:
OperationMenu.update();
break;
case 3:
OperationMenu.add();
break;
case 4:
OperationMenu.delete();
break;
case 5:
System.exit(0);
}
}
}
}
4.4 OperationMenu每一个case里的具体操作
package student.utils;
import student.dao.StudentDao;
import student.dao.imp.StudentDaoImp;
import student.pojo.Student;
import student.service.StudentService;
import student.service.imp.StudentServiceImp;
import java.util.Scanner;
/**
* @description: TODO
* @date 2023/7/15 14:28
*/
public class OperationMenu {
private static StudentDao studentDaoImp = new StudentDaoImp();
private static StudentService studentService = new StudentServiceImp();
private static Scanner scanner = new Scanner(System.in);
public static void find(){
System.out.print("输入要查询学生的学号:");
int id = scanner.nextInt();
Student student = studentService.findById(id);
System.out.println(student);
}
public static void update() {
System.out.println("请输入要修改的信息");
System.out.println("请输入要修改学生的学号:");
int updateid = scanner.nextInt();
System.out.println("请输入修改后的姓名:");
String name = scanner.next();
System.out.println("请输入修改后的班级:");
String classname = scanner.next();
System.out.println("请输入要修改的成绩:");
String score = scanner.next();
Student student = new Student();
student.setId(updateid);
student.setName(name);
student.setScore(score);
student.setClassName(classname);
studentService.updateStudent(student);
}
public static void add(){
System.out.println("请输入要增加的学生信息:");
System.out.println("输入学生学号:");
int addid = scanner.nextInt();
System.out.println("输入学生姓名:");
String addname = scanner.next();
System.out.println("输入学生班级:");
String addclass = scanner.next();
System.out.println("输入学生成绩:");
String addscore = scanner.next();
Student newstudent = new Student(addid,addname,addclass,addscore);
studentService.addStudent(newstudent);
}
public static void delete(){
System.out.println("请输入要删除的学生学号");
int delid = scanner.nextInt();
studentService.deleteStudent(delid);
}
public static void start(){
System.out.println("=============欢迎登录学生管理系统=============");
System.out.println("1.查询学生信息");
System.out.println("2.修改学生信息");
System.out.println("3.增加学生信息");
System.out.println("4.删除学生信息");
System.out.println("5.退出");
System.out.println("选择功能:");
}
}
4.5 AppMain程序入口
public class AppMain {
public static void main(String[] args) {
Menu.menuStudent();
}
}