污pringboot绑数据html,Spring Boot学习日志------连接数据库三方法之Springboot属性注入...

目录

操作说明

创建application.properties(如果有jdbc.properties文件合并以下,然后删除jdbc.properties)

创建配置类文件

将配置类文件注入到对象中

操作详解

创建application.properties文件

d914b39bd9807003e2126e1d475fab69.png

#数据库连接配置

jdbc.driverClassNamr=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://127.0.0.1:3306/ssm

jdbc.username=root

jdbc.password=root

创建配置类文件

package com.example.ycrk.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.stereotype.Component;/**

* ConfigurationProperties从application配置文件中读取配置

* prefix配置项前缀

* 配置项前缀的类变量名必须与前缀之后的配置项名称保持 松散绑定*/@Component

@ConfigurationProperties(prefix="jdbc")public classJdbcProperties {privateString driverClassNamr;privateString url;privateString username;privateString password;publicString getDriverClassNamr() {returndriverClassNamr;

}public voidsetDriverClassNamr(String driverClassNamr) {this.driverClassNamr =driverClassNamr;

}publicString getUrl() {returnurl;

}public voidsetUrl(String url) {this.url =url;

}publicString getUsername() {returnusername;

}public voidsetUsername(String username) {this.username =username;

}publicString getPassword() {returnpassword;

}public voidsetPassword(String password) {this.password =password;

}

}

创建对象

package com.example.ycrk.config;

import com.alibaba.druid.pool.DruidDataSource;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.boot.context.properties.EnableConfigurationProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

@Configuration

@EnableConfigurationProperties(JdbcProperties.class)public classJdbcConfig {

@BeanpublicDataSource dataSource(JdbcProperties jdbcProperties){

DruidDataSource druidDataSource=newDruidDataSource();

druidDataSource.setDriverClassName(jdbcProperties.getDriverClassNamr());

druidDataSource.setUrl(jdbcProperties.getUrl());

druidDataSource.setUsername(jdbcProperties.getUsername());

druidDataSource.setPassword(jdbcProperties.getPassword());returndruidDataSource;

}

}

调用

package com.example.ycrk.controller;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;

@RestControllerpublic classHelloController {

@AutowiredprivateDataSource dataSource;

@GetMapping("hello")publicString hello(){

System.out.println("dataSource="+dataSource.toString());return "Hello,Spring Boot!";

}

}

结果

b5a72f8031214f20212eed46c1c71d00.png

操作优缺点

繁琐,如果变量过多不是很方便

代码优化

将JdbcConfig 代码更换为以下代码

package com.example.ycrk.config;

import com.alibaba.druid.pool.DruidDataSource;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.boot.context.properties.EnableConfigurationProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

@Configuration

@EnableConfigurationProperties(JdbcProperties.class)public classJdbcConfig {/***

* 自动根据前缀调取相应的方法

* @return*/@Bean

@ConfigurationProperties(prefix= "jdbc")publicDataSource dataSource(){return newDruidDataSource();

}

}

操作出现的问题

错误:ConfigurationProperties注入爆红

2f7fd62f540a1d7a9da6b922678da08e.png

解决方法

1、在pom.xml中添加以下依赖

org.springframework.boot

spring-boot-configuration-processor

true

2、ConfigurationProperties注入方式上面添加@Component

@Component

@ConfigurationProperties(prefix="jdbc")

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值