web.xml StudentManagerSSM.rar
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://java.sun.com/jsf/composite" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>Archetype Created Web Application</display-name> <listener> <listener-class>com.kikyo.listener.StartSystemListener</listener-class> </listener> <!-- 编码过滤器 --> <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> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置DispatcherServlet --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置springMVC需要加载的配置文件--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- 匹配所有请求 --> <url-pattern>/</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> </web-app>
spring-mvc.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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 扫描web相关的bean --> <context:component-scan base-package="com.kikyo.controller"/> <!-- 开启SpringMVC注解模式 --> <mvc:annotation-driven/> <!-- 配置jsp 显示ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
------
<!-- 配置springmvc对文件上传的支持 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设置文件名的编码 --> <property name="defaultEncoding" value="utf-8"></property> <!-- 配置最上传文件的支持 20M --> <property name="maxUploadSize" value="20971520"></property> <!-- 设置文件上传的临时目录 --> <property name="uploadTempDir" value="upload/temp" /> </bean> <!-- 设置静态资源不过滤 --> <mvc:resources location="/css/" mapping="/css/**" /> <mvc:resources location="/images/" mapping="/images/**" /> <mvc:resources location="/js/" mapping="/js/**" /> <!-- 配置静态文件放行 --> <mvc:default-servlet-handler />
spring-mybatis.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" 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"> <!-- 扫描service包下所有使用注解的类型 --> <context:component-scan base-package="com.kikyo.service"/> <!-- 配置数据库相关参数properties的属性:${url} --> <context:property-placeholder location="classpath:properties/jdbc.properties"/> <!-- 数据库连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/> <property name="minPoolSize" value="${c3p0.minPoolSize}"/> <property name="autoCommitOnClose" value="${c3p0.autoCommitOnClose}"/> <property name="checkoutTimeout" value="${c3p0.checkoutTimeout}"/> <property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}"/> </bean> <!-- 配置SqlSessionFactory对象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource"/> <!-- 扫描entity包 使用别名 --> <property name="typeAliasesPackage" value="com.kikyo.entity"/> <!-- 扫描sql配置文件:mapper需要的xml文件 --> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!-- 配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- 给出需要扫描Dao接口包 --> <property name="basePackage" value="com.kikyo.dao"/> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置基于注解的声明式事务 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
1
<!-- 配置SqlSessionFactory对象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource"/> <!-- 扫描entity包 使用别名 --> <property name="typeAliasesPackage" value="com.kikyo.*.domain"/> <!-- 扫描sql配置文件:mapper需要的xml文件 --> <property name="mapperLocations" value="classpath:mapper/*/*.xml"/> </bean> <!-- 配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- 给出需要扫描Dao接口包 --> <property name="basePackage" value="com.kikyo.*.mapper"/> </bean>
pom.xml
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <!-- 设置项目编码编码 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- spring版本号 --> <spring.version>4.3.5.RELEASE</spring.version> <!-- mybatis版本号 --> <mybatis.version>3.4.1</mybatis.version> <ehcache.version>2.6.11</ehcache.version> <ehcache-web.version>2.0.4</ehcache-web.version> </properties> <dependencies> <!-- CACHE begin --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>${ehcache.version}</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-web</artifactId> <version>${ehcache-web.version}</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.5.1</version> </dependency> <!-- CACHE end --> <!-- jstl标签 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-impl</artifactId> <version>1.2.5</version> </dependency> <!-- java ee --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!-- 实现slf4j接口并整合 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.2</version> </dependency> <!-- JSON --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.7</version> </dependency> <!-- 数据库 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.41</version> <scope>runtime</scope> </dependency> <!-- 数据库连接池 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- mybatis/spring整合包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> </dependencies>
StartSystemListener
import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class StartSystemListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { //1.将项目上下文路径(request.getContextPath())放置到application域中. ServletContext application = sce.getServletContext(); application.setAttribute("ctx", application.getContextPath()); System.err.println("---------Servlet容器创建成功 ctx 被放到ServletContext作用域-------"); //2.加载所有许可路径 //ApplicationContext ioc = WebApplicationContextUtils.getWebApplicationContext(application); } @Override public void contextDestroyed(ServletContextEvent sce) { } }
StudentController
package com.kikyo.controller; import com.kikyo.entity.Student; import com.kikyo.service.StudentService; import com.kikyo.util.Page; 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.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; @Controller @RequestMapping("") public class StudentController { @Autowired private StudentService studentService; @RequestMapping("/addStudent") public String addStudent(HttpServletRequest request, HttpServletResponse response) { Student student = new Student(); int studentID = Integer.parseInt(request.getParameter("student_id")); String name = request.getParameter("name"); int age = Integer.parseInt(request.getParameter("age")); String sex = request.getParameter("sex"); Date birthday = null; // String 类型按照 yyyy-MM-dd 的格式转换为 java.util.Date 类 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { birthday = simpleDateFormat.parse(request.getParameter("birthday")); } catch (ParseException e) { e.printStackTrace(); } student.setStudent_id(studentID); student.setName(name); student.setAge(age); student.setSex(sex); student.setBirthday(birthday); studentService.addStudent(student); return "redirect:listStudent"; } @RequestMapping("/listStudent") public String listStudent(HttpServletRequest request, HttpServletResponse response) { // 获取分页参数 int start = 0; int count = 10; try { start = Integer.parseInt(request.getParameter("page.start")); count = Integer.parseInt(request.getParameter("page.count")); } catch (Exception e) { } Page page = new Page(start, count); List<Student> students = studentService.list(page.getStart(), page.getCount()); int total = studentService.getTotal(); page.setTotal(total); request.setAttribute("students", students); request.setAttribute("page", page); return "listStudent"; } @RequestMapping("/deleteStudent") public String deleteStudent(int id) { studentService.deleteStudent(id); return "redirect:listStudent"; } @RequestMapping("/editStudent") public ModelAndView editStudent(int id) { ModelAndView mav = new ModelAndView("editStudent"); Student student = studentService.getStudent(id); mav.addObject("student", student); return mav; } @RequestMapping("/updateStudent") public String updateStudent(HttpServletRequest request, HttpServletResponse response) { Student student = new Student(); int id = Integer.parseInt(request.getParameter("id")); int student_id = Integer.parseInt(request.getParameter("student_id")); String name = request.getParameter("name"); int age = Integer.parseInt(request.getParameter("age")); String sex = request.getParameter("sex"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date birthday = null; try { birthday = simpleDateFormat.parse(request.getParameter("birthday")); } catch (ParseException e) { e.printStackTrace(); } student.setId(id); student.setStudent_id(student_id); student.setName(name); student.setAge(age); student.setSex(sex); student.setBirthday(birthday); studentService.updateStudent(student); return "redirect:listStudent"; } }
StudentService
package com.kikyo.service; import com.kikyo.entity.Student; import java.util.List; public interface StudentService { /** * 获取到 Student 的总数 * @return */ int getTotal(); /** * 增加一条数据 * @param student */ void addStudent(Student student); /** * 删除一条数据 * @param id */ void deleteStudent(int id); /** * 更新一条数据 * @param student */ void updateStudent(Student student); /** * 找到一条数据 * @param id * @return */ Student getStudent(int id); /** * 列举出从 start 位置开始的 count 条数据 * @param start * @param count * @return */ List<Student> list(int start, int count); }
StudentServiceImpl
package com.kikyo.service; import com.kikyo.dao.StudentDao; import com.kikyo.entity.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class StudentServiceImpl implements StudentService { @Autowired StudentDao studentDao; public int getTotal() { return studentDao.getTotal(); } public void addStudent(Student student) { studentDao.addStudent(student); } public void deleteStudent(int id) { studentDao.deleteStudent(id); } public void updateStudent(Student student) { studentDao.updateStudent(student); } public Student getStudent(int id) { return studentDao.getStudent(id); } public List<Student> list(int start, int count) { return studentDao.list(start, count); } }
StudentDao
package com.kikyo.dao; import com.kikyo.entity.Student; import java.util.List; public interface StudentDao { int getTotal(); void addStudent(Student student); void deleteStudent(int id); void updateStudent(Student student); Student getStudent(int id); List<Student> list(int start, int count); }
StudentDao.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 将namespace的值设置为DAO类对应的路径 --> <mapper namespace="com.kikyo.dao.StudentDao"> <!-- 查询数据条目 --> <select id="getTotal" resultType="int"> SELECT COUNT(*) FROM student </select> <!-- 增加一条数据 --> <insert id="addStudent" parameterType="Student"> INSERT INTO student VALUES(NULL, #{student_id}, #{name}, #{age}, #{sex}, #{birthday}) </insert> <!-- 删除一条数据 --> <delete id="deleteStudent" parameterType="int"> DELETE FROM student WHERE id = #{id} </delete> <!-- 更新一条数据 --> <update id="updateStudent" parameterType="Student"> UPDATE student SET student_id = #{student_id}, name = #{name}, age = #{age}, sex = #{sex}, birthday = #{birthday} WHERE id = #{id} </update> <!-- 查询一条数据 --> <select id="getStudent" resultMap="student" parameterType="int"> SELECT * FROM student WHERE id = #{id} </select> <resultMap id="student" type="student"> <id column="id" property="id"/> <result column="student_id" property="student_id"/> <result column="name" property="name"/> <result column="age" property="age"/> <result column="sex" property="sex"/> <result column="birthday" property="birthday" javaType="java.sql.Date"/> </resultMap> <!-- 查询从start位置开始的count条数据--> <select id="list" resultMap="student"> SELECT * FROM student ORDER BY student_id desc limit #{param1}, #{param2} </select> </mapper>
Student
package com.kikyo.entity; import java.util.Date; public class Student { private int id; private int student_id; private String name; private int age; private String sex; private Date birthday; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getStudent_id() { return student_id; } public void setStudent_id(int student_id) { this.student_id = student_id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
Page
package com.kikyo.util; public class Page { int start; // 开始数据 int count; // 每一页的数量 int total; // 总共的数据量 public Page(int start, int count) { super(); this.start = start; this.count = count; } public boolean isHasPreviouse() { if (start == 0) return false; return true; } public boolean isHasNext() { if (start == getLast()) return false; return true; } public int getTotalPage() { int totalPage; // 假设总数是50,是能够被5整除的,那么就有10页 if (0 == total % count) totalPage = total / count; // 假设总数是51,不能够被5整除的,那么就有11页 else totalPage = total / count + 1; if (0 == totalPage) totalPage = 1; return totalPage; } public int getLast() { int last; // 假设总数是50,是能够被5整除的,那么最后一页的开始就是40 if (0 == total % count) last = total - count; // 假设总数是51,不能够被5整除的,那么最后一页的开始就是50 else last = total - total % count; last = last < 0 ? 0 : last; return last; } public int getStart() { return start; } public void setStart(int start) { this.start = start; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } }
学生管理系统(SSM简易版)总结
importjavax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class StartSystemListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
//1.将项目上下文路径(request.getContextPath())放置到application域中.
ServletContext application = sce.getServletContext();
application.setAttribute("ctx", application.getContextPath());
System.err.println("---------Servlet容器创建成功 APP_PATH 被放到ServletContext作用域-------");
//2.加载所有许可路径
//ApplicationContext ioc = WebApplicationContextUtils.getWebApplicationContext(application);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}