SpringBoot +MSSQL
____SpringBoot +MSSQL________________________________________________________________________________
https://www.cnblogs.com/wang-yaz/p/9561188.html ******************这篇最重要
4.从事务管理器中选择一个事务,在方法上加@Transactional(value = "mysqlTransactionManager",rollbackFor = Exception.class)
背景:原来一直都是使用mysql数据库,在application.properties 中配置数据库信息
spring.datasource.url=jdbc:mysql://xxxx/test
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=xxx
spring.datasource.password=xxx
这样spring boot会自动化配置,我们用spring boot 约定的配置,现在由于业务的需要,另加一个数据源sqlServer。下面是具体步骤以及遇到的一系列问题。

1 pom.xml文件:
2 <dependency>
3 <groupId>com.microsoft.sqlserver</groupId>
4 <artifactId>mssql-jdbc</artifactId>
5 <version>6.4.0.jre8</version>
6 <scope>runtime</scope>
7 </dependency>


1 spring.datasource.second.jdbc-url=jdbc:sqlserver://xxxxx:1433;DatabaseName=test
2 spring.datasource.second.username=xxx
3 spring.datasource.second.password=xxxx
4 spring.datasource.second.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
5
6 注意1:连接数据库的方式不一样,mysql是/test ,sqlServer是;DatabaseName=test
7 spring.datasource.url=jdbc:mysql://xxxx/test
8 spring.datasource.second.jdbc-url=jdbc:sqlserver://xxxxx:1433;DatabaseName=test


1 package com.ieou.qmt.common;
2 import org.apache.ibatis.session.SqlSessionFactory;
3 import org.mybatis.spring.SqlSessionFactoryBean;
4 import org.springframework.beans.factory.annotation.Qualifier;
5 import org.springframework.boot.context.properties.ConfigurationProperties;
6 import org.springframework.boot.jdbc.DataSourceBuilder;
7 import org.springframework.context.annotation.Bean;
8 import org.springframework.context.annotation.Configuration;
9 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
10 import org.springframework.core.io.support.ResourcePatternResolver;
11 import org.springframework.jdbc.core.JdbcTemplate;
12 import org.springframework.jdbc.datasource.DataSourceTransactionManager;
13 import org.springframework.transaction.PlatformTransactionManager;
14 import javax.sql.DataSource;
15
16 @Configuration
17 public class SqlServerDataSourceConfig {
18
19 @Bean(name = "sqlServerDataSource")
20 @Qualifier("sqlServerDataSource")
21 @ConfigurationProperties(prefix="spring.datasource.second")
22 public DataSource getMyDataSource(){
23 return DataSourceBuilder.create().build();
24 }
25
26 @Bean(name = "secondaryJdbcTemplate")
27 public JdbcTemplate secondaryJdbcTemplate(
28 @Qualifier("sqlServerDataSource") DataSource dataSource) {
29 return new JdbcTemplate(dataSource);
30 }
31 }

总结:

1 "org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
2
3 'org.springframework.transaction.PlatformTransactionManager' available\n\tat
4
5 org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:347)\n\tat
6
7 org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:334)\n\tat
8
9 org.springframework.transaction.interceptor.TransactionAspectSupport.determineTransactionManage


1 spring.datasource.first.jdbc-url=jdbc:mysql://xxxx:3306/test
2 (这里要是jdbc-url,不然会报jdbcUrl is required with driverClassName的错误)
3 spring.datasource.first.type=com.alibaba.druid.pool.DruidDataSource
4 spring.datasource.first.driver-class-name=com.mysql.jdbc.Driver
5 spring.datasource.first.username=xxx
6 spring.datasource.first.password=xxx
7
8 spring.datasource.second.jdbc-url=jdbc:sqlserver://xxxx:1433;DatabaseName=test
9 spring.datasource.second.username=xxx
10 spring.datasource.second.password=xxx
11 spring.datasource.second.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver


1 package com.ieou.qmt.common;
2
3 import org.apache.ibatis.session.SqlSessionFactory;
4 import org.mybatis.spring.SqlSessionFactoryBean;
5 import org.springframework.beans.factory.annotation.Qualifier;
6 import org.springframework.boot.context.properties.ConfigurationProperties;
7 import org.springframework.boot.jdbc.DataSourceBuilder;
8 import org.springframework.context.annotation.Bean;
9 import org.springframework.context.annotation.Configuration;
10 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
11 import org.springframework.core.io.support.ResourcePatternResolver;
12 import org.springframework.jdbc.core.JdbcTemplate;
13 import org.springframework.jdbc.datasource.DataSourceTransactionManager;
14 import org.springframework.transaction.PlatformTransactionManager;
15
16 import javax.sql.DataSource;
17
18 @Configuration
19 public class SqlServerDataSourceConfig {
20
21 private static final String MAPPER_PATH = "classpath:mybatis/mapping/mapper/*.xml";
22
23 private static final String ENTITY_PACKAGE = "com.ieou.qmt.mapper";
24
25 @Bean(name = "sqlServerDataSource")
26 @Qualifier("sqlServerDataSource")
27 @ConfigurationProperties(prefix="spring.datasource.second")
28 public DataSource getMyDataSource(){
29 return DataSourceBuilder.create().build();
30 }
31
32 @Bean(name = "secondaryJdbcTemplate")
33 public JdbcTemplate secondaryJdbcTemplate(
34 @Qualifier("sqlServerDataSource") DataSource dataSource) {
35 return new JdbcTemplate(dataSource);
36 }
37
38 @Bean(name = "second.SqlSessionTemplate")
39 public SqlSessionFactory devSqlSessionFactory(
40 @Qualifier("sqlServerDataSource") DataSource ddataSource)
41 throws Exception {
42 final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
43 sessionFactory.setDataSource(ddataSource);
44 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
45 sessionFactory.setMapperLocations(resolver.getResources(MAPPER_PATH));
46 sessionFactory.setTypeAliasesPackage(ENTITY_PACKAGE);
47 return sessionFactory.getObject();
48 }
49
50 @Bean
51 public PlatformTransactionManager sqlServerTransactionManager(@Qualifier("sqlServerDataSource") DataSource sqlServerDataSource)
52 {
53 return new DataSourceTransactionManager(sqlServerDataSource);
54 }
55
56 }


1 package com.ieou.qmt.common;
2
3 import org.apache.ibatis.session.SqlSessionFactory;
4 import org.mybatis.spring.SqlSessionFactoryBean;
5 import org.springframework.beans.factory.annotation.Qualifier;
6 import org.springframework.boot.context.properties.ConfigurationProperties;
7 import org.springframework.boot.jdbc.DataSourceBuilder;
8 import org.springframework.context.annotation.Bean;
9 import org.springframework.context.annotation.Configuration;
10 import org.springframework.context.annotation.Primary;
11 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
12 import org.springframework.core.io.support.ResourcePatternResolver;
13 import org.springframework.jdbc.datasource.DataSourceTransactionManager;
14 import org.springframework.transaction.PlatformTransactionManager;
15
16 import javax.sql.DataSource;
17
18 @Configuration
19 public class MysqlDataSourceConfig {
20
21 private static final String MAPPER_PATH = "classpath:mybatis/mapping/*.xml";
22
23 private static final String ENTITY_PACKAGE = "com.ieou.qmt.mapper";
24
25
26 @Bean(name = "dataSource")
27 @Primary
28 @Qualifier("dataSource")
29 @ConfigurationProperties(prefix="spring.datasource.first")
30 public DataSource getMyDataSource(){
31 return DataSourceBuilder.create().build();
32 }
33
34 @Bean(name = "first.SqlSessionTemplate")
35 @Primary
36 public SqlSessionFactory devSqlSessionFactory(
37 @Qualifier("dataSource") DataSource ddataSource)
38 throws Exception {
39 final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
40 sessionFactory.setDataSource(ddataSource);
41 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
42 sessionFactory.setMapperLocations(resolver.getResources(MAPPER_PATH));
43 sessionFactory.setTypeAliasesPackage(ENTITY_PACKAGE);
44 return sessionFactory.getObject();
45 }
46
47 @Bean
48 @Primary
49 public PlatformTransactionManager mysqlTransactionManager(@Qualifier("dataSource") DataSource mysqlDataSource)
50 {
51 return new DataSourceTransactionManager(mysqlDataSource);
52 }
53 }

_____________________________________________________________________________________________________
https://www.cnblogs.com/memoryXudy/p/7767741.html
基本项目框架搭建 sqlserver druid配置
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;
}

搞定
SpringBoot +MSSQL的更多相关文章
- SpringBoot快速开发REST服务最佳实践
一.为什么选择SpringBoot Spring Boot是由Pivotal团队提供的全新框架,被很多业内资深人士认为是可能改变游戏规则的新项目.早期我们搭建一个SSH或者Spring Web应用,需 ...
- 微服务之分布式跟踪系统(springboot+pinpoint)
这篇文章介绍一下在微服务(springboot开发)的项目中使用pintpoint监控的过程及效果展示. 背景 随着项目微服务的进行,微服务数量逐渐增加,服务间的调用也越来越复杂,我们急切需要一个AP ...
- 【面试突击】-SpringBoot面试题(一)
Spring Boot 是微服务中最好的 Java 框架. 我们建议你能够成为一名 Spring Boot 的专家. 问题一 Spring Boot.Spring MVC 和 Spring 有什么区别 ...
- 解决 Springboot Unable to build Hibernate SessionFactory @Column命名不起作用
问题: Springboot启动报错: Caused by: org.springframework.beans.factory.BeanCreationException: Error creati ...
- 【微框架】Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo
Maven+springboot+阿里大于短信验证服务 纠结点:Maven库没有sdk,需要解决 Maven打包找不到相关类,需要解决 ps:最近好久没有写点东西了,项目太紧,今天来一篇 一.本文简介 ...
- [干货来袭]MSSQL Server on Linux预览版安装教程(先帮大家踩坑)
前言 昨天晚上微软爸爸开了全国开发者大会,会上的内容,我就不多说了,园子里面很多.. 我们唐总裁在今年曾今透漏过SQL Server love Linux,果不其然,这次开发者大会上就推出了MSSQL ...
- Springboot搭建web项目
最近因为项目需要接触了springboot,然后被其快速零配置的特点惊呆了.关于springboot相关的介绍我就不赘述了,大家自行百度google. 一.pom配置 首先,建立一个maven项目,修 ...
- 分享MSSQL、MySql、Oracle的大数据批量导入方法及编程手法细节
1:MSSQL SQL语法篇: BULK INSERT [ database_name . [ schema_name ] . | schema_name . ] [ table_name | vie ...
- Java——搭建自己的RESTful API服务器(SpringBoot、Groovy)
这又是一篇JavaWeb相关的博客,内容涉及: SpringBoot:微框架,提供快速构建服务的功能 SpringMVC:Struts的替代者 MyBatis:数据库操作库 Groovy:能与Java ...
随机推荐
- ImageView.ScaleType
前言 对ImageView.ScaleType,学习安卓需掌握.以官方链接:http://android.xsoftlab.net/reference/android/widget/ImageView ...
- C博客作业00——我的第一篇博客
1.你对网络专业或计算机专业了解是怎样? 初看字眼,便觉得是理工性很强的专业,所以需要较强的开拓思维,创新精神,探索未知事物的勇气,才能掌握并且熟练应用相关知识.计算机类专业都需要学习计算机语言,而计 ...
- Git删除某个文件夹或整个仓库
删除仓库: 进入仓库,选择settting: 拉到最下面,有个Danger Zone,里面有删除仓库选项: 输入仓库名称,即可删除: 删除某个文件:删除文件和文件夹只能用命令行删除. 如果直接git ...
- 解决Shell脚本$'\r': command not found问题
造成这个问题的原因是Windows下的空行,我们只需要把文件转成unix就好 Centos下,执行yum install dos2unix,然后dos2unix [file],再执行shell命令就好 ...
- 解决vs2010按ctrl+f5,调试窗口一闪而过的方法
vs2010调试按F5与按Ctrl+F5有什么区别 Ctrl F5测试运行后不自动推出控制台,直接按F5会自动退出去 解决vs2010按ctrl+f5,调试窗口一闪而过的方法 http://hi.ba ...
- oracle 19c jdbc之Reactive Streams Ingestion (RSI) Library
19c jdbc新特性 https://blogs.oracle.com/dev2dev/whats-new-in-193-and-183-jdbc-and-ucp jdbc实现直接路径加载 http ...
- windows server2012 R2安装python3.x版本报错0x80240017
windows server2012 R2安装python3.x版本报错0x80240017 环境: windows server 2012 R2系统 问题: 安装python3.5版本时候出现错误0 ...
- String.format()详细用法
String.format()字符串常规类型格式化的两种重载方式 format(String format, Object… args) 新字符串使用本地语言环境,制定字符串格式和参数生成格式化的新字 ...
- Node.js 实现第一个应用以及HTTP模块和URL模块应用
/* 实现一个应用,同时还实现了整个 HTTP 服务器. * */ //1.引入http模块 var http=require('http'); //2.用http模块创建服务 /* req获取url ...
- 系统重装之认识UEFI
UEFI是一种新型的引导方式?他与传统的BIOS引导不同,传统BIOS引导需要经过(开机→BIOS初始化→BIOS自检→引导系统→进入系统)五个步骤来完成引导操作,UEFI只需要(开机→UEFI初始化 ...