源文件:https://download.csdn.net/download/flyingshadower/10628472
package com.student.sys.entity;
import javax.persistence.*;
@Entity
@Table(name = "student", schema = "sm", catalog = "")
public class StudentEntity {
private int studentId;
private String studentName;
private String studentNumber;
private String studentScore;
@Id
@Column(name = "student_id")
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
@Basic
@Column(name = "student_name")
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
@Basic
@Column(name = "student_number")
public String getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}
@Basic
@Column(name = "student_score")
public String getStudentScore() {
return studentScore;
}
public void setStudentScore(String studentScore) {
this.studentScore = studentScore;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
StudentEntity that = (StudentEntity) o;
if (studentId != that.studentId) return false;
if (studentName != null ? !studentName.equals(that.studentName) : that.studentName != null) return false;
if (studentNumber != null ? !studentNumber.equals(that.studentNumber) : that.studentNumber != null)
return false;
if (studentScore != null ? !studentScore.equals(that.studentScore) : that.studentScore != null) return false;
return true;
}
@Override
public int hashCode() {
int result = studentId;
result = 31 * result + (studentName != null ? studentName.hashCode() : 0);
result = 31 * result + (studentNumber != null ? studentNumber.hashCode() : 0);
result = 31 * result + (studentScore != null ? studentScore.hashCode() : 0);
return result;
}
}
package com.student.sys.dao;
import com.student.sys.entity.StudentEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.List;
@Repository
public class StudentDao {
@Resource
private SessionFactory sessionFactory;
private Session getSession() {
return sessionFactory.getCurrentSession();
}
/*
* 查询所有
* */
@SuppressWarnings("unchecked")
public List getStudentEntity() {
return this.getSession().createCriteria(StudentEntity.class).list();
}
/*
* 根据ID查询
* */
public StudentEntity getStudentEntityById(int id) {
return (StudentEntity) this.getSession().createQuery("from StudentEntity where id=?")
.setParameter(0, id).uniqueResult();
}
/*
* 添加
* */
public void addStudent(StudentEntity studentEntity) {
try {
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(studentEntity);
session.getTransaction().commit();
session.close();
}catch (Exception e)
{
e.printStackTrace();
}
}
/*
*更新
* */
public void updateStudent(StudentEntity studentEntity) {
try {
Session session = sessionFactory.openSession();
session.beginTransaction();
session.update(studentEntity);
session.getTransaction().commit();
session.close();
}catch (Exception e)
{
e.printStackTrace();
}
}
/*
* 删除
* */
public void deleteStudent(StudentEntity studentEntity) {
//this.getSession().createQuery("delete StudentEntity where id=?")
//.setParameter(0, id).executeUpdate();
try {
Session session = sessionFactory.openSession();
session.beginTransaction();
session.delete(studentEntity);
session.getTransaction().commit();
session.close();
}catch (Exception e)
{
e.printStackTrace();
}
}
}
package com.student.sys.service;
import com.student.sys.dao.StudentDao;
import com.student.sys.entity.StudentEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentService {
@Autowired
private StudentDao studentDao;
/*
* 查询所有
* */
public List getStudentEntity() {
return studentDao.getStudentEntity();
}
/*
* 根据ID查询
* */
public StudentEntity getStudentById(int id) {
return studentDao.getStudentEntityById(id);
}
/*
* 添加
* */
public void addStudent(StudentEntity studentEntity) {
studentDao.addStudent(studentEntity);
}
/*
* 更新
* */
public void updateStudent(StudentEntity studentEntity) {
studentDao.updateStudent(studentEntity);
}
/*
* 删除
* */
public void deleteStudent(StudentEntity studentEntity) {
studentDao.deleteStudent(studentEntity);
}
}
package com.student.sys.controller;
import com.student.sys.entity.StudentEntity;
import com.student.sys.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("student")
public class StudentController {
@Autowired
private StudentService studentService;
/*
* 跳转登录界面
* */
@RequestMapping("toLogin")
public String toLogin() {
return "login";
}
/*
* 登录请求
* */
@RequestMapping(value = "login", method = RequestMethod.POST)
public String login(HttpServletRequest httpServletRequest) {
String userId = httpServletRequest.getParameter("userId");
String password = httpServletRequest.getParameter("userPassword");
if (userId.equals("admin") && password.equals("1234")) {
return "index";
} else {
return "fail";
}
}
/*
* 转向首页
* */
@RequestMapping("toIndex")
public String toindex() {
return "index";
}
/*
* 查看所有人员信息
* */
@RequestMapping(value = "main")
public String main(Model model) {
model.addAttribute("studentList",studentService.getStudentEntity());
// Map map: map.put("studentList", studentService.getStudentEntity());
return "main";
}
/*
* 跳转到添加页面
* */
@RequestMapping(value = "addStudent")
public String saveStudent() {
return "addPage";
}
/*
* 保存添加的数据
* */
@RequestMapping(value = "saveStudent", method = RequestMethod.POST)
public String saveStudent(HttpServletRequest httpServletRequest) {
try {
String name = httpServletRequest.getParameter("name");
String number = httpServletRequest.getParameter("number");
String score = httpServletRequest.getParameter("score");
StudentEntity studentEntity = new StudentEntity();
if(name==null||number==null||score==null){
studentEntity.setStudentName(name);
studentEntity.setStudentNumber(number);
studentEntity.setStudentScore(score);
studentService.addStudent(studentEntity);
}
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:/student/main";
}
/*
* 获得修改学生信息,并跳转到编辑界面
* */
@RequestMapping("editStudent")
public String toEditStudent(Model model,HttpServletRequest request) {
String id=request.getParameter("id");
int id1=Integer.parseInt(id);
StudentEntity studentEntity=studentService.getStudentById(id1);
model.addAttribute("studentEntity",studentEntity);
return "editPage";
}
@RequestMapping(value = "updateStudent",method = RequestMethod.POST)
public String updateStudent(HttpServletRequest httpServletRequest){
String id=httpServletRequest.getParameter("id");
int id1=Integer.parseInt(id);
String name = httpServletRequest.getParameter("name");
String number = httpServletRequest.getParameter("number");
String score = httpServletRequest.getParameter("score");
StudentEntity studentEntity=studentService.getStudentById(id1);
studentEntity.setStudentName(name);
studentEntity.setStudentNumber(number);
studentEntity.setStudentScore(score);
studentService.updateStudent(studentEntity);
return "redirect:/student/main";
}
/*
* 删除一条数据
* */
@RequestMapping(value = "deleteStudent")
public String deleteStudent(HttpServletRequest request) {
String id = request.getParameter("id");
int id1 = Integer.parseInt(id);
StudentEntity studentEntity = studentService.getStudentById(id1);
studentService.deleteStudent(studentEntity);
return "redirect:/student/main";
}
/*
* 更新数据
* */
@RequestMapping(value = "updateStudent")
public String updatePrompt(StudentEntity studentEntity) {
System.out.println(studentEntity.toString());
studentService.updateStudent(studentEntity);
return "main";
}
}