簡單選課系統
一.實體圖
二.功能
三.代碼實現
1.SSM環境搭建
(1)pom.xml
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <!-- 1.日志 --> <!-- 實現slf4j接口並整合 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.1</version> </dependency> <!-- 2.數據庫 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> <scope>runtime</scope> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- DAO: MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.3</version> </dependency> <!-- 3.Servlet web --> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.4</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <!-- 4.Spring --> <!-- 1)Spring核心 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.7.RELEASE</version> </dependency> <!-- 2)Spring DAO層 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.1.7.RELEASE</version> </dependency> <!-- 3)Spring web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.7.RELEASE</version> </dependency> <!-- 4)Spring test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.1.7.RELEASE</version> </dependency> <!-- redis客戶端:Jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>com.dyuproject.protostuff</groupId> <artifactId>protostuff-core</artifactId> <version>1.0.8</version> </dependency> <dependency> <groupId>com.dyuproject.protostuff</groupId> <artifactId>protostuff-runtime</artifactId> <version>1.0.8</version> </dependency> <!-- Map工具類 --> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2</version> </dependency> <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance --> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.9.3</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency> </dependencies>
(2)web.xml
<web-app> <display-name>Archetype Created Web Application</display-name> <!--配置spring的監聽器,默認只加載WEB-INF文件下的applicationContext.xml配置文件--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--設置配置文件的路徑--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--過濾器,處理中文亂碼--> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--配置前端控制器--> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!--啟動服務器,創建該servlet--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
(3)applicationContext.xml
<?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:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--掃描包,只掃描service和dao,controller不需要掃描--> <context:component-scan base-package="com.ssm.dao,com.ssm.service"></context:component-scan> <!--spring整合Mybatis框架--> <!--配置連接池--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/zml03"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> </bean> <!--配置SqlSessionFactory工廠--> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!--自動掃描UserDao.xml配置文件--> <property name="mapperLocations" value="classpath:com/ssm/dao/*.xml"></property> </bean> <!--配置UserDao接口所在的包--> <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property> <property name="basePackage" value="com.ssm.dao"></property> </bean> <!--配置spring框架聲明式事務管理--> <!--配置事務管理--> <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--配置事務通知--> <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager"> <tx:attributes> <tx:method name="find*" read-only="true"/> <tx:method name="*" isolation="DEFAULT"/> </tx:attributes> </tx:advice> <!-- 配置事務切面 --> <!--<aop:config>--> <!--<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ssm.service.Impl.*ServiceImpl.*(..))"></aop:advisor>--> <!--</aop:config>--> </beans>
(4)springmvc.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!--開啟注解掃描,只掃描controller中的注解--> <context:component-scan base-package="com.ssm.controller"></context:component-scan> <!--配置視圖解析器對象--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--配置前綴--> <property name="prefix" value="/"></property> <!--配置后綴--> <property name="suffix" value=".jsp"></property> </bean> <!--過濾靜態資源--> <mvc:resources mapping="/css/**" location="/css/"></mvc:resources> <mvc:resources mapping="/images/**" location="/images/"></mvc:resources> <mvc:resources mapping="/js/**" location="/js/"></mvc:resources> <!--開啟springmvc注解的支持--> <mvc:annotation-driven/> <mvc:default-servlet-handler/> </beans>
2.entity
(1)Course.java
package com.ssm.entity; public class Course { private String cno; private String cname; private int ctime; private int ccredit; private String tno; public String getCno() { return cno; } public void setCno(String cno) { this.cno = cno; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public int getCtime() { return ctime; } public void setCtime(int ctime) { this.ctime = ctime; } public int getCcredit() { return ccredit; } public void setCcredit(int ccredit) { this.ccredit = ccredit; } public String getTno() { return tno; } public void setTno(String tno) { this.tno = tno; } @Override public String toString() { return "Course{" + "cno='" + cno + '\'' + ", cname='" + cname + '\'' + ", ctime=" + ctime + ", ccredit=" + ccredit + ", tno='" + tno + '\'' + '}'; } }
(2)Dective.java
package com.ssm.entity; public class Dective { private int dno; private String sno; private String cno; private double achievement; public int getDno() { return dno; } public void setDno(int dno) { this.dno = dno; } public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getCno() { return cno; } public void setCno(String cno) { this.cno = cno; } public double getAchievement() { return achievement; } public void setAchievement(double achievement) { this.achievement = achievement; } @Override public String toString() { return "Dective{" + "dno=" + dno + ", sno='" + sno + '\'' + ", cno='" + cno + '\'' + ", achievement=" + achievement + '}'; } }
(3)Student.java
package com.ssm.entity; public class Student { private String sno; private String sname; private String ssex; private String spassword; public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSsex() { return ssex; } public void setSsex(String ssex) { this.ssex = ssex; } public String getSpassword() { return spassword; } public void setSpassword(String spassword) { this.spassword = spassword; } @Override public String toString() { return "Student{" + "sno='" + sno + '\'' + ", sname='" + sname + '\'' + ", ssex='" + ssex + '\'' + ", spassword='" + spassword + '\'' + '}'; } }
(4)Teacher.java
package com.ssm.entity; public class Teacher { private String tno; private String tname; private String tsex; private String tpassword; private String ttitle; public String getTno() { return tno; } public void setTno(String tno) { this.tno = tno; } public String getTname() { return tname; } public void setTname(String tname) { this.tname = tname; } public String getTsex() { return tsex; } public void setTsex(String tsex) { this.tsex = tsex; } public String getTpassword() { return tpassword; } public void setTpassword(String tpassword) { this.tpassword = tpassword; } public String getTtitle() { return ttitle; } public void setTtitle(String ttitle) { this.ttitle = ttitle; } @Override public String toString() { return "Teacher{" + "tno='" + tno + '\'' + ", tname='" + tname + '\'' + ", tsex='" + tsex + '\'' + ", tpassword='" + tpassword + '\'' + ", ttitle='" + ttitle + '\'' + '}'; } }
3.dao接口
(1)CourseDao.java
package com.ssm.dao; import com.ssm.entity.Course; import com.ssm.view.courseVo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface CourseDao { //查詢所有課程信息 public List<Course> selectCourse(); //查看可選的所有課程 public List<courseVo> selectAllCourse(); }
(2)DectiveDao.java
package com.ssm.dao; import com.ssm.entity.Dective; import com.ssm.entity.Student; import com.ssm.view.DectiveVo; import com.ssm.view.StudentVo; import com.ssm.view.courseVo; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface DectiveDao { //插入選課記錄 public void insertDective(Dective dective); //學生查詢自己的選課記錄 public List<courseVo> selectMy(String sno); //刪除選課記錄 public void deleteDective(Dective dective); //查詢成績 public List<DectiveVo> selectGrade(String sno); //查詢選擇了某個課程的所有學生信息 public List<StudentVo> courseStudent(String cname); //錄入成績 public void grade(@Param("sno") String sno,@Param("cno") String cno,@Param("achievement") double achievement); }
(3)StudentDao.java
package com.ssm.dao; import com.ssm.entity.Student; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; @Repository public interface StudentDao { //根據sno查找用戶的密碼,檢測登錄 public String selectSpassword(String sno); //根據sno查詢學生信息 public Student selectone(String sno); //修改學生信息 public void stuUpdate(@Param("spassword") String spassword,@Param("sno") String sno); }
(4)TeacherDao.java
package com.ssm.dao; import com.ssm.entity.Teacher; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface TeacherDao { //根據tno查找用戶的密碼,檢測登錄 public String selectTpassword(String tno); //根據tno查詢老師信息 public Teacher selectte(String tno); //修改老師信息 public void updatete(@Param("tpassword") String tpassword,@Param("tno") String tno); //查詢某個老師所教的所有課程 public List<String> selectMyCourse(String tno); }
4.dao映射文件
(1)CourseDao.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPEmapper PUBLIC"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.ssm.dao.CourseDao"> <select id="selectCourse" resultType="com.ssm.entity.Course"> select * from course </select> <!--查詢課程信息--> <select id="selectAllCourse" resultType="com.ssm.view.courseVo"> select c.*,t.tname from course c,teacher t where c.tno = t.tno </select> </mapper>
(2)DectiveDao.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPEmapper PUBLIC"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.ssm.dao.DectiveDao"> <!--插入選課記錄--> <insert id="insertDective" parameterType="com.ssm.entity.Dective"> insert into dective(sno,cno) values(#{sno},#{cno}) </insert> <!--刪除選課記錄--> <delete id="deleteDective" parameterType="com.ssm.entity.Dective"> delete from dective where cno=#{cno} and sno=#{sno} </delete> <!--學生查詢自己的選課記錄,包括課程名稱和授課教師姓名--> <select id="selectMy" parameterType="String" resultType="com.ssm.view.courseVo"> select c.*,t.tname from dective d LEFT OUTER JOIN course c on d.cno=c.cno LEFT OUTER JOIN teacher t on c.tno=t.tno where d.sno=#{sno} </select> <!--學生查詢成績--> <select id="selectGrade" parameterType="String" resultType="com.ssm.view.DectiveVo"> select achievement ,c.cname from dective d LEFT OUTER JOIN course c on d.cno=c.cno where sno=#{sno} </select> <!--查詢選擇某個課程的所有學生--> <select id="courseStudent" parameterType="String" resultType="com.ssm.view.StudentVo"> select s.* ,c.cno,d.achievement from course c LEFT OUTER JOIN dective d on c.cno=d.cno LEFT OUTER JOIN student s on s.sno=d.sno where c.cname=#{cname} </select> <!--錄入成績--> <update id="grade" parameterType="com.ssm.entity.Dective"> update dective set achievement=#{achievement} where sno=#{sno} and cno=#{cno} </update> </mapper>
(3)StudentDao.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPEmapper PUBLIC"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.ssm.dao.StudentDao"> <!--學生登錄--> <select id="selectSpassword" resultType="String"> select spassword from student where sno=#{sno} </select> <!--根據sno查詢學生的信息--> <select id="selectone" resultType="com.ssm.entity.Student" parameterType="String"> select * from student where sno=#{sno} </select> <!--修改學生信息--> <update id="stuUpdate" parameterType="String"> update student set spassword=#{spassword} where sno=#{sno} </update> </mapper>
(4)TeacherDao.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPEmapper PUBLIC"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.ssm.dao.TeacherDao"> <!--老師登錄--> <select id="selectTpassword" resultType="String"> select tpassword from teacher where tno=#{tno} </select> <select id="selectte" parameterType="String" resultType="com.ssm.entity.Teacher"> select * from teacher where tno=#{tno} </select> <!--老師修改信息--> <update id="updatete" parameterType="String"> update teacher set tpassword=#{tpassword} where tno=#{tno} </update> <!--查詢老師的所有課程--> <select id="selectMyCourse" resultType="String" parameterType="String"> select cname from course where tno=#{tno} </select> </mapper>
5.service接口
(1)CourseService.java
package com.ssm.service; import com.ssm.entity.Course; import com.ssm.view.courseVo; import java.util.List; public interface CourseService { //查詢所有課程信息 public List<Course> selectCourse(); //查看可選的所有課程 public List<courseVo> selectAllCourse(); }
(2)DectiveService.java
package com.ssm.service; import com.ssm.entity.Dective; import com.ssm.entity.Student; import com.ssm.view.DectiveVo; import com.ssm.view.StudentVo; import com.ssm.view.courseVo; import org.apache.ibatis.annotations.Param; import java.util.List; public interface DectiveService { //插入選課記錄 public void insertDective(Dective dective); //學生查詢自己的選課記錄 public List<courseVo> selectMy(String sno); //刪除選課記錄 public void deleteDective(Dective dective); //查詢成績 public List<DectiveVo> selectGrade(String sno); //查詢選擇了某個課程的所有學生信息 public List<StudentVo> courseStudent(String cname); //錄入成績 public void grade(String sno, String cno,double achievement); }
(3)StudentService.java
package com.ssm.service; import com.ssm.entity.Student; public interface StudentService { //根據sno查找用戶的密碼,檢測登錄 public String selectSpassword(String sno); //根據sno查詢學生信息 public Student selectone(String sno); //修改學生信息 public void stuUpdate(String spassword,String sno); }
(4)TeacherService.java
package com.ssm.service; import com.ssm.entity.Teacher; import java.util.List; public interface TeacherService { //根據tno查找用戶的密碼,檢測登錄 public String selectTpassword(String tno); //根據tno查詢老師信息 public Teacher selectte(String tno); //修改老師信息 public void updatete(String tpassword,String tno); //查詢某個老師所教的所有課程 public List<String> selectMyCourse(String tno); }
6.service實現類
(1)CourseServiceImpl.java
package com.ssm.service.Impl; import com.ssm.dao.CourseDao; import com.ssm.entity.Course; import com.ssm.service.CourseService; import com.ssm.view.courseVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class CourseServiceImpl implements CourseService { @Autowired private CourseDao courseDao; @Override public List<Course> selectCourse() { return courseDao.selectCourse(); } @Override public List<courseVo> selectAllCourse() { return courseDao.selectAllCourse(); } }
(2)DectiveServiceImpl.java
package com.ssm.service.Impl; import com.ssm.dao.DectiveDao; import com.ssm.entity.Dective; import com.ssm.entity.Student; import com.ssm.service.DectiveService; import com.ssm.view.DectiveVo; import com.ssm.view.StudentVo; import com.ssm.view.courseVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class DectiveServiceImpl implements DectiveService { @Autowired private DectiveDao dectiveDao; //插入選課記錄 @Override public void insertDective(Dective dective) { dectiveDao.insertDective(dective); } //學生查詢自己的選課記錄 @Override public List<courseVo> selectMy(String sno) { return dectiveDao.selectMy(sno); } //刪除選課記錄 @Override public void deleteDective(Dective dective) { dectiveDao.deleteDective(dective); } @Override public List<DectiveVo> selectGrade(String sno) { return dectiveDao.selectGrade(sno); } @Override public List<StudentVo> courseStudent(String cname) { return dectiveDao.courseStudent(cname); } @Override public void grade(String sno, String cno,double achievement) { dectiveDao.grade(sno,cno,achievement); } }
(3)StudentServiceImpl.java
package com.ssm.service.Impl; import com.ssm.dao.StudentDao; import com.ssm.entity.Student; import com.ssm.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class StudentServiceImpl implements StudentService { @Autowired private StudentDao studentDao; @Override public String selectSpassword(String sno) { return studentDao.selectSpassword(sno); } @Override public Student selectone(String sno) { return studentDao.selectone(sno); } @Override public void stuUpdate(String spassword,String sno) { studentDao.stuUpdate(spassword,sno); } }
(4)TeacherServiceImpl.java
package com.ssm.service.Impl; import com.ssm.dao.TeacherDao; import com.ssm.entity.Teacher; import com.ssm.service.TeacherService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class TeacherServiceImpl implements TeacherService { @Autowired private TeacherDao teacherDao; @Override public String selectTpassword(String tno) { return teacherDao.selectTpassword(tno); } @Override public Teacher selectte(String tno) { System.out.println(teacherDao.selectte(tno)); return teacherDao.selectte(tno); } @Override public void updatete(String tpassword, String tno) { teacherDao.updatete(tpassword,tno); } @Override public List<String> selectMyCourse(String tno) { return teacherDao.selectMyCourse(tno); } }
7.controller
(1)CourseController.java
package com.ssm.controller; import com.ssm.service.Impl.CourseServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/courseController") public class CourseController { @Autowired private CourseServiceImpl courseService; @RequestMapping("/selectCourse") public ModelAndView selectCourse(){ ModelAndView modelAndView=new ModelAndView(); modelAndView.addObject("list",courseService.selectCourse()); modelAndView.setViewName("list"); return modelAndView; } //查詢所有課程信息 @RequestMapping("/selectAllCourse") public ModelAndView selectAllCourse(){ ModelAndView modelAndView=new ModelAndView(); modelAndView.addObject("courseVo",courseService.selectAllCourse()); modelAndView.setViewName("courseVo"); return modelAndView; } //查詢所有課程信息 @RequestMapping("/selectAll") public ModelAndView selectAll(){ ModelAndView modelAndView=new ModelAndView(); modelAndView.addObject("lookCourse",courseService.selectAllCourse()); modelAndView.setViewName("lookCourse"); return modelAndView; } }
(2)DectiveController.java
package com.ssm.controller; import com.ssm.entity.Dective; import com.ssm.service.Impl.DectiveServiceImpl; import com.ssm.view.DectiveList; import com.ssm.view.DectiveVo; import com.ssm.view.StudentVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpSession; import java.util.List; @Controller @RequestMapping("/dectiveController") public class DectiveController { @Autowired private DectiveServiceImpl dectiveService; //插入選課記錄 @RequestMapping("/insertDective") public String insertDective(HttpSession httpSession,String cno){ Dective dective=new Dective(); dective.setCno(cno); dective.setSno((String)httpSession.getAttribute("sno")); System.out.println("cno:"+cno+",sno:"+httpSession.getAttribute("sno")); dectiveService.insertDective(dective); return "redirect:/courseController/selectAllCourse"; } //查詢自己選過的課程 @RequestMapping("/selectMy") public ModelAndView selectMy(HttpSession httpSession){ ModelAndView modelAndView=new ModelAndView(); modelAndView.addObject("selectMy",dectiveService.selectMy((String)httpSession.getAttribute("sno"))); modelAndView.setViewName("myDective"); return modelAndView; } //刪除選課記錄 @RequestMapping("/deleteDective") public String deleteDective(HttpSession httpSession,String cno){ Dective dective=new Dective(); dective.setCno(cno); dective.setSno((String)httpSession.getAttribute("sno")); dectiveService.deleteDective(dective); return "redirect:selectMy"; } //查詢成績 @RequestMapping("selectGrade") public ModelAndView selectGrade(HttpSession session){ ModelAndView modelAndView=new ModelAndView(); List<DectiveVo> list=dectiveService.selectGrade((String)session.getAttribute("sno")); System.out.println(list); modelAndView.addObject("selectGrade",list); modelAndView.setViewName("myGrade"); return modelAndView; } //查詢選擇了某個課程的所有學生信息 @RequestMapping("/courseStudent") public ModelAndView courseStudent(String cname){ ModelAndView modelAndView=new ModelAndView(); modelAndView.addObject("student",dectiveService.courseStudent(cname)); modelAndView.setViewName("courseStudent"); return modelAndView; } //查詢選擇某個課程的所有學生 @RequestMapping("/courseAllStudent") public ModelAndView courseAllStudent(String cname){ ModelAndView modelAndView=new ModelAndView(); modelAndView.addObject("studentVo",dectiveService.courseStudent(cname)); modelAndView.setViewName("grade"); return modelAndView; } //錄入成績 @RequestMapping("/grade") public String grade(DectiveList dectiveList){ System.out.println(dectiveList); for(Dective dective:dectiveList.getDectiveList()){ String sno=dective.getSno(); String cno=dective.getCno(); double achievement=dective.getAchievement(); dectiveService.grade(sno,cno,achievement); } return "redirect:/teacherController/selectAllCourse"; } }
(3)StudentController.java
package com.ssm.controller; import com.ssm.service.Impl.StudentServiceImpl; import org.omg.CORBA.Request; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpSession; @Controller @RequestMapping("/studentController") public class StudentController { @Autowired private StudentServiceImpl studentService; //檢測登錄 @RequestMapping("/selectSpassword") public String selectSpassword(HttpSession session, String sno, String password){ String pwd=studentService.selectSpassword(sno); System.out.println(pwd); if(pwd.equals(password)){ session.setAttribute("sno",sno); return "redirect:selectone"; }else{ return "index"; } } //根據sno查詢信息 @RequestMapping("/selectone") public ModelAndView selectone(HttpSession session){ ModelAndView modelAndView=new ModelAndView(); modelAndView.addObject("student",studentService.selectone((String)session.getAttribute("sno"))); modelAndView.setViewName("student"); return modelAndView; } //根據sno查詢信息 @RequestMapping("/selectstu") public ModelAndView selectstu(HttpSession session){ ModelAndView modelAndView=new ModelAndView(); modelAndView.addObject("student",studentService.selectone((String)session.getAttribute("sno"))); modelAndView.setViewName("studentUpdate"); return modelAndView; } //修改學生信息 @RequestMapping("stuUpdate") public String stuUpdate(HttpSession session,String spassword){ System.out.println("密 碼: "+spassword); studentService.stuUpdate(spassword,(String)session.getAttribute("sno")); return "redirect:selectone"; } }
(4)TeacherController.java
package com.ssm.controller; import com.ssm.entity.Teacher; import com.ssm.service.Impl.TeacherServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpSession; @Controller @RequestMapping("/teacherController") public class TeacherController { @Autowired private TeacherServiceImpl teacherService; //老師登錄 @RequestMapping("/selectTpassword") public String selectTpassword(HttpSession session,String tno ,String tpassword){ String pwd=teacherService.selectTpassword(tno); System.out.println(pwd); if(pwd.equals(tpassword)){ session.setAttribute("tno", tno); return "redirect:selectte"; }else{ return "index"; } } //根據tno查詢老師信息 @RequestMapping("/selectte") public ModelAndView selectte(HttpSession session){ ModelAndView modelAndView=new ModelAndView(); String t=(String)session.getAttribute("tno"); Teacher te=teacherService.selectte(t); modelAndView.addObject("teacher",te); System.out.println(te); modelAndView.setViewName("teacher"); return modelAndView; } @RequestMapping("/selectone") public ModelAndView selectone(HttpSession session){ ModelAndView modelAndView=new ModelAndView(); String t=(String)session.getAttribute("tno"); Teacher te=teacherService.selectte(t); modelAndView.addObject("teacher",te); System.out.println(te); modelAndView.setViewName("teacherUpdate"); return modelAndView; } //修改老師信息 @RequestMapping("/updatete") public String updatete(HttpSession session,String tpassword){ teacherService.updatete(tpassword,(String)session.getAttribute("tno")); return "redirect:selectte"; } //查詢老師的所有課程 @RequestMapping("/selectMyCourse") public ModelAndView selectMyCourse(HttpSession session){ ModelAndView modelAndView=new ModelAndView(); modelAndView.addObject("cname",teacherService.selectMyCourse((String)session.getAttribute("tno"))); modelAndView.setViewName("teacherCourse"); return modelAndView; } //查詢老師的所有課程 @RequestMapping("/selectAllCourse") public ModelAndView selectAllCourse(HttpSession session){ ModelAndView modelAndView=new ModelAndView(); modelAndView.addObject("cname",teacherService.selectMyCourse((String)session.getAttribute("tno"))); modelAndView.setViewName("courseAll"); return modelAndView; } }
8.view
(1)CourseVo.java
package com.ssm.view; public class courseVo { private String cno; private String cname; private int ctime; private int tno; private String tname; public courseVo() { } public String getCno() { return cno; } public void setCno(String cno) { this.cno = cno; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public int getCtime() { return ctime; } public void setCtime(int ctime) { this.ctime = ctime; } public int getTno() { return tno; } public void setTno(int tno) { this.tno = tno; } public String getTname() { return tname; } public void setTname(String tname) { this.tname = tname; } public courseVo(String cno, String cname, int ctime, int tno, String tname) { this.cno = cno; this.cname = cname; this.ctime = ctime; this.tno = tno; this.tname = tname; } @Override public String toString() { return "courseVo{" + "cno='" + cno + '\'' + ", cname='" + cname + '\'' + ", ctime=" + ctime + ", tno=" + tno + ", tname='" + tname + '\'' + '}'; } }
(2)DectiveList.java
package com.ssm.view; import com.ssm.entity.Dective; import java.util.List; public class DectiveList { private List<Dective> dectiveList; public List<Dective> getDectiveList() { return dectiveList; } public void setDectiveList(List<Dective> dectiveList) { this.dectiveList = dectiveList; } @Override public String toString() { return "DectiveList{" + "dectiveList=" + dectiveList + '}'; } }
(3)DectiveVo.java
package com.ssm.view; public class DectiveVo { private String cname; private double achievement; public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public double getAchievement() { return achievement; } public void setAchievement(double achievement) { this.achievement = achievement; } @Override public String toString() { return "DectiveVo1{" + "cname='" + cname + '\'' + ", achievement=" + achievement + '}'; } }
(4)StudentVo.java
package com.ssm.view; public class StudentVo { private String sno; private String sname; private String ssex; private String spassword; private String cno; private double achievement; public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSsex() { return ssex; } public void setSsex(String ssex) { this.ssex = ssex; } public String getSpassword() { return spassword; } public void setSpassword(String spassword) { this.spassword = spassword; } public String getCno() { return cno; } public void setCno(String cno) { this.cno = cno; } public double getAchievement() { return achievement; } public void setAchievement(double achievement) { this.achievement = achievement; } @Override public String toString() { return "StudentVo{" + "sno='" + sno + '\'' + ", sname='" + sname + '\'' + ", ssex='" + ssex + '\'' + ", spassword='" + spassword + '\'' + ", cno='" + cno + '\'' + ", achievement=" + achievement + '}'; } }
9.jsp頁面
(1)courseAll.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@page isELIgnored="false" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>Title</title> </head> <body> <c:forEach items="${cname}" var="name"> <a href="/dectiveController/courseAllStudent?cname=${name}">${name}</a> </c:forEach> </body> </html>
(2)courseStudent.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@page isELIgnored="false" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>Title</title> </head> <body> <table> <tr> <td>學號</td> <td>姓名</td> </tr> <c:forEach items="${student}" var="Student"> <tr> <td>${Student.sno}</td> <td>${Student.sname}</td> </tr> </c:forEach> </table> </body> </html>
(3)courseVo.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@page isELIgnored="false" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>Title</title> </head> <body> <form> <table> <tr> <td>課程編號</td> <td>課程名稱</td> <td>課時</td> <td>教師編號</td> <td>教師姓名</td> <td></td> </tr> <c:forEach items="${courseVo}" var="CourseVo"> <tr> <td>${CourseVo.cno}</td> <td>${CourseVo.cname}</td> <td>${CourseVo.ctime}</td> <td>${CourseVo.tno}</td> <td>${CourseVo.tname}</td> <td><a href="/dectiveController/insertDective?cno=${CourseVo.cno}">選課</a> </td> </tr> </c:forEach> </table> </form> </body> </html>
(4)grade.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@page isELIgnored="false" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>Title</title> </head> <body> <%! int i=-1; %> <form action="/dectiveController/grade" method="post"> <table> <tr> <td>課程編號</td> <td>學號</td> <td>姓名</td> <td>成績</td> </tr> <c:forEach items="${studentVo}" var="Student"> <% i=i+1;%> <tr> <td><input type="text" name="dectiveList[<%=i%>].cno" value="${Student.cno}" readonly="readonly"></td> <td><input type="text" name="dectiveList[<%=i%>].sno" value="${Student.sno}" readonly="readonly"></td> <td><input type="text" name="sname" value="${Student.sname}" readonly="readonly"></td> <td><input type="text" name="dectiveList[<%=i%>].achievement" value="${Student.achievement}" ></td> </tr> </c:forEach> </table> <input type="submit"> </form> </body> </html>
(5)index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> 學生<a href="teacherlogin.jsp">老師</a> <form action="studentController/selectSpassword" method="post"> 學號:<input type="text" name="sno"> 密碼:<input type="text" name="password"> <input type="submit" > </form> </body> </html>
(6)list.jsp
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/8/17 0017 Time: 上午 10:44 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@page isELIgnored="false" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>Title</title> </head> <body> ${list} <table> <c:forEach items="${list2}" var="CourseVo"> <tr> <td>${CourseVo.cno}</td> <td>${CourseVo.cname}</td> <td>${CourseVo.ctime}</td> <td>${CourseVo.tno}</td> <td>${CourseVo.tname}</td> <td></td> </tr> </c:forEach> </table> </body> </html>
(7)lookCourse.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@page isELIgnored="false" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>Title</title> </head> <body> <table> <tr> <td>課程編號</td> <td>課程名稱</td> <td>課時</td> <td>教師編號</td> <td>教師姓名</td> </tr> <c:forEach items="${lookCourse}" var="CourseVo"> <tr> <td>${CourseVo.cno}</td> <td>${CourseVo.cname}</td> <td>${CourseVo.ctime}</td> <td>${CourseVo.tno}</td> <td>${CourseVo.tname}</td> </tr> </c:forEach> </table> </body> </html>
(8)myDective.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@page isELIgnored="false" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>Title</title> </head> <body> <form> <table> <tr> <td>課程編號</td> <td>課程名稱</td> <td>課時</td> <td>教師編號</td> <td>教師姓名</td> <td></td> </tr> <c:forEach items="${selectMy}" var="CourseVo"> <tr> <td>${CourseVo.cno}</td> <td>${CourseVo.cname}</td> <td>${CourseVo.ctime}</td> <td>${CourseVo.tno}</td> <td>${CourseVo.tname}</td> <td><a href="/dectiveController/deleteDective?cno=${CourseVo.cno}">刪除</a> </td> </tr> </c:forEach> </table> </form> </body> </html>
(9)myGrade.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@page isELIgnored="false" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>Title</title> </head> <body> <form> <table> <tr> <td>課程名稱</td> <td>成績</td> </tr> <c:forEach items="${selectGrade}" var="DectiveVo"> <tr> <td>${DectiveVo.cname}</td> <td>${DectiveVo.achievement}</td> </tr> </c:forEach> </table> </form> </body> </html>
(10)student.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@page isELIgnored="false" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>Title</title> </head> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <a href="/courseController/selectAllCourse">選課</a><br> <a href="/dectiveController/selectMy">查詢已選課程</a><br> <a href="/dectiveController/selectGrade">查詢成績</a><br> <a href="/studentController/selectstu">修改信息</a> <table> <tr> <td>學號</td> <td>${student.sno}</td> </tr> <tr> <td>姓名</td> <td>${student.sname}</td> </tr> <tr> <td>性別</td> <td>${student.ssex}</td> </tr> <tr> <td>密碼</td> <td>${student.spassword}</td> </tr> </table> </body> </html>
(11)studentUpdate.jsp
<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <form action="/studentController/stuUpdate" method="post"> <table> <tr> <td>學號</td> <td><input type="text" value="${student.sno}" readonly="readonly"></td> </tr> <tr> <td>姓名</td> <td><input type="text" value="${student.sname}" readonly="readonly"></td> </tr> <tr> <td>登錄密碼</td> <td><input type="text" name="spassword" value="${student.spassword}"></td> </tr> </table> <input type="submit"> </form> </body> </html>
(12)teacher.jsp
<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <%@page isELIgnored="false" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <a href="/teacherController/selectone">修改</a><br> <a href="/courseController/selectAll">查詢課程信息</a></a><br> <a href="/teacherController/selectMyCourse">查詢學生選課信息</a></a><br> <a href="/teacherController/selectAllCourse">錄入成績</a></a><br> <table> <tr> <td>教師編號:</td> <td>${teacher.tno}</td> </tr> <tr> <td>教師姓名:</td> <td>${teacher.tname}</td> </tr> <tr> <td>登錄密碼:</td> <td>${teacher.tpassword}</td> </tr> </table> </body> </html>
(13)teacherCourse.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@page isELIgnored="false" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>Title</title> </head> <body> <c:forEach items="${cname}" var="name"> <a href="/dectiveController/courseStudent?cname=${name}">${name}</a> </c:forEach> </body> </html>
(14)teacherlogin.jsp
<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <a href="index.jsp">學生</a>老師 <form action="teacherController/selectTpassword" method="post"> 教師編號:<input type="text" name="tno"> 密碼:<input type="text" name="tpassword"> <input type="submit"/> </form> </body> </html>
(15)teacherUpdate.jsp
<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <form action="/teacherController/updatete" method="post"> <table> <tr> <td>教師編號</td> <td><input type="text" value="${teacher.tno}" readonly="readonly"></td> </tr> <tr> <td>教師姓名</td> <td><input type="text" value="${teacher.tname}" readonly="readonly"></td> </tr> <tr> <td>登錄密碼</td> <td><input type="text" name="tpassword" value="${teacher.tpassword}"></td> </tr> </table> <input type="submit"> </form> </body> </html>
=========================demo21==========================