Code Monkey home page Code Monkey logo

pg-dao's Introduction

PG-DAO

A simple Data Access tool which wrapped apache commons-dbutils for PostgreSQL.

pg-dao github

特性

  • 支持实体ID自增长
  • 逻辑删除
  • 自动维护createdDateupdatedDatedelFlag字段
  • 常用CRUD操作
  • 支持条件查询及分页

使用方式

  1. Conf.java文件中配置PostgreSQL连接信息
    //  数据库连接配置
    /**
     * DB URL
     */
    String DB_URL = "jdbc:postgresql://[host]:[ip]/[databasename]";
    /**
     * DB DRIVER
     */
    String DB_DRIVER = "org.postgresql.Driver";
    /**
     * DB USER
     */
    String DB_USER = "[username]";
    /**
     * DB PASSWORD
     */
    String DB_PASSWORD = "[password]";
  1. 使用示例

实体类:User.java

用户自定义的实体必须继承自AbstractModel

public class User extends AbstractModel {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{\"id\": "+this.getId()+", \"name\": "+this.getName()+", \"age\": "+this.getAge()+", \"createdDate\": "+this.getCreatedDate()+", \"updatedDate\": "+this.getUpdatedDate()+", \"delFlag\": "+this.getDelFlag()+"}";
    }
}

2.1 插入对象

    @Test
    public void testSave() {
        User user = new User();
        user.setName("Gordon");
        user.setAge(20);
        User userDb = dao.save(user, User.class);
        Assert.assertEquals("Gordon", userDb.getName());
    }

2.2 根据ID查询对象

    @Test
    public void testFindUserById() {
        Long id = 1L;
        User user = dao.findById(id, User.class);
        Assert.assertEquals(id, user.getId());
    }

2.3 修改对象

    @Test
    public void testUpdateUser() {
        User user = dao.findById(1L, User.class);
        user.setName("Gordon Xiao");
        User updatedUser = dao.update(user, User.class);
        Assert.assertEquals("Gordon Xiao", updatedUser.getName());
    }

2.4 删除对象

    @Test
    public void testDeleteUserById() {
        Long id = 1L;
        dao.deleteById(id, User.class);
        Assert.assertEquals(null, dao.findById(id, User.class));
    }

2.5 分页及条件查询

    @Test
    public void testFindPageUsers() {
        PageResult<User> page = dao.findPage(User.class, 15, 1, "id > 0 and createdDate > '2018-03-01 08:12:00'");
        Assert.assertTrue(page.getCount() > 0);
        Assert.assertTrue(page.getData().size() > 0);
    }

pg-dao's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  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.