基本项目框架搭建 sqlserver druid配置

  1.  我的连接池采用的是阿里云的druid的连接池,工具是IDEA 框架是springboot+maven

   以下是我的项目框架结构

  基本项目框架搭建 sqlserver druid配置_第1张图片

 2. pom  中配置

 

"1.0" encoding="UTF-8"?>
"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">
    4.0.0

    cn.xudy.sqlservice
    StorageSqlService
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.4.RELEASE
        
    

    


        
            com.alibaba
            druid
            1.0.25
        
        
            com.microsoft.sqlserver
            sqljdbc4
            4.0
        
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        


        
            org.springframework.boot
            spring-boot-starter-web
        

    


    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


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> getInfo() {


        // 传统方法
        List> list = new ArrayList>();
        String  sql  =   "SELECT * FROM " + "Table_1" +";";
        list = jdbcTemplate.queryForList(sql);
        System.out.println("-------------"+list);

        return list;
    }

   搞定

 

转载于:https://www.cnblogs.com/memoryXudy/p/7767741.html

你可能感兴趣的:(数据库,c#,开发工具)