Code Monkey home page Code Monkey logo

common-basic-service's Introduction

common-basic-service

A simple and useful efficiency tool for java developers

Currently, the common basic service supported functions as below:

  • converting sql(create table sql script) to java POJO class
  • converting sql(create table sql script) to paoding rose framework DAO class

This tool does not need to connect to database, you ONLY need paste your sql script into the input textarea and click button, you will get the parsed result!

operation

For Example, you input:

CREATE TABLE IF NOT EXISTS bill (
id              BIGINT                  UNSIGNED NOT NULL AUTO_INCREMENT
COMMENT '自增主键',
user_id         BIGINT                  NOT NULL DEFAULT 0
COMMENT '用户id',
create_time     BIGINT                  NOT NULL DEFAULT 0
COMMENT '创建时间',
update_time     BIGINT                  NOT NULL DEFAULT 0
COMMENT '更新时间',
PRIMARY KEY (id),
KEY idx_bill_uid (user_id)
)  ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8mb4 COLLATE utf8mb4_bin;

you will get the POJO definition(Xxx.java, you can download it directly):

public class Bill {
    // 自增主键
    private long id;
    // 用户id
    private long userId;
    // 创建时间
    private long createTime;
    // 更新时间
    private long updateTime;
}

and also you can get the paoding rose dao class(XxxDAO.java, also you can download it):

import net.paoding.rose.jade.annotation.DAO;

@DAO 
public interface BillDAO {
    String TABLE_NAME = "`bill`";
    String INSERT_COLUMNS = "`id`,`user_id`,`create_time`,`update_time`";
    String SELECT_COLUMNS = "`id`,`user_id`,`create_time`,`update_time`";
    String INSERT_VALUES = ":1.id, :1.userId, :1.createTime, :1.updateTime";
    String UPDATE_COLUMNS = "id=:1.id, user_id=:1.userId, create_time=:1.createTime, update_time=:1.updateTime";
}

The core implementation is using alibaba druid SQLParserUtils to parse sql, as below:

           SQLStatementParser sqlStatementParser = SQLParserUtils.createSQLStatementParser(sql, dbType);
           SQLCreateTableStatement sqlCreateTableStatement = sqlStatementParser.parseCreateTable();
           List<SQLTableElement> tableElementList = sqlCreateTableStatement.getTableElementList();
   
           List<SQLColumnDefinition> columnDefinitions = new ArrayList<>();
           for (SQLTableElement tableElement : tableElementList) {
               if (!(tableElement instanceof SQLColumnDefinition)) {
                   // primary key, key, unique key等等过滤掉, 只保留列定义
                   continue;
               }
               columnDefinitions.add((SQLColumnDefinition) tableElement);
           }

when we get the column definitions, we can get the column data type, column name, and column comments and so on. Then we can generate the corresponding java POJO definition and DAO definition.

Benefits

  • as you can see, after handwriting creating table sql statements, we do not need to handwrite POJO definition and DAO class, which is a disaster when the table is very complex.
  • the tool is very simple to use, we don't need to connect to database(comparision with some other solutions)

common-basic-service's People

Contributors

zybotian avatar

Watchers

James Cloos avatar hiekay avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.