springmvc-210805-03—ssm整合演示
pom.xml(引入相关依赖)
<?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>
< groupId> com.bgy</ groupId>
< artifactId> springmvc-210805-03</ artifactId>
< version> 1.0-SNAPSHOT</ version>
< packaging> war</ packaging>
< 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>
</ properties>
< dependencies>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.11</ 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> javax.servlet.jsp</ groupId>
< artifactId> jsp-api</ artifactId>
< version> 2.2.1-b03</ version>
< scope> provided</ scope>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-webmvc</ artifactId>
< version> 5.2.5.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-tx</ artifactId>
< version> 5.2.5.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-jdbc</ artifactId>
< version> 5.2.5.RELEASE</ version>
</ dependency>
< dependency>
< groupId> com.fasterxml.jackson.core</ groupId>
< artifactId> jackson-core</ artifactId>
< version> 2.9.0</ version>
</ dependency>
< dependency>
< groupId> com.fasterxml.jackson.core</ groupId>
< artifactId> jackson-databind</ artifactId>
< version> 2.9.0</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis-spring</ artifactId>
< version> 1.3.1</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> 3.5.1</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 8.0.16</ version>
</ dependency>
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> druid</ artifactId>
< version> 1.1.12</ version>
</ dependency>
</ dependencies>
< build>
< resources>
< resource>
< directory> src/main/java</ directory>
< includes>
< include> **/*.properties</ include>
< include> **/*.xml</ include>
</ includes>
< filtering> false</ filtering>
</ resource>
</ resources>
< plugins>
< plugin>
< artifactId> maven-compiler-plugin</ artifactId>
< version> 3.1</ version>
< configuration>
< source> 1.8</ source>
< target> 1.8</ target>
</ configuration>
</ plugin>
</ plugins>
</ build>
</ project>
web.xml(注册DispatcherServlet,Spring监听器,字符过滤器)
<?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"
xsi: schemaLocation= " http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version = " 4.0"
metadata-complete = " true" >
< 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:conf/dispatchServlet.xml</ param-value>
</ init-param>
< load-on-startup> 1</ load-on-startup>
</ servlet>
< servlet-mapping>
< servlet-name> dispatcherServlet</ servlet-name>
< url-pattern> *.do</ url-pattern>
</ servlet-mapping>
< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener</ listener-class>
</ listener>
< context-param>
< param-name> contextConfigLocation</ param-name>
< param-value> classpath:conf/applicationContext.xml</ param-value>
</ context-param>
< 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> forceResponseEncoding</ param-name>
< param-value> true</ param-value>
</ init-param>
< init-param>
< param-name> forceRequestEncoding</ param-name>
< param-value> true</ param-value>
</ init-param>
</ filter>
< filter-mapping>
< filter-name> characterEncodingFilter</ filter-name>
< url-pattern> /*</ url-pattern>
</ filter-mapping>
</ web-app>
dispatchServlet.xml(Springmvc配置文件)
<?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.xsd" >
< context: component-scan base-package = " com.bgy.controller" />
< bean class = " org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name = " prefix" value = " /WEB-INF/jsp/" />
< property name = " suffix" value = " .jsp" />
</ bean>
< mvc: annotation-driven/>
</ beans>
applicationContext.xml(spring配置文件)
<?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"
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" >
< context: property-placeholder location = " classpath:conf/jdbc.properties" />
< bean id = " dataSource" class = " com.alibaba.druid.pool.DruidDataSource" init-method = " init" destroy-method = " close" >
< property name = " url" value = " ${jdbc.url}" />
< property name = " username" value = " ${jdbc.username}" />
< property name = " password" value = " ${jdbc.password}" />
</ bean>
< bean id = " sqlSessionFactory" class = " org.mybatis.spring.SqlSessionFactoryBean" >
< property name = " dataSource" ref = " dataSource" />
< property name = " configLocation" value = " classpath:conf/mybatis.xml" />
</ bean>
< bean class = " org.mybatis.spring.mapper.MapperScannerConfigurer" >
< property name = " sqlSessionFactoryBeanName" value = " sqlSessionFactory" />
< property name = " basePackage" value = " com.bgy.dao" />
</ bean>
< context: component-scan base-package = " com.bgy.service" />
</ beans>
mybatis.xml(MyBatis配置文件)
<?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>
< typeAliases>
< package name = " com.bgy.domain" />
</ typeAliases>
< environments default = " development" >
< environment id = " development" >
< transactionManager type = " JDBC" />
< dataSource type = " POOLED" >
< property name = " driver" value = " com.mysql.cj.jdbc.Driver" />
< property name = " url" value = " jdbc:mysql://localhost:3306/springmvc_test_210805_01?serverTimezone=UTC& useSSL=false" />
< property name = " username" value = " root" />
< property name = " password" value = " admin" />
</ dataSource>
</ environment>
</ environments>
< mappers>
< package name = " com.bgy.dao" />
</ mappers>
</ configuration>
jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springmvc_test_210805_01?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
jdbc.username=root
jdbc.password=admin
StudentDao.java(编写dao)
package com. bgy. dao ;
import com. bgy. domain. Student ;
import java. util. List ;
public interface StudentDao {
int insertStudent ( Student student) ;
List < Student > selectStudents ( ) ;
}
StudentDao.xml(编写dao)
<?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.bgy.dao.StudentDao" >
< select id = " selectStudents" resultType = " com.bgy.domain.Student" >
select
id,name,age
from
student
order by
id desc
</ select>
< insert id = " insertStudent" >
insert into
student(name,age)
values(#{name},#{age})
</ insert>
</ mapper>
StudentService.java(编写service)
package com. bgy. service ;
import com. bgy. domain. Student ;
import java. util. List ;
public interface StudentService {
int addStudent ( Student student) ;
List < Student > findStudents ( ) ;
}
StudentServiceImpl.java(编写service)
package com. bgy. service. impl ;
import com. bgy. dao. StudentDao ;
import com. bgy. domain. Student ;
import com. bgy. service. StudentService ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. stereotype. Service ;
import java. util. List ;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
public int addStudent ( Student student) {
int nums = studentDao. insertStudent ( student) ;
return nums;
}
@Override
public List < Student > findStudents ( ) {
List < Student > students = studentDao. selectStudents ( ) ;
return students;
}
}
StudentController.java(编写controller)
package com. bgy. controller ;
import com. bgy. domain. Student ;
import com. bgy. service. StudentService ;
import org. springframework. stereotype. Controller ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. ResponseBody ;
import org. springframework. web. servlet. ModelAndView ;
import javax. annotation. Resource ;
import java. util. List ;
@Controller
@RequestMapping ( "/student" )
public class StudentController {
@Resource
private StudentService service;
@RequestMapping ( "/addStudent.do" )
public ModelAndView addStudent ( Student student) {
ModelAndView mv = new ModelAndView ( ) ;
String tips = "注册失败" ;
int nums = service. addStudent ( student) ;
if ( nums > 0 ) {
tips = "学生<" + student. getName ( ) + ">注册成功" ;
}
mv. addObject ( "tips" , tips) ;
mv. setViewName ( "result" ) ;
return mv;
}
@ResponseBody
@RequestMapping ( "/queryStudent.do" )
public List < Student > queryStudent ( ) {
List < Student > students = service. findStudents ( ) ;
return students;
}
}
index.jsp(主界面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basePath = request.getScheme() + "://" +
request.getServerName() + ":" +
request.getServerPort() +
request.getContextPath() + "/";
%>
<html>
<head>
<title>ssm整合</title>
<base href="<%=basePath%>"/>
</head>
<body>
<table>
<tr>
<td><a href="addStudent.jsp">学生注册</a> </td>
</tr>
<tr>
<td><a href="listStudent.jsp">浏览学生</a> </td>
</tr>
</table>
</body>
</html>
addStudent.jsp(添加学生界面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basePath = request.getScheme() + "://" +
request.getServerName() + ":" +
request.getServerPort() +
request.getContextPath() + "/";
%>
<html>
<head>
<title>学生注册</title>
<base href="<%=basePath%>"/>
</head>
<body>
<div align="center">
<form action="student/addStudent.do" method="post">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"/> </td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age"/> </td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="注册"/> </td>
</tr>
</table>
</form>
</div>
</body>
</html>
result.jsp(添加学生结果页面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册结果页面</title>
</head>
<body>
<span>result.jsp 结果页面</span>
<br/>
注册结果:${tips}
</body>
</html>
listStudent.jsp(查询学生页面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basePath = request.getScheme() + "://" +
request.getServerName() + ":" +
request.getServerPort() +
request.getContextPath() + "/";
%>
<html>
<head>
<title>查询学生</title>
<base href="<%=basePath%>"/>
<script type="text/javascript" src="js/jquery-3.4.1.js"></script>
<script type="text/javascript">
$(function(){
// 当页面dom对象加载后,执行loadStudentData()
loadStudentData();
$("#btnLoader").click(function (){
loadStudentData();
})
})
function loadStudentData(){
$.ajax({
url:"student/queryStudent.do",
type:"get",
dataType:"json",
success:function (data){
// 清除旧数据
$("#info").html("");
// 添加新数据
$.each(data,function (i,n){
$("#info").append("<tr>")
.append("<td>"+n.id+"</td>")
.append("<td>"+n.name+"</td>")
.append("<td>"+n.age+"</td>")
.append("</tr>")
})
}
})
}
</script>
</head>
<body>
<div align="center">
<table>
<thead>
<tr>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
</tr>
</thead>
<tbody id="info">
</tbody>
<input type="button" id="btnLoader" value="查询数据"/>
</table>
</div>
</body>
</html>
我的目录结构