SpringBoot2(二)

SpringBoot2(二),第1张

SpringBoot2(二)

SpringBoot2
  • SpringBoot2(一)
  • 三、JDBC使用
  • 四、Druid连接池
  • 五、Mybatis
  • 六、SpringSecurity(安全)
  • 七、Swagger
    • 6.1、导入配置swagger2与swagger-ui
    • 6.2、简单配置
    • 6.3、测试运行
    • 6.4、Swangger配置扫描接口
    • 6.5、小结
  • 八、任务
    • 7.1、异步
    • 7.2、邮件
    • 7.3、定时执行
  • 九、Redis
  • 十、分布式 Dubbo + Zookeeper+ SpringBoot

SpringBoot2(一)

一、SpringBoot2 基础
二、SpringBoot Web开发
点此跳转

三、JDBC使用

	org.springframework.boot
	spring-boot-starter-jdbc



	mysql
	mysql-connector-java
	runtime

spring:
	datasource:
		username: root
		password: 123456
		url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
		driver-class-name: com.mysql.jdbc.Driver
四、Druid连接池

	com.alibaba
	druid
	1.1.21

spring:
	datasource:
		username: root
		password: 123456
		url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
		driver-class-name: com.mysql.jdbc.Driver
		type: com.alibaba.druid.pool.DruidDataSourcel
		#Spring Boot默认是不注入这些属性值的,需要自己绑定
		#druid数据源专有配置
		initialsize: 5
		minIdle: 5
		maxActive: 20maxWait: 60000
		timeBetweenEvictionRunsMi11is: 60000
		minEvictableIdleTimeMillis: 30000
		validationQuery: SELECt 1 FROM DUAL
		testWhileIdle: true
		testOnBorrow: false
		test0nReturn: false
		poolPreparedStatements: true
		#配置监控统计拦截的filters, stat:监控统计、Log4j:日志记录、wall:防御sqL注入#如果允许时报错 java.Lang.CLassNotFoundException: org.apache.Log4j.Priority
		#则导入Log4j依赖即可,Maven地址: https ://mvnrepository.com/artifact/Log4j/Log4jfilters: stat,wall,log4j
		maxPoo1PreparedstatementPerConnectionsize: 20
		useGlobalDataSourceStat: true
		connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSq1Millis=500

五、Mybatis

	org.mybatis.spring.boot
	mybatis-spring-boot-starter
	2.1.1

六、SpringSecurity(安全)

记住几个类:

  1. WebSecurityConfigurerAdapter:自定义Security策略
  2. AuthenticationManagerBuilder:自定义认证策略
  3. @EnableWebSecurity:开启WebSecurity模式

	org.springframework.boot
	spring-boot-starter-security

@EnablewebSecurity
public class securityconfig extends websecurityconfigurerAdapter {
	@override
	protected void configure(Httpsecurity http) throws Exception {
		//首页所有人可以访问,功能页只有对应有权限的人才能访问
		http.authorizeRequests()
			.antMatchers( "/").permitAl1()
			.antMatchers("/1evel1/**").hasRole("vip1")
			.antMatchers("/leve12/**").hasRole("vip2")
			.antMatchers("/1eve13/**").hasRole("vip3");
		//没有权限默认会到登录页面
		http.formLogin();
		//注销.开启了注销功能,跳到首页
		http. logout().logoutsuccessUrl("/");
		//防止网站工具:get.post
		http.csrf().disable();//关闭csrf功能
		//开启记住我功能,默认保存14天
		http.rememberMe();

	}
}
七、Swagger 6.1、导入配置swagger2与swagger-ui

	io.springfox
	springfox-swagger2
	2.9.2



	io.springfox
	springfox-swagger-ui
	2.9.2

6.2、简单配置
@configuration
@Enableswagger2 //开启Swagger2
public class SwaggerConfig {
	
}
6.3、测试运行

http://localhost:8080/swagger-ui.html

6.4、Swangger配置扫描接口
//配置Swagger的Docket的bean实例
//enable是否启动Swagger,如果为False,则Swagger不能再浏览器中访问
@Bean
public Docket docket(){
	return new Docket(documentationType.SwAGGER_2)
		.enable(true)
		.select()
		// RequestHandlerselectors,配置要扫描接口的方式
		// basePackage:指定要扫描的包
		// any( ):扫描全部
		//none():不扫描
		//withclassAnnotation:扫描类上的注解,参数是一个注解的反射对象
		// withMethodAnnotation:扫描方法上的注解
		.apis(RequestHandlerselectors.basePackage("com.YoRHa.swagger.controller"))
		//paths()。过滤什么路径
		.paths(PathSelectors.ant( antPattern:"/select/**"))//(只扫描select下的接口)
		.build();
}

配置API文档的分组

@Bean
public Docket docket(){
	return new Docket(documentationType.SwAGGER_2)
		.groupName ("hello")
}

如何配置多个分组;多个Docket实例即可,扫描不同的包

6.5、小结

只要我们的接口中,返回值中存在实体类,他就会被扫描到Swagger中

八、任务 7.1、异步
@Async //告诉Spring这是一个异步的方法

主程序也要开启异步功能

@EbableAsync //开启异步注解功能
7.2、邮件

	org.springframework.boot
	spring-boot-starter-mail

spring.mail.username=66666qq.com
spring.mail.password=goxwydfndkssbhjg
spring.mai1.host=smtp.qq.com
#开启加密验证(qq特有)
spring.mail.properties.mail.smtp.ssl.enable=true
@SpringBootTest
class Springboot09TestApplicationTests {
	@Autowired
	JavaMailsenderImpl mailsender;
	@Test
	void contextLoads() {
		simpleMailMessage mailMessage = new simpleMailMessage();
		mailMessage.setsubject("你好");
		mailMessage.setText("谢谢你");
		mai1Message.setTo("[email protected]"2;
		mailMessage.setFrom( "[email protected]" ) ;
		mailsender.send( mailMessage);
	}
}
7.3、定时执行
@EnableScheduling //开启定时功能的注解
@Scheduled //什么时候执行

Taskscheduler任务调度者
TaskExecutor任务执行者

@Service
public class scheduledService {
	//在一个特定的时问执行这个方法
	//cron表达式
	//秒  分  时  日  月  星期
	@Scheduled(cron = "****0-7")
	public void hello(){
		system.out.println( "hello执行了");
	}
}
九、Redis

	org.springframework.boot
	spring-boot-starter-data-redis

spring.redis.host=127.0.0.1
spring.redis.port=6379
十、分布式 Dubbo + Zookeeper+ SpringBoot

RPC两个核心模块:通讯,序列化。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5688378.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存