SpringBoot-数据访问配置

目录

一、SQL

1.1 数据源的自动配置-HikariDataSource

1.2 使用Druid数据源

1.3 整合MyBatis

1.4 整合 MyBatis-Plus

二、NoSQL

1、Redis自动配置


一、SQL

1.1 数据源的自动配置-HikariDataSource

        导入依赖:

<dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
/dependency>

         记得配修改置项:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/db_account
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver

        测试:

@SpringBootTest
class Boot05WebAdminApplicationTests {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Test
    void contextLoads() {

    Long aLong = jdbcTemplate.queryForObject("select count(*) from account_tbl", Long.class);
    }

1.2 使用Druid数据源

        引入starter

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.17</version>
</dependency>

        配置示例:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/db_account
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver

    druid:
      aop-patterns: com.atguigu.admin.*  #监控SpringBean
      filters: stat,wall     # 底层开启功能,stat(sql监控),wall(防火墙)

      stat-view-servlet:   # 配置监控页功能
        enabled: true
        login-username: admin #进入后台的账号密码
        login-password: admin
        resetEnable: false

      web-stat-filter:  # 监控web
        enabled: true
        urlPattern: /*
        exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'


      filter:
        stat:    # 对上面filters里面的stat的详细配置
          slow-sql-millis: 1000
          logSlowSql: true
          enabled: true
        wall:
          enabled: true
          config:
            drop-table-allow: false

         druid后台访问地址:

http://localhost:8080/druid

1.3 整合MyBatis

         引入starter:

<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
</dependency>

         配置文件:

# 配置mybatis规则
mybatis:
  mapper-locations: classpath:mybatis/mapper/*.xml  #mapper配置文件的位置
  configuration:
    map-underscore-to-camel-case: true
    
#可以不写全局配置文件,所有全局配置文件的配置都放在configuration配置项中即可
  • 编写的mapper接口@Mapper注解标注。
  • 或者在application入口类上标注@MapperScan("com.xxx.mapper"),这样会自动扫描mapper

1.4 整合 MyBatis-Plus

        建议安装 MybatisX 插件

        引入starter,该starter已经包括了mybatis的配

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

置,无需再单独引入mybatis了:

 <dependency>
         <groupId>com.baomidou</groupId>
         <artifactId>mybatis-plus-boot-starter</artifactId>                            
         <version>3.4.1</version>
 </dependency>
  • mapperLocations 自动配置好了默认值:classpath*:/mapper/**/*.xml
  • 任意包的类路径下的所有mapper文件夹下任意路径下的所有xml都是sql映射文件。 建议以后sql映射文件,放在mapper
  • @Mapper 标注的接口也会被自动扫描;
  • 建议直接@MapperScan("com.xxx.mapper") 批量扫描

二、NoSQL

1、Redis自动配置

 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>
  • 连接工厂是准备好的。LettuceConnectionConfigurationJedisConnectionConfiguration
  • 底层只要我们使用 StringRedisTemplateRedisTemplate就可以操作redis

        切换至jedis:

<!-- 导入jedis--> 
<dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
</dependency>

        配置文件:

spring:
  redis:
      host: xxxx
      port: xxxx
      password: xxxx
      client-type: jedis
      jedis:
        pool:
          max-active: 10

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值