(二十二)ORM框架Bee,Spring配置多数据源实例

200 篇文章 0 订阅

(二十二)ORM框架Bee,Spring配置多数据源实例

applicationContext.xml

<?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>

bee.properties

#--------Notice: do not support define sql in this model.
#DOSQL:  Distributed Object SQL
bee.dosql.multiDS.enable=true

# 1:only r/w, one master and more slave;  2:only more database (table name is same)
bee.dosql.multiDS.type=1
bee.dosql.multiDS.defalutDS=ds1
#when type is 1
#write DB just set one db.
bee.dosql.multiDS.writeDB=ds1
#bee.dosql.multiDS.readDB=ds2,ds3
bee.dosql.multiDS.readDB=ds2
#poll:1, rand:2
#bee.dosql.multiDS.rDbRouteWay=1

Rest 后端Java代码(SpringMVC):

/*
 * Copyright 2016-2019 the original author.All rights reserved.
 * Kingstar(aiteasoft@163.com)
 * The license,see the LICENSE file.
 */

package com.automvc.enet.order.rest;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.teasoft.bee.osql.BeeSQLException;
import org.teasoft.bee.osql.service.ObjSQLRichService;
import org.teasoft.bee.osql.service.ObjSQLService;

import com.automvc.common.jquery.Result;
import com.automvc.enet.order.entity.Orderhistory;

/**
 * @author AiTeaSoft.com
 * @since  1.0
 * Create on 2019-04-16 11:48:24
 */
@RestController
@RequestMapping("/orderhistory")
public class OrderhistoryRest {
	@Autowired
	ObjSQLService objSQLService;
	
	@Autowired
	ObjSQLRichService objSQLRichService;
	
	@RequestMapping("/list")
	public Result list(Orderhistory orderhistory,
	     @RequestParam(value = "page", defaultValue = "1", required = false) int page, 
		 @RequestParam(value = "rows", defaultValue = "20", required = false) int rows) {	
	  Result  result =new Result();
	  try{
		  int total=objSQLRichService.count(orderhistory);
		  List<Orderhistory> list=objSQLRichService.select(orderhistory, (page-1)*rows, rows);
		  result.setRows(list);
		  result.setTotal(total);
	  } catch (BeeSQLException e) {
	      System.err.println(e.getMessage());
		  result.setErrorMsg(e.getMessage());
	  }
		
	   return result;
	}
	
	@RequestMapping("/add")
	public Result insert(Orderhistory orderhistory){
		
	  Result  result =new Result();
	  try{
		  int num=objSQLService.insert(orderhistory);
		  result.setTotal(num);
		  if(num<=0) result.setErrorMsg("insert failed!");
	  } catch (BeeSQLException e) {
		  result.setErrorMsg(e.getMessage());
	  }
		return result;
	}
	
	@RequestMapping("/edit")
	public Result update(Orderhistory orderhistory){
		Result  result =new Result();
		try{
			int num=objSQLService.update(orderhistory);
			result.setTotal(num);
			if(num<=0) result.setErrorMsg("update failed!");
	    } catch (BeeSQLException e) {
		    result.setErrorMsg(e.getMessage());
	    }
		return result;
	}
	
	@RequestMapping("/del")
	public Result delete(String ids) {
		Result result = new Result();
		try {
			int num=objSQLRichService.deleteById(Orderhistory.class, ids);
			result.setTotal(num);
			if (num <= 0) result.setErrorMsg("delete failed!");
		} catch (BeeSQLException e) {
			result.setErrorMsg(e.getMessage());
		}
		return result;
	}
}

参考: bee-spring-springmvc: 更快的开发Java Web的新组合, 让Java的开发速度超过php和Rails!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值