zoukankan      html  css  js  c++  java
  • springboot2.0和Druid整合配置数据源

    1. idea使用spring 初始化工具初始化springboot项目(要选中web)


    下一步,下一步

    2. 在pom.xml中,引入Druid连接池依赖:

    <dependency>
        <groupId>com.github.drtrang</groupId>
        <artifactId>druid-spring-boot2-starter</artifactId>
        <version>1.1.10</version>
    </dependency>
    

    3. 添加jdbc.properties

    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://127.0.0.1:3306/leyou
    jdbc.username=root
    jdbc.password=123
    

    4. 新建JdbcProperties,用来进行属性注入

    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    /**
     * @author john
     * @date 2019/12/4 - 19:17
     */
    @ConfigurationProperties(prefix = "jdbc")
    public class JdbcProperties {
        private String url;
        private String driverClassName;
        private String username;
        private String password;
    
        public String getUrl() {
            return url;
        }
    
        public void setUrl(String url) {
            this.url = url;
        }
    
        public String getDriverClassName() {
            return driverClassName;
        }
    
        public void setDriverClassName(String driverClassName) {
            this.driverClassName = driverClassName;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    }
    
    

    5. 配置数据源

    创建JdbcConfiguration类:

    import com.alibaba.druid.pool.DruidDataSource;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import javax.sql.DataSource;
    
    /**
     * @author john
     * @date 2019/12/4 - 18:05
     */
    @Configuration
    @EnableConfigurationProperties(JdbcProperties.class)
    public class JdbcConfiguration {
    
    
        @Autowired
        private JdbcProperties jdbcProperties;
    
        @Bean
        public DataSource dataSource() {
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setUrl(jdbcProperties.getUrl());
            dataSource.setDriverClassName(jdbcProperties.getDriverClassName());
            dataSource.setUsername(jdbcProperties.getUsername());
            dataSource.setPassword(jdbcProperties.getPassword());
            return dataSource;
        }
    }
    

    配置结束

  • 相关阅读:
    基于V8引擎的C++和JS的相互交互
    C++和JavaScript脚本的相互调用
    用VC++MFC做文本编辑器(单文档模式)
    模式识别原理(Pattern Recognition)、概念、系统、特征选择和特征
    人工智能代码搜索
    QQ聊天机器人 Delphi代码
    代码自我清除 自我加密、解密的实现
    深入理解JVM虚拟机7:JNDI,OSGI,Tomcat类加载器实现
    深入理解JVM虚拟机6:深入理解JVM类加载机制
    深入理解JVM虚拟机5:虚拟机字节码执行引擎
  • 原文地址:https://www.cnblogs.com/ifme/p/11984827.html
Copyright © 2011-2022 走看看