Skip to content

Its purpose is to send java objects to database without writing one SQL statement.

License

Notifications You must be signed in to change notification settings

chncwang/easy2db

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

easy2db

easy2db is to send Java objects to database(only MySQL is supported currently) without writing one line SQL statement.

Why not use Mybatis?

Mybatis supplies enogh flexibility as it requires you to write SQL statements by hand. But for sending objects to database, simplity is mouch more important than flexibility. Why need we write INSERT and SELECT statements again and again, if the logic can be well abstract?

Why not use Hibernate?

Hibernate seems too hard to master, so I have no willing to learn it.

Usage

easy2db is damn easy to use. There are nearly only two things you need do:

  1. Define a Java class with table name, primary key, unique key and other columns, using Java annotations.
  2. Call com.chncwang.easy2db.Engine#sendObject

Simple example

  1. Define the Java class:
@Table("github_repo")
public class GithubRepo {
    @PrimaryKey
    @Column("id")
    private String mId;

    @UniqueKey
    @Column("url")
    private String mUrl;

    @Column("name")
    private String mName;

    @Column("star_count")
    private Integer mStarCount;

    public GithubRepo() {}

    ...
}
  1. Example object:
public static GithubRepo fool2048() {
    final GithubRepo repo = new GithubRepo();
    repo.setUrl("https://github.com/chncwang/fool2048");
    repo.setName("fool2048");
    repo.setStarCount(1);
    return repo;
}
  1. Send it!
final Connection connection = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/github", "wqs", "");
new DefaultEngine(GithubRepo.class, connection).
        sendObjectToDb(GithubRepos.fool2048());

foreign key dependency

After I create a new table named 'user', and add a field named 'user_id' to 'github_repo' table as a foreign key, how shall I send GithubRepo objects now?

It's still damn simple!

  1. User class:
@Table("user")
public class User {
    @PrimaryKey
    @Column("id")
    private String mId;

    @UniqueKey
    @Column("user_name")
    private String mUserName;

    public User() {}

    ...
}
  1. Add 'user_id' field to GithubRepo class:
@Table("github_repo")
public class GithubRepo {

    ...

    @Foreign
    @Column("user_id")
    private User mUser;

    ...
}
  1. Set User object to GithubRepo object:
public static GithubRepo fool2048() {
    final GithubRepo repo = new GithubRepo();
    ...

    final User user = new User();
    user.setUserName("Chauncey Wang");
    repo.setUser(user);

    return repo;
}
  1. Send it!
final Connection connection = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/github", "wqs", "");
new DefaultEngine(GithubRepo.class, connection).
        sendObjectToDb(GithubRepos.fool2048());

How does it work?

  1. A target object, with its foreign objects, is sent to database recursivly.
  2. When a object is sent, it firstly check whether the object already exists in database. If is, update it, or if isn't, insert it.

Maven dependency

<groupId>com.chncwang.easy2db</groupId>
<artifactId>core</artifactId>
<version>0.0.1-SNAPSHOT</version>

Currently it's a SNAPSHOT version and hasn't been uploaded to maven central repository. So you may add it to your own nexus server.

Question?

If you have any question or advices, feel free to send me an email: chncwang@gmail.com.

About

Its purpose is to send java objects to database without writing one SQL statement.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages