____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>
二.在application.properties中配置
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
三.配置sqlServer数据源
 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 }

总结:

配置到这里就可以使用JdbcTemplate来操作sqlServer了,(mysql是spring boot的自动化配置,sqlServer是我们手动配置的)只要在类中注入即可
例如:(JdbcTemplate 的用法自行百度)
public class IEOUMallServiceImpl implements IEOUMallService{
@Autowired
@Qualifier("secondaryJdbcTemplate")
protected JdbcTemplate jdbcTemplate2;
}
四.以上配置完成后在执行带有@Transactional 事务的接口时会发现报错:
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
所以:我们必须手动分别配置mysql与sqlServer
1.配置文件:application.properties 需要改为如下:
 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
2.原来的SqlServerDataSourceConfig改为如下:
 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 }
3.新建mysql配置文件如下:
 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 }
4.从事务管理器中选择一个事务,在方法上加@Transactional(value = "mysqlTransactionManager",rollbackFor = Exception.class)
 
至此:配置完毕,如有不足或不对的地方,请补充。
hello world!!!
 
分类: spring boot

_____________________________________________________________________________________________________

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的更多相关文章

  1. SpringBoot快速开发REST服务最佳实践

    一.为什么选择SpringBoot Spring Boot是由Pivotal团队提供的全新框架,被很多业内资深人士认为是可能改变游戏规则的新项目.早期我们搭建一个SSH或者Spring Web应用,需 ...

  2. 微服务之分布式跟踪系统(springboot+pinpoint)

    这篇文章介绍一下在微服务(springboot开发)的项目中使用pintpoint监控的过程及效果展示. 背景 随着项目微服务的进行,微服务数量逐渐增加,服务间的调用也越来越复杂,我们急切需要一个AP ...

  3. 【面试突击】-SpringBoot面试题(一)

    Spring Boot 是微服务中最好的 Java 框架. 我们建议你能够成为一名 Spring Boot 的专家. 问题一 Spring Boot.Spring MVC 和 Spring 有什么区别 ...

  4. 解决 Springboot Unable to build Hibernate SessionFactory @Column命名不起作用

    问题: Springboot启动报错: Caused by: org.springframework.beans.factory.BeanCreationException: Error creati ...

  5. 【微框架】Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo

    Maven+springboot+阿里大于短信验证服务 纠结点:Maven库没有sdk,需要解决 Maven打包找不到相关类,需要解决 ps:最近好久没有写点东西了,项目太紧,今天来一篇 一.本文简介 ...

  6. [干货来袭]MSSQL Server on Linux预览版安装教程(先帮大家踩坑)

    前言 昨天晚上微软爸爸开了全国开发者大会,会上的内容,我就不多说了,园子里面很多.. 我们唐总裁在今年曾今透漏过SQL Server love Linux,果不其然,这次开发者大会上就推出了MSSQL ...

  7. Springboot搭建web项目

    最近因为项目需要接触了springboot,然后被其快速零配置的特点惊呆了.关于springboot相关的介绍我就不赘述了,大家自行百度google. 一.pom配置 首先,建立一个maven项目,修 ...

  8. 分享MSSQL、MySql、Oracle的大数据批量导入方法及编程手法细节

    1:MSSQL SQL语法篇: BULK INSERT [ database_name . [ schema_name ] . | schema_name . ] [ table_name | vie ...

  9. Java——搭建自己的RESTful API服务器(SpringBoot、Groovy)

    这又是一篇JavaWeb相关的博客,内容涉及: SpringBoot:微框架,提供快速构建服务的功能 SpringMVC:Struts的替代者 MyBatis:数据库操作库 Groovy:能与Java ...

随机推荐

  1. 关于移动虚拟机后,linux网卡启动异常问题解决

    废话不多说,直接上解决办法. 首先执行命令:ifconfig -a 会发现原来是eth0, 而现在变成了eth1了 然后我们编辑规则配置信息: vim /etc/udev/rule.d/70-pers ...

  2. [HAOI2018]染色(NTT)

    前置芝士 可重集排列 NTT 前置定义 \[\begin{aligned}\\ f_i=C_m^i\cdot \frac{n!}{(S!)^i(n-iS)!}\cdot (m-i)^{n-iS}\\ ...

  3. ICEM-双管

    原视频下载地址:http://yunpan.cn/cLHCm7Uejw4eG  访问密码 b8a1

  4. 【解决方案】Chrome崩溃问题解决

    问题描述 出现异常之前做的操作就是,因为换工位的需要,所以关闭电脑,修改网络配置. 问题分析 Firefox和其他应用网络正常 Chrome设置.帮助等选项均打不开 分析,很可能是电脑重启后,Wind ...

  5. 【大数据应用技术】作业十|分布式文件系统HDFS 练习

    本次作业的要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3292 1.目录操作 在HDFS中为hadoop用户创建一个用户目 ...

  6. js十大排序算法收藏

    十大经典算法排序总结对比 转载自五分钟学算法&https://www.cnblogs.com/AlbertP/p/10847627.html 一张图概括: 主流排序算法概览 名词解释: n: ...

  7. 实验与作业(Python)-02 Python函数入门与温度转换程序(函数、input、eval、int、float、列表)

    截止日期 实验目标 学会定义函数,使用函数.学会导入在某个文件中定义的函数. input获得值,然后通过eval或者int.float将其转换为相应的类型. 学会使用列表:访问列表.append.遍历 ...

  8. linux升级openssl和php_openssl模块

    一.OpenSSL源码升级 2014年4月8日,XP宣布正式停止服务的日子,也是OpenSSL爆出大漏洞的日子. OpenSSL主要是负责在一些敏感的数据提交上面被广泛使用,不乏大家经常访问的一些网站 ...

  9. [转]白话HTTP短连接中的Session和Token

    我经常想象并怀念三十年前那原始而美好的互联网旧时光, 工作很轻松, 生活很悠闲. 上班的时候偶尔有些HTTP的请求发到我这里, 我简单的看一下, 取出相对应的html文档,图片,发回去就可以了, 然后 ...

  10. Node.js 实现第一个应用以及HTTP模块和URL模块应用

    /* 实现一个应用,同时还实现了整个 HTTP 服务器. * */ //1.引入http模块 var http=require('http'); //2.用http模块创建服务 /* req获取url ...