目录



使用工具

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​

环境搭建

spring框架实现一个学生管理系统_spring
spring框架实现一个学生管理系统_java_02
spring框架实现一个学生管理系统_spring_03

代码示例

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 set
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 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
@Override
public 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 {
@Autowired
private JdbcTemplate jdbcTemplate;
private boolean flag = false;

//注入方法
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}


@Override
public 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;
}

@Override
public 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;
}

@Override
public 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;
}

@Override
public 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;
}

@Override
public 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.xml
static 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 {

@Autowired
private IStudentDao studentDao;

@Override
public boolean add(Student student) {
return studentDao.add(student);
}

@Override
public boolean delStu(int stuId) {
return studentDao.delStu(stuId);
}

@Override
public boolean updStu(Student student) {
return studentDao.updStu(student);
}

@Override
public Student findById(int stuId) {
return studentDao.findById(stuId);
}

@Override
public List<Student> findAll() {
return studentDao.findAll();
}

@Override
public 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.xml
ApplicationContext ac = new ClassPathXmlApplicationContext("/applicationContext.xml");

@Test
public void addStu(){
//1.创建对象
Student student = new Student("fantasy","男",5);
//2.获取自动创建的对象 -IOC
StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");
boolean b = ssi.add(student);
if (b){
System.out.println("学生添加成功");
}else {
System.out.println("学生添加失败");
}
}

@Test
public void findById(){
StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");
Student student = ssi.findById(1);
System.out.println("单次查询到的学生信息为"+student.toString());
}

@Test
public 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框架实现一个学生管理系统_spring_04

添加功能

spring框架实现一个学生管理系统_java_05

查询功能

spring框架实现一个学生管理系统_xml_06

查询全部功能

spring框架实现一个学生管理系统_java_07

删除功能

spring框架实现一个学生管理系统_java_08

更改功能

spring框架实现一个学生管理系统_xml_09