码迷,mamicode.com
首页 > 其他好文 > 详细

连接server druid配置

时间:2017-11-01 18:14:54      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:mvc   snap   uid   apt   es2017   lease   class   sql   .config   

  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配置

标签:mvc   snap   uid   apt   es2017   lease   class   sql   .config   

原文地址:http://www.cnblogs.com/memoryXudy/p/7767741.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!