mssql连接mysql update_用SQLSERVER链接服务器实现MSSQL和MYSQL数据交互

-- 创建链接服务器

EXEC sp_addlinkedserver @server ='MySQLTest', @srvproduct='MySQL',@provider ='MSDASQL', @datasrc ='cpc'

GO

-- 创建或更新 SQL Server 本地实例上的登录名与远程服务器中安全帐户之间的映射。

-- 好像没什么用,不做这一步也能进行数据交互

EXEC sp_addlinkedsrvlogin @rmtsrvname='MySQLTest',@useself='false',@locallogin='sa',@rmtuser='cpc',@rmtpassword='123'

go

-- 把SQL SERVER里的TT表的记录插入到MYSQL的T1表里,这这里也可以插入具体值。

INSERT OPENQUERY(MySQLTest,'select * from t1') select * from TT;

INSERT OPENQUERY(MySQLTest,'select * from t1') values (99,99,99);

-- 对T1表进行UPDATE操作

UPDATE OPENQUERY(MySQLTest,'select value from t1') set value=value +100;

-- 对T1表进行DELETE 操作,不明白为什么一次只能删除一条,

DELETE OPENQUERY (MySQLTest,

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以在Spring Boot项目中同时连接MySQLSQL Server数据库,具体步骤如下: 1. 在pom.xml文件中添加MySQLSQL Server数据库的驱动依赖,例如: ```xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>${sqlserver.version}</version> </dependency> ``` 2. 在application.yml文件中配置两个数据源,例如: ```yaml spring: datasource: primary: url: jdbc:mysql://localhost:3306/db_mysql driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 secondary: url: jdbc:sqlserver://localhost:1433;databaseName=db_sqlserver driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver username: sa password: 123456 ``` 3. 在Spring Boot项目中配置两个数据源,例如: ```java @Configuration public class DataSourceConfig { @Bean(name = "primaryDataSource") @Qualifier("primaryDataSource") @ConfigurationProperties(prefix = "spring.datasource.primary") public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "secondaryDataSource") @Qualifier("secondaryDataSource") @ConfigurationProperties(prefix = "spring.datasource.secondary") public DataSource secondaryDataSource() { return DataSourceBuilder.create().build(); } } ``` 4. 在MyBatis中配置两个数据源,并指定使用哪个数据源,例如: ```java @Configuration @MapperScan(basePackages = "com.example.mapper", sqlSessionTemplateRef = "primarySqlSessionTemplate") public class MybatisConfig { @Bean(name = "primarySqlSessionFactory") @Primary public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); return bean.getObject(); } @Bean(name = "secondarySqlSessionFactory") public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); return bean.getObject(); } @Bean(name = "primarySqlSessionTemplate") @Primary public SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } @Bean(name = "secondarySqlSessionTemplate") public SqlSessionTemplate secondarySqlSessionTemplate(@Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } @Bean @Primary public PlatformTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean public PlatformTransactionManager secondaryTransactionManager(@Qualifier("secondaryDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } } ``` 在定义Mapper时,可以通过@Qualifier注解指定使用哪个数据源的SqlSessionTemplate,例如: ```java public interface UserMapper { @Select("SELECT * FROM user") @Results({ @Result(property = "id", column = "id"), @Result(property = "name", column = "name"), @Result(property = "age", column = "age") }) List<User> getAllUsers(); @Select("SELECT * FROM user WHERE id = #{id}") @Results({ @Result(property = "id", column = "id"), @Result(property = "name", column = "name"), @Result(property = "age", column = "age") }) User getUserById(@Param("id") Long id); } ``` 在Service层中,可以通过@Autowired注解注入UserMapper,并指定使用哪个数据源的SqlSessionTemplate,例如: ```java @Service public class UserService { @Autowired @Qualifier("primarySqlSessionTemplate") private SqlSessionTemplate primarySqlSessionTemplate; @Autowired @Qualifier("secondarySqlSessionTemplate") private SqlSessionTemplate secondarySqlSessionTemplate; public List<User> getAllUsersFromPrimaryDataSource() { UserMapper userMapper = primarySqlSessionTemplate.getMapper(UserMapper.class); return userMapper.getAllUsers(); } public User getUserByIdFromSecondaryDataSource(Long id) { UserMapper userMapper = secondarySqlSessionTemplate.getMapper(UserMapper.class); return userMapper.getUserById(id); } } ``` 这样就可以在Spring Boot项目中同时连接MySQLSQL Server数据库了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值