SSM框架复习
目录
- 1.项目结构
- 2.pom
- 3.resources
- 3.1 db.properties
- 3.2 mybatis.xml
- 3.3 spring_core.xml
- 3.4 spring_mvc.xml
- 3.5 mapper
- 4.java代码
- 4.1 pojo
- 4.2 dao
- 4.3 service
- 4.4 controller
- 5.前端
- 5.1 add.jsp
- 5.2 show.jsp
- 5.3 update.jsp
- 5.4 web.xml
- 6. 运行
1.项目结构
2.pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<groupId>com.wo</groupId>
<artifactId>SSMCRUD</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerVersion>1.8</compilerVersion>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!--数据库 druid mysql mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.3</version>
</dependency>
<!--srping mybatis-spring spring-webmvcspring-jdbc spring-aspect-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!--junit servlet-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.4.6</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-ecs</artifactId>
<version>4.17.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.2</version>
</dependency>
</dependencies>
</project>
3.resources
3.1 db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
jdbc.user=root
jdbc.pass=123456
3.2 mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"/>
</plugins>
</configuration>
3.3 spring_core.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.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">
<!--扫包-->
<context:component-scan base-package="com.wo.service"/>
<!--数据源-->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.pass}"/>
</bean>
<!--整合mybatis-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
<!--扫描dao接口-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wo.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--Aop-->
<aop:aspectj-autoproxy/>
<!--事务注解-->
<tx:annotation-driven/>
</beans>
3.4 spring_mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd">
<!--扫包-->
<context:component-scan base-package="com.wo.controller"/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/><!--/WEB-INF/view/show.jsp-->
<property name="suffix" value=".jsp"/>
</bean>
<!--释放静态资源-->
<mvc:default-servlet-handler/>
<!--注解开发-->
<mvc:annotation-driven/>
<!-- 接口跨域配置 -->
<mvc:cors>
<mvc:mapping path="/**"
allowed-origins="*"
allowed-methods="POST, GET, OPTIONS, DELETE, PUT"
allowed-headers="Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"
allow-credentials="true" />
</mvc:cors>
<!--上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1048576"></property>
</bean>
</beans>
3.5 mapper
<?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.wo.dao.IStudentDao">
<!--查所有-->
<select id="findAll" resultType="com.wo.pojo.Student">
select * from student
</select>
<!--根据ID查询-->
<select id="findById" parameterType="int" resultType="com.wo.pojo.Student">
select * from student where id=#{id}
</select>
<!-- 删除 -->
<delete id="deleteById" parameterType="int">
delete from Student where id=#{id}
</delete>
<!--更新-->
<update id="update" parameterType="com.wo.pojo.Student">
update Student set id=#{id},name=#{name},age=#{age},address=#{address} where id=#{id}
</update>
<!--增加-->
<insert id="add" parameterType="com.wo.pojo.Student">
insert into Student values(null,#{name},#{age},#{address})
</insert>
</mapper>
4.java代码
4.1 pojo
id,name,age,address
4.2 dao
package com.wo.dao;
import com.wo.pojo.Student;
import java.util.List;
public interface IStudentDao {
//查所有
public List<Student> findAll();
//根据ID查询一个
public Student findById(int id);
//新增
public int add(Student student);
//更新
public int update(Student student);
//删除
public int deleteById(int id);
}
4.3 service
package com.wo.service;
import com.wo.pojo.Student;
import java.util.List;
public interface IStudentService {
//查所有
public List<Student> findAll();
//根据ID查询一个
public Student findById(int id);
//更新
public int Update(Student student);
//新增
public int add(Student student);
//删除
public int deleteById(int id);
}
impl
package com.wo.service.impl;
import com.wo.dao.IStudentDao;
import com.wo.pojo.Student;
import com.wo.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("studentService")
public class StudentServiceImpl implements IStudentService{
@Autowired
private IStudentDao studentDao;
@Override
public List<Student> findAll() {
return studentDao.findAll();
}
@Override
public Student findById(int id) {
return studentDao.findById(id);
}
@Override
public int Update(Student student) {
return studentDao.update(student);
}
@Override
public int add(Student student) {
return studentDao.add(student);
}
@Override
public int deleteById(int id) {
return studentDao.deleteById(id);
}
}
4.4 controller
package com.wo.controller;
import com.wo.dao.IStudentDao;
import com.wo.pojo.Student;
import com.wo.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
public class StudentController {
@Autowired
private IStudentService studentService;
//查询所有
@RequestMapping("/")
public ModelAndView findAll(){
List<Student> StudentList = studentService.findAll();
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("show");
modelAndView.addObject("StudentList",StudentList);
return modelAndView;
}
//根据ID删除
@RequestMapping("/deleteById")
public String deleteById(@RequestParam("id")int id){
studentService.deleteById(id);
return "redirect:/";
}
//根据ID查找
@RequestMapping("/findById")
public ModelAndView findById(@RequestParam("id")int id){
Student student = studentService.findById(id);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("student",student);
modelAndView.setViewName("update");
return modelAndView;
}
//去新增页面
@RequestMapping("/goAddPage")
public String goUpdatePage(){
return "add";
}
//更新
@RequestMapping("/update")
public String insertOrUpdate(Student student){
studentService.Update(student);
return "redirect:/";
}
//新增
@RequestMapping("/add")
public String add(Student student){
studentService.add(student);
return "redirect:/";
}
}
5.前端
5.1 add.jsp
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" language="java" %>
<!DOCTYPE html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta charset="utf-8">
<title>新增</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<center>
<div>
<form action="/add" method="post">
<input name="id" type="hidden" value="50">
用户姓名:<input name="name" type="text" ><br/>
用户年龄:<input name="age" type="text" ><br/>
用户地址:<input name="address" type="text"><br/>
<input type="submit" value="提交更改"><br/>
</form>
</div>
</center>
</body>
</html>
5.2 show.jsp
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" language="java" %>
<!DOCTYPE html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta charset="utf-8">
<title>学生信息表</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<center>
<table class="table">
<a href="/goAddPage">新增</a>
<thead>
<tr>
<th>学生ID</th>
<th>姓名</th>
<th>年龄</th>
<th>地址</th>
</tr>
</thead>
<tbody>
<c:forEach items="${StudentList}" var="student">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>${student.address}</td>
<td><a href="/findById?id=${student.id}">修改</a>||<a href="/deleteById?id=${student.id}">删除</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</center>
</body>
</html>
5.3 update.jsp
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" language="java" %>
<!DOCTYPE html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta charset="utf-8">
<title>新增或修改</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<center>
<div>
<form action="/update" method="post">
<input name="id" type="hidden" value="${student.id}">
用户姓名:<input name="name" type="text" value="${student.name}"><br/>
用户年龄:<input name="age" type="text" value="${student.age}"><br/>
用户地址:<input name="address" type="text" value="${student.address}"><br/>
<input type="submit" value="提交更改"><br/>
</form>
</div>
</center>
</body>
</html>
5.4 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!--spring监听-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring_core.xml</param-value>
</context-param>
<!--springMVC拦截-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring_mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--编码方式-->
<filter>
<filter-name>CharacterEncodingFilter</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>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--restful请求风格-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--<!– 跨域请求 –>-->
<!--<filter>-->
<!--<filter-name>SimpleCORSFilter</filter-name>-->
<!--<filter-class>com.wo.filter.SimpleCORSFilter</filter-class>-->
<!--<init-param>-->
<!--<param-name>IsCross</param-name>-->
<!--<param-value>true</param-value>-->
<!--</init-param>-->
<!--</filter>-->
<!--<filter-mapping>-->
<!--<filter-name>SimpleCORSFilter</filter-name>-->
<!--<url-pattern>/*</url-pattern>-->
<!--</filter-mapping>-->
</web-app>
6. 运行
赞 (0)