spring boot的日常配置

配置篇

#数据库连接配置msql
spring.datasource.url:jdbc:mysql://127.0.0.1:3306/test
spring.datasource.username: root
spring.datasource.password: 123456
spring.datasource.driver-class-name: com.mysql.jdbc.Driver


#控制台打印sql语句
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl 

#配置打印sql语句
spring.jpa.show-sql=true
#配置表中字段大写,不配置此项mysql字段默认为小写
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl 

#rabbitmq配置
spring.application.name=rabbitmq-hello
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=123456

#邮箱配置
#邮箱的服务器
spring.mail.host=smtp.qq.com
#发件人邮箱
spring.mail.username=
#对于qq邮箱而言 密码指的就是发送方的授权码
spring.mail.password=
#是否需要验证码
spring.mail.properties.mail.smtp.auth=true
# starttls 是对纯文本通信协议的扩展。它提供一种方式将纯文本连接升级为加密连接(TLSSSL
spring.mail.properties.mail.smtp.starttls.enable=true

spring.mail.properties.mail.smtp.starttls.required=true
#Redis配置信息
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=-1
 

spring.jpa.properties.hibernate.hbm2ddl.auto

spring.jpa.properties.hibernate.hbm2ddl.auto是hibernate的配置属性,其主要作用是:自动创建、更新、验证数据库表结构。该参数的几种配置如下:

  • create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。
  • create-drop:每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除。
  • update:最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等应用第一次运行起来后才会。
  • validate:每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。

注解篇

   @EnableScheduling  Spring Boot的主类中加入@EnableScheduling注解,启用定时任务的配置

   @Scheduled(fixedRate=2000):上一次开始执行时间点后2秒再次执行;

   @Scheduled(fixedDelay=2000):上一次执行完毕时间点后2秒再次执行;

   @Scheduled(initialDelay=1000, fixedDelay=2000):第一次延迟1秒执行,然后在上一次执行完毕时间点后2秒再次执行;

   @Scheduled(cron="* * * * * ?"):按cron规则执行。

 缓存注解

  @EnableCaching 注解自动化配置合适的缓存管理器

  @CacheConfig  主要用于配置该类中会用到的一些共用的缓存配置。

  @Cacheable  配置了指定函数的返回值将被加入缓存。同时在查询时,会先从缓存中获取,若不存在才再发起对数据库的访问

  @CachePut  配置于函数上,能够根据参数定义条件来进行缓存,它与@Cacheable不同的是,它每次都会真是调用函数,所以主要用于数据新增和修改操作上。它的参数与@Cacheable类似,具体功能可参考上面对@Cacheable参数的解析

  @CacheEvict  配置于函数上,通常用在删除方法上,用来从缓存中移除相应数据。除了同@Cacheable一样的参数之外,它还有下面两个参数:

    allEntries  非必需,默认为false。当为true时,会移除所有数据

    beforeInvocation  非必需,默认为false,会在调用方法之后移除数据。当为true时,会在调用方法之前移除数据

  @RabbitListener  注解定义该类对指定队列的监听

  @RabbitHandler  注解来指定对消息的处理方法。所以,该消费者实现了对指定队列的消费,消费操作为输出消息的字符串内容。

  @EnableStateMachine  注解用来启用Spring StateMachine状态机功能

配置springboot后台运行 linux

  在pom.xml 中添加spring boot的插件并设置executable配置

  

<build> 
  <plugins> 
    <plugin> 
      <groupId>org.springframework.boot</groupId>  
      <artifactId>spring-boot-maven-plugin</artifactId>  
      <configuration> 
        <executable>true</executable> 
      </configuration> 
    </plugin> 
  </plugins> 
</build>
  完成后 使用mvn install 打包构建成一个可以运行的jar包
  创建软连接到/etc/init.d/目录下
sudo ln -s /var/yourapp/xxx.jar /etc/init.d/xxx

  完成后就可以通过下面命令进行控制

/etc/init.d/xxx start|stop|restart
原文地址:https://www.cnblogs.com/oldzhang1222/p/9285130.html