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.