目录
十、SSM框架整合实现学生信息注册案例
1. 创建06-ssm的web项目
2. 修改web.xml版本为4.0
3. 更新pom.xml文件
4.?jdbc的属性文件和日志文件
5. applicationContext-dao.xml文件
6. applicationContext-service.xml文件
7. applicationContext-service.trans文件
8. springmvc.xml文件
9. Dao层中的接口和映射文件及实体类
10. 业务层中的接口和实现类
11. 控制层中的处理器类及方法StudentController?
12. 默认访问的index.jsp页面
13. 注册学生信息的页面addStudent.jsp
14. 显示学生信息列表的页面listStudent.jsp
15. 注册学生结果的页面result.jsp
16. web.xml中的数据
十、SSM框架整合实现学生信息注册案例
1. 创建06-ssm的web项目
2. 修改web.xml版本为4.0
?????我们现在使用了三个框架,Spring,SpringMVC,Mybatis,这三个框架需要启动,加载和初始化才能使用。先加载Spring框架,没有Spring框架是不可能由其他两个框架,因为所需的资源是交给Spring框架进行管理的,如果没有这个管理者,容器就没有了,其他框架的组件也不会存在。
1)先初始化Spring框架,使用监听器监听即可。
2)再注册前端控制器(初始化springmvc的容器文件)
3)最后配置post提交中文处理的过滤器
3. 更新pom.xml文件
- 单元测试包
- Mysql驱动包
- Mybatis的依赖包
- 数据库连接池的druid包
- Spring的核心jar包
- Spring-test的包
- Spring-tx的包
- Spring-jdbc的包
- Mybatis-spring整合包
- Cglib的包
- Spring-aspects包
- Spring-web包
- Spring-webmvc包
- 日志包
- Jstl包
- Servlet包
- Jsp的包
- Javax-annotation包
- Jackson包
<?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.suke</groupId>
<artifactId>06-ssm</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>06-ssm Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<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>
<!--添加Junit的单元测试包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!--添加Mysql驱动程序包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<!--添加数据库连接池的druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
<!--spring的jdbc模板包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!--添加mybatis的依赖包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!--添加Spring的核心包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!--添加Spring的test依赖包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!--添加mybatis和spring的整合包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- @Resource注解依赖的jar包-->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<!--添加log4j日志的jar包-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
<scope>test</scope>
</dependency>
<!--添加一个AspectJ的依赖包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!--添加Cglib的包-->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.1_3</version>
</dependency>
<!--配置spring的事务管理-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!--添加spring-web包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!--spring-webmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!--添加jstl-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!--添加Servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--添加Jsp-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!--添加json的依赖-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
<!--找映射文件:spring与mybatis整合肯定要使用到mybatis的映射文件-->
<build>
<resources>
<resource>
<directory>src/main/java</directory><!--映射文件所有在目录-->
<!--包含目录下的.properties文件,.xml文件都会进行扫描-->
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
4.?jdbc的属性文件和日志文件
db.properties?
## 数据库的参数设置
jdbc_driver=com.mysql.cj.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/myjdbc?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
jdbc_user=root
jdbc_password=root
## 连接池的参数设置
## 初始化连接数
initialSize=10
## 最小空闲数
mindLe=10
## 最大连接池
maxActive=30
## 最大的等待时间
maxWait=6000
??
?log4j.properties
log4j.rootLogger=debug,stdout,D,E
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
log4j.appender.D=org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File=D://logs/log.log
log4j.appender.D.Append=true
log4j.appender.D.Threshold=DEBUG
log4j.appender.D.layout=org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
log4j.appender.E=org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File=D://logs/error.log
log4j.appender.E.Append=true
log4j.appender.E.Threshold=ERROR
log4j.appender.E.layout=org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
5. applicationContext-dao.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--加载jdbc的属性文件-->
<context:property-placeholder location="classpath:jdbc/db.properties"/>
<!--配置mysql数据库参数:使用的是Druid技术-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close" lazy-init="false">
<!--获取mysql的参数-->
<property name="driverClassName" value="${jdbc_driver}"/>
<property name="url" value="${jdbc_url}"/>
<property name="username" value="${jdbc_user}"/>
<property name="password" value="${jdbc_password}"/>
<!--获取连接池中参数-->
<property name="initialSize" value="${initialSize}"/>
<property name="minIdle" value="${mindLe}"/>
<property name="maxActive" value="${maxActive}"/>
<property name="maxWait" value="${maxWait}"/>
</bean>
<!--管理mybatis的工厂类对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--加载数据源-->
<property name="dataSource" ref="dataSource"/>
<!--加载mybatis的主配置文件-->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
<!--配置别名包扫描器-->
<property name="typeAliasesPackage" value="com.suke.pojo"/>
</bean>
<!--Spring配置mybatis的动态代理的过程-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--配置映射文件的包扫描器-->
<property name="basePackage" value="com.suke.mapper"/>
</bean>
</beans>
6. applicationContext-service.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--添加Service层的注解的包扫描器-->
<context:component-scan base-package="com.suke.service"/>
</beans>
7. applicationContext-service.trans文件
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--<import resource="classpath:spring/applicationContext-dao.xml"/>-->
<!--配置平台事务的管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--配置数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事务的隔离级别和传播行为-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!--事务管理器和切入点配置-->
<aop:config>
<aop:pointcut id="txService" expression="execution(* com.suke.service.*.*(..))"/>
<!--事务使用内置的切面-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txService"/>
</aop:config>
</beans>
8. 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: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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--@Controller注解开发的包扫描器-->
<context:component-scan base-package="com.suke.controller"/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--配置视图的前缀名称-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--配置视图的后缀名称-->
<property name="suffix" value=".jsp"/>
</bean>
<!--配置SpringMVC的注解驱动
1)代替处理器映射器和处理器适配器
2)对Json数据响应提供支持
3)可以引用日期转换器的服务-->
<!--配置了注解驱动后Spring就知道了我们会启动了这个驱动,然后Spring通过context:component-scan标签会自动扫描
@Controller,@Componet,@Service,@Repository等注解标记的组件到工厂中,来处理我们的请求-->
<mvc:annotation-driven />
<!--如果直接使用<url-pattern>/</url-pattern>会出现访问不了静态资源
静态资源访问的问题:直接将静态资源的访问交给服务器处理-->
<mvc:default-servlet-handler/>
</beans>
9. Dao层中的接口和映射文件及实体类
????????1)动态接口StudentMapper?
/*
* dao层:学生功能接口
* */
public interface StudentMapper {
//1、注册学生信息的方法
public int insertStudent(Student student);
//2、查询所有学生信息列表的方法
public List<Student> selectStudents();
}
????????2)映射文件StudentMapper.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">
<mapper namespace="com.suke.mapper.StudentMapper">
<!--注册的sql语句-->
<insert id="insertStudent" parameterType="student">
insert into student(name,email,age) values (#{name},#{email},#{age})
</insert>
<!--查询所有学生信息的sql语句-->
<select id="selectStudents" resultType="student">
select id,name,email,age from student;
</select>
</mapper>
????????3)实体类Student
public class Student {
private Integer id;
private String name;
private String email;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email == null ? null : email.trim();
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", age=" + age +
'}';
}
}
10. 业务层中的接口和实现类
????????1)业务接口StudentService?
/*
* service层:学生业务的接口
* */
public interface StudentService {
//1、注册学生信息的业务方法
public int addStudent(Student student);
//2、查询所有学生信息列表的业务方法
public List<Student> findStudents();
}
????????2)业务实现类及方法StudentServiceImpl?
/*
* service层:生业务的接口实现类
* */
@Service
public class StudentServiceImpl implements StudentService {
/*注入dao层对象*/
@Autowired
private StudentMapper studentMapper;
/*
* 实现注册学生信息的业务方法
* @param student
* @return
* */
@Override
public int addStudent(Student student) {
return studentMapper.insertStudent(student);
}
/*
* 实现查询所有学生信息列表的业务方法
* @return
* */
@Override
public List<Student> findStudents() {
return studentMapper.selectStudents();
}
}
11. 控制层中的处理器类及方法StudentController?
/*
* 控制层:处理器的类
* */
@Controller
@RequestMapping("student")
public class StudentController {
/*注入Service层对象*/
@Autowired
private StudentService studentService;
/*
* 处理注册学生的方法
* */
@RequestMapping("/addStudent")
public ModelAndView addStudent(Student student) {
//1、创建ModelAndView对象
ModelAndView mav = new ModelAndView();
String msg = "处理失败!";
//判断
int result = studentService.addStudent(student);
if (result > 0) {
msg = "学生信息【" + student.getName() + "】注册成功!";
}
//2、处理数据
mav.addObject("msg", msg);
//3、设置视图名称
mav.setViewName("result");
return mav;
}
/*
* 处理查询所有学生的方法
* */
@RequestMapping(value = "/queryStudent")
@ResponseBody
public List<Student> queryStudent() {
List<Student> studentList = studentService.findStudents();
System.out.println(studentList);
return studentList;
}
}
12. 默认访问的index.jsp页面
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<%
String basePath = request.getScheme()+"://"+request.getServerName()+":"
+request.getServerPort()+request.getContextPath()+"/";
%>
<%--http://localhost:8080/06_ssm/--%>
<html>
<head>
<title>操作学生信息的页面</title>
<base href="<%=basePath%>">
</head>
<body>
<div align="center">
<h2>SSM整合案例</h2>
<img src="img/ssm.jpg" style="width: 300px"/>
<table>
<tr>
<td><a href="addStudent.jsp">注册学生</a></td>
</tr>
<tr>
<td><a href="listStudent.jsp">浏览学生</a></td>
</tr>
</table>
</div>
</body>
</html>
13. 注册学生信息的页面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>
<h1 align="center">学生信息注册页面</h1>
<div align="center">
<form action="student/addStudent.action" method="post">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>邮箱:</td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age"/></td>
</tr>
<tr>
<td></td>
<td align="center"><input type="submit" value="注册"/></td>
</tr>
</table>
</form>
</div>
</body>
</html>
14. 显示学生信息列表的页面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%>">
<%--添加jquery库文件--%>
<script type="text/javascript" src="js/jquery-3.6.0.js"></script>
<%--使用ajax请求数据--%>
<script type="text/javascript">
$(function () {
loadStudentData();
});
function loadStudentData() {
$.ajax({
/*获取数据(将List<Student>转换为json串)*/
url:"student/queryStudent.action",
type:"post",
dataType:"json",
success:function (result) {
//清空tbody中旧的数据
$("#info").html("");
$.each(result,function (i,n) {
$("#info").append("<tr>")
.append("<td>"+n.id+"</td>")
.append("<td>"+n.name+"</td>")
.append("<td>"+n.email+"</td>")
.append("<td>"+n.age+"</td>")
.append("</tr>")
});
}
});
}
</script>
</head>
<body>
<h1 align="center">学生信息列表显示</h1>
<div align="center">
<table border="1" cellspacing="0" cellpadding="5">
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>邮箱</th>
<th>年龄</th>
</tr>
</thead>
<tbody id="info">
</tbody>
</table>
</div>
</body>
</html>
15. 注册学生结果的页面result.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>学生信息注册结果!</h1>
${msg}
</body>
</html>
??
16. web.xml中的数据
<?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">
<!--初始化Spring的容器文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<!--使用监听器监听Spring的容器文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--注册前端控制器-->
<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:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
<!--<url-pattern>/</url-pattern>--><!--不能对惊天资源起效果-->
</servlet-mapping>
<!--配置一个post提交的中文乱码的过滤器-->
<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>
<!--强制请求对象(HttpServletRequest)-->
<init-param>
<param-name>forRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<!--强制响应对象(HttpServletResponse)-->
<init-param>
<param-name>forResponseEncoding</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>
|