赞
踩
1. Java 配置方式
- public class InitSameDsUtil {
-
- public static void initDS() {
- try {
-
- DruidDataSource dataSource0;
- dataSource0 = new DruidDataSource();
- dataSource0.setUrl("jdbc:mysql://localhost:3306/bee?characterEncoding=UTF-8&useSSL=false");
- dataSource0.setUsername("root");
- dataSource0.setPassword("123456");
- dataSource0.init();
-
- DruidDataSource dataSource1;
- dataSource1 = new DruidDataSource();
- dataSource1.setUrl("jdbc:mysql://localhost:3306/pro?characterEncoding=UTF-8&useSSL=false");
- dataSource1.setUsername("root");
- dataSource1.setPassword("123456");
- dataSource1.init();
-
- // 以上数据源,也可以通过XML方式配置
- // (二十二)ORM框架Bee,Spring配置多数据源实例
- // https://blog.csdn.net/abckingaa/article/details/120956130
- Map<String, DataSource> dataSourceMap = new HashMap<>();
- dataSourceMap.put("ds0", dataSource0);
- dataSourceMap.put("ds1", dataSource1);
- // BeeFactory.getInstance().setDataSourceMap(dataSourceMap);
- HoneyContext.setDataSourceMap(dataSourceMap);//V2.1
-
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
-
- }

2. Spring xml配置方式
原文: https://blog.csdn.net/abckingaa/article/details/120956130
- <?xml version="1.0" encoding="UTF-8" ?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
-
- <!-- ds1 -->
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
- destroy-method="close">
- <property name="driverClass" value="com.mysql.jdbc.Driver" />
- <property name="jdbcUrl"
- value="jdbc:mysql://localhost:3306/teasoft?characterEncoding=UTF-8" />
- <property name="user" value="root" />
- <property name="password" value="" />
- <property name="initialPoolSize" value="10" />
- <property name="minPoolSize" value="10" />
- <property name="maxPoolSize" value="20" />
- <property name="maxIdleTime" value="1800" />
- </bean>
-
- <bean id="ds2" class="com.mchange.v2.c3p0.ComboPooledDataSource"
- destroy-method="close">
- <property name="driverClass" value="com.mysql.jdbc.Driver" />
- <property name="jdbcUrl"
- value="jdbc:mysql://localhost:3306/bee?characterEncoding=UTF-8" />
- <property name="user" value="root" />
- <property name="password" value="" />
- <property name="initialPoolSize" value="10" />
- <property name="minPoolSize" value="10" />
- <property name="maxPoolSize" value="20" />
- <property name="maxIdleTime" value="1800" />
- </bean>
-
- <!-- <bean id="beeFactory" class="org.teasoft.honey.osql.core.BeeFactory">
- <property name="dataSource" ref="dataSource"></property>
- </bean> -->
-
- <bean id="beeFactory" class="org.teasoft.honey.osql.core.BeeFactory">
- <property name="dataSourceMap">
- <map>
- <entry key="ds1" value-ref="dataSource" />
- <entry key="ds2" value-ref="ds2" />
- </map>
- </property>
- </bean>
-
- <bean id="sessionFactory" class="org.teasoft.honey.osql.core.SessionFactory">
- <property name="beeFactory" ref="beeFactory"></property>
- </bean>
-
- <import resource="beeContext.xml"></import>
-
- </beans>

3. 配置文件方式
放在bee.properties, application.properties,application.yml三种文件都可以.
以下是bee.properties, application.properties的风格写法,
application.yml 可以使用其相应的风格.
#druid #dbs数组,其它下标,是否从首个元素继承属性(但dsName不能继承) bee.db.extendFirst=true bee.db.dbs[0].dsName=ds0 bee.db.dbs[0].type=Druid bee.db.dbs[0].driverName = com.mysql.jdbc.Driver bee.db.dbs[0].url =jdbc:mysql://localhost:3306/bee?characterEncoding=UTF-8&useSSL=false bee.db.dbs[0].username = root bee.db.dbs[0].password =123456 bee.db.dbs[0].initialSize=10 bee.db.dbs[0].minIdle=10 bee.db.dbs[0].maxActive=50 bee.db.dbs[0].maxWait=60000 bee.db.dbs[0].timeBetweenEvictionRunsMillis=60000 bee.db.dbs[1].dsName=ds1 bee.db.dbs[1].type=Druid bee.db.dbs[1].driverName = com.mysql.jdbc.Driver bee.db.dbs[1].url =jdbc:mysql://localhost:3306/pro?characterEncoding=UTF-8&useSSL=false bee.db.dbs[1].username = root bee.db.dbs[1].password =123456
使用多数据源, 可以使用分库分表, 也可以只使用分库或只使用分表.
还有可能就是为了从不同的数据库中获取数据,这些数据库还有可能同时有MySQL, Oracle, Mongodb等,折腾的是,还可能有MS Access这种数据库.
没事, Java ORM Bee都支持, 它还是个低代码的框架, 可以让你不用再写Dao代码.
setDataSourceName("ds0"); //就可以指定数据源
还可以通过包名, 表名规则路由到指定数据源.
Sharding分片的话, 只需要一句话,就可以完成分片. 有以下两种写法,与表名关系或与实现类关联.
数据源有ds0,ds1; test_user总共有6个表, ds0是test_user0,test_user1,test_user2,另外三个在ds1.分片键是id, 默认是根据id的值求余,余数的下标即可表的下标.
- ShardingConfig.addShardingBean("test_user",new ShardingBean("ds[0..1].test_user[0..5]", "id"));
- ShardingConfig.addShardingBean(TestMyUser.class,new ShardingBean("ds[0..1].test_my_user[0..5]", "id"));
Java ORM Bee 同时支持JDBC(比如JavaWeb),Android和Harmony;支持Sharding分片;支持多种关系型数据库(MySQL,MariaDB,Oracle,H2,SQLite,PostgreSQL,SQL Server,Access等),还支持NoSQL的Cassandra,Mongodb等
源码下载:
https://github.com/automvc/bee
https://gitee.com/automvc/bee-springboot
https://blog.csdn.net/abckingaa/article/details/121664398
ORM Bee资料大全(入门实例, 多数据源,整合Spring boot等)_abckingaa的博客-CSDN博客_orm bee
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。