程序猿和码农的区别
springboot
- 程序猿=数据结构+算法
- 码农=面向对象+框架
Springboot存在的目的
解决企业级开发的复杂度,提供简单、高效的开发。
简化:约定大于配置
创建项目
- 联网版
- 阿里云版
- 官方版
- 手工版
顾名思义
原理
自动配置原理
- 收集Spring开发者的编程习惯,整理开发过程中使用的常用技术列表->(技术集A)
- @SpringBootApplication
- @EnableAutoConfiguration
- @Import({AutoConfigurationImportSelector.class})
- @Import({AutoConfigurationImportSelector.class})
- @EnableAutoConfiguration
- @SpringBootApplication
- 收集常用技术(技术集A)的使用参数,整理开发过程中每个技术的常用设置列表->(设置集B)
- 初始化SpringBoot基础环境,加载用户自定义的bean和导入的其他坐标,形成初始化环境
- 将技术集A包含的所有技术都定义出来,在Spring/SpringBoot启动时默认全部加载
- 将技术集A中具有使用条件的技术约定出来,设置成按条件加载,由开发者决定是由使用该技术
- 将设置集B作为默认配置加载(约定大于配置),减少开发者配置工作量
- 开放设置集B的配置覆盖接口,由开发者根据自身需要决定是否覆盖默认配置
Bean加载方式
- xml
spring中xml配置bean
-
注解
springmvc中@Component及其衍生注解@Controller、@Service、@Repository 以及非自定义Bean@Bean -
xml+注解 ComponentScan
<context:component-scan=“xxx.xxx.xxx” -
@ComponentScan({“xxx.xxx”})
-
实例工厂
作用:在new对象之前有操作空间
public class DogFactoryBean implements FactoryBean<Dog> {
@Override
public Dog getObject() throws Exception {
return new Dog();
}
@Override
public Class<?> getObjectType() {
return Dog.class;
}
}
@Component
public class DogFacrory {
@Bean
public DogFactoryBean dog(){
return new DogFactoryBean();
}
}
- @ImportResource
按条件加载bean(ConditionalOn*****)
ConditionalOnClass|ConditionalOnMissingClass|ConditionalOnBean等其他
按属性绑定加载bean
@EnableConfigurationProperties的作用:则是将让使用了 @ConfigurationProperties 注解的配置类生效,将该类注入到 IOC 容器中,交由 IOC 容器进行管理,此时则不用再配置类上加上@Component。
@Configuration
源码:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
@AliasFor(
annotation = Component.class
)
String value() default "";
boolean proxyBeanMethods() default true;
}
重点 :proxyBeanMethods
proxyBeanMethods默认是true:使用代理,也就是说该配置类会被代理,直接从IOC容器之中取得bean对象,不会创建新的对象。SpringBoot总会检查这个组件是否在容器中是否存在,保持组件的单实例
proxyBeanMethods设置为false:每次调用@Bean标注的方法获取到的对象是一个新的bean对象,和之前从IOC容器中获取的不一样,SpringBoot会跳过检查这个组件是否在容器中是否存在,保持组件的多实例
- @Import({Class.class})
parent
spring-boot-starter-parent 是一个Maven的parent POM。当你将spring-boot-starter-parent添加到你的项目的parent POM时,它会提供以下功能:
- 依赖管理:它包含了Spring Boot项目中常用的依赖的版本管理。这意味着你可以在你的项目中直接使用这些依赖,而无需指定版本号。Spring Boot会为你管理这些依赖的版本。
- 插件配置:它配置了一些常用的Maven插件,如spring-boot-maven-plugin,用于打包和运行Spring Boot应用。
- 默认属性:它包含了一些默认的配置属性,这些属性可以在你的项目中进行覆盖。
源码
有继承了 spring-boot-dependencies(核心)
有大量适用于当前Springbooot版本的其他依赖版本号,方便统一管理。
作用:子模块添加依赖不要设置版本号
dependencyManagement
Maven中的dependencyManagement元素提供了一种管理依赖版本号的方式。在dependencyManagement元素中声明所依赖的jar包的版本号等信息,那么所有子项目再次引入此依赖jar包时则无需显式的列出版本号。Maven会沿着父子层级向上寻找拥有dependencyManagement 元素的项目,然后使用它指定的版本号。
注:阿里云版直接引入spring-boot-dependencies依赖,不在需要引入parent
starter启动器(springboot的启动场景)
springboot将所有的功能场景变成一个个启动器,会帮助我们导入所需的环境依赖。
spring提供了大量的启动器如:
org.springframework.boot
spring-boot-starter-web
-
官网文档
主程序(引导类)
核心:ApplicationContex 初始化Spring容器
@SpringBootApplication
public class SpringbootSpringmvcApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootSpringmvcApplication.class, args);
}
}
final ConfigurableApplicationContext run = SpringApplication.run(Springboot01Application.class,
注解
springboot所有自动配置都是在启动的时候扫描加载:spring.factories所有的自动配置类都在这里面,但是不一定生效,要判断条件是否成立,只要导入对应的stater,就有对用的的启动器,对应的自动装配就会生效。
以前需要手动配置的现在都是自动配置。自动配置的东西都在-spring-boot-autoconfigure-release.jar
自动装配
主启动类
- 推断应用的类型是普通的项目还是web项目
- 查找并加载所有可用初始化器,设置到initializers属性中
- 找出所有的应用程序监听器,设置到listeners属性中
- 推断并设置main方法的定义类,找到运行的主类
内置web容器
- tomcat 人数使用最多,应用面广,负载了若干较重的组件
- jetty 更轻量级,负载性能远不及tomcat
- undertow 负载性能勉强跑赢tomcat
配置文件
核心配置文件:application.yml
语法格式:key:空格+value
application配置文件格式
- application.properties
- application.yaml
- application.yml
修改端口、上下问
server:
port: 8081
servlet:
context-path: /
单属性、对象、数组
username: asd
person:
name:xsx
pets:
- cat
- dog
多环境下的核心配置文件(开发、测试):
1.方式一 多环境下配置文件名格式:application-名称;eg:application-dev,application-test
核心配置文件(application,yml)去加载不同的环境文件,多文件激活,用逗号隔开
spring:
profiles:
active: test // active:后为 application-的文件名
- 方式二 group分组
spring:
profiles:
active: con2
group:
"dev": con1
"test": con2
注:springboot2.4后 group 取代了include
属性获取
获取配置文件中的属性值
单个获取 @Value
spring:
profiles:
active: test
person:
name: myxusx
age: 18
// 获取属性值
@Value("${person.name}")
private String name;
对象获取 @ConfigurationProperties
在@ConfigurationProperties的使用,把配置类的属性与yml配置文件绑定起来的时候,还需要加上@Component注解才能绑定并注入IOC容器中,若不加上@Component,则会无效。
属性松散绑定:即配置文件属性名可以是大写、小写、驼峰、非驼峰等格式
// 配置文件
# 用户信息
person:
name: myxusx
age: 18
boolean: true
map: {k1:v1}
list:
- l1
- l2
dog:
name: aaa
age: 1
// ioc 注册 获取
@Component
// 指定前缀配置文件
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
······
bean属性校验
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
属性绑定非自定义bean
@Bean
@ConfigurationProperties(prefix = "datasource")
public DruidDataSource dataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
System.out.println(druidDataSource);
return druidDataSource;
}
读取数据(全部)
@Autowired Environment对象获取全部属性
com:
user:
name: xusx
age: 12
@Autowired
private Environment environment;
final String property = environment.getProperty("com.user.age");
@Value和 @ConfigurationProperties比较
- 松散绑定是指: bean对象属性lastName和配置文件属性 last-name 可以匹配到一起
- JSR303数据校验:
使用@ Validated
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
@Validated
public class Person {
@NotBlank
private String name;
private Integer age;
private Boolean hanppy;
private Date birth;
private Map<String,Object> maps;
private List<Object> list;
private Dog dog;
yaml中可使用一些表达式
person:
name: ${random.uuid}
age: 12
hanppy: false
birth: 2020/20/20
maps: {k1:v1;k2:v2}
list:
- a
- b
- c
dog:
name: ${person.hello:hello}____dogg
age:
打印彩蛋
banner.txt
可以自定义设置banner图(在线生成工具)
配置文件优先级
日志
使用
private static final org.slf4j.Logger logger1=LoggerFactory.getLogger(AccountController.class);
logger.error("error");
logger.warn("warn~~");
logger.info("info");
logger.debug("debug");
日志级别(输出当前级别以及更高级别的日志信息)
Log4j建议只使用四个级别,优先级从高到低分别是ERROR、WARN、INFO、DEBUG
日志级别 | 描述 |
---|---|
OFF | 关闭:不输出日志 |
FATAL | 致命:输出可能会导致应用程序终止(崩溃)的错误 |
ERROR | 错误:输出程序的错误(不会导致程序崩溃的错误) |
WARN | 警告:提示可能出现的问题 |
INFO | 信息:输出应用运行过程的详细信息 |
DEBUG | 调试:输出对调试有用的信息 |
TRACE | 跟踪:输出程序的运行轨迹 |
ALL | 所有:输出所有级别的信息 |
设置日志级别
默认info
root:根目录即整体应用的日志
logging:
level:
root: info
日志格式
- %d 日期
- %m 消息
- %n 换行
logging:
pattern:
console: "%d %clr(%p) - %m %n"
只应用于单个包或类,也可分组
logging:
level:
root: info
com.yinhai.springbootmybatis.controller: debug
热部署 spring-boot-devtools
会自动重新启动应用,这使得改动几乎立即生效,无需手动重启 Spring Boot 应用。它通过两种方式实现这一功能:使用了两个类加载器,一个用于加载不变的类(如 Spring Framework 类),另一个用于加载会变动的类(如你的应用程序类)。当检测到变化时,它会重启后者,而保持前者不变,从而实现快速重启。
- 手动热部署
添加devtools坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
Build Project:Ctrl+F9
- 自动热部署 (失去焦点5秒)
2. 在项目中按:CTRL + ALT + SHIFT + / 找到Registry点击
3.
完成
如果3中找未找到则
设置非热部署范围 exclude
禁用热部署 enable
spring
devtools:
restart:
exclude: application.yml
enable: false
Over
单元测试
@RunWith(SpringRunner.class)的作用表明Test测试类要使用注入的类,比如@Autowired注入的类,有了@RunWith(SpringRunner.class)这些类才能实例化到spring容器中,自动注入才能生效
临时属性设置
@SpringBootTest(properties = {"test.namee=xusx"})
class SpringbootMybatisApplicationTests {
@Value("${test.namee}")
private String namee;
@Test
void contextLoads() {
System.out.println(namee);
}
}
发送虚拟请求
// 设置web启动端口
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
// 开启虚拟MVC调用
@AutoConfigureMockMvc
class SpringbootMybatisApplicationTests {
@Test
void contextLoads(@Autowired MockMvc mvc) throws Exception {
// 创建虚拟请求,
MockHttpServletRequestBuilder mockHttpServletRequestBuilder= MockMvcRequestBuilders.post("/all");
// 发起请求
final ResultActions perform = mvc.perform(mockHttpServletRequestBuilder);
System.out.println(perform);
}
}
集成jsp
<!--springboot内嵌tomcat去解析jsp-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<build>
<!--
springboot默认推荐是用的前端引擎是thymeleaf
springboot集成jsp,手动指定jsp最后编译的路径
springboot集成的jsp编译jsp路径是springboot规定好的位置META-INF/resources
-->
<resources>
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>*.*</include>
</includes>
</resource>
</resources>
静态资源—WebMvcAutoConfiguration配置类
- webjars (依赖)
this.addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.4.1</version>
</dependency>
- resouces文件夹下—WebProperties配置类 ->读取resources下文件
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
优先级:resources>static>public
首页设置–WebMvcAutoConfiguration配置类
private Resource getIndexHtml(Resource location) {
try {
Resource resource = location.createRelative("index.html");
if (resource.exists() && resource.getURL() != null) {
return resource;
}
} catch (Exception var3) {
;
}
return null;
}
获取:在静态资源路径下查找index.html
模板引擎
什么是模板引擎
模板引擎可以让(网站)程序实现界面与数据分离,业务代码与逻辑代码的分离,这就大大提升了开发效率,良好的设计也使得代码重用变得更加容易。
简单来说如下图:
springboot默认推荐是用的前端引擎是thymeleaf,还有其他引擎如 jsp、freemarker
所有html的元素都可以被thymeleaf接管 可以th:+元素名
thymeleaf中文文档
4. mvc-界面跳转
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
在template目录下的所有界面,只能通过controller来跳转
需要模板引擎的支持-thymeleaf
// 在template目录下的所有界面,只能通过controller来跳转
// 需要模板引擎的支持
@Controller
public class MyController {
@RequestMapping("/test")
public String test(Model model){
model.addAttribute("msg","hello---thymeleaf");
return "test";
}
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 th:text="${msg}"></h1>
</body>
</html>
ThymeleafProperties.class
templates出现的原因就在于此
路径、前、后缀名已定义
private static final Charset DEFAULT_ENCODING;
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
private boolean checkTemplate = true;
private boolean checkTemplateLocation = true;
private String prefix = "classpath:/templates/";
private String suffix = ".html";
private String mode = "HTML";
扩展使用spingmvc
- 实现WebMvcConfigurer
- 添加@Configuration
不可使用@EnableWebMvc,负责自动装配会失效,具体源码
1、EnableWebMvc
2、DelegatingWebMvcConfiguration
3、WebMvcAutoConfiguration
重点:@ConditionalOnMissingBean({WebMvcConfigurationSupport.class}):不存在WebMvcConfigurationSupport时WebMvcAutoConfiguration才会生效,如果使用了EnableWebMvc 后果可想而知。
/**
* WebMvcConfigurer 手动实现
*/
@Configuration
public class WebMvcController implements WebMvcConfigurer {
// 跳转视图
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
/**
* 添加拦截器 --- LoginInterceptor implements HandlerInterceptor
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html");
}
}
错误界面
相关报错会自动跳转相关界面
在templates文件夹下error文件夹
404.html
500.html
JDBC数据源
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
默认使用的是hikari数据源
spring:
datasource:
driver-class-name: oracle.jdbc.OracleDriver
url: jdbc:oracle:thin:@//127.0.0.1:1521/orcl
username: scott
password: tiger
Druid(德鲁伊)
Druid是阿里巴巴旗下的一款数据库连接池,结合了c3po,dbcp等数据库连接池的优点,同时加入了日志监控。
Druid可以很好的监控DB池的连接和SQL的执行情况,天生就是针对j监控而生的DB连接池。
SpringBoot2.0以上默认使用的是hikari数据源,可以说hikari与druid都是当前JAVA web上最优秀的数据源
依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.17</version>
</dependency>
<!-- druid日志记录功能用到log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
spring.datasource.type:指定数据源
spring:
datasource:
username: scott
password: tiger
url: jdbc:oracle:thin:@//127.0.0.1:1521/orcl
driver-class-name: oracle.jdbc.driver.OracleDriver
type: com.alibaba.druid.pool.DruidDataSource
#默认不绑定属性值
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
#配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入---自带
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
dataSource、jdbcTemplate 拿来即用-springboot已经集成。
@Autowired
DataSource dataSource;
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
Person person;
@Test
void contextLoads() throws SQLException {
System.out.println(dataSource.getClass());
List<Map<String,Object>> list=jdbcTemplate.queryForList("select * from emp");
System.out.println(list);
}
Druid(德鲁伊)configuration j监控
重: springboot 内置了 servlet容器,无web.xml文件。 替代方法ServletRegistrationBean
注册DataSource ;设置durid监控地址、用户名、密码
@Configuration
public class DuridConfigg {
@ConfigurationProperties("spring.datasource")
@Bean
public DataSource DruidDataSource(){
return new DruidDataSource();
}
// 后台监控
// 以为springboot 内置了 servlet容器,无web.xml文件。 替代方法ServletRegistrationBean
@Bean
public ServletRegistrationBean statViewServlet(){
// 固定代码 设置映射地址
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
HashMap<String,String> initParameters=new HashMap<String,String>();
// 设置登录账号密码 key-固定
initParameters.put("loginUsername","admin");
initParameters.put("loginPassword","123456");
// 访问控制 value为空都可以访问
initParameters.put("allow","");
bean.setInitParameters(initParameters);
return bean;
}
@Bean
public FilterRegistrationBean registrationBean(){
FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
filterFilterRegistrationBean.setFilter(new WebStatFilter());
HashMap<String,String> initParameters=new HashMap<String,String>();
initParameters.put("exclusions","/durid/**");
filterFilterRegistrationBean.setInitParameters(initParameters);
return filterFilterRegistrationBean;
}
监控界面
监控界面
springboot三个内嵌数据库
-
H2
-
HSQL
-
Derby
异常处理机制
MyBatis整合
依赖包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
接口包
@Mapper
@Repository
public interface PersonMapper {
List<Person> getAllPerson();
int updateById(int id);
int deleteById(int id);
int addUser(Person person);
}
实体类
pojo自动模式:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
@Data
@NoArgsConstructor
@AllArgsConstructor
pojo手动模式: 自己写构造函数 get set
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person {
private int id;
private String name;
private int age;
private String extend;
private String password;
}
mapper.xml
数据库核心配置文件
#整合 mybatis
mybatis:
type-aliases-package: com.study.springboot.pojo
我们在mapper.xml映射文件中不需要写出实体类的完整路径,只需要写出类名即可
mapper-locations: classpath:mybatis/mapper/*.xml
开启sql日志输出
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
测试
@Autowired
PersonMapper personMapper;
@RequestMapping("/getAll")
@ResponseBody
public List<Person> getAll(){
return personMapper.getAllPerson();
}
异步
配置
- 开启异步-@EnableAsync
@EnableAsync
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
- 方法上添加异步注释-@Async
@Async
public void printMsg() throws InterruptedException {
Thread.sleep(3000);
System.out.println("执行了········");
}
定时任务
配置
- Java内部
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("start schedule");
}
},0,2000);
- quartz
springboot 整合了quartz ,但是因为其配置复杂,则出现了spring-task - spring-task
- 开启定时-EnableScheduling
- 执行方法-Scheduled
@SpringBootApplication
@EnableScheduling
public class SpringbootXmemcachedApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootXmemcachedApplication.class, args);
}
}
@Scheduled(cron = "0/2 * * * * * ")
private void task(){
System.out.println("------spring task run ");
}
缓存
Simple(spring默认缓存)
坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
使用
- 开启缓存@EnableCaching
- @Cacheable(value = “ad”,key = “#id”)
value: 内存空间
key : 主键
- @CacheEvict:方法执行成功后删除指定的本地缓存;
- @CachePut:方法执行成功后将返回值写入缓存,已存在则覆盖。
Ehcache
整合Ehcache
- 坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
- ehcache.xml
resource 下创建ehcache.xml
服务默认会去resource读取ehcache.xml,如发生变更,则application.yaml配置位置
spring.cache.ehcache.config
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<defaultCache
eternal="false"
diskPersistent="false"
maxElementsInMemory="10000"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
<cache name="account"
eternal="false"
diskPersistent="false"
maxElementsInMemory="10000"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
memoryStoreEvictionPolicy="LRU">
</cache>
</ehcache>
- application.yaml
设置缓存类型
spring:
cache:
type: ehcache
- 开启
- 作用
NoSql数据库
- Redis
- Mongo
- ES
redis
redis学习笔记
客户端技术:
说明:在SpringBoot2.x之后,原来使用的jedis被替换为lettuce
jedis:采用智联,多线程操作不安全,避免线程不安全采用jedis pool
lettuce( 默认):采用netty,实例可以在多个线程共享,不存在安全问题
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
源码
Redis序列化
StringRedisTemplate和RedisTemplate
- 两者的关系: StringRedisTemplate 继承 RedisTemplate
- 两者的数据是不共通的:也就是说 StringRedisTemplate 只能管理 StringRedisTemplate 里面的数据,RedisTemplate 只能管理 RedisTemplate 中的数据。
效果(序列化不同):
RedisTemplate
StringRedisTemplate
序列化不同 - StringRedisTemplate 的API假定所有的数据类型化都是字符类型,即key和value都是字符串类型。默认采用的是 String 的序列化策略,即 StringRedisSerializer ,保存的key和value都是采用此策略序列化保存的。
- RedisTemplate 默认采用的是JDK的序列化策略,即 JdkSerializationRedisSerializer ,保存的key和value都是采用此策略序列化保存的
看源码-RedisTemplate -JdkSerializationRedisSerializer
看源码-StringRedisTemplate -StringRedisSerializer
结论:如果你想使用默认的配置来操作 Redis,则如果操作的数据是字节数组,就是用RedisTemplate,如果操作的数据是明文,使用 StringRedisTemplate
配置
官方配置
# common spring boot settings
spring.redis.database=
spring.redis.host=
spring.redis.port=
spring.redis.password=
spring.redis.ssl=
spring.redis.timeout=
spring.redis.cluster.nodes=
spring.redis.sentinel.master=
spring.redis.sentinel.nodes=
# Redisson settings
#path to config - redisson.yaml
spring.redis.redisson.config=classpath:redisson.yaml
自定义配置
spring:
redis:
##redis 单机环境配置 # 主从配置与单机相同
# host: 127.0.0.1
# port: 6379
password:
database: 0 # 连接哪个数据库
ssl: false #启用SSL终端识别
##redis 集群环境配置
cluster:
nodes: 127.0.0.1:6601,127.0.0.1:6602,127.0.0.1:6603,127.0.0.1:6701,127.0.0.1:6702,127.0.0.0:6703
commandTimeout: 5000 #设置命令的执行时间,如果超过这个时间,则报错;
max-redirects: 3 # 获取失败 最大重定向次数
# sentinel: #配置哨兵
# nodes: # 节点
# master: # 主
lettuce:
pool:
max-active: 20 # 连接池最大连接数(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接
max-wait: 5000ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
timeout: 6000s #连接超时时间
测试
@SpringBootTest
class SpringbootApplicationTests {
@Autowired
RedisTemplate redisTemplate;
@Test
void contextLoads() throws SQLException {
// 获取链接
RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
System.out.println(connection);
// 选择要操作的数据类型 opsForxxxx,进行crud
redisTemplate.opsForValue().set("name","xusx");
redisTemplate.opsForValue().set("mykey","爱神的");
System.out.println(redisTemplate.opsForValue().get("name"));
System.out.println(redisTemplate.opsForValue().get("mykey"));
// 存储对象
TestBean testBean = new TestBean("xusx",12);
redisTemplate.opsForValue().set("TestBean",testBean);
System.out.println(redisTemplate.opsForValue().get("TestBean"));
}
}
测试结果
自定义RedisTemplate --直接使用即可
@Configuration
public class RedisConfig {
@Bean
@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
// 默认采用的是jdk序列化 JdkSerializationRedisSerializer
// json序列化配置
Jackson2JsonRedisSerializer objectJackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om=new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance ,
ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
// String 序列化
StringRedisSerializer stringRedisSerializer=new StringRedisSerializer();
// 设置序列化
template.setKeySerializer(stringRedisSerializer);
template.setValueSerializer(objectJackson2JsonRedisSerializer);
template.setHashKeySerializer(stringRedisSerializer);
template.setHashValueSerializer(objectJackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
使用自定义后的测试结果
序列化的多种方式
redis存储对象必须序列化
否:
是:
- 手动序列化 String s = new ObjectMapper().writeValueAsString(testBean);
- 自动序列化 对象实现Serializable接口
RedisUtil–工具类
整合Mongdb
客户端可视化工具-Robot-3T
简介
MongDB是一个开源、高性能、无模式的文档型数据库。NoSql数据库产品中最像关系型数据库的非关系型数据库
启动
默认端口:27017
在根目录新建data/bin来存放数据
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
配置
spring:
data:
mongodb:
uri: mongodb://localhost/local
测试CRUE
@SpringBootTest
class SpringbootMongdbApplicationTests {
@Autowired
private MongoTemplate mongoTemplate;
@Test
void contextLoads() {
Account account = new Account();
account.setName("xusx");
account.setType("222");
// 保存插入
mongoTemplate.save(account);
// 查询所有
mongoTemplate.findAll(Account .class)
// 根据id查询,此id为mongdb自动创建的id索引
mongoTemplate.findById()
// 按条件查询
Query query = new Query(Criteria.where("name").is("xusx6666"));
final List<Account> accounts = mongoTemplate.find(query, Account.class);
// 按条件删除
Query query = new Query(Criteria.where("name").is("xusx"));
final long deletedCount = mongoTemplate.remove(query, Account.class).getDeletedCount();
System.out.println(deletedCount);
// 按条件更新
Query query = new Query(Criteria.where("name").is("xusx7777"));
Update update = new Update();
update.set("name", "xusx");
final long modifiedCount = mongoTemplate.updateFirst(query, update, Account.class).getModifiedCount();
}
}
整合Elasticsearche(ES)
分布式全文搜索引擎
流程:分词–>索引id–>数据
下载
启动
启动: 在bin目录下双击elasticsearch.bat
启动中的异常问题
-
乱码
在 config/jvm.options 添加
-Dfile.encoding=GBK -
无法http访问(默认开始ssl)
-
无法免密登录
成功
http://localhost:9200/
创建索引
分词器
索引规则
xmemcached
简介
使用
下载 memcached
windows环境管理员权限启动:
memcached.exe -d install
memcached.exe -d start
memcached.exe -d stop
使用
- 坐标
<dependency>
<groupId>com.googlecode.xmemcached</groupId>
<artifactId>xmemcached</artifactId>
<version>2.4.7</version>
</dependency>
- 创建MemcachedClient
- 数据存储
@Service("smsService")
public class SMS_serviceImpl implements SMS_service {
@Autowired
private MemcachedClient memcachedClient;
@Override
public String send(String phone) throws InterruptedException, TimeoutException, MemcachedException {
final String substring = UUID.randomUUID().toString().substring(0, 4);
memcachedClient.set(phone,0,substring);
return substring;
}
@Override
public Boolean checked(String phone, String code) throws InterruptedException, TimeoutException, MemcachedException {
String string = memcachedClient.get(phone);
return code.equals(string);
}
}