SpringMVC 基础知识三解析

你猜 阅读:215 2021-03-31 14:13:33 评论:0

第一:搭建SSM框架

1.1 环境准备

1.1.1 数据库和表结构

create database ssm; 
create table account( 
id int primary key auto_increment, 
name varchar(100), 
money double(7,2), 
);

1.1.2 创建Maven 工程

创建父工程(ssm)

创建关联的子模块

ssm-dao  jar 
ssm-service  jar 
ssm-domain  jar 
ssm-web     war

1.1.3 编写实体对象、业务层接口、持久层接口

实体对象:

package com.zzg.entity; 
 
import java.io.Serializable; 
 
@SuppressWarnings("serial") 
public class Account implements Serializable { 
	private Integer id; 
	private String name; 
	private Float money; 
 
	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; 
	} 
 
	public Float getMoney() { 
		return money; 
	} 
 
	public void setMoney(Float money) { 
		this.money = money; 
	} 
 
	@Override 
	public String toString() { 
		return "Account [id=" + id + ", name=" + name + ", money=" + money + "]"; 
	} 
 
} 

业务层接口:

package com.zzg.service; 
 
import java.util.List; 
 
import com.zzg.entity.Account; 
 
public interface IAccountService { 
	/** 
	* 保存账户 
	* @param account 
	*/ 
	void saveAccount(Account account); 
	/** 
	* 查询所有账户 
	* @return 
	*/ 
	List<Account> findAllAccount(); 
} 

持久层接口

package com.zzg.dao; 
 
import java.util.List; 
 
import com.zzg.entity.Account; 
 
public interface IAccountDao { 
	/** 
	* 保存 
	* @param account 
	*/ 
	void save(Account account); 
	/** 
	* 查询所有 
	* @return 
	*/ 
	List<Account> findAll(); 
} 

第二:SSM框架整合步骤

2.1 Web项目整合SpringMVC

2.1.1 在web.xml 文件中配置核心控制器(DispatcherServlet)

<?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_2_5.xsd" 
	version="2.5"> 
	<display-name>ssm_web</display-name> 
	<!-- 配置 spring mvc 的核心控制器 --> 
	<servlet> 
		<servlet-name>springmvcDispatcherServlet</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 的对象的创建时间点:应用加载时创建。取值只能是非 0 正整数,表示启动顺 序 --> 
		<load-on-startup>1</load-on-startup> 
	</servlet> 
	<servlet-mapping> 
		<servlet-name>springmvcDispatcherServlet</servlet-name> 
		<url-pattern>/</url-pattern> 
	</servlet-mapping> 
	<!-- 配置 springMVC 编码过滤器 --> 
	<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>forceEncoding</param-name> 
				<param-value>true</param-value> 
			</init-param> 
	</filter> 
	<!-- 过滤所有请求 --> 
	<filter-mapping> 
		<filter-name>CharacterEncodingFilter</filter-name> 
		<url-pattern>/*</url-pattern> 
	</filter-mapping> 
	 
	<welcome-file-list> 
		<welcome-file>index.html</welcome-file> 
		<welcome-file>index.htm</welcome-file> 
		<welcome-file>index.jsp</welcome-file> 
		<welcome-file>default.html</welcome-file> 
		<welcome-file>default.htm</welcome-file> 
		<welcome-file>default.jsp</welcome-file> 
	</welcome-file-list> 
</web-app>

2.1.2 编写Springmvc配置文件

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	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/mvc 
 http://www.springframework.org/schema/mvc/spring-mvc.xsd 
 http://www.springframework.org/schema/context  
 http://www.springframework.org/schema/context/spring-context.xsd"> 
	<!-- 配置创建 spring 容器要扫描的包 --> 
	<context:component-scan 
		base-package="com.zzg"> 
		<!-- 制定扫包规则 ,[email protected] 注解的 JAVA 类 --> 
		<context:include-filter type="annotation" 
			expression="org.springframework.stereotype.Controller" /> 
	</context:component-scan> 
 
	<!-- 配置视图解析器 --> 
	<bean 
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
		<property name="prefix" value="/WEB-INF/pages/"></property> 
		<property name="suffix" value=".jsp"></property> 
	</bean> 
 
	<mvc:annotation-driven></mvc:annotation-driven> 
</beans>

2.1.3 整合Spring和SpringMVC

在web.xml 文件中配置监听器实现启动服务创建容器(在web.xml 文件中添加如下代码片段)

<!-- 配置 spring 提供的监听器,用于启动服务时加载容器 。 
	该间监听器只能加载 WEB-INF 目录中名称为 applicationContext.xml 的配置文件 -->  
	<listener> 
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
	</listener> 
	<!-- 手动指定 spring 配置文件位置 -->  
	<context-param>  
		<param-name>contextConfigLocation</param-name>  
		<param-value>classpath:spring.xml</param-value> 
	</context-param>

2.1.4 编写spring.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:tx="http://www.springframework.org/schema/tx" 
	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/tx  
 http://www.springframework.org/schema/tx/spring-tx.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"> 
 
</beans>

2.1.5 Spring 整合MyBatis

2.1.5.1 Spring 接管 MyBatis 的 Session 工厂(在spring.xml 文件中添加如下代码片段)

<!-- 加载配置文件 --> 
	<context:property-placeholder 
		location="classpath:jdbcConfig.properties" /> 
 
 
	<!-- 配置 MyBatis 的 Session 工厂 --> 
	<bean id="sqlSessionFactory" 
		class="org.mybatis.spring.SqlSessionFactoryBean"> 
		<!-- 数据库连接池 --> 
		<property name="dataSource" ref="dataSource" /> 
		<!-- 自动扫描mapping.xml文件 -->  
		<property name="mapperLocations" 
			value="classpath:mapper/*.xml" /> 
 
	</bean> 
 
<!-- 配置数据源 --> 
	<bean id="dataSource" 
		class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
		<property name="driverClass" value="${jdbc.driver}"></property> 
		<property name="jdbcUrl" value="${jdbc.url}"></property> 
		<property name="user" value="${jdbc.username}"></property> 
		<property name="password" value="${jdbc.password}"></property> 
	</bean>

2.1.5.2 配置自动扫描所有 Mapper 接口和文件

<!-- 配置 Mapper 扫描器 --> 
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
		<property name="basePackage" value="com.zzg.dao" /> 
	</bean>

2.1.5.3 配置 spring 的事务

<!-- 配置事务管理器 --> 
	<bean id="transactionManager" 
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
		<property name="dataSource" ref="dataSource"></property> 
	</bean> 
	<!-- 配置事务的通知 --> 
	<tx:advice id="txAdvice" 
		transaction-manager="transactionManager"> 
		<tx:attributes> 
			<tx:method name="*" propagation="REQUIRED" 
				read-only="false" /> 
			<tx:method name="find*" propagation="SUPPORTS" 
				read-only="true" /> 
		</tx:attributes> 
	</tx:advice> 
	<!-- 配置 aop --> 
	<aop:config> 
		<!-- 配置切入点表达式 --> 
		<aop:pointcut 
			expression="execution(* com.zzg.service.impl.*.*(..))" id="pt1" /> 
		<!-- 建立通知和切入点表达式的关系 --> 
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1" /> 
	</aop:config>

spring.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:tx="http://www.springframework.org/schema/tx" 
	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/tx  
 http://www.springframework.org/schema/tx/spring-tx.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"> 
	<!-- 加载配置文件 --> 
	<context:property-placeholder 
		location="classpath:jdbcConfig.properties" /> 
 
 
	<!-- 配置 MyBatis 的 Session 工厂 --> 
	<bean id="sqlSessionFactory" 
		class="org.mybatis.spring.SqlSessionFactoryBean"> 
		<!-- 数据库连接池 --> 
		<property name="dataSource" ref="dataSource" /> 
		<!-- 自动扫描mapping.xml文件 -->  
		<property name="mapperLocations" 
			value="classpath:mapper/*.xml" /> 
 
	</bean> 
	<!-- 配置数据源 --> 
	<bean id="dataSource" 
		class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
		<property name="driverClass" value="${jdbc.driver}"></property> 
		<property name="jdbcUrl" value="${jdbc.url}"></property> 
		<property name="user" value="${jdbc.username}"></property> 
		<property name="password" value="${jdbc.password}"></property> 
	</bean> 
 
 
	<!-- 配置 Mapper 扫描器 --> 
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
		<property name="basePackage" value="com.zzg.dao" /> 
	</bean> 
 
	<!-- 配置事务管理器 --> 
	<bean id="transactionManager" 
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
		<property name="dataSource" ref="dataSource"></property> 
	</bean> 
	<!-- 配置事务的通知 --> 
	<tx:advice id="txAdvice" 
		transaction-manager="transactionManager"> 
		<tx:attributes> 
			<tx:method name="*" propagation="REQUIRED" 
				read-only="false" /> 
			<tx:method name="find*" propagation="SUPPORTS" 
				read-only="true" /> 
		</tx:attributes> 
	</tx:advice> 
	<!-- 配置 aop --> 
	<aop:config> 
		<!-- 配置切入点表达式 --> 
		<aop:pointcut 
			expression="execution(* com.zzg.service.impl.*.*(..))" id="pt1" /> 
		<!-- 建立通知和切入点表达式的关系 --> 
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1" /> 
	</aop:config> 
</beans>

第三:SSM功能展示

3.1 ssm_web:补充Controller 、jsp 页面和 MySQL8 数据库配置

Controller

package com.zzg.controller; 
 
import java.util.List; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 
 
import com.zzg.entity.Account; 
import com.zzg.service.IAccountService; 
 
/** 
 * 账户的控制器 
 *  
 * @Version 1.0 
 */ 
@Controller 
@RequestMapping("/account") 
public class AccountController { 
	 
	@Autowired 
	private IAccountService accountService; 
	/** 
	 * 查询所有账户 
	 *  
	 * @return 
	 */ 
	@RequestMapping(value="/findAllAccount", method= {RequestMethod.GET,RequestMethod.POST}) 
	public ModelAndView findAllAccount() { 
		List<Account> accounts = accountService.findAllAccount(); 
		ModelAndView mv = new ModelAndView(); 
		mv.addObject("accounts", accounts); 
		mv.setViewName("accountlist"); 
		return mv; 
	} 
	 
	/** 
		 * 保存账户 
		 *  
		 * @param account 
		 * @return 
		 */ 
 
	@RequestMapping(value="/saveAccount", method= {RequestMethod.GET,RequestMethod.POST}) 
	public String saveAccount(Account account) { 
		accountService.saveAccount(account); 
		return "redirect:findAllAccount"; 
	} 
} 

JSP 

index.jsp 默认首页

<%@ page language="java" contentType="text/html; charset=UTF-8" 
	pageEncoding="UTF-8"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>主页</title> 
</head> 
<body> 
	<a href="account/findAllAccount">访问查询账户</a> 
	<hr /> 
	<form action="account/saveAccount" method="post"> 
		账户名称:<input type="text" name="name" /><br />  
		账户金额:<input type="text" name="money"><br /> 
		<input type="submit" value="保存" /> 
	</form> 
</body> 
</html>

accountlist.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" 
	pageEncoding="UTF-8"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>账户的列表页面</title> 
</head> 
<body> 
	<table border="1" width="300px"> 
		<tr> 
			<th>编号</th> 
			<th>账户名称</th> 
			<th>账户金额</th> 
		</tr> 
		<c:forEach items="${accounts}" var="account"> 
			<tr> 
				<td>${account.id}</td> 
				<td>${account.name }</td> 
				<td>${account.money }</td> 
			</tr> 
		</c:forEach> 
	</table> 
</body> 
</html>

jdbcConfig.properties

jdbc.driver=com.mysql.cj.jdbc.Driver 
jdbc.url=jdbc:mysql://localhost:3306/ssm?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true 
jdbc.username=root 
jdbc.password=123456

3.2 ssm_dao :补充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.zzg.dao.IAccountDao"> 
	<!-- 查询所有账户 --> 
	<select id="findAll" resultType="com.zzg.entity.Account"> 
		select * from account 
	</select> 
	<!-- 新增账户 --> 
	<insert id="save" parameterType="com.zzg.entity.Account"> 
		insert into account(name,money) values(#{name},#{money}); 
	</insert> 
</mapper>

源码下载:待补充

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们