java spring boot的配置文件

java spring boot的配置文件

boot项目中有很多的配置,那么基础的配置有哪些呢?下面是笔者在实际开发中用到的yml配置文件:

spring:
  datasource:
    driver-class-name: oracle.jdbc.OracleDriver
    url: jdbc:oracle:thin:@192.168.100.234:1521/orcl
    username: test
    password: test
    filters: slf4j,wall,mergeStat
    initialSize: 5
    minIdle: 5
    maxActive: 10
    #连接等待超时时间
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM dual
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    useGlobalDataSourceStat: true
    type: com.alibaba.druid.pool.DruidDataSource
#############################################################################################
mybatis-plus:
  /* 如果是放在src/main/java目录下 classpath:/com/yourpackage/*/mapper/*Mapper.xml*/
  mapper-locations: classpath:/com/example/demo/**/mapping/*.xml
  /*实体扫描,多个package用逗号或者分号分隔*/
  typeAliasesPackage: com.example.demo.*.model
  global-config:
    /*主键类型  0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";*/
    id-type: 1
    /*字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"*/
    field-strategy: 2
    /*驼峰下划线转换*/
    db-column-underline: true
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: true #配置的缓存的全局开关
    lazyLoadingEnabled: true #延时加载的开关
    multipleResultSetsEnabled: true #开启的话,延时加载一个属性时会加载该对象全部属性,否则按需加载属性
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印sql语句,调试用
server:
  port: 8886
  servlet:
    context-path: /sw

笔者用的是oracle数据库,读者可依据自己的实际的数据库进行调整。有了这些yml的基础配置,就可以满足大部分的开发需求了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Spring Boot中,YAML(Yet Another Markup Language)是一种简洁的配置文件格式,用于替代XML配置。Spring Boot通过Spring Boot Actuator提供对YAML的支持,使得配置管理更加直观和易读。 以下是使用Spring Boot配置YAML的基本步骤: 1. **创建YAML配置文件**: 在`src/main/resources`目录下创建一个名为`application.yml`或`application.properties`(默认为properties格式,但Spring Boot也支持YAML)的文件。例如,对于YAML配置: ```yaml server: port: 8080 spring: datasource: url: jdbc:mysql://localhost:3306/mydb username: user password: pass ``` 2. **引用配置**: 在主应用类上添加`@SpringBootApplication`注解,并启用YAML加载,可以通过`spring.config.location`属性指定配置文件位置,如`spring.config.location=classpath:application.yml`。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ConfigurationProperties; @SpringBootApplication @ConfigurationProperties(prefix = "spring.datasource") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 3. **使用配置属性**: 使用@ConfigurationProperties注解可以自动将YAML中的键值对映射到Java类的属性上,使得配置更易于管理和使用。 4. **使用Profile(环境变量)**: Spring Boot支持基于环境的配置,可以在YAML中定义不同的环境模式,如`application-dev.yml`、`application-prod.yml`等,通过`spring.profiles.active`属性切换环境。 相关问题-- 1. Spring Boot如何区分YAML和properties配置? 2. 如何在Spring Boot中启用YAML配置并指定文件路径? 3. 什么情况下会使用Spring Boot的Profile功能?

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值