Bee使用postgresql数据库插入记录后返回ID值

205 篇文章 0 订阅
121 篇文章 1 订阅

 Bee使用postgresql数据库插入记录后返回ID值

Bee支持生成分布式全局唯一ID,是Long型数据;1.17开始支持Integer,String,UUID的ID值。

此文演示返回由DB生成的ID;

postgresql旧版驱动不支持返回插入记录的ID(数据库生成); 此时,可使用Bee返回由Bee生成的id.

使用以下驱动依赖,可以返回记录ID(DB生成)。

<!--jdk8以上 <dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.3.3</version>
</dependency> -->
		
		<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.2.24.jre7</version>
</dependency>

效果如下:

Java代码:

主要语句:

Long r_id=suidRich.insertAndReturnId(orders1,IncludeType.INCLUDE_EMPTY);//不包含null的字段

package org.teasoft.exam.bee.osql;

import java.math.BigDecimal;

import org.teasoft.bee.osql.BeeException;
import org.teasoft.bee.osql.IncludeType;
import org.teasoft.bee.osql.SuidRich;
import org.teasoft.exam.bee.osql.entity.Orders;
import org.teasoft.honey.osql.core.BeeFactory;
import org.teasoft.honey.osql.core.HoneyConfig;
import org.teasoft.honey.osql.core.Logger;

/**
 * 测试postgresql返回自增长的id.
 * @author Kingstar
 * @since  1.9
 */
public class InsertAndReturnIdTest2 {
	private static SuidRich suidRich = BeeFactory.getHoneyFactory().getSuidRich();

	public static void main(String[] args) {
		test();
	}
	
	public static void test() {
		boolean oldFlag=HoneyConfig.getHoneyConfig().genid_forAllTableLongId;
		
		HoneyConfig.getHoneyConfig().genid_forAllTableLongId=false; //测试由DB生成ID
		test0();
		HoneyConfig.getHoneyConfig().genid_forAllTableLongId=oldFlag;
	}

	public static void test0() {
		try {
			Orders orders1 = new Orders();
			orders1.setName("Bee(ORM Framework)");
			orders1.setRemark("InsertAndReturnId");
			orders1.setUserid("Bee");
			orders1.setTotal(new BigDecimal("80.80"));
			Logger.info("------------------------getId : " + orders1.getId());
			
//		    Long r_id=suidRich.insertAndReturnId(orders1,IncludeType.EXCLUDE_BOTH); //不包含null和空字符.可以
//		    Long r_id=suidRich.insertAndReturnId(orders1); //id是null,postgresql不支持
		    Long r_id=suidRich.insertAndReturnId(orders1,IncludeType.INCLUDE_EMPTY);//不包含null的字段
			
			Logger.info("-------------------返回的id----- : " + r_id);
			
			//要使用新的依赖
//		<dependency>
//		    <groupId>org.postgresql</groupId>
//		    <artifactId>postgresql</artifactId>
//		    <version>42.2.24.jre7</version>
//		</dependency>

		} catch (BeeException e) {
			Logger.error("In InsertAndReturnIdTest (BeeException):" + e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			Logger.error("In InsertAndReturnIdTest (Exception):" + e.getMessage());
			e.printStackTrace();
		}
	}
	
}

Bee生成ID配置相关说明

#为所有实体Javabean生成id. 通过配置,不用在Javabean中添加侵入式代码
bee.distribution.genid.forAllTableLongId=true
#当实体有值时,可以不覆盖,这样方便使用自定义的值。
bee.distribution.genid.replaceOldId=false

#when forAllTableLongId=true, it woulb be disable.    指定部分要生成ID的实体类, 当配置了forAllTableLongId=true时,此项被忽略。
#bee.distribution.genid.includesEntityList=com.xxx.aa.Orders,com.xxx.bb.*,com.xxx.cc.**
#bee.distribution.genid.includesEntityList=org.teasoft.exam.bee.osql.entity.*,org.teasoft.exam.bee.osql.entity.sqlite.*
#排除部分不需要生成ID的类
#bee.distribution.genid.excludesEntityList=com.xxx.aa.User,com.xxx.bb.*

V1.17 还可以使用GenId注解,声明某个实体类要生成id.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值