码迷,mamicode.com
首页 > 数据库 > 详细

springboot+mybatis 多数据库支持

时间:2019-12-30 14:13:05      阅读:549      评论:0      收藏:0      [点我收藏+]

标签:数据   database   VID   info   server   gre   return   oracl   多数据库   

1.配置application.yml

mybatis:
mapper-locations: classpath:/mybatis/mapping/*.xml
configuration:
map-underscore-to-camel-case: true #开启驼峰命名
database-id: ${database.type}
type-aliases-package:
com.mtwl.entity

注意database-id 的配置
2.在springboot中配置DatabaseIdProvider
@Bean
public DatabaseIdProvider getDatabaseIdProvider(){
DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
Properties properties = new Properties();
properties.setProperty("Oracle","oracle");
properties.setProperty("MySQL","mysql");
properties.setProperty("DB2","db2");
properties.setProperty("Derby","derby");
properties.setProperty("H2","h2");
properties.setProperty("HSQL","hsql");
properties.setProperty("Informix","informix");
properties.setProperty("Microsoft SQL Server","sqlserver");
properties.setProperty("PostgreSQL","postgresql");
properties.setProperty("Sybase","sybase");
properties.setProperty("Hana","hana");
databaseIdProvider.setProperties(properties);
return databaseIdProvider;
}
注意:数据库版本是否正确
3.在mapper.xml中使用
方法1
<select id="selectTop" resultType="java.util.Map" parameterType="java.lang.Integer" databaseId="sqlserver">
select top(#{value})*
from kcjs
</select>

<select id="selectTop" resultType="java.util.Map" parameterType="java.lang.Integer" databaseId="mysql">
select *
from kcjs limit 0,#{value}
</select>



方法2
<select id="selectTop" resultType="java.util.Map" parameterType="java.lang.Integer">
select
<if test="_databaseId==sqlserver">
top(#{value})*
from kcjs
</if>
<if test="_databaseId==mysql">
*
from kcjs limit 0,#{value}
</if>

</select>
 

springboot+mybatis 多数据库支持

标签:数据   database   VID   info   server   gre   return   oracl   多数据库   

原文地址:https://www.cnblogs.com/binkai/p/12119283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!