基于SSM實(shí)現(xiàn)學(xué)生管理系統(tǒng)
本文實(shí)例為大家分享了SSM實(shí)現(xiàn)學(xué)生管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
概述
基于Spring + Spring MVC 的學(xué)生管理系統(tǒng),使用Maven進(jìn)行包管理。
主要代碼
@RequestMapping("/student")
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
@Autowired
private ClazzService clazzService;
/**
* 學(xué)生列表頁(yè)
* @param model
* @return
*/
@RequestMapping(value="/list",method=RequestMethod.GET)
public ModelAndView list(ModelAndView model){
model.setViewName("student/student_list");
List<Clazz> clazzList = clazzService.findAll();
model.addObject("clazzList",clazzList );
model.addObject("clazzListJson",JSONArray.fromObject(clazzList));
return model;
}
/**
* 獲取學(xué)生列表
* @param name
* @param page
* @return
*/
@RequestMapping(value="/get_list",method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> getList(
@RequestParam(value="name",required=false,defaultValue="") String name,
@RequestParam(value="clazzId",required=false) Long clazzId,
HttpServletRequest request,
Page page
){
Map<String, Object> ret = new HashMap<String, Object>();
Map<String, Object> queryMap = new HashMap<String, Object>();
queryMap.put("username", "%"+name+"%");
Object attribute = request.getSession().getAttribute("userType");
if("2".equals(attribute.toString())){
//說明是學(xué)生
Student loginedStudent = (Student)request.getSession().getAttribute("user");
queryMap.put("username", "%"+loginedStudent.getUsername()+"%");
}
if(clazzId != null){
queryMap.put("clazzId", clazzId);
}
queryMap.put("offset", page.getOffset());
queryMap.put("pageSize", page.getRows());
ret.put("rows", studentService.findList(queryMap));
ret.put("total", studentService.getTotal(queryMap));
return ret;
}
/**
* 編輯學(xué)生信息
* @param clazz
* @return
*/
@RequestMapping(value="/edit",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> edit(Student student){
Map<String, String> ret = new HashMap<String, String>();
if(StringUtils.isEmpty(student.getUsername())){
ret.put("type", "error");
ret.put("msg", "學(xué)生姓名不能為空!");
return ret;
}
if(StringUtils.isEmpty(student.getPassword())){
ret.put("type", "error");
ret.put("msg", "學(xué)生登錄密碼不能為空!");
return ret;
}
if(student.getClazzId() == null){
ret.put("type", "error");
ret.put("msg", "請(qǐng)選擇所屬班級(jí)!");
return ret;
}
if(isExist(student.getUsername(), student.getId())){
ret.put("type", "error");
ret.put("msg", "該姓名已存在!");
return ret;
}
student.setSn(StringUtil.generateSn("S", ""));
if(studentService.edit(student) <= 0){
ret.put("type", "error");
ret.put("msg", "學(xué)生添加失??!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "學(xué)生修改成功!");
return ret;
}
/**
* 添加學(xué)生信息
* @param student
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> add(Student student){
Map<String, String> ret = new HashMap<String, String>();
if(StringUtils.isEmpty(student.getUsername())){
ret.put("type", "error");
ret.put("msg", "學(xué)生姓名不能為空!");
return ret;
}
if(StringUtils.isEmpty(student.getPassword())){
ret.put("type", "error");
ret.put("msg", "學(xué)生登錄密碼不能為空!");
return ret;
}
if(student.getClazzId() == null){
ret.put("type", "error");
ret.put("msg", "請(qǐng)選擇所屬班級(jí)!");
return ret;
}
if(isExist(student.getUsername(), null)){
ret.put("type", "error");
ret.put("msg", "該姓名已存在!");
return ret;
}
student.setSn(StringUtil.generateSn("S", ""));
if(studentService.add(student) <= 0){
ret.put("type", "error");
ret.put("msg", "學(xué)生添加失??!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "學(xué)生添加成功!");
return ret;
}
/**
* 刪除學(xué)生信息
* @param ids
* @return
*/
@RequestMapping(value="/delete",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> delete(
@RequestParam(value="ids[]",required=true) Long[] ids
){
Map<String, String> ret = new HashMap<String, String>();
if(ids == null || ids.length == 0){
ret.put("type", "error");
ret.put("msg", "請(qǐng)選擇要?jiǎng)h除的數(shù)據(jù)!");
return ret;
}
try {
if(studentService.delete(StringUtil.joinString(Arrays.asList(ids), ",")) <= 0){
ret.put("type", "error");
ret.put("msg", "刪除失??!");
return ret;
}
} catch (Exception e) {
// TODO: handle exception
ret.put("type", "error");
ret.put("msg", "該學(xué)生下存在其他信息,請(qǐng)勿沖動(dòng)!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "學(xué)生刪除成功!");
return ret;
}
/**
* 上傳用戶頭像圖片
* @param photo
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping(value="/upload_photo",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> uploadPhoto(MultipartFile photo,
HttpServletRequest request,
HttpServletResponse response
) throws IOException{
Map<String, String> ret = new HashMap<String, String>();
if(photo == null){
//文件沒有選擇
ret.put("type", "error");
ret.put("msg", "請(qǐng)選擇文件!");
return ret;
}
if(photo.getSize() > 10485760){
//文件沒有選擇
ret.put("type", "error");
ret.put("msg", "文件大小超過10M,請(qǐng)上傳小于10M的圖片!");
return ret;
}
String suffix = photo.getOriginalFilename().substring(photo.getOriginalFilename().lastIndexOf(".") + 1,photo.getOriginalFilename().length());
if(!"jpg,png,gif,jpeg".contains(suffix.toLowerCase())){
ret.put("type", "error");
ret.put("msg", "文件格式不正確,請(qǐng)上傳jpg,png,gif,jpeg格式的圖片!");
return ret;
}
String savePath = request.getServletContext().getRealPath("/") + "\\upload\\";
System.out.println(savePath);
File savePathFile = new File(savePath);
if(!savePathFile.exists()){
savePathFile.mkdir();//如果不存在,則創(chuàng)建一個(gè)文件夾upload
}
//把文件轉(zhuǎn)存到這個(gè)文件夾下
String filename = new Date().getTime() + "." + suffix;
photo.transferTo(new File(savePath + filename ));
ret.put("type", "success");
ret.put("msg", "圖片上傳成功!");
ret.put("src", request.getServletContext().getContextPath() + "/upload/" + filename);
return ret;
}
private boolean isExist(String username,Long id){
Student student = studentService.findByUserName(username);
if(student != null){
if(id == null){
return true;
}
if(student.getId().longValue() != id.longValue()){
return true;
}
}
return false;
}
}
運(yùn)行配置
1、首先安裝Mysql5.7,設(shè)置用戶名為root,密碼為root,并保證其在運(yùn)行狀態(tài),并執(zhí)行sql文件導(dǎo)入數(shù)據(jù)。
2、然后再配置Maven到環(huán)境變量中,在源代碼目錄下運(yùn)行
3、使用瀏覽器訪問http://localhost:8080即可進(jìn)入系統(tǒng)。
功能展示
1. 首頁(yè)登陸

2.2 管理

2.3 管理

2.4 管理

2.5 管理

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java+Mysql學(xué)生管理系統(tǒng)源碼
- java學(xué)生管理系統(tǒng)界面簡(jiǎn)單實(shí)現(xiàn)(全)
- 簡(jiǎn)單實(shí)現(xiàn)Android學(xué)生管理系統(tǒng)(附源碼)
- 簡(jiǎn)單實(shí)現(xiàn)Java版學(xué)生管理系統(tǒng)
- Java基于MySQL實(shí)現(xiàn)學(xué)生管理系統(tǒng)
- Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)
- java設(shè)計(jì)簡(jiǎn)單學(xué)生管理系統(tǒng)
- JDBC+GUI實(shí)現(xiàn)簡(jiǎn)單學(xué)生管理系統(tǒng)
- java實(shí)現(xiàn)學(xué)生管理系統(tǒng)(面向?qū)ο?
- JDBC實(shí)現(xiàn)學(xué)生管理系統(tǒng)
相關(guān)文章
java 中使用maven shade plugin 打可執(zhí)行Jar包
這篇文章主要介紹了java 中使用maven shade plugin 打可執(zhí)行Jar包的相關(guān)資料,需要的朋友可以參考下2017-05-05
springBoot集成Elasticsearch 報(bào)錯(cuò) Health check failed的解決
這篇文章主要介紹了springBoot集成Elasticsearch 報(bào)錯(cuò) Health check failed的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
一文詳解如何在Java?Maven項(xiàng)目中使用JUnit?5進(jìn)行測(cè)試
這篇文章主要介紹了如何在Java?Maven項(xiàng)目中使用JUnit?5進(jìn)行測(cè)試的相關(guān)資料,JUnit5是一個(gè)流行的Java測(cè)試框架,它涵蓋了JUnit5的概述、環(huán)境配置、編寫測(cè)試用例、運(yùn)行測(cè)試、高級(jí)特性和最佳實(shí)踐,需要的朋友可以參考下2025-04-04
JetBrains IntelliJ IDEA 2020安裝與使用教程詳解
這篇文章主要介紹了JetBrains IntelliJ IDEA 2020安裝與使用教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06

