spring框架实现一个学生管理系统
生活随笔
收集整理的這篇文章主要介紹了
spring框架实现一个学生管理系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 使用工具
- 使用jar包
- 環境搭建
- 代碼示例
- java目錄
- Student.java
- StudentDaoImpl.java
- IStudentDao.java
- Main.java
- StudentServiceImpl.java
- IStudentService.java
- TesrSpring.java
- resources目錄
- applicationContext.xml 配置
- 數據庫
- item.sql
- 效果展示
- 界面
- 添加功能
- 查詢功能
- 查詢全部功能
- 刪除功能
- 更改功能
使用工具
IDEA2018.2 MySQL5.6 JDK1.8 Tomcat8.5.33
使用jar包
commons-logging-1.2.jar
log4j-1.2.17.jar
log4j-api-2.3.jar
log4j-core-2.3.jar
mysql-connector-java-5.1.8.jar
spring-aop-4.3.6.RELEASE.jar
spring-aspects-4.3.6.RELEASE.jar
spring-beans-4.3.6.RELEASE.jar
spring-context-4.3.6.RELEASE.jar
spring-core-4.3.6.RELEASE.jar
spring-expression-4.3.6.RELEASE.jar
spring-jdbc-4.3.6.RELEASE.jar
spring-tx-4.3.6.RELEASE.jar
jar下載微云鏈接:https://share.weiyun.com/5UWqHhY
環境搭建
代碼示例
java目錄
Student.java
package com.zr.bean;import org.springframework.stereotype.Component;/*** @Author VVcat* @Date 2019/10/11 10:11**/@Component public class Student {//屬性的封裝private int id;private String name;private String sex;private int age;//get and setpublic 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 getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Student( String name, String sex, int age,int id) {this.name = name;this.sex = sex;this.age = age;this.id = id;}public Student(String name, String sex, int age) {this.name = name;this.sex = sex;this.age = age;}public Student() {}//toString@Overridepublic String toString() {return "Student{" +"id=" + id +", name='" + name + '\'' +", sex='" + sex + '\'' +", age=" + age +'}';} }StudentDaoImpl.java
package com.zr.dao.impl;import com.zr.bean.Student; import com.zr.dao.IStudentDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository;import java.util.List;/*** @Author VVcat* @Date 2019/10/11 12:13**/ @Repository public class StudentDaoImpl implements IStudentDao {@Autowiredprivate JdbcTemplate jdbcTemplate;private boolean flag = false;//注入方法public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}@Overridepublic boolean add(Student student) {String sql = "insert into tb_student values(null,?,?,?)";int row = jdbcTemplate.update(sql, student.getName(), student.getSex(), student.getAge());if (row>0){flag = true;}return flag;}@Overridepublic boolean delStu(int stuId) {String sql = "delete from tb_student where id =?";int row = jdbcTemplate.update(sql, stuId);if (row>0){flag=true;}return flag;}@Overridepublic boolean updStu(Student student) {String sql = "update tb_student set name = ?, sex = ?,age = ? where id = ? ";int row = jdbcTemplate.update(sql, student.getName(), student.getSex(), student.getAge(), student.getId());if (row>0){flag=true;}return flag;}@Overridepublic Student findById(int stuId) {String sql = "select * from tb_student where id = ?";BeanPropertyRowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class);Student student = null;try {student = jdbcTemplate.queryForObject(sql, new Object[]{stuId}, rowMapper);}catch (Exception e){student = null;}return student;}@Overridepublic List<Student> findAll() {String sql = "select * from tb_student";BeanPropertyRowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class);List<Student> studentList = jdbcTemplate.query(sql, rowMapper);return studentList;} }IStudentDao.java
package com.zr.dao;import com.zr.bean.Student;import java.util.List;public interface IStudentDao {//增加boolean add(Student student);//刪除boolean delStu(int stuId);//修改boolean updStu(Student student);//單查詢Student findById(int stuId);//全查List<Student> findAll(); }Main.java
package com.zr.main;import com.zr.bean.Student; import com.zr.service.impl.StudentServiceImpl; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.List; import java.util.Scanner;/*** @Author VVcat* @Date 2019/10/11 16:29**/ public class Main {//加載配置文件applicationContext.xmlstatic ApplicationContext ac = new ClassPathXmlApplicationContext("/applicationContext.xml");public static void main(String[] args) {boolean flag=true;while (flag){System.out.println("學生管理系統");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("6.退出系統");System.out.println("----------------------------");Scanner input = new Scanner(System.in);System.out.print("請選擇功能:");boolean flag2 = true;int i = input.nextInt();switch (i){case 1:while (flag2){StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");Student student = new Student();System.out.print("請輸入學生姓名:");String name = input.next();System.out.print("請輸入學生性別:");String sex = input.next();System.out.print("請輸入學生年齡:");int age = input.nextInt();student.setName(name);student.setSex(sex);student.setAge(age);boolean b = ssi.add(student);if (b){System.out.println("添加成功");System.out.println("---------------------");System.out.println("1.退出添加");System.out.println("2.繼續添加");System.out.println("請輸入您的選擇");int i1 = input.nextInt();flag2 = ssi.isNext(i1, flag2);}else {System.out.println("添加失敗");System.out.println("---------------------");System.out.println("1.退出添加");System.out.println("2.繼續添加");System.out.println("請輸入您的選擇");int i1 = input.nextInt();flag2 = ssi.isNext(i1, flag2);}}break;case 2:while (flag2){StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");System.out.print("請輸入要刪除學生的ID:");int id = input.nextInt();boolean b = ssi.delStu(id);if (b){System.out.println("刪除成功");System.out.println("---------------------");System.out.println("1.退出刪除");System.out.println("2.繼續刪除");System.out.println("請輸入您的選擇");int i1 = input.nextInt();flag2 = ssi.isNext(i1, flag2);}else {System.out.println("刪除失敗,沒有該用戶");System.out.println("---------------------");System.out.println("1.退出刪除");System.out.println("2.繼續刪除");System.out.println("請輸入您的選擇");int i1 = input.nextInt();flag2 = ssi.isNext(i1, flag2);}}break;case 3:while (flag2){StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");Student student = new Student();System.out.print("請輸入要更改學生的ID:");int id = input.nextInt();Student studentById = ssi.findById(id);if (studentById != null){boolean gengai = true;while (gengai){System.out.println("此用戶的信息為:");System.out.println("ID:"+studentById.getId());System.out.println("name:"+studentById.getName());System.out.println("sex:"+studentById.getSex());System.out.println("age:"+studentById.getAge());System.out.println("-------------------------");System.out.println("是否更改該用戶");System.out.println("1.更改");System.out.println("2.換一個更改");System.out.println("3.退出更改");int j = input.nextInt();switch (j){case 1:System.out.print("請輸入學生姓名:");String name = input.next();System.out.print("請輸入學生性別:");String sex = input.next();System.out.print("請輸入學生年齡:");int age = input.nextInt();student.setId(id);student.setName(name);student.setSex(sex);student.setAge(age);boolean b = ssi.updStu(student);if (b){System.out.println("更改成功");System.out.println("---------------------");System.out.println("1.退出更改");System.out.println("2.繼續更改");System.out.println("請輸入您的選擇");int i1 = input.nextInt();flag2 = ssi.isNext(i1, flag2);gengai = false;}else {System.out.println("更改失敗");System.out.println("---------------------");System.out.println("1.退出更改");System.out.println("2.繼續更改");System.out.println("請輸入您的選擇");int i1 = input.nextInt();flag2 = ssi.isNext(i1, flag2);gengai = false;}break;case 2:flag2 = true;gengai = false;break;case 3:flag2 = false;gengai = false;break;default:gengai = true;break;}}} else {System.out.println("查無此人");System.out.println("---------------------");System.out.println("1.退出更改");System.out.println("2.繼續更改");System.out.println("請輸入您的選擇");int i1 = input.nextInt();flag2 = ssi.isNext(i1, flag2);}}break;case 4:while (flag2){StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");System.out.print("請輸入要查詢學生的ID:");int id = input.nextInt();Student studentById = ssi.findById(id);if (studentById != null){System.out.println("此用戶的信息為:");System.out.println("ID:"+studentById.getId());System.out.println("name:"+studentById.getName());System.out.println("sex:"+studentById.getSex());System.out.println("age:"+studentById.getAge());System.out.println("-------------------------");System.out.println("1.退出查詢");System.out.println("2.繼續查詢");System.out.println("請輸入您的選擇");int i1 = input.nextInt();flag2 = ssi.isNext(i1, flag2);}else {System.out.println("查無此人");System.out.println("---------------------");System.out.println("1.退出查詢");System.out.println("2.繼續查詢");System.out.println("請輸入您的選擇");int i1 = input.nextInt();flag2 = ssi.isNext(i1, flag2);}}break;case 5:while (flag2){StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");List<Student> studentList = ssi.findAll();for (Student student : studentList) {System.out.print("ID:"+student.getId()+",");System.out.print("name:"+student.getName()+",");System.out.print("sex:"+student.getSex()+",");System.out.print("age:"+student.getAge()+",");System.out.println();}System.out.println("-------------------------");System.out.println("1.退出查詢");System.out.println("2.更新查詢");System.out.println("請輸入您的選擇");int i1 = input.nextInt();flag2 = ssi.isNext(i1, flag2);}break;case 6:flag=false;break;default:flag = true;break;}}System.out.println("歡迎下次光臨");} }StudentServiceImpl.java
package com.zr.service.impl;import com.zr.bean.Student; import com.zr.dao.IStudentDao; import com.zr.service.IStudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import java.util.List;/*** @Author VVcat* @Date 2019/10/11 11:21**/ @Service public class StudentServiceImpl implements IStudentService {@Autowiredprivate IStudentDao studentDao;@Overridepublic boolean add(Student student) {return studentDao.add(student);}@Overridepublic boolean delStu(int stuId) {return studentDao.delStu(stuId);}@Overridepublic boolean updStu(Student student) {return studentDao.updStu(student);}@Overridepublic Student findById(int stuId) {return studentDao.findById(stuId);}@Overridepublic List<Student> findAll() {return studentDao.findAll();}@Overridepublic boolean isNext(int i,boolean flag2) {boolean flag3 = true;while (flag3){switch (i){case 1:flag2 = false;flag3 = false;break;case 2:flag2 = true;flag3 = false;break;default:flag3 = true;flag2 = true;break;}}return flag2;} }IStudentService.java
package com.zr.service;import com.zr.bean.Student;import java.util.List;public interface IStudentService {//增加boolean add(Student student);//刪除boolean delStu(int stuId);//修改boolean updStu(Student student);//單查詢Student findById(int stuId);//全查List<Student> findAll();//判斷功能是否繼續boolean isNext(int i,boolean flag2); }TesrSpring.java
package com.zr.test;import com.zr.bean.Student; import com.zr.service.impl.StudentServiceImpl; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.List;/*** @Author VVcat* @Date 2019/10/11 15:12**/ public class TesrSpring {//加載配置文件applicationContext.xmlApplicationContext ac = new ClassPathXmlApplicationContext("/applicationContext.xml");@Testpublic void addStu(){//1.創建對象Student student = new Student("fantasy","男",5);//2.獲取自動創建的對象 -IOCStudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");boolean b = ssi.add(student);if (b){System.out.println("學生添加成功");}else {System.out.println("學生添加失敗");}}@Testpublic void findById(){StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");Student student = ssi.findById(1);System.out.println("單次查詢到的學生信息為"+student.toString());}@Testpublic void findAll(){StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");List<Student> studentList = ssi.findAll();for (Student student:studentList){System.out.println("獲取到的每一個對象是:\n"+student);}} }resources目錄
applicationContext.xml 配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 1.提供注解支持 --><context:component-scan base-package="com.zr"/><!-- 2.配置數據源 --><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/item?useSSL=true&characterEncoding=utf-8"/><property name="username" value="root"/><property name="password" value="123456"/></bean><!-- 3.創建JDBCTemplate對象,注入dataSource --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"/></bean></beans>數據庫
item.sql
CREATE DATABASE item; USE `item`; CREATE TABLE `tb_student` (`id` INT(11) NOT NULL AUTO_INCREMENT,`name` VARCHAR(50) NULL,`sex` VARCHAR(50) NULL,age INT(10) NULL,PRIMARY KEY (`id`) ) ENGINE=INNODB AUTO_INCREMENT=1 CHARSET=utf8;效果展示
界面
添加功能
查詢功能
查詢全部功能
刪除功能
更改功能
總結
以上是生活随笔為你收集整理的spring框架实现一个学生管理系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot和FreeMarke
- 下一篇: Mybatis框架实现简单的学生管理系统