栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

以下用读写模式.


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;


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 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  , 综合应用 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/349012.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号