4,Spring Boot集成功能

Spring Boot 可以快速集成其他框架和功能,本文例举几个常用的集成功能,只保证能使用,不做细致配置,后期开专栏针对配置做细节说明。

  • Spring Farmework:核心功能

  • Spring Data:数据获取

  • Spring Security:认证授权

  • Spring AMQP:消息传递

  • Spring Cloud:服务治理

集成一般两个步骤,

  1. pom.xml引入依赖

  2. yml文件配置

pom.xml文件引入依赖需要注意版本,一般使用Idea工具,会有自动提示功能

集成mybatis

pom.xml

<!--mysql 驱动-->
<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
</dependency>

<!--mybatis 起步依赖-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>3.0.4</version>
</dependency>

.yml

spring:
  datasource:
    # 数据库连接地址
    url: jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC
    # 用户名
    username: your_username
    # 密码
    password: your_password
    # 连接驱动
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis: 
  #设置.xml存放位置
  mapper-locations: classpath:mapper/*.xml

集成redis

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

.yml

spring:
  redis:    
    host: localhost
    port: 6379
    # 如果redis没设密码,需要去掉这行
    password: password

集成Security

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

.yml

spring:
  security:
    user:
      name: yourUsername  # 自定义用户名
      password: yourPassword  # 自定义密码(注意:在生产环境中,密码应该进行加密存储)
      roles: USER  # 用户的角色,可以是多个,用逗号分隔
    session:
      timeout: 30m  # 会话超时时间
      store-type: redis  # 会话存储类型,可以是jdbc、hazelcast、redis等

通过上面简单的引用即可实现复杂的功能,并且能个性化的去配置,实现自己想要的效果,这样是不是对Spring Boot有了更清晰的认识了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值