Protobuf4j

Introduction

A Facility framework to develop with Google Protobuf, including:

Modules

ORM Usage

Gradle

implementation("xyz.codemeans.protobuf4j:protobuf4j-orm-starter:$protobuf4jVersion")

The latest version can be found in maven central repository.

Properties

protobuf4j.datasource.auto-enable=<if-auto-configuration-enalbed:default true>

Java

Example Code: example

interface

public interface ExampleDao extends IPrimaryKeyMessageDao<Long, Example> {
}

impl

@Repository
public class ExampleDaoImpl extends PrimaryKeyProtoMessageDao<Long, Example> implements ExampleDao {
  public ExampleDaoImpl() {
    super(Example.class, ExampleNaming.ID);
  }
}

CRUD

@Autowired
ExampleDao exampleDao;

public Example createExample(Example example) {
  long id = exampleDao.insertReturnKey(example).longValue();
  return exampleDao.selectOneByPrimaryKey(id);
}

public Example getExample(long id) {
  return exampleDao.selectOneByPrimaryKey(id);
  // or
  //return exampleDao.selectOneByCond(FieldAndValue.eq(ExampleNaming.ID, id));
}

public Example updateExample(Example newValue, Example oldValue) {
  exampleDao.updateMessageByPrimaryKey(newValue, oldValue);
  return exampleDao.selectOneByPrimaryKey(newValue.getId());
}

public void deleteExample(long id) {
  exampleDao.deleteByPrimaryKey(id);
}

// complex conditions and pagination
public List<Example> listExamples(WhereClause where) {
  return exampleDao.selectByWhere(where);
}

public Map<Long, Example> getExamples(Collection<Long> ids) {
  return exampleDao.selectMultiByPrimaryKey(ids);
}

SQL Usage

// condition
List<IExpression> conds = Lists.newArrayList();
conds.add(FieldAndValue.like(ExampleNaming.NAME, SqlUtil.likePrefix(prefix)));
conds.add(FieldAndValue.gte(ExampleNaming.ID, beg));
conds.add(FieldAndValue.lt(ExampleNaming.ID, end));
IExpression allAndCond = LogicalExpr.and(conds));

// pagination
PaginationClause.newBuilder(limit).buildByOffset(offset);
PaginationClause.newBuilder(limit).buildByPageNo(pn)

// select specified columns
SelectClause selectClause = new SelectClause();
selectClause.select(ExampleNaming.NAME);
FromClause fromClause = QueryCreator.fromType(Example.class);
SelectSql sql = new SelectSql(selectClause, fromClause);
exampleDao.doSelect(sql, new SingleColumnRowMapper<>(String.class));

// update whole message with different fields
exampleDao.updateMessageByPrimaryKey(newValue, oldValue);

Reference