当前位置:   article > 正文

【yml】yml基础配置文件一点通(Maven项目 + springboot项目)_yml数据库配置

yml数据库配置

【yml】yml基础配置文件一点通(Maven项目 + springboot项目)

(1)application.yml 配置文件示例

先来看一个springboot工程的 application.yml 配置文件,主要配置内容如下:

server:
  port: 8080
spring:
  application:
    #应用程序启动实例名称
    name: daqi_reggie_take_out
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/reggie?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
      username: root
      password: root
mybatis-plus:
  configuration:
    #在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射
    map-underscore-to-camel-case: true
    #mybatis打印(sql日志)
    log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
  global-config:
    db-config:
      id-type: ASSIGN_ID
      logic-delete-field: isDeleted # 全局逻辑删除的实体字段名,配置后可不用@TableLogic注解
      logic-delete-value: 1         # 逻辑已删除
      logic-not-delete-value: 0     # 逻辑未删除
  #定义所有操作类的别名所在包
  type-aliases-package: com.daqi.**.entity
  
#mapper 打印日志
logging:
  level:
    com.daqi: debug
    com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean: INFO

reggie:
  #自定义属性 存放图片路径
  path: D:\img\
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

上述结构是yml格式的,树状结构,配置内容直观清晰有条理,与properties格式虽写法迥异,但内容是大差不差的。

(2)YML简介

YAML(YAML Ain’t Markup Language),简称YML,一种数据序列化格式。具有容易阅读、容易与脚本语言交互、以数据为核心,重数据轻格式的特点。常见的文件扩展名有两种:
(1)yml格式(主流,推荐使用)
(2)yaml格式

1, YAML 语法(yml配置文件格式的特点)

1、大小写敏感
2、属性层级关系使用多行描述,每行结尾使用冒号结束
3、使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(注意:不允许使用Tab键)
4、属性值前面添加空格(属性名与属性值之间使用 冒号+空格 作为分隔)
5、可以使用 “-小写字母” 或 "_小写字母"来 代替 “大写字母”,如 userName与user_name,user-name含义相同
6、key: value 整体为k-v结构,key 后面跟着冒号,再后面跟着一个空格,然后是值。如:port: 8080
5、#号表示注释

(3)几种常见数据格式的表示方式举例

3.1,配置普通数据举例:

boolean: TRUE  						 #TRUE,true,True,FALSE,false,False均可
float: 3.14    						 #6.8523015e+5  #支持科学计数法
int: 123       						 #0b1010_0111_0100_1010_1110    #支持二进制、八进制、十六进制
null: ~        						 #使用~表示null
string: HelloWorld      			 #字符串可以直接书写
string2: "Hello World"  			 #可以使用双引号包裹特殊字符
date: 2018-02-17        			 #日期必须使用yyyy-MM-dd格式
datetime: 2018-02-17T15:02:31+08:00  #时间和日期之间使用T连接,最后使用+代表时区
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3.2,对象、Map (属性和值 (k-v键值对方式))

#方式一
users:
  - name: zhangsan
    age: 18
  - name: lisi
    age: 17
 
#方式二
users2:
  -
    name: zhangsan
    age: 18
  -
    name: lisi
    age: 17
 
#方式三
users3: [{name:zhangsan,age:18},{name:lisi,age:17}]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

3.3.数组 (List、Set)

item-states:
  - 0  
  - 1
  - -1
#数组书写缩略格式
likes: [太阳,地球,月亮,水星,金星,木星]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

(4)那java程序如何从yml文件取值呢?

方法一:

在定义属性的字段上添加@value注解(搭配@Component注解使用)
如,我们要取得

server:
  username: root
  password: root
  • 1
  • 2
  • 3

可以这样:

import org.springframework.beans.factory.annotation.Value;
@Component  //实体类需要添加@Component注解才会生效
public class DatabaseInfo{
    @Value("${server.username}")  //使用${}来取值
    private String username;
    @Value("${server.password}")
    private String password;
}    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
方法二:

使用@ConfigurationProperties注解(配置JavaBean映射方案)
如,我们要取得

consumer:
  username: 张三
  addresss: 蓬莱 
  • 1
  • 2
  • 3

可以这样:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Component  //实体类/封装类需要定义为Spring管理的bean,否则无法进行属性注入
@ConfigurationProperties(prefix = "consumer")  //绑定配置信息到实体类/封装类中
public class Consumer{
    private String username;
    private String addresss;
}    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

附:yaml配置文件

(5)MyBatis中typeAliases属性详解

给Java类型的实体类取别名

typeAliases别名用法

参例上面的介绍:

mybatis-plus:
  #定义所有操作类的别名所在包
  type-aliases-package: com.daqi.**.entity
  • 1
  • 2
  • 3
1,用法一:给指定的实体类起别名
<!--configuration核心配置文件-->
<configuration>
    <!-- 引入外部配置文件 -->
    <properties resource="db.properties">
        <property name="username" value="root"/>
        <property name="password" value="Wang118821"/>
    </properties>

    <!-- 用于给实体类起别名 -->
    <typeAliases>
        <typeAlias type="com.example.demo.pojo.User" alias="User"/>
    </typeAliases> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

mapper里使用

    <select id="getUserList" resultType="User">
        select * from mybatis.user
    </select>
  • 1
  • 2
  • 3
2,用法二:指定一个包名,MyBatis会在包名下搜索需要的Java Bean,

比如:扫描实体类的包,在没有注解的情况下,它的默认别名就为这个类的类名,首字母小写;若有注解,别名为其注解值,见下面例子:

<!--configuration核心配置文件-->
<configuration>
    <!-- 引入外部配置文件 -->
    <properties resource="db.properties">
        <property name="username" value="root"/>
        <property name="password" value="Wang118821"/>
    </properties>

    <!-- 用于给实体类起别名 -->
    <typeAliases>
        <package name="com.example.demo.pojo" />
    </typeAliases>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
    <select id="getUserList" resultType="user">
        select * from mybatis.user
    </select>
  • 1
  • 2
  • 3

此时还可以在实体类增加注解

//实体类
@Alias("userInfo")
public class User {
    private Integer id;
    private String name;
    private String pwd;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
    <select id="getUserList" resultType="userInfo">
        select * from mybatis.user
    </select>
  • 1
  • 2
  • 3

传送门:MyBatis中typeAliases属性——给Java类型的实体类取别名

这一篇写的较为详细:

传送门:SpringBoot - 配置文件application.yml使用详解(附:Profile多环境配置)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/953725?site
推荐阅读
相关标签
  

闽ICP备14008679号