亿级流量架构|day02-框架搭建过程

0 前期准备

静态页面资源:
链接:https://pan.baidu.com/s/1AfGzzPo9ij0z-wOJboAVgw
密码:5t7f

1 框架架构设计

1.1 架构设计图

  1. SpringMVC 定位:与用户进行交互/负责参数封装 Controller
  2. Spring 定位:整合其他的主流框架,使软件调用浑然一体. 主要管理Service
  3. Mybatis 定位:数据持久化 mapper/dao
    在这里插入图片描述
    在这里插入图片描述

2 业务需求

要求:用户输入localhost:8091/findAll时展现用户列表信息.页面使用JSP(jstl/el ${key})

2.1 导入数据库

  1. 通过mysql客户端导入
    Source 路径/jtdb.sql;
  2. 通过工具导入
    在这里插入图片描述
    导入数据库
    在这里插入图片描述
    添加java视图
    在这里插入图片描述

2.2 编辑Controller

@Controller
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("/findAll")
	public String findUser(Model model){
		List<User> userList = userService.findUserList();
		//将数据保存到request域中,
		model.addAttribute("userList", userList);
		return "userList";
	}
}

2.3 编辑Service

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserMapper userMapper;

	@Override
	public List<User> findUserList() {
		
		return userMapper.findUserList();
	}
}

2.4 编辑Mapper接口/映射文件

public interface UserMapper {
	
	List<User> findUserList();
	
}
<!--
	namespace="它是映射文件的唯一标识"
	mapper接口调用方式,表明 mapper接口/映射文件/表映射关系
  -->
<mapper namespace="com.jt.manage.mapper.UserMapper">
	
	<!--
		mysql:  没有任何影响
		oracle: ;不能正确识别
		resultType:之后配置别名包
	  -->
	<select id="findUserList" resultType="User">
		select * from user
	</select>
	
</mapper>

2.5 编辑页面

userList.jsp页面代码:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!--引入标签库  -->
<%@ taglib  uri="http://java.sun.com/jsp/jstl/core"  prefix="c" %>
<html>
	<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
		<title>用户列表页面</title>
	</head>
	<body>
		<table width="60%" algin="center" border="1">
			<tr align="center">
				<td colspan="5"><h2>用户信息</h2></td>
			</tr>
			<tr align="center">
				<td>ID</td>
				<td>姓名</td>
				<td>年龄</td>
				<td>性别</td>
			</tr>
			<!--循环遍历用户列表信息  -->
			<c:forEach items="${userList}" var="u">
				<tr align="center">
					<td>${u.id}</td>
					<td>${u.name}</td>
					<td>${u.age}</td>
					<td>${u.sex}</td>
				</tr>
			</c:forEach>
		</table>
	</body>
</html>

3 编辑框架的配置文件

3.1 配置约束文件

在这里插入图片描述

3.2 编辑SpringMVC配置文件

applicationContext-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/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

	<!--1.开启mvc注解  -->
	<mvc:annotation-driven/>
	
	<!--2.放行静态资源文件  -->
	<mvc:default-servlet-handler/>
	
	<!--3.配置视图解析器  内部资源视图解析器 
		作用:为返回页面拼接前缀和后缀      /WEB-INF    .jsp  
	-->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	
	<!--4.添加文件上传视图解析器  -->
</beans>

3.3 编辑web.xml

<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/applicationContext-mvc.xml</param-value>
		</init-param>
	</servlet>
	
	<!--.do  .action 
		路径:
		前缀型: /service/*    只要请求以service开头被前端控制器拦截
		后缀型: .do           请求拦截以.do结尾
		全路径: /servet/*.do  以service开头以.do结尾请求
		
		/*  不管请求是什么路径统统都拦截   请求/html/jsp/CSS/js
		/	规定:拦截请求 和静态资源   放行jsp等动态资源
		为restFul格式做准备
	   -->
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<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>
	</filter>
	
	<filter-mapping>
		<filter-name>characterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

3.4 编辑spring配置文件

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	
	<!--1.配置包扫描  -->
	<context:component-scan base-package="com.jt"/>
	
	<!--2.配置数据源  -->
	
	<!--2.1导入pro配置文件  -->
	<!-- <context:property-placeholder location="classpath:/property/*.properties"/> -->
	<!-- private Resource[] locations; -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:/property/jdbc.properties</value>
			</list>
		</property>
	</bean>
	
	<!--2.2配置数据源  -->
	<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.username}" />
        <property name="password" value="${jdbc.password}" />
	</bean>
	
	
	<!--3.配置事务控制  -->
	<tx:annotation-driven/>
	
	<!--3.1 定义事务管理器  -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<!--3.2 定义事务策略
		
		propagation="REQUIRED" 执行该操作 必须添加事务
		propagation="SUPPORTS" 事务支持的,原来的操作有事务,则添加事务,
							        原有的操作没有事务,则不添加事务
							        
		propagation="NEVER"    从不添加事务	
		propagation="REQUIRES_NEW"   都会创建一个新的事务	
		read-only="true"       该操作只读		        
	  -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="save*"   propagation="REQUIRED"/>
			<tx:method name="delete*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="find*"   propagation="SUPPORTS" read-only="true"/>
			<tx:method name="*"       propagation="SUPPORTS" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	
	<!--3.3 定义事务切面  
		(pointcut*, advisor*, aspect*)
		
		expression 切入点表达式
		within(包名.类名)  按类匹配  控制粒度 粗粒度
		execution(返回值类型 包名.类名.方法名(参数列表))
	-->
	<aop:config>
		<aop:pointcut expression="execution(* com.jt.manage.service..*.*(..))" id="pc"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
	</aop:config>
</beans>

3.5 配置Mybatis-配置文件

mybatis-config.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>
	
	<!--
		配置顺序
		 (properties?, settings?, typeAliases?, typeHandlers?, objectFactory?, 
 objectWrapperFactory?, plugins?, environments?, databaseIdProvider?, mappers?)
		
	
		开启驼峰规则
		对象			表
		属性			字段
		id			id
		userId      user_id   不能映射/开启驼峰映射后
		说明:当查询sql返回结果时,会自动的根据驼峰映射规则
		实现字段和属性的一一映射.
	  -->
	<settings>
		<!-- 开启驼峰自动映射 -->
		<setting name="mapUnderscoreToCamelCase" value="true" />
		
	</settings>
	
</configuration>

3.6 Spring整合Mybatis

applicationContext-mybatis.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	
	<!--spring整合mybatis
		1.配置数据源
		2.导入mybatis自身配置文件
		3.导入映射文件
		4.配置别名包
	  -->
	<bean class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<property name="configLocation" 
		value="classpath:/mybatis/mybatis-config.xml"/>
		<property name="mapperLocations" 
		value="classpath:/mybatis/mappers/*.xml"/>
		<property name="typeAliasesPackage" 
		value="com.jt.manage.pojo"/>
	</bean>
	
	<!--Spring为Mapper接口创建代理对象  -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.jt.manage.mapper"/>
	</bean>
</beans>

3.7 Web容器加载spring容器

web.xml

<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/applicationContext*.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>

3.8 页面效果

在这里插入图片描述

3.9 框架调用流程图

在这里插入图片描述

4 完整项目代码

链接:https://pan.baidu.com/s/1cTq8SVdADl5m1xIXYCQZcQ
密码:ogo2

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值