用ssm写一个web的增删改查
如果不想自己写的话,这里有完整的项目连接
链接:https://pan.baidu.com/s/1YnAyGy7Hpxoyma5sqGFX8Q 密码:lwdh
项目结构
每个包下包含哪些文件如下图
首先写
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/itcast
//itcast是你的数据库名
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.config-location=classpath:Mybatis-config.xml
//这条信息是关于Mybatis的总配置文件的路径
第二写总配置文件
Mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
//总配置文件的规则
<configuration>
<typeAliases>
<typeAlias alias="Clazz" type="com.gxj.model.Clazz"/>
<typeAlias alias="Student" type="com.gxj.model.Student"/>
</typeAliases>
<mappers>
<mapper resource="com/gxj/model/Clazz.xml"></mapper>
//分文件Clazz.xml的路径
<mapper resource="com/gxj/model/Student.xml"></mapper>
</mappers>
//分文件Student.xml的路径
</configuration>
Clazz.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gxj.dao.ClassDao">
<insert id="InsertClass" parameterType="Clazz">
insert into class (name,size) values(#{name},#{size})
</insert>
<select id="SelectClass" resultType="Clazz">
select * from class
</select>
<delete id="DeleteClass" parameterType="int">
delete from class where id=#{id}
</delete>
<update id="UpdateClass" parameterType="Clazz">
update class set name=#{name},size=#{size} where id=#{id}
</update>
<select id="SelectClassById" resultType="Clazz">
select * from class where id=#{id}
</select>
</mapper>
Student.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gxj.dao.StudentDao">
<resultMap type="Student" id="m1">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
<result column="cid" property="cid"/>
<association property="clazz" javaType="Clazz" column="cid" select="SelectClassByCid"></association>
</resultMap>
<select id="SelectClassByCid" parameterType="int" resultType="Clazz">
select * from Class where id=#{cid}
</select>
<select id="SelectStudent" resultMap="m1">
select * from student
</select>
<delete id="DeleteStudentByCid" parameterType="int">
delete from Student where cid=#{cid}
</delete>
<insert id="InsertStudent" parameterType="Student">
insert into student (name,age,sex,cid) values(#{name},#{age},#{sex},#{cid})
</insert>
<select id="SelectStudentById" parameterType="int" resultType="Student">
select * from student where id=#{id}
</select>
<delete id="DeleteStudentById" parameterType="int">
delete from Student where id=#{id}
</delete>
<update id="UpdateStudent" parameterType="Student">
update student set name=#{name},age=#{age},sex=#{sex},cid=#{cid} where id=#{id}
</update>
</mapper>
第三包内文件
com.gxj.medol下
Clazz.java
package com.gxj.model;
public class Clazz {
private Integer id;
private String name;
private Integer size;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
}
Student.java
package com.gxj.model;
public class Student {
private Integer id;
private String name;
private Integer age;
private Integer sex;
private Integer cid;
private Clazz clazz;
public Clazz getClazz() {
return clazz;
}
public void setClazz(Clazz clazz) {
this.clazz = clazz;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
}
com.gxj.dao
ClassDao.java
package com.gxj.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.gxj.model.Clazz;
@Mapper
public interface ClassDao {
public int InsertClass(Clazz clazz);
public List<Clazz> SelectClass();
public int DeleteClass(Integer id);
public int UpdateClass(Clazz clazz);
public Clazz SelectClassById(Integer id);
}
StudentDao.java
package com.gxj.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.gxj.model.Student;
@Mapper
public interface StudentDao {
public int DeleteStudentByCid(Integer cid);
public int InsertStudent(Student student);
public List<Student> SelectStudent();
public int DeleteStudentById(Integer id);
public int UpdateStudent(Student student);
public Student SelectStudentById(Integer id);
}
com.gxj.controller
ClassController.java
package com.gxj.controller;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.gxj.biz.ClassBiz;
import com.gxj.model.Clazz;
@Controller
public class ClassController {
@Resource
private ClassBiz biz;
@RequestMapping("/AddClass")
public String AddClass(Clazz clazz) {
boolean flag = biz.InsertClass(clazz);
if(flag) {
return "redirect:/Success.html";
}else {
return "redirect:/Error.html";
}
}
@RequestMapping("/SelectClass")
public String SelectClass(HttpServletRequest request) {
List<Clazz> list = biz.SelectClass();
request.setAttribute("list", list);
return "ShowClass.jsp";
}
@RequestMapping("/DeleteClass")
public String DeleteClass(@RequestParam(value="id") int id) {
boolean flag = biz.DeleteClass(id);
if(flag) {
return "redirect:/Success.html";
}else {
return "redirect:/Error.html";
}
}
@RequestMapping("/UpdateClassById")
public String FUdateClass(@RequestParam(value="id") int id,HttpServletRequest request) {
Clazz clazz = biz.SelectClassById(id);
request.setAttribute("clazz", clazz);
return "UpdateClass.jsp";
}
@RequestMapping("/UpdateClass")
public String UpdateClass(Clazz clazz){
boolean flag = biz.UpdateClass(clazz);
if(flag) {
return "redirect:/Success.html";
}else {
return "redirect:/Error.html";
}
}
}
StudentController.java
package com.gxj.controller;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.gxj.biz.ClassBiz;
import com.gxj.biz.StudentBiz;
import com.gxj.model.Clazz;
import com.gxj.model.Student;
@Controller
public class StudentController {
@Resource
private StudentBiz sbiz;
@Resource
private ClassBiz biz;
@RequestMapping("/InsertStudent")
public String InsertStudent(Student student) {
boolean flag = sbiz.InsertStudent(student);
if(flag) {
return "redirect:/Success.html";
}else {
return "redirect:/Error.html";
}
}
@RequestMapping("/GetAllClass")
public String GetAllClass(HttpServletRequest request) {
List<Clazz> list = biz.SelectClass();
request.setAttribute("list", list);
return "AddStudent.jsp";
}
@RequestMapping("/SelectStudent")
public String SelectStudent(HttpServletRequest request) {
List<Student> list = sbiz.SelectStudent();
request.setAttribute("list", list);
return "ShowStudent.jsp";
}
@RequestMapping("/DeleteStudent")
public String DeleteStudent(Integer id) {
boolean flag = sbiz.DeleteStudent(id);
if(flag) {
return "redirect:/Success.html";
}else {
return "redirect:/Error.html";
}
}
@RequestMapping("/FUpdateStudent")
public String UpdateStudent(Student student) {
boolean flag = sbiz.UpdateStudent(student);
if(flag) {
return "redirect:/Success.html";
}else {
return "redirect:/Error.html";
}
}
@RequestMapping("/SelectStudentById")
public String SelectStudentById(@RequestParam(value="id") int id,HttpServletRequest request) {
Student student = sbiz.SelectStudentById(id);
List<Clazz> list = biz.SelectClass();
request.setAttribute("student", student);
request.setAttribute("list", list);
return "UpdateStudent.jsp";
}
}
com.gxj.biz
ClassBiz.java
package com.gxj.biz;
import java.util.List;
import com.gxj.model.Clazz;
public interface ClassBiz {
public boolean InsertClass(Clazz clazz);
public List<Clazz> SelectClass();
public boolean DeleteClass(Integer id);
public boolean UpdateClass(Clazz clazz);
public Clazz SelectClassById(Integer id);
}
ClassBizImpl.java
package com.gxj.biz;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.gxj.dao.ClassDao;
import com.gxj.dao.StudentDao;
import com.gxj.model.Clazz;
@Service
public class ClassBizImpl implements ClassBiz {
@Resource
private ClassDao dao;
@Resource
private StudentDao sdao;
@Override
public boolean InsertClass(Clazz clazz) {
// TODO Auto-generated method stub
boolean flag = false;
int rows = dao.InsertClass(clazz);
if(rows>0) {
flag = true;
}
return flag;
}
@Override
public List<Clazz> SelectClass() {
// TODO Auto-generated method stub
return dao.SelectClass();
}
@Transactional(propagation=Propagation.REQUIRED)
@Override
public boolean DeleteClass(Integer id) {
// TODO Auto-generated method stub
boolean flag = false;
sdao.DeleteStudentByCid(id);
int rows1 = dao.DeleteClass(id);
if(rows1>0) {
flag = true;
}
return flag;
}
@Override
public boolean UpdateClass(Clazz clazz) {
// TODO Auto-generated method stub
boolean flag = false;
int rows = dao.UpdateClass(clazz);
if(rows>0) {
flag = true;
}
return flag;
}
@Override
public Clazz SelectClassById(Integer id) {
// TODO Auto-generated method stub
return dao.SelectClassById(id);
}
}
StudentBiz.java
package com.gxj.biz;
import java.util.List;
import com.gxj.model.Student;
public interface StudentBiz {
public boolean InsertStudent(Student student);
public List<Student> SelectStudent();
public boolean DeleteStudent(Integer id);
public boolean UpdateStudent(Student student);
public Student SelectStudentById(Integer id);
}
StudentBizImpl.java
package com.gxj.biz;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.gxj.dao.StudentDao;
import com.gxj.model.Student;
@Service
public class StudentBizImpl implements StudentBiz {
@Resource
private StudentDao dao;
@Override
public boolean InsertStudent(Student student) {
// TODO Auto-generated method stub
boolean flag = false;
int rows = dao.InsertStudent(student);
if(rows>0) {
flag = true;
}
return flag;
}
@Override
public List<Student> SelectStudent() {
// TODO Auto-generated method stub
return dao.SelectStudent();
}
@Override
public boolean DeleteStudent(Integer id) {
// TODO Auto-generated method stub
boolean flag = false;
int rows = dao.DeleteStudentById(id);
if(rows>0) {
flag = true;
}
return flag;
}
@Override
public boolean UpdateStudent(Student student) {
// TODO Auto-generated method stub
boolean flag = false;
int rows = dao.UpdateStudent(student);
if(rows>0) {
flag = true;
}
return flag;
}
@Override
public Student SelectStudentById(Integer id) {
// TODO Auto-generated method stub
return dao.SelectStudentById(id);
}
}
放jsp和HTML页面
AddClass.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<div><a href="AddClass.jsp" style="margin-left: 10px">添加班级</a><a href="SelectClass" style="margin-left: 10px">查看班级</a><a href="GetAllClass" style="margin-left: 10px">添加学员</a><a href="SelectStudent" style="margin-left: 10px">查看学员</a></div>
<form action="AddClass" method="post">
班级名称:<input type="text" name="name"><br>
班级容量:<input type="number" name="size"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
AddStudent.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@page import="com.gxj.model.Clazz" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<div><a href="AddClass.jsp" style="margin-left: 10px">添加班级</a><a href="SelectClass" style="margin-left: 10px">查看班级</a><a href="GetAllClass" style="margin-left: 10px">添加学员</a><a href="SelectStudent" style="margin-left: 10px">查看学员</a></div>
<form action="InsertStudent" method="post">
学生姓名:<input type="text" name="name"><br>
学生年龄:<input type="number" name="age"><br>
学生性别:<input type="radio" name="sex" value="0">女<input type="radio" name="sex" value="1" checked>男<br>
所在班级:
<option value="-1">请选择</option>
<c:forEach items="${requestScope.list }" var="item">
<option value="${item.id }">${item.name}</option>
</c:forEach>
</select>
<br>
<input type="submit" value="提交">
</form>
</body>
</html>
Error.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div><a href="AddClass.jsp" style="margin-left: 10px">添加班级</a><a href="SelectClass" style="margin-left: 10px">查看班级</a><a href="GetAllClass" style="margin-left: 10px">添加学员</a><a href="SelectStudent" style="margin-left: 10px">查看学员</a></div>
<div>操作失败</div>
</body>
</html>
Success.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div><a href="AddClass.jsp" style="margin-left: 10px">添加班级</a><a href="SelectClass" style="margin-left: 10px">查看班级</a><a href="GetAllClass" style="margin-left: 10px">添加学员</a><a href="SelectStudent" style="margin-left: 10px">查看学员</a></div>
<div>操作成功</div>
</body>
</html>
ShowClass.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<div><a href="AddClass.jsp" style="margin-left: 10px">添加班级</a><a href="SelectClass" style="margin-left: 10px">查看班级</a><a href="GetAllClass" style="margin-left: 10px">添加学员</a><a href="SelectStudent" style="margin-left: 10px">查看学员</a></div>
<table>
<tr>
<th>班级名称</th>
<th>班级容量</th>
<th>操作</th>
</tr>
<c:forEach items="${requestScope.list }" var="item">
<tr>
<td>${item.name }</td>
<td>${item.size }</td>
<td><a href="DeleteClass?id=${item.id }" style="margin-left: 10px">删除</a><a href="UpdateClassById?id=${item.id }" style="margin-left: 10px">更新</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
ShowStudent.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<div><a href="AddClass.jsp" style="margin-left: 10px">添加班级</a><a href="SelectClass" style="margin-left: 10px">查看班级</a><a href="GetAllClass" style="margin-left: 10px">添加学员</a><a href="SelectStudent" style="margin-left: 10px">查看学员</a></div>
<table>
<tr>
<th>学生姓名</th>
<th>学生年龄</th>
<th>学生性别</th>
<th>所在班级</th>
<td>操作</td>
<c:forEach items="${requestScope.list }" var="item">
</tr>
<td>${item.name}</td>
<td>${item.age}</td>
<c:if test="${item.sex==0}">
<td>女</td>
</c:if>
<c:if test="${item.sex==1}">
<td>男</td>
</c:if>
<td>${item.clazz.name}</td>
<td><a href="DeleteStudent?id=${item.id }" style="margin-left: 10px">删除</a><a href="SelectStudentById?id=${item.id }" style="margin-left: 10px">更新</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
UpdateClass.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ut-8">
<title>Insert title here</title>
</head>
<body>
<div><a href="AddClass.jsp" style="margin-left: 10px">添加班级</a><a href="SelectClass" style="margin-left: 10px">查看班级</a><a href="GetAllClass" style="margin-left: 10px">添加学员</a><a href="SelectStudent" style="margin-left: 10px">查看学员</a></div>
<form action="UpdateClass" method="post">
<input type="hidden" name="id" value="${requestScope.clazz.id }">
班级名称:<input type="text" name="name" value="${requestScope.clazz.name }"><br>
班级容量:<input type="number" name="size" value="${requestScope.clazz.size }"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
UpdateStudent.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<div><a href="AddClass.jsp" style="margin-left: 10px">添加班 级</a><a href="SelectClass" style="margin-left: 10px">查看班级</a><a href="GetAllClass" style="margin-left: 10px">添加学员</a><a href="SelectStudent" style="margin-left: 10px">查看学员</a></div>
<form action="FUpdateStudent" method="post">
<input type="hidden" name="id" value="${requestScope.student.id }">
学生姓名:<input type="text" name="name" value="${requestScope.student.name }"><br>
学生年龄:<input type="number" name="age" value="${requestScope.student.age }"><br>
学生性别:
<c:if test="${requestScope.student.sex==0 }">
<input type="radio" name="sex" value="0" checked>女
</c:if>
<c:if test="${requestScope.student.sex!=0 }">
<input type="radio" name="sex" value="0">女
</c:if>
<c:if test="${requestScope.student.sex==1 }">
<input type="radio" name="sex" value="1" checked>男
</c:if>
<c:if test="${requestScope.student.sex!=1 }">
<input type="radio" name="sex" value="1">男
</c:if>
所在班级:<select name="cid">
<option value="-1">请选择</option>
<c:forEach items="${requestScope.list }" var="item">
<c:if test="${item.id== requestScope.student.cid}">