SpringBoot入门与常用配置
目录
入门
常用配置
配置数据库连接池
MyBatisPuls开启驼峰映射
MyBatisPuls开启打印SQL
在springboot中设置过滤器
在springboot中设置监听器
设置自动填充
自动填充时间
配置Redis
入门
1
编写控制器测试
package com.example.controller;import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController @RequestMapping("/books") public class BookController {@GetMapping("/{id}")public String getById(@PathVariable Integer id){System.out.println("id ==> "+id);return "hello , spring boot!";} }
直接运行即可
对比
SpringBoot配置文件
yaml<yml<properties
SpringBoot Mybatis
常用配置
配置数据库连接池
yaml配置文件
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/ourblog?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8username: rootpassword: xxtype: com.alibaba.druid.pool.DruidDataSource
MyBatisPuls开启驼峰映射
yaml配置文件
mybatis-plus:configuration: # 开启驼峰映射map-underscore-to-camel-case: true
MyBatisPuls开启打印SQL
yaml配置文件
mybatis-plus:configuration: # 开启SQL日志输出log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
在springboot中设置过滤器
1 springboot配置类,加入注解
@ServletComponentScan("com.scm.myblog.filter")
2 过滤器设置
@WebFilter("/*") public class SessionFilter implements Filter {
在springboot中设置监听器
只用在监听器上加上注解即可
@Configuration @WebListener public class MyRequestListener implements ServletRequestListener{
设置自动填充
自动填充时间
1 需要的字段上设置注解
@TableField(fill = FieldFill.UPDATE)private Timestamp articleUpdateTime;@TableField(fill = FieldFill.INSERT)private Timestamp articleCreateTime;
2 配置mp字段填充预处理类
package com.scm.myblog.bo;import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import org.apache.ibatis.reflection.MetaObject; import org.springframework.stereotype.Component;import java.sql.Timestamp; import java.util.Date;//自动填充时间配置 @Component public class TimeAutoFill implements MetaObjectHandler {@Overridepublic void insertFill(MetaObject metaObject) {//将所有需要插入更新的时间写在这里,第一个参数为需要自动填充的字段名this.setFieldValByName("commentCreateTime",new Timestamp(new Date().getTime()), metaObject);this.setFieldValByName("articleCreateTime",new Timestamp(new Date().getTime()), metaObject);}@Overridepublic void updateFill(MetaObject metaObject) {this.setFieldValByName("articleUpdateTime", new Timestamp(new Date().getTime()), metaObject);} }
配置Redis
依赖引入
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis-reactive</artifactId></dependency>
配置文件
spring:redis:host: localhostport: 6379password: xxdatabase: 0jedis:pool:max-active: 10max-wait: 5000max-idle: 500
简单使用
package com.learn.redis;import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate;@SpringBootTest class Redis1ApplicationTests {@Autowiredprivate RedisTemplate<String,String> redisTemplate;@Testvoid contextLoads() {redisTemplate.opsForValue().set("list1", "2");Object o = redisTemplate.opsForValue().get("list1");System.out.println(o);} }
Swagger配置
引入
<!-- Swagger配置--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><!-- 引入swagger-ui-layer包 /docs.html--><dependency><groupId>com.github.caspar-chen</groupId><artifactId>swagger-ui-layer</artifactId><version>1.1.3</version></dependency>
新建配置类
package com.scm.myblog.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2;import java.util.ArrayList;@Configuration //配置类 @EnableSwagger2// 开启Swagger2的自动配置 public class SwaggerConfig {//配置文档信息private ApiInfo apiInfo() {Contact contact = new Contact("Lancer", "", "xx@qq.com");return new ApiInfo("个人博客系统", // 标题"练手项目", // 描述"v1.0", // 版本"", // 组织链接contact, // 联系人信息"Apach 2.0 许可", // 许可"",new ArrayList<>());}@Beanpublic Docket docket() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).enable(true)//为false的话,不能通过浏览器访问swagger.select()// 通过.select()方法,去配置扫描接口,RequestHandlerSelectors配置如何扫描接口.apis(RequestHandlerSelectors.basePackage("com.scm.myblog.controller")).build();} }
mybatis-plus:
#外部化xml配置
#config-location: classpath:mybatis-config.xml
#指定外部化 MyBatis Properties 配置,通过该配置可以抽离配置,实现不同环境的配置部署
#configuration-properties: classpath:mybatis/config/properties
#xml扫描,多个目录用逗号或者分号分割(告诉 Mapper 所对应的 XML 文件位置)
mapper-locations: classpath*:com/wongoing/sys/mapper/xml/*.xml
#MyBatis 别名包扫描路径,通过该属性可以给包中的类注册别名,多个路径用逗号分割
type-aliases-package: com.wongoing.sys.model
#如果配置了该属性,则仅仅会扫描路径下以该类作为父类的域对象
type-aliases-super-type: java.lang.Object
#枚举类 扫描路径,如果配置了该属性,会将路径下的枚举类进行注入,让实体类字段能够简单快捷的使用枚举属性
#type-enums-package: com.wongoing.sys.model
#项目启动会检查xml配置存在(只在开发时打开)
check-config-location: true
#SIMPLE:该执行器类型不做特殊的事情,为每个语句的执行创建一个新的预处理语句,REUSE:改执行器类会复用预处理语句,BATCH:该执行器类型会批量执行所有的更新语句
executor-type: REUSE
configuration:
# 是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN(下划线命名) 到经典 Java 属性名 aColumn(驼峰命名) 的类似映射
map-underscore-to-camel-case: true
# 全局地开启或关闭配置文件中的所有映射器已经配置的任何缓存,默认为 true
cache-enabled: true
#懒加载
aggressive-lazy-loading: true
#none:不启用自动映射 partial:只对非嵌套的 resultMap 进行自动映射 full:对所有的 resultMap 都进行自动映射
auto-mapping-behavior: partial
#none:不做任何处理 (默认值)warning:以日志的形式打印相关警告信息 failing:当作映射失败处理,并抛出异常和详细信息
auto-mapping-unknown-column-behavior: none
#如果查询结果中包含空值的列,则 MyBatis 在映射的时候,会不会映射这个字段
call-setters-on-nulls: true #允许在resultType="map"时映射null值
#这个配置会将执行的sql打印出来,在开发或测试的时候可以用
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#是否允许映射结果为多个数据集
multiple-result-sets-enabled: false
global-config:
db-config:
#表名下划线命名默认为true
table-underline: false
#id类型。
id-type: ASSIGN_ID # 默认为ASSIGN_ID
#是否开启大写命名,默认不开启
capital-mode: false
#逻辑已删除值(逻辑删除下有效)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
#逻辑未删除值(逻辑删除下有效)
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
SpringBoot入门与常用配置相关推荐
- SpringBoot中Logback常用配置以及自定义输出到MySql数据库
之前基于SpringBoot开发的项目运行一段时间后,客户使用网站偶尔会出现接口调用失败的情况,每次产品经理询问是怎么回事的时候,都需要让运维提下最近的日志才能分析具体原因,这样时效性和便利性不能满足 ...
- springboot 入门二- 读取配置信息一
在上篇入门中简单介绍下springboot启动使用了大量的默认配置,在实际开发过程中,经常需要启动多个服务,那端口如何手动修改呢? 此篇就是简单介绍相关的配置文件信息. Spring Boot允许外部 ...
- 最全面 Nginx 入门教程 + 常用配置解析
http://blog.csdn.net/shootyou/article/details/6093562 个人整理资料,转帖注明出处,谢谢~ Nginx介绍和安装 一个简单的配置文件 模块介绍 常用 ...
- HAProxy入门及常用配置模拟测试
HAProxy简介 HAProxy是一个使用C语言编写的,提供负载均衡,以及基于TCP(伪四层)和HTTP(七层)的应用程序代理. HAProxy特别适用于那些负载大的web站点,这些站 ...
- Spring入门与常用配置
什么是Spring Spring:SE/EE开发的一站式框架. 一站式框架:有EE开发的每一层解决方案. WEB层 :SpringMVC Service层 :Spring的Bean管理,Spri ...
- SpringBoot入门之简单配置
今天下载了<JavaEE开发的颠覆者SpringBoot实战>这本书,发现Spring还有好多遗漏的部分,算是又恶补了一下,今天主要是学习下SpringBoot的配置. 一.基本配置 1. ...
- 3.SpringBoot 常用配置
删除这几个文件 cp -R ~/IdeaProjects/liuqi-boot-helloworld ~/IdeaProjects/agan-boot cp -R ~/IdeaProjects/ag ...
- 【超级详细教程】IntelliJ IDEA 从入门到上瘾,常用配置、插件、多光标操作、快捷键。
本文共计 1.5 W 字,80 张图介绍 IDEA 中令人相见恨晚的技巧,本文中从入门.简单项目创建开始,介绍 IDEA 中多光标操作.常用配置.插件.版本控制等等.一定包含你在别的文章没有看到的内容 ...
- 【Springboot 入门培训】# 17 WebJars + BootStrap5 常用JS组件应用
在传统的前后一体项目开发中,大部分人会使用到BootStrap加其它JS组件的配合方式来完成页面UI功能的实现.下面介绍几种常用的JS库的使用方法.代码例子下载 目录 1 树形组件 1.1 TreeJ ...
最新文章
- 生信人写程序2. Editplus添加Perl, Shell, R模板和语法高亮
- 洛谷3933 Chtholly Nota Seniorious 二分答案+贪心
- 5.6 SMO-机器学习笔记-斯坦福吴恩达教授
- C++11新特性,利用std::chrono精简传统获取系统时间的方法
- lua学习之类型与值篇
- 【报告分享】2020年5G芯片行业研究报告.pdf(附下载链接)
- python模块之logging
- Sublime搭建Python开发环境
- netcore 之docker
- 物流的趋势和计算机科技,计算机仿真技术在物流领域的前景分析
- 桌面ie图标删除不了
- 基于Java的qq截图工具(毕业设计含源码)
- Android.mk编译错误 FAILED: ninja: unknown target ‘MODULES-IN-packages-apps-XXXX‘
- php Guzzle源码,PHP Guzzle获取请求
- 电动汽车(EV)电池粘合剂市场现状及未来发展趋势
- 企业IT资产年终盘点实录——踩过的坑如月球表面
- Java汇集接口、异常处理、常用使用类和集合等技术的实验项目
- Shell脚本实现MySQL主从自动化配置
- Windows系统深度学习Anaconda、PyTorch软件安装教程
- 微信小程序实现点击生成随机验证码功能
热门文章
- Allegro PCB封装焊盘介绍(一)
- Linux 系统下 CodeBlocks安装与使用
- mysql没有my.ini但是有 my-default.ini原因以及解决办法
- sparksql2.0整理-自用
- 【全文翻译】Edge Intelligence: Paving the Last Mile of Artificial Intelligence With Edge Computing
- oracle 的三个主要内存结构SGA,PGA,UGA
- spi ioctl无效参数解决
- 铁路系统各专业介绍(车机工电辆)
- C++常见编译WARNING小结
- 锐龙R7 PRO 6860Z怎么样 相当于什么水平级别