SSM 小demo(很详细,适合新手)
自己做了一个基于SSM框架的小项目,跟大家分享一下..
首先,你需要开发工具netbeans或者eclipse一枚,我习惯用netbeans,这个随意,mysql数据库,
此为前提条件,因为是小项目,所以需求分析和用例图暂免了吧,有兴趣可以画。下面正式开始
先看一下大概的项目分层
看一眼jar包及JSP页面
我习惯先于数据库下手,然后映射数据库和pojo类,然后配置文件,然后dao->service层,控制器和jsp页面看需求
1)创建一个 student_clazz表,也就是学生-教室-老师表,涉及表与表之间的关系,老师与学生之间为多对多的关系,即一个学生有多个老师,化学啦生物啦,一个老师也有很多学生;教室与学生之间为一对多的关系,即一间教室有多位学生(假定在这个教室的这些学生只在这一个教室上课),人物关系介绍完毕~
至于主外键,为数据库基础不再赘述
CREATE DATABASE student_clazzUSE student_clazzCREATE TABLE Clazz(
C_Id INT PRIMARY KEY NOT NULL,
C_Address VARCHAR(20)
);
INSERT INTO Clazz VALUES(1,'博知');
INSERT INTO Clazz VALUES(2,'静思');
INSERT INTO Clazz VALUES(3,'博文');
INSERT INTO Clazz VALUES(4,'博学');CREATE TABLE Student(
S_Id INT PRIMARY KEY NOT NULL,
S_Name VARCHAR(20),
S_Gender VARCHAR(20),
S_Age VARCHAR(20),
clazz_id INT,
FOREIGN KEY (clazz_id) REFERENCES Clazz(C_Id)
);
INSERT INTO Student VALUES(10111,'anna','女','18',2);
INSERT INTO Student VALUES(10222,'juin','男','12',1);
INSERT INTO Student VALUES(10333,'edwina','女','11',1);
INSERT INTO Student VALUES(10444,'david','男','14',2);CREATE TABLE Teacher(
T_Id INT PRIMARY KEY NOT NULL,
T_Name VARCHAR(20) NOT NULL,
T_Type VARCHAR(20) NOT NULL,
T_Gender VARCHAR(20) NOT NULL,
T_Age VARCHAR(20) NOT NULL,
T_Mobile INT
);INSERT INTO Teacher VALUES(2201,'里番番','数学','女','21',279376);
INSERT INTO Teacher VALUES(22002,'大卫','语文','男','22',279326);
INSERT INTO Teacher VALUES(22003,'卡瑟琳','英语','女','23',279326);
INSERT INTO Teacher VALUES(22004,'鲁迅','NIIT','男','24',279326);CREATE TABLE ItemOne(
student_id INT,
teacher_id INT,
PRIMARY KEY(student_id,teacher_id),
FOREIGN KEY(student_id) REFERENCES Student(S_Id),
FOREIGN KEY(teacher_id) REFERENCES Teacher(T_Id)
);
INSERT INTO ItemOne VALUES(20111,22003);
INSERT INTO ItemOne VALUES(20111,2201);
INSERT INTO ItemOne VALUES(20111,22002);
INSERT INTO ItemOne VALUES(30332,22004);
INSERT INTO ItemOne VALUES(30332,22002);
INSERT INTO ItemOne VALUES(20221,22003);
INSERT INTO ItemOne VALUES(20221,22004);
2)再做pojo类和数据库的映射
先建三个pojo类,有人问为什么要继承Serializable,其实我们在自己电脑上做程序的时候可以不用写,
它可以把对象转换成字节流在网络上传输,如果你不写自然没法传输,那程序也就没法使用
然后挨个写映射文件,这个对数据库的熟练还是有点要求的,增删改查相关操作都写在映射文件里,
association是用来映射一对一的关系及多对一的关系,collection用来映射一对多和多对多的关系,具体
方法如下
(和hibernate的区别参考上一篇博文。这些增删改查的语句的引用都在dao包的实现类里,通过sqlSession
提供的方法具体操作。)
1>Clazz
public class Clazz implements Serializable {private int clazzId;private String clazzAddress;private List<Student> students;public int getClazzId() {return clazzId;}public void setClazzId(int clazzId) {this.clazzId = clazzId;}public String getClazzAddress() {return clazzAddress;}public void setClazzAddress(String clazzAddress) {this.clazzAddress = clazzAddress;}public List<Student> getStudents() {return students;}public void setStudents(List<Student> students) {this.students = students;}@Overridepublic String toString() {return "Clazz{" + "clazzId=" + clazzId + ", clazzAddress=" + clazzAddress + ", students=" + students + '}';}}
,与它匹配的映射文件
<?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.qdu.mapping.ClazzMapper"><select id="selectClazzById" resultMap="ClazzResultMap" parameterType="int">select * from Clazz where C_Id= #{clazzId}</select><resultMap id="ClazzResultMap" type="com.qdu.pojo.Clazz"><id property="clazzId" column="C_Id"/><result property="clazzAddress" column="C_Address"/> <!--下面有个column是“C_Id”,我个人的理解是这个为当前表的主键做了另一个表的外键,起到一个关联作用,这是提供给另一个表的--><collection property="students" javaType="ArrayList" column="C_Id" ofType="com.qdu.pojo.Student" select="com.qdu.mapping.StudentMapper.selectStudentByClazzId"><id property="stuId" column="S_Id"/><result property="stuName" column="S_Name"/><result property="stuGender" column="S_Gender"/><result property="stuAge" column="S_Age"/></collection></resultMap></mapper>
2>Student
public class Student implements Serializable{private int stuId;private String stuName;private String stuGender;private String stuAge;private Clazz clazz;private List<Teacher> teachers;public int getStuId() {return stuId;}public void setStuId(int stuId) {this.stuId = stuId;}public String getStuName() {return stuName;}public void setStuName(String stuName) {this.stuName = stuName;}public String getStuGender() {return stuGender;}public void setStuGender(String stuGender) {this.stuGender = stuGender;}public String getStuAge() {return stuAge;}public void setStuAge(String stuAge) {this.stuAge = stuAge;}public Clazz getClazz() {return clazz;}public void setClazz(Clazz clazz) {this.clazz = clazz;}public List<Teacher> getTeachers() {return teachers;}public void setTeachers(List<Teacher> teachers) {this.teachers = teachers;}@Overridepublic String toString() {return "Student{" + "stuId=" + stuId + ", stuName=" + stuName + ", stuGender=" + stuGender + ", stuAge=" + stuAge + ", clazz=" + clazz + ", teachers=" + teachers + '}';}}
,Student的映射文件为
<?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.qdu.mapping.StudentMapper"><!--之所以会用到两个表我认为是因为在数据库中Student表中引用了clazz--><select id="selectStudentById" resultMap="StudentResultMap" parameterType="int">select * from Student s,Clazz cwhere s.clazz_id = c.C_Id And s.S_id = #{stuId}</select><select id="selectStudentByClazzId" resultMap="StudentResultMap" parameterType="int">select * from Student where clazz_id = #{C_id}</select><select id="selectStudentByTeacherId" resultMap="StudentResultMap" parameterType="int">select * from Student where S_Id in (select student_id from ItemOne where teacher_id = #{T_Id} )</select><insert id="insertStudent" parameterType="com.qdu.pojo.Student">INSERT INTO Student(S_Id,S_Name,S_Gender,S_Age,clazz_id) VALUES (#{stuId},#{stuName},#{stuGender},#{stuAge},#{clazz.clazzId});</insert><update id="updateStudent" parameterType="com.qdu.pojo.Student" statementType="PREPARED">update Student setS_Name=#{stuName},S_Gender=#{stuGender},S_Age=#{stuAge},clazz_id=#{clazz.clazzId}where S_Id = #{stuId}</update><delete id="deleteStudentById" parameterType="com.qdu.pojo.Student">delete from Student where S_Id = #{stuId}</delete><resultMap id="StudentResultMap" type="com.qdu.pojo.Student"><id property="stuId" column="S_Id"/><result property="stuName" column="S_Name"/><result property="stuGender" column="S_Gender"/><result property="stuAge" column="S_Age"/><!--多对一--><association property="clazz" javaType="com.qdu.pojo.Clazz"><id property="clazzId" column="C_Id"/><result property="clazzAddress" column="C_Address"/></association><!--多对多--><collection property="teachers" javaType="ArrayList" column="S_Id" ofType="com.qdu.pojo.Teacher" select="com.qdu.mapping.TeacherMapper.selectTeacherByStudentId"><id property="teacherId" column="T_Id"/><result property="teacherName" column="T_Name"/><result property="teacherType" column="T_Type"/><result property="teacherGender" column="T_Gender"/><result property="teacherAge" column="T_Age"/><result property="teacherMobile" column="T_Mobile"/></collection></resultMap></mapper>
3>Teacher
public class Teacher implements Serializable {private int teacherId;private String teacherName;private String teacherType;private String teacherGender;private String teacherAge;private int teacherMobile;private List<Student> students;public int getTeacherId() {return teacherId;}public void setTeacherId(int teacherId) {this.teacherId = teacherId;}public String getTeacherName() {return teacherName;}public void setTeacherName(String teacherName) {this.teacherName = teacherName;}public String getTeacherType() {return teacherType;}public void setTeacherType(String teacherType) {this.teacherType = teacherType;}public String getTeacherGender() {return teacherGender;}public void setTeacherGender(String teacherGender) {this.teacherGender = teacherGender;}public String getTeacherAge() {return teacherAge;}public void setTeacherAge(String teacherAge) {this.teacherAge = teacherAge;}public int getTeacherMobile() {return teacherMobile;}public void setTeacherMobile(int teacherMobile) {this.teacherMobile = teacherMobile;}public List<Student> getStudents() {return students;}public void setStudents(List<Student> students) {this.students = students;}}
,映射文件为
<?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.qdu.mapping.StudentMapper"><!--之所以会用到两个表我认为是因为在数据库中Student表中引用了clazz--><select id="selectStudentById" resultMap="StudentResultMap" parameterType="int">select * from Student s,Clazz cwhere s.clazz_id = c.C_Id And s.S_id = #{stuId}</select><select id="selectStudentByClazzId" resultMap="StudentResultMap" parameterType="int">select * from Student where clazz_id = #{C_id}</select><select id="selectStudentByTeacherId" resultMap="StudentResultMap" parameterType="int">select * from Student where S_Id in (select student_id from ItemOne where teacher_id = #{T_Id} )</select><insert id="insertStudent" parameterType="com.qdu.pojo.Student">INSERT INTO Student(S_Id,S_Name,S_Gender,S_Age,clazz_id) VALUES (#{stuId},#{stuName},#{stuGender},#{stuAge},#{clazz.clazzId});</insert><update id="updateStudent" parameterType="com.qdu.pojo.Student" statementType="PREPARED">update Student setS_Name=#{stuName},S_Gender=#{stuGender},S_Age=#{stuAge},clazz_id=#{clazz.clazzId}where S_Id = #{stuId}</update><delete id="deleteStudentById" parameterType="com.qdu.pojo.Student">delete from Student where S_Id = #{stuId}</delete><resultMap id="StudentResultMap" type="com.qdu.pojo.Student"><id property="stuId" column="S_Id"/><result property="stuName" column="S_Name"/><result property="stuGender" column="S_Gender"/><result property="stuAge" column="S_Age"/><!--多对一--><association property="clazz" javaType="com.qdu.pojo.Clazz"><id property="clazzId" column="C_Id"/><result property="clazzAddress" column="C_Address"/></association><!--多对多--><collection property="teachers" javaType="ArrayList" column="S_Id" ofType="com.qdu.pojo.Teacher" select="com.qdu.mapping.TeacherMapper.selectTeacherByStudentId"><id property="teacherId" column="T_Id"/><result property="teacherName" column="T_Name"/><result property="teacherType" column="T_Type"/><result property="teacherGender" column="T_Gender"/><result property="teacherAge" column="T_Age"/><result property="teacherMobile" column="T_Mobile"/></collection></resultMap></mapper>
3) 接下来就是配置文件,重头戏
配置文件分为Spring-mybatis配置文件和Spring MVC配置文件
Spring-mybatis配置文件的作用就是作为持久层框架起一个水渠的作用。
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
><context:component-scan base-package="com.qdu"/><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/student_clazz" /><property name="username" value="sa" /><property name="password" value="niit" /></bean><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="mapperLocations" value="classpath:com/qdu/mapping/*.xml"/></bean><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><tx:annotation-driven transaction-manager="txManager"/></beans>
Spring MVC配置文件作为请求分发器用来分发请求到制定的控制器
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"
><context:component-scan base-package="com.qdu.controller"/><mvc:annotation-driven/><mvc:default-servlet-handler/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"id="internalResourceViewResolver"><!-- 前缀 --><property name="prefix" value="/WEB-INF/jsp/"/><!-- 后缀 --><property name="suffix" value=".jsp" /></bean></beans>
4)再就是配置web.xml了,把两个配置文件向项目向程序说明一下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- <init-param>Servlet范围内的参数<param-name>contextConfigLocation</param-name><param-value>classpath:dispatcher-servlet.xml</param-value></init-param>--><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping><session-config><session-timeout>30</session-timeout></session-config><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
</web-app>
5) 我习惯dao和service都写接口再实现,这是个好习惯,符合框架低耦合的观念,这次例外,dao接口没写,
可以自己补上。
@Repository用于标注数据访问组件,即DAO组件
statement为mapper文件中的具体的sql语句
@Repository
public class StudentDao {@Autowiredprivate SqlSessionFactory sqlSessionFactory;public List studentList(int clazzId){String statement="com.qdu.mapping.StudentMapper.selectStudentByClazzId";return sqlSessionFactory.openSession().selectList(statement, clazzId);}public List selectTeacherByStudentId(int stuId){String statement = "com.qdu.mapping.TeacherMapper.selectTeacherByStudentId";return sqlSessionFactory.openSession().selectList(statement, stuId);}public Student selectStudentById(int stuId) {String statement = "com.qdu.mapping.StudentMapper.selectStudentById";System.out.println(sqlSessionFactory);return sqlSessionFactory.openSession().selectOne(statement, stuId);}public Clazz selectClazzById(int clazzId) {String statement = "com.qdu.mapping.ClazzMapper.selectClazzById";return sqlSessionFactory.openSession().selectOne(statement, clazzId);}public void insertStudent(Student student){String statement="com.qdu.mapping.StudentMapper.insertStudent";sqlSessionFactory.openSession().insert(statement, student);}public void updateStudent(Student student){String statement="com.qdu.mapping.StudentMapper.updateStudent";sqlSessionFactory.openSession().update(statement, student);}public void deleteStudent(int stuId){String statement="com.qdu.mapping.StudentMapper.deleteStudentById";sqlSessionFactory.openSession().delete(statement, stuId);}public Teacher selectTeacherById(int teacherId){String statement="com.qdu.mapping.TeacherMapper.selectTeacherById";return sqlSessionFactory.openSession().selectOne(statement,teacherId);}}
Service接口:
public interface StudentService {public Student getStudentById(int stuId);public List selectTeacherByStudentId(int stuId);public Clazz getClazzById(int clazzId);public void insertStudent(Student student);public void updateStudent(Student student);public void deleteStudent(int stuId);public List studentList(int clazzId);public Teacher selectTeacherById(int teacherId);}
@Transactional为Spring的事务注解,表示该类里面的所有方法或者这个方法的事务由spring处理,
来保证事务的原子性, 每一个业务方法开始时都会打开一个事务,这样的好处,可以省去一些XML
配置文件的繁琐编写
@Transactional 注解应该只被应用到 public 方法上,这是由 Spring AOP 的本质决定的。如果你在 protected、private 或者默认可见性的方法上使用 @Transactional 注解,这将被忽略,也不会抛出任何异常。
默认情况下,只有来自外部的方法调用才会被AOP代理捕获,也就是,类内部方法调用本类内部的其他方法并不会引起事务行为,即使被调用方法使用@Transactional注解进行修饰。
事务管理对于企业应用来说是至关重要的,即使出现异常情况,它也可以保证数据的一致性
@Service为Spring的service注解,标注服务类
//为什么要用接口?!
//第一种方式:建立个接口
//第二种方式:直接实例化
//第一种:比如你用Spring框架,可以在用到UserServiceImpl的时候定义接口,最后使用XML方式实例化,这样以后需要修改,只要改xml(所谓的低耦合)
//第二种:假设你直接在java文件中直接实例化,万一你不在用这个类了,要用另外的类来代替,需要改java文件,很麻烦(即所谓的低内聚高耦合)
//耦合度低的程序要好@Transactional
@Service("studentServiceImpl")
public class StudentServiceImpl implements StudentService {@Autowiredprivate StudentDao studentDao;@Overridepublic Student getStudentById(int stuId) {
// System.out.println(studentDao.selectStudentById(10111));return studentDao.selectStudentById(stuId);}@Overridepublic Clazz getClazzById(int clazzId) {System.out.println(studentDao.selectClazzById(1));return studentDao.selectClazzById(clazzId);}@Overridepublic void insertStudent(Student student) {studentDao.insertStudent(student);}@Overridepublic void updateStudent(Student student) {studentDao.updateStudent(student);}@Overridepublic void deleteStudent(int stuId) {studentDao.deleteStudent(stuId);}@Overridepublic List studentList(int clazzId) {return studentDao.studentList(clazzId);}@Overridepublic List selectTeacherByStudentId(int stuId) {return studentDao.selectTeacherByStudentId(stuId);}@Overridepublic Teacher selectTeacherById(int teacherId) {return studentDao.selectTeacherById(teacherId);}}
@Controller控制器注解,用于处理多个URL请求@RequestMapping 可以标注在类定义处,将 Controller 和特定请求关联起来;还可以标注在方法签名处,以便进一步对请求进行分流
@Controller
@RequestMapping(value = "/anna")
public class TestController {@Autowiredprivate StudentService studentServiceImpl;//调用父类的方法,再调用子类中的方法@RequestMapping(value = "/student.do")public String studentLogin(ModelMap map) {return "student";}@RequestMapping(value = "/admin.do")public String teacherLogin(ModelMap map) {return "admin";}@RequestMapping(value = "/juin.do")public String queryStudent(HttpServletRequest request, ModelMap map) {int id = Integer.parseInt(request.getParameter("stuId"));int password = Integer.parseInt(request.getParameter("password"));Student student = studentServiceImpl.getStudentById(id);System.out.println(student);
// int转String验证可以+""啊if (student != null && (id + "") != null && (password + "") != null && id == student.getStuId() && password == 123) {map.addAttribute("student", student);return "success";} else {return "fail";}}@RequestMapping(value = "/adminLogin.do")public String teacherLoginDo(HttpServletRequest request, ModelMap map) {int id = Integer.parseInt(request.getParameter("id"));int password = Integer.parseInt(request.getParameter("password"));Clazz clazz = studentServiceImpl.getClazzById(id);if (id == clazz.getClazzId() && password == 123) {map.addAttribute("clazz", clazz);return "adminSuccess";} else {return "fail";}}@RequestMapping(value = "forInsertStudent.do")public String forInsertStudent(ModelMap map, int clazzId, HttpServletRequest request) {clazzId = Integer.parseInt(request.getParameter("clazzId"));Clazz clazz = studentServiceImpl.getClazzById(clazzId);Date time = new Date(System.currentTimeMillis());SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");String current = sdf.format(time);Random random = new Random();int cc=Integer.parseInt(current);int x = random.nextInt(900) + 100;System.out.println(cc);map.addAttribute("clazz", clazz);map.addAttribute("date", cc);map.addAttribute("random", x);return "insertStudent";}@RequestMapping(value = "insertStudent.do")public String insertStudent(HttpServletRequest request, ModelMap map, Student student) {studentServiceImpl.insertStudent(student);int id = Integer.parseInt(request.getParameter("clazz.clazzId"));Clazz clazz = studentServiceImpl.getClazzById(id);map.addAttribute("clazz", clazz);return "adminSuccess";}@RequestMapping(value = "forUpdateStudent.do")public String forUpdateStudent(HttpServletRequest request, ModelMap map) {int id = Integer.parseInt(request.getParameter("stuId"));Student student = studentServiceImpl.getStudentById(id);map.addAttribute("student", student);return "updateStudent";}@RequestMapping(value = "updateStudent.do")public String updateStudent(ModelMap map, Student student, int stuId) {studentServiceImpl.updateStudent(student);student = studentServiceImpl.getStudentById(stuId);Clazz clazz = studentServiceImpl.getClazzById(student.getClazz().getClazzId());map.addAttribute("clazz", clazz);return "adminSuccess";}@RequestMapping(value = "forDeleteStudent.do")public String forDeleteStudent(HttpServletRequest request, ModelMap map) {int id = Integer.parseInt(request.getParameter("stuId"));Student student = studentServiceImpl.getStudentById(id);map.addAttribute("student", student);return "deleteStudent";}// clazzId来源于前端的传值,免去request,是不是很有趣?另外,逻辑语句要有先有后,第n次逻辑颠倒@RequestMapping(value = "deleteStudent.do")public String deleteStudent(ModelMap map, int clazzId, int stuId, Student student, Clazz clazz) {studentServiceImpl.deleteStudent(stuId);clazz = studentServiceImpl.getClazzById(clazzId);map.addAttribute("clazz", clazz);return "adminSuccess";}@RequestMapping(value = "teacher.do")public String teacher() {return "teacher";}@RequestMapping(value = "teacherLogin.do")public String teacherLogin(ModelMap map, HttpServletRequest request) {int teacherId = Integer.parseInt(request.getParameter("teacherId"));int password = Integer.parseInt(request.getParameter("password"));Teacher teacher = studentServiceImpl.selectTeacherById(teacherId);if (teacherId == teacher.getTeacherId() && password == 123) {// for (int i = 0; i < teacher.getStudents().size(); i++) {
// Student student = studentServiceImpl.getStudentById(teacher.getStudents().get(i).getStuId());map.addAttribute("teacher", teacher);
// map.addAttribute("sss", student);// }return "teacherSuccess";} else {return "fail";}}@RequestMapping(value = "firstPage.do")public String firstPage() {return "translate";}
}
最后就是页面了,JSP页面与JSTL以及EL表达式相结合,足够满足一般需求
页面很多,贴出最主要的一两个
首页
不同身份登录
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>首页</title></head><body><form><h2>登录身份选择</h2><a href="anna/student.do">Student</a><a href="anna/teacher.do">Teacher</a><a href="anna/admin.do">Admin</a></form></body>
</html>
教师登录
<%-- Document : teacherCreated on : 2017-4-27, 16:50:03Author : ACER
--%><%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>教师登录</title></head><body><form action="teacherLogin.do">教师账号: <input type="text" name="teacherId" placeholder="在此输入账号"/><br/><br/>教师密码: <input type="password" name="password" placeholder="在此输入密码"/><br/><br/><input type="submit" value="提交"/></form></body>
</html>
登录成功页面
<%-- Document : teacherSuccessCreated on : 2017-4-27, 17:59:20Author : ACER
--%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>教师登录成功</title></head><body><h2>登录成功-<code style="color: #00CC00">${teacher.teacherName}</code>老师</h2><table border="2"><caption>学生列表</caption><tr><th>学生Id</th><th>学生姓名</th><th>学生性别</th><th>学生年龄</th><!--<th>学生教室</th>--></tr><c:forEach items="${teacher.students}" var="s"><tr id="${s.stuId}" ><td>${s.stuId}</td><td>${s.stuName}</td><td>${s.stuGender}</td><td>${s.stuAge}</td></tr></c:forEach></table></body>
</html>
运行结果
此为结束,欢迎大家提出问题,共同探讨
____Juin
SSM 小demo(很详细,适合新手)相关推荐
- python可以写dnf外挂么_易语言写DNF外挂各种功能(很详细适合新手)
3S==================== 写内存字节集 (进程ID, 十六到十 ("0177E5C2"), { 216, 60, 131 }) ================ ...
- 用Go建千亿级微服务 分析详细,适合新手理解概念
今日头条当前后端服务超过80%的流量是跑在 Go 构建的服务上.微服务数量超过100个,高峰 QPS 超过700万,日处理请求量超过3000亿,是业内最大规模的 Go 应用. Go 构建微服务的历程 ...
- python小程序-整理了适合新手的20个Python练手小程序
即刻关注公众号,发现世界的美好 100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. [程序1] 题目:有1.2.3 ...
- python编程100个小程序-整理了适合新手的20个Python练手小程序
即刻关注公众号,发现世界的美好 100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. [程序1] 题目:有1.2.3 ...
- Python爬虫樱花动漫多线程下载附源码(超详细适合新手练习)
前言 别瞅了!认真看完你肯定行 一.打开动漫详细页面 二.查看网页源码 查看网页源码搜索关词能够找到相关内容,我们可以看见详情页地址并不完整,所以我们需要出拼接出完整url def url_parse ...
- C语言编程学习:制作掷骰子小游戏!超适合新手的练手项目!
C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...
- SSM整合框架搭建(适合新手)
SSM整合(SpringMvc+Spring+Mybatis) 首先创建一个Dynamic Web Project(JavaWeb动态工程) 创建一个xxx.xxx.xxx.controller包用来 ...
- 【Python爬虫项目】链家房屋信息抓取(超详细适合新手练习附源码)
爬取链家房屋信息 爬取信息具体如下: 1.标题 2.位置 3.房屋介绍 4.房屋总价 5.房屋单价 一.检查网页源码 搜索标题中的关键字发现目标信息可以在源码中找到,所以我们请求该url网址就可以拿到 ...
- 下载Windows ISO镜像的方法 (超详细 适合新手入门)
前言
最新文章
- mac terminal
- Java并发编程之线程池及示例
- 从FCN到DeepLab
- OpenCV GrabCut分割的实例(附完整代码)
- Puzzle (II) UVA - 519
- SQL Server 2012安装图解
- 【AC自动机】AC自动机(二次加强版)(luogu 5357)
- ATL 开发 COM 过程中的一些经验、问题总结
- 文本区域css,如何知道文本从一个CSS区域溢出到另一个区域?
- 系统分析师教程电子版免费下载
- Weblogic部署项目
- Unity IAP接入google支付文档(2022年最新)
- 【DRP】将DRP物理模型导出SQL脚本
- gbdt python_GBDT回归的原理及Python实现
- net proxy FortiGate 200A / SANGFOR / Blue Coat
- 苹果授权登录Sign In With Apple亲测通过版[100%成功]
- android 黑白色主题、滤镜效果(公祭日、追悼日)
- ES6 标签模板(Tagged templates)
- Tetris game编程过程
- SQL Server SA权限总结