Spring 项目搭建

spring boot 项目搭建

基础

  • 位置
    1. MainApplication放在主包下面,会自动扫描当前包以及当前包的子包
server:
  port: 8080

数据库集成

基础

  • 配置文件添加对应数据库配置
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/demospringboot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&autoReconnect=true&maxReconnects=1&initialTimeout=50000&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull&connectTimeout=50000&socketTimeout=50000
    username: root
    password: 123456
  • mybatis
    1. 生成mapper,xml mapper.java(接口) entity后,执行mapper.xml文件的加载位置,xml会被mybatis自动加载,xml指明对应mapper.java的位置
    2. 使用时直接注入对应的接口
    3. yml 文件写入
# mybatid
mybatis:
  mapperLocations: classpath:mapper/*.xml

日志集成

基础

  • spring boot 默认日志系统为 logback
  • 引入 spring-boot-starter-logging 依赖,一般在spring-boot-starter中已经导入;完整的依赖为
 <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-logging</artifactId>
      <version>2.2.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>

使用

  • 配置默认日志级别
    1. logging: level: root: debug ;root 属性必须要添加 。此方式指定默认所有的日志级别
    2. root 可以改为包名,用来配置指定包下的日志级别;(可以使用项目的最顶级的包名,一般不会重复)
      在这里插入图片描述
  • 配置日志输出位置
    1. 默认存在控制台输出
    2. 文件方式:
      1. logging.file.name: log/debug.log ,指定日志输出的文件位置;目录+文件名;使用相对路径方式时,其相对于项目本身,而非src;当仅指定文件名时,会生成于src 同级别的日志文件;
      2. logging.file.path 指定目录,会在该目录下生成spring.log 文件;
  • 日志输出
    1. 使用sel4j 时,日志的占位符为“{}”;在输出前其会判断级别,如未达到输出级别,则不会进行日志内容的拼接

测试集成

  • 导入依赖
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
  • 创建测试类
    1. 添加注释@RunWith(SpringRunner.class) @SpringBootTest(classes={Application.class})
    2. Application 指项目启动类

Redis集成

  • 引入依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
  • 配置pom文件
spring:
  redis:
    database: 0
    host: 39.97.101.82
    port: 6379
    timeout: 1000
  • 在类中注入RedisTemplate即可
 @Autowired
    RedisTemplate<String, String> redisTemplate;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值