首先打开IDEA,File—>New—>Project创建项目
选择左侧导航栏里的Maven,勾上勾,选择webapp
按如下图进行填写
创建完成后进入项目,右下角弹出的提示点击右边的Enable Auto-Import,自动配置
连接数据库,我用的是Mysql数据库,准备好有数据的数据库表
在pom.xml里导入所需jar包:
4.0.0
war
Student-ssm
com.accp
Student-ssm
1.0-SNAPSHOT
org.aspectj
aspectjweaver
1.8.8
org.springframework
spring-webmvc
4.3.12.RELEASE
org.springframework
spring-tx
4.3.12.RELEASE
org.springframework
spring-jdbc
4.3.12.RELEASE
org.mybatis
mybatis
3.4.5
org.mybatis
mybatis-spring
1.3.1
mysql
mysql-connector-java
5.1.44
com.alibaba
druid
1.1.2
javax.servlet
jstl
1.2
javax.servlet
javax.servlet-api
3.0.1
provided
总体结构:
web.xml代码:
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
characterEncodingFilter
/*
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
springmvc
/
springmvc.xml代码:
entity学生实体类代码:
package com.accp.entity;
public class Studentinfo {
private long sid;
private String sname;
private String sgender;
private long sage;
private String saddress;
private String semail;
public long getSid() {
return sid;
}
public void setSid(long sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSgender() {
return sgender;
}
public void setSgender(String sgender) {
this.sgender = sgender;
}
public long getSage() {
return sage;
}
public void setSage(long sage) {
this.sage = sage;
}
public String getSaddress() {
return saddress;
}
public void setSaddress(String saddress) {
this.saddress = saddress;
}
public String getSemail() {
return semail;
}
public void setSemail(String semail) {
this.semail = semail;
}
}
dao层StudentinfoDao代码:
package com.accp.dao;
import com.accp.entity.Studentinfo;
import java.util.List;
public interface StudentinfoDao {
ListqueryStudent();
int addStudentinfo(Studentinfo studentinfo);
int deleteStudentinfo(Studentinfo studentinfo);
int updateStudentinfo(Studentinfo studentinfo);
Studentinfo getByStudentId(Studentinfo studentinfo);
}
resources下xml里Studentinfo.xml代码:
select * from studentinfo;
select * from studentinfo where sid = #{sid}
insert into studentinfo value (default ,#{sname},#{sgender},#{sage},#{saddress},#{semail})
delete from studentinfo where sid = #{sid}
update studentinfo
sname = #{sname},
sgender = #{sgender},
sage = #{sage},
saddress = #{saddress},
semail = #{semail},
service层StudentinfoService代码:
package com.accp.service;
import com.accp.entity.Studentinfo;
import java.util.List;
public interface StudentinfoService {
ListqueryStudent();
int addStudentinfo(Studentinfo studentinfo);
int deleteStudentinfo(Studentinfo studentinfo);
int updateStudentinfo(Studentinfo studentinfo);
Studentinfo getByStudentId(Studentinfo studentinfo);
}
service层Impl实现类StudentinfoServiceImpl代码:
package com.accp.service.impl;
import com.accp.dao.StudentinfoDao;
import com.accp.entity.Studentinfo;
import com.accp.service.StudentinfoService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class StudentinfoServiceImpl implements StudentinfoService {
@Resource
private StudentinfoDao studentinfoDao;
public ListqueryStudent() {
return studentinfoDao.queryStudent();
}
public int addStudentinfo(Studentinfo studentinfo) {
return studentinfoDao.addStudentinfo(studentinfo);
}
public int deleteStudentinfo(Studentinfo studentinfo) {
return studentinfoDao.deleteStudentinfo(studentinfo);
}
public int updateStudentinfo(Studentinfo studentinfo) {
return studentinfoDao.updateStudentinfo(studentinfo);
}
public Studentinfo getByStudentId(Studentinfo studentinfo) {
return studentinfoDao.getByStudentId(studentinfo);
}
}
controller控制层StudentinfoController代码:
package com.accp.controller;
import com.accp.entity.Studentinfo;
import com.accp.service.StudentinfoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
@Controller
public class StudentinfoController {
@Resource
private StudentinfoService studentinfoService;
@RequestMapping("/showList")
public String showList(Model model){
model.addAttribute("students",studentinfoService.queryStudent());
return "index";
}
@RequestMapping("/JumpAdd")
public String jumpAdd(){
return "add";
}
@RequestMapping("/AddList")
public String addList(Studentinfo studentinfo){
studentinfoService.addStudentinfo(studentinfo);
return "redirect:showList";
}
@RequestMapping("/DeleteS")
public String deleteS(Studentinfo studentinfo){
studentinfoService.deleteStudentinfo(studentinfo);
return "redirect:showList";
}
@RequestMapping("/JumpUpdate")
public String jumpUpdate(Studentinfo studentinfo,Model model){
model.addAttribute("stu",studentinfoService.getByStudentId(studentinfo));
return "update";
}
@RequestMapping("/UpdateS")
public String updateS(Studentinfo studentinfo){
studentinfoService.updateStudentinfo(studentinfo);
return "redirect:showList";
}
}
jsp页面代码:
显示页面(包含删除操作):
Title
增加
编号
姓名
性别
年龄
地址
操作
${stu.sid}
${stu.sname}
${stu.sgender}
${stu.sage}
${stu.saddress}
${stu.semail}
添加页面:
添加
姓名:
性别:
年龄:
地址:
邮箱:
修改页面:
修改
姓名:
性别:
年龄:
地址:
邮箱:
显示效果: