【java框架之Spring框架】创建ssm框架项目详细步骤之 上(spring集成mybatis)
【java框架之Spring框架】创建ssm框架项目详细步骤之下(搭建SpringMVC)
SpringBoot
回顾spring :
优点:
- 轻量级的、非侵入式的、IOC和AOP一站式框架 ( jdbc事物管理 、springmvc的web层封装)
- 简化企业级开发
缺点:
-
spring配置却是重量级的 有许多模板化配置,比如开启注解扫描… -
配置的jar的依赖过多
SpringBoot是什么?
在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集成JDBC
springboot添加jdbc模块,默认使用自己框架爱内的数据源管理
可以把数据源的管理类配置为阿里的 德鲁一 数据源
-
导入 jar 包 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
-
配置数据源链接 application.yml
server:
port: 9999
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")
public DataSource druid() {
DruidDataSource druidDataSource = new DruidDataSource();
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();
corsConfiguration.setAllowedOriginPatterns(Collections.singletonList("*"));
corsConfiguration.addAllowedHeader(CorsConfiguration.ALL);
corsConfiguration.addAllowedMethod(CorsConfiguration.ALL);
corsConfiguration.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", corsConfiguration);
return new CorsFilter(source);
}
}
springboot集成mybatis
-
导入springboot mybatis 的jar包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
-
在 application.yml 中配置数据源信息
mybatis:
type-aliases-package: com.ffyc.ssmback.model
mapper-locations: classpath:mapper/*Mapper.xml
configuration:
map-underscore-to-camel-case: true
cache-enabled: true
springboot配置拦截器
package com.ffyc.ssmback.config;
import com.ffyc.ssmback.model.User;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@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("/**");
inter.excludePathPatterns("/loginCtl/login");//放行地址
// inter.excludePathPatterns("/swagger*/**");
}
}
|