1. 程式人生 > >連接server druid配置

連接server druid配置

map ont pan spring star lns object -name base

1. 我的連接池采用的是阿裏雲的druid的連接池,工具是IDWEA 框架是springboot+maven

以下是我的項目框架結構

技術分享

2. pom 中配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd
"> <modelVersion>4.0.0</modelVersion> <groupId>cn.xudy.sqlservice</groupId> <artifactId>StorageSqlService</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5
.4.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.25</version> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>sqljdbc4</artifactId> <version>4.0
</version> </dependency> <!-- Spring Boot JDBC --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

2. .properties 配置

server.port=8011

druid.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver


druid.url=jdbc:sqlserver://localhost:1433;DatabaseName=test
druid.username=sa
druid.password=123456

3. SystemConfig 配置

package cn.xudy.group.config;




import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.sql.DataSource;

/**
 * Created by Ulegal on 2017/8/19.
 */
@Configuration
public class SystemConfig {


            @Bean(name = "dataSource")
            @Qualifier(value = "dataSource")
            @Primary
            @ConfigurationProperties(prefix = "druid")
            public DataSource dataSource() {
                return DataSourceBuilder.create().type(DruidDataSource.class).build();

            }


    /**
     * 跨域
     * @return
     */
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**");
            }
        };
    }

}

4. dao數據層測試

@Repository
public class StorageDaoImpl implements StorageDao{

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public List<Map<String, Object>> getInfo() {


        // 傳統方法
        List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
        String  sql  =   "SELECT * FROM " + "Table_1" +";";
        list = jdbcTemplate.queryForList(sql);
        System.out.println("-------------"+list);

        return list;
    }

搞定

連接server druid配置