【java框架之Spring框架】创建ssm框架项目详细步骤之 上(spring集成mybatis)
【java框架之Spring框架】创建ssm框架项目详细步骤之下(搭建SpringMVC)
SpringBoot
SpringBoot是什么?SpringBoot核心功能springboot搭建
配置文件两种格式springboot集成JDBCspringboot集成mybatisspringboot配置拦截器
SpringBoot回顾spring :
优点:
- 轻量级的、非侵入式的、IOC和AOP一站式框架 ( jdbc事物管理 、springmvc的web层封装)简化企业级开发
缺点:
spring配置却是重量级的
有许多模板化配置,比如开启注解扫描…
配置的jar的依赖过多
在spring基础上进行封装
spring搭建比较麻烦: 配置多、依赖过多
springboot 对spring项目的搭建进行简化
基于"约定"优于配置的思想
搭建更加简化,配置不需要xml,使用其他方式进行配置,不需要在java和xml之间进行切换
SpringBoot核心功能起步依赖 会自动依赖框架中能够使用到的第三方Jar
自动配置 明天再说
springboot搭建和前端对等开发方式
方式一: 去官网在线生成下载 https://start.spring.io/
方式二: 在idea中创建
springboot中内置了tomcat
配置文件两种格式①application.properties
键= 值
②application.yml
一级和一级之间要有缩进
访问:http://127.0.0.1:9999/loginCtl/login (类路径/方法路径)访问springboot项目不需要加项目名,因为springboot项目中,内嵌了专属的服务器,所以9999端口的服务器只有一个项目
springboot集成JDBCspringboot添加jdbc模块,默认使用自己框架爱内的数据源管理
可以把数据源的管理类配置为阿里的 德鲁一 数据源
导入 jar 包 pom.xml
org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java 8.0.28 com.alibaba druid 1.1.10
配置数据源链接
application.yml
#为内置的tomcat配置信息 port配置端口号 冒号后面必须有空格,层级之间必须有缩进
server:
port: 9999
#spiring配置
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/web_db?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
package config;
import javax.sql.DataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration //也是用来创建对象的,表示此类是配置类
public class DruidDataSourceConfig {
@Bean //只能用在方法上,表示此方法会返回一个对象
@ConfigurationProperties(prefix = "spring.datasource") //读取application.yml文件汇总spring.datasource下面的内容
public DataSource druid() {
DruidDataSource druidDataSource = new DruidDataSource();
//druidDataSource.setUrl("");
return new DruidDataSource();
}
}
package config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlbasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import java.util.Collections;
@Configuration
public class CorsConfig {
@Bean
public CorsFilter corsFilter() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
//1,允许任何来源
corsConfiguration.setAllowedOriginPatterns(Collections.singletonList("*"));
//2,允许任何请求头
corsConfiguration.addAllowedHeader(CorsConfiguration.ALL);
//3,允许任何方法
corsConfiguration.addAllowedMethod(CorsConfiguration.ALL);
//4,允许凭证
corsConfiguration.setAllowCredentials(true);
UrlbasedCorsConfigurationSource source = new UrlbasedCorsConfigurationSource();
source.registerCorsConfiguration("
@Configuration
public class IsLoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("后端验证用户登录是否有效");
HttpSession session = request.getSession();
User user = (User)session.getAttribute("user");
if(user!=null){
return true;
}else{
response.getWriter().print(5);//表示后端登录失效
return false;//不继续向下执行
}
}
}
package com.ffyc.ssmback.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
public void addInterceptors(InterceptorRegistry registry){
InterceptorRegistration inter = registry.addInterceptor(new IsLoginInterceptor());
inter.addPathPatterns("**");//放行swagger
// inter.excludePathPatterns("/v2/**");//放行swagger
// inter.excludePathPatterns("/webjars/**");//放行swagger
}
}



