(二十一)ORM框架Bee,多数据源Java编码设置实例

209 篇文章 0 订阅
本文档提供了一个使用 ORM 框架 Bee 进行多数据源操作的 Java 编码实例。展示了如何初始化 Druid 数据源、设置读写分离配置,并执行更新、查询等操作。同时,代码中包含了事务管理的示例,适用于需要高可用和读写分离的数据库应用场景。
摘要由CSDN通过智能技术生成

(二十一)ORM框架Bee,多数据源Java编码设置实例

以下用读写模式.

/*
 * Copyright 2016-2020 the original author.All rights reserved.
 * Kingstar(honeysoft@126.com)
 * The license,see the LICENSE file.
 */

package org.teasoft.exam.bee.osql.ds;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.teasoft.bee.osql.Condition;
import org.teasoft.bee.osql.DatabaseConst;
import org.teasoft.bee.osql.SuidRich;
import org.teasoft.bee.osql.transaction.Transaction;
import org.teasoft.exam.bee.osql.entity.LeafAlloc;
import org.teasoft.honey.osql.core.BeeFactory;
import org.teasoft.honey.osql.core.ConditionImpl;
import org.teasoft.honey.osql.core.HoneyConfig;
import org.teasoft.honey.osql.core.HoneyContext;
import org.teasoft.honey.osql.core.Logger;
import org.teasoft.honey.osql.core.SessionFactory;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * @author Kingstar
 * @since  1.8
 */
public class RwDsExam {

	private static SuidRich suidRich = BeeFactory.getHoneyFactory().getSuidRich();
//	private static String oldDbName="";
//	static {
//
//	}

	private static boolean isMysql() {
		return DatabaseConst.MYSQL.equalsIgnoreCase(HoneyContext.getDbDialect());
	}

	public static void test() {
	    String  oldDbName=HoneyConfig.getHoneyConfig().getDbName();
		HoneyConfig.getHoneyConfig().setDbName(DatabaseConst.MYSQL);
		if (isMysql()) initDS();
		
		if (isMysql()) {
			HoneyConfig.getHoneyConfig().multiDS_enable = true;
			HoneyConfig.getHoneyConfig().multiDS_type = 1;
			HoneyConfig.getHoneyConfig().multiDS_defalutDS = "ds1";
			HoneyConfig.getHoneyConfig().multiDS_writeDB = "ds1";
			HoneyConfig.getHoneyConfig().multiDS_readDB = "ds2,ds3";
			HoneyContext.setConfigRefresh(true);

			test1();
			test2();
			
			HoneyConfig.getHoneyConfig().multiDS_enable = false;
			HoneyConfig.getHoneyConfig().multiDS_type = 0;
			HoneyConfig.getHoneyConfig().multiDS_defalutDS = null;
			HoneyConfig.getHoneyConfig().multiDS_writeDB = null;
			HoneyConfig.getHoneyConfig().multiDS_readDB = null;
			BeeFactory.getInstance().setDataSourceMap(null);
			HoneyContext.setConfigRefresh(true);
		}
		
		HoneyConfig.getHoneyConfig().setDbName(oldDbName);
		
	}

	public static void main(String[] args) {
		test();
	}

	public static void initDS() {
		try {

			DruidDataSource dataSource1;
			dataSource1 = new DruidDataSource();
			dataSource1.setUrl("jdbc:mysql://localhost:3306/pro?characterEncoding=UTF-8");
			dataSource1.setUsername("root");
			dataSource1.setPassword("");
			dataSource1.init();

			DruidDataSource dataSource2;
			dataSource2 = new DruidDataSource();
			dataSource2.setUrl("jdbc:mysql://localhost:3306/bee?characterEncoding=UTF-8");
			dataSource2.setUsername("root");
			dataSource2.setPassword("");
			dataSource2.init();

			DruidDataSource dataSource3;
			dataSource3 = new DruidDataSource();
			dataSource3.setUrl("jdbc:mysql://localhost:3306/bee?characterEncoding=UTF-8");
			dataSource3.setUsername("root");
			dataSource3.setPassword("");
			dataSource3.init();

			Map<String, DataSource> dataSourceMap = new HashMap<>();
			dataSourceMap.put("ds1", dataSource1);
			dataSourceMap.put("ds2", dataSource2); //ds2
			dataSourceMap.put("ds3", dataSource3); //ds3
			BeeFactory.getInstance().setDataSourceMap(dataSourceMap);
			HoneyContext.setConfigRefresh(true);

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void test1() {

		LeafAlloc leafAlloc = new LeafAlloc();
		leafAlloc.setBizTag("bee");
		Condition condition = new ConditionImpl();
		condition.setAdd("maxId", "step");
		int num = suidRich.update(leafAlloc, "maxId", condition);
		Logger.info("---------------------------------update num is :" + num);

		//"SELECT biz_tag, max_id, step FROM leaf_alloc WHERE biz_tag = #{tag}"
		LeafAlloc result = suidRich.selectOne(leafAlloc);
		if (result != null) Logger.info(result.toString());

		result = suidRich.selectOne(leafAlloc);
		if (result != null) Logger.info(result.toString());
		
		suidRich.select(leafAlloc,0,10);
	}

	public static void test2() {
		LeafAlloc result = null;
		Transaction transaction = SessionFactory.getTransaction();
		try {
			transaction.begin();

//			"UPDATE leaf_alloc SET max_id = max_id + step WHERE biz_tag = #{tag}"
			LeafAlloc leafAlloc = new LeafAlloc();
			leafAlloc.setBizTag("bee");
			Condition condition = new ConditionImpl();
			condition.setAdd("maxId", "step");
//		    suidRich.update(leafAlloc, "maxId", condition);
			suidRich.update(leafAlloc, condition); //v1.8

//		    "SELECT biz_tag, max_id, step FROM leaf_alloc WHERE biz_tag = #{tag}"
			result = suidRich.selectOne(leafAlloc);
			if(result!=null) Logger.info(result.toString());
			
			suidRich.select(leafAlloc,0,10);

			transaction.commit();
		} catch (Exception e) {
			transaction.rollback();
		}
	}

}

参考:src/main/java/org/teasoft/exam/bee/osql/ds/RwDsExam.java · automvc/bee-exam - Gitee.com

ORM Bee更多资料:

1.快速开始

2. FAQ 常见问题   

3. 综合应用 (与SpringMVC, Spring boot结合实例, 连接池等)

到源码官网

bee: 互联网新时代的Java ORM工具,简单、高效,开发速度快!

GitHub - automvc/bee: Bee is an AI, easy and high efficiency ORM framework.

分别Ctrl+F 搜索:  快速开始,  FAQ_CN.md  , 综合应用 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值