日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

SSM 小demo(很详细,适合新手)

發(fā)布時(shí)間:2023/12/14 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SSM 小demo(很详细,适合新手) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

自己做了一個(gè)基于SSM框架的小項(xiàng)目,跟大家分享一下..

首先,你需要開(kāi)發(fā)工具netbeans或者eclipse一枚,我習(xí)慣用netbeans,這個(gè)隨意,mysql數(shù)據(jù)庫(kù),

此為前提條件,因?yàn)槭切№?xiàng)目,所以需求分析和用例圖暫免了吧,有興趣可以畫(huà)。下面正式開(kāi)始

先看一下大概的項(xiàng)目分層


看一眼jar包及JSP頁(yè)面



我習(xí)慣先于數(shù)據(jù)庫(kù)下手,然后映射數(shù)據(jù)庫(kù)和pojo類(lèi),然后配置文件,然后dao->service層,控制器和jsp頁(yè)面看需求


1)創(chuàng)建一個(gè)?student_clazz表,也就是學(xué)生-教室-老師表,涉及表與表之間的關(guān)系,老師與學(xué)生之間為多對(duì)多的關(guān)系,即一個(gè)學(xué)生有多個(gè)老師,化學(xué)啦生物啦,一個(gè)老師也有很多學(xué)生;教室與學(xué)生之間為一對(duì)多的關(guān)系,即一間教室有多位學(xué)生(假定在這個(gè)教室的這些學(xué)生只在這一個(gè)教室上課),人物關(guān)系介紹完畢~

至于主外鍵,為數(shù)據(jù)庫(kù)基礎(chǔ)不再贅述

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,'博學(xué)');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,'里番番','數(shù)學(xué)','女','21',279376); INSERT INTO Teacher VALUES(22002,'大衛(wèi)','語(yǔ)文','男','22',279326); INSERT INTO Teacher VALUES(22003,'卡瑟琳','英語(yǔ)','女','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類(lèi)和數(shù)據(jù)庫(kù)的映射

先建三個(gè)pojo類(lèi),有人問(wèn)為什么要繼承Serializable,其實(shí)我們?cè)谧约弘娔X上做程序的時(shí)候可以不用寫(xiě)
它可以把對(duì)象轉(zhuǎn)換成字節(jié)流在網(wǎng)絡(luò)上傳輸,如果你不寫(xiě)自然沒(méi)法傳輸,那程序也就沒(méi)法使用
然后挨個(gè)寫(xiě)映射文件,這個(gè)對(duì)數(shù)據(jù)庫(kù)的熟練還是有點(diǎn)要求的,增刪改查相關(guān)操作都寫(xiě)在映射文件里,

association是用來(lái)映射一對(duì)一的關(guān)系及多對(duì)一的關(guān)系,collection用來(lái)映射一對(duì)多和多對(duì)多的關(guān)系,具體

方法如下

(和hibernate的區(qū)別參考上一篇博文。這些增刪改查的語(yǔ)句的引用都在dao包的實(shí)現(xiàn)類(lèi)里,通過(guò)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"/> <!--下面有個(gè)column是“C_Id”,我個(gè)人的理解是這個(gè)為當(dāng)前表的主鍵做了另一個(gè)表的外鍵,起到一個(gè)關(guān)聯(lián)作用,這是提供給另一個(gè)表的--><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"><!--之所以會(huì)用到兩個(gè)表我認(rèn)為是因?yàn)樵跀?shù)據(jù)庫(kù)中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"/><!--多對(duì)一--><association property="clazz" javaType="com.qdu.pojo.Clazz"><id property="clazzId" column="C_Id"/><result property="clazzAddress" column="C_Address"/></association><!--多對(duì)多--><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"><!--之所以會(huì)用到兩個(gè)表我認(rèn)為是因?yàn)樵跀?shù)據(jù)庫(kù)中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"/><!--多對(duì)一--><association property="clazz" javaType="com.qdu.pojo.Clazz"><id property="clazzId" column="C_Id"/><result property="clazzAddress" column="C_Address"/></association><!--多對(duì)多--><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) 接下來(lái)就是配置文件,重頭戲

配置文件分為Spring-mybatis配置文件和Spring MVC配置文件

Spring-mybatis配置文件的作用就是作為持久層框架起一個(gè)水渠的作用。

<?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配置文件作為請(qǐng)求分發(fā)器用來(lái)分發(fā)請(qǐng)求到制定的控制器

<?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了,把兩個(gè)配置文件向項(xiàng)目向程序說(shuō)明一下

<?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范圍內(nèi)的參數(shù)<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) 我習(xí)慣dao和service都寫(xiě)接口再實(shí)現(xiàn),這是個(gè)好習(xí)慣,符合框架低耦合的觀念,這次例外,dao接口沒(méi)寫(xiě),

可以自己補(bǔ)上。

@Repository用于標(biāo)注數(shù)據(jù)訪(fǎng)問(wèn)組件,即DAO組件

statement為mapper文件中的具體的sql語(yǔ)句

@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的事務(wù)注解,表示該類(lèi)里面的所有方法或者這個(gè)方法的事務(wù)由spring處理,
來(lái)保證事務(wù)的原子性, 每一個(gè)業(yè)務(wù)方法開(kāi)始時(shí)都會(huì)打開(kāi)一個(gè)事務(wù),這樣的好處,可以省去一些XML
配置文件的繁瑣編寫(xiě)
@Transactional 注解應(yīng)該只被應(yīng)用到 public 方法上,這是由 Spring AOP 的本質(zhì)決定的。如果你在 protected、private 或者默認(rèn)可見(jiàn)性的方法上使用 @Transactional 注解,這將被忽略,也不會(huì)拋出任何異常。


默認(rèn)情況下,只有來(lái)自外部的方法調(diào)用才會(huì)被AOP代理捕獲,也就是,類(lèi)內(nèi)部方法調(diào)用本類(lèi)內(nèi)部的其他方法并不會(huì)引起事務(wù)行為,即使被調(diào)用方法使用@Transactional注解進(jìn)行修飾。


事務(wù)管理對(duì)于企業(yè)應(yīng)用來(lái)說(shuō)是至關(guān)重要的,即使出現(xiàn)異常情況,它也可以保證數(shù)據(jù)的一致性


@Service為Spring的service注解,標(biāo)注服務(wù)類(lèi)


//為什么要用接口?! //第一種方式:建立個(gè)接口 //第二種方式:直接實(shí)例化 //第一種:比如你用Spring框架,可以在用到UserServiceImpl的時(shí)候定義接口,最后使用XML方式實(shí)例化,這樣以后需要修改,只要改xml(所謂的低耦合) //第二種:假設(shè)你直接在java文件中直接實(shí)例化,萬(wàn)一你不在用這個(gè)類(lèi)了,要用另外的類(lèi)來(lái)代替,需要改java文件,很麻煩(即所謂的低內(nèi)聚高耦合) //耦合度低的程序要好@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控制器注解,用于處理多個(gè)URL請(qǐng)求@RequestMapping 可以標(biāo)注在類(lèi)定義處,將 Controller 和特定請(qǐng)求關(guān)聯(lián)起來(lái);還可以標(biāo)注在方法簽名處,以便進(jìn)一步對(duì)請(qǐng)求進(jìn)行分流

@Controller @RequestMapping(value = "/anna") public class TestController {@Autowiredprivate StudentService studentServiceImpl;//調(diào)用父類(lèi)的方法,再調(diào)用子類(lèi)中的方法@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轉(zhuǎn)String驗(yàn)證可以+""啊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來(lái)源于前端的傳值,免去request,是不是很有趣?另外,邏輯語(yǔ)句要有先有后,第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";} }

最后就是頁(yè)面了,JSP頁(yè)面與JSTL以及EL表達(dá)式相結(jié)合,足夠滿(mǎn)足一般需求

頁(yè)面很多,貼出最主要的一兩個(gè)

首頁(yè)

不同身份登錄

<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>首頁(yè)</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">教師賬號(hào): <input type="text" name="teacherId" placeholder="在此輸入賬號(hào)"/><br/><br/>教師密碼: <input type="password" name="password" placeholder="在此輸入密碼"/><br/><br/><input type="submit" value="提交"/></form></body> </html>
登錄成功頁(yè)面

<%-- 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>學(xué)生列表</caption><tr><th>學(xué)生Id</th><th>學(xué)生姓名</th><th>學(xué)生性別</th><th>學(xué)生年齡</th><!--<th>學(xué)生教室</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>

運(yùn)行結(jié)果





? ? ? ? ?此為結(jié)束,歡迎大家提出問(wèn)題,共同探討

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?____Juin





總結(jié)

以上是生活随笔為你收集整理的SSM 小demo(很详细,适合新手)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 天天操天天摸天天干 | 日女人网站 | 国产精品调教 | 黄色三级三级三级三级 | 国产福利视频网站 | 日韩久久影视 | 九九九在线 | 国产一区二区三区网站 | av在线电影院| 动漫艳母在线观看 | 国产一级理论 | 久久久久成人精品无码 | 亚洲区国产区 | 四虎影视成人永久免费观看亚洲欧美 | 欧美 日韩 国产 成人 | 香港三日本8a三级少妇三级99 | 亚洲一区二区乱码 | 午夜影院久久 | 国产二区自拍 | 福利午夜视频 | 神马午夜伦 | 制服.丝袜.亚洲.中文.综合懂 | 国产av成人一区二区三区高清 | 国内精品视频一区 | 国产色一区二区 | 国产免费观看久久黄av片 | 色婷婷久久久亚洲一区二区三区 | 亚洲精品一区二区18漫画 | 欧美国产在线观看 | 久久av免费看 | 91av在线视频观看 | 色屋在线 | 永久免费,视频 | 可以免费观看的av网站 | 中文天堂在线观看 | 日本人妖在线 | 91精品中文字幕 | 91国产精品一区 | 色综合图片区 | 东方成人av在线 | 高级家教课程在线观看 | 亚洲性片| 国产欧美一区二区三区视频在线观看 | 久久久久久久久久成人 | av激情网站| 国产午夜精品久久 | 欧美www在线观看 | 国产电影一区在线观看 | 日本熟妇成熟毛茸茸 | 欧美草逼视频 | 人人澡人人透人人爽 | 开心激情深爱 | 黄色小视频免费 | 亚洲成人黄色片 | 69视频在线看 | 日本在线视频二区 | 久久精品国产亚洲av麻豆图片 | 性xx紧缚网站 | 国产一二三精品 | 精品久久久久久久久久久国产字幕 | 熟妇人妻一区二区三区四区 | 男生女生插插插 | 日日日干 | 韩日黄色片 | 好吊妞视频在线 | 一区二区三区精品在线 | 99re6热在线精品视频播放 | 91福利区 | 色屁屁在线 | 久草国产视频 | 亚洲黄视频 | 一级片在线免费播放 | 成人精品一区二区三区电影黑人 | 久久99久久久久 | 国产在线麻豆 | xxxxxx黄色| 国产欧美精品一区二区三区app | 日韩片在线观看 | 青娱乐97 | 欧美久草 | 成人91在线 | 亚洲一区二区色图 | 草久影院| free性欧美hd另类 | 美女av一区二区 | 欧美 日韩 成人 | 久久精品中文闷骚内射 | 一级黄色大片免费 | 国产在线18 | 偷拍第1页 | 在线观看av网站 | 少妇一边呻吟一边说使劲视频 | 成人黄色在线免费观看 | 亚洲少妇毛片 | 亚洲免费网 | 色在线看 | 日韩在线观看精品 | 国产精品99久久久久久久女警 | 永久免费在线看片 |