1. 我的连接池采用的是阿里云的druid的连接池,工具是IDEA 框架是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; }
搞定