又一个重复的轮子,O/R Mapping工具包kamike.db发布到Github上开源



删除了不少对其余包的引用,如今应该是最小可用的版本了。html

索性opensource,LGPL协议,欢迎你们试用。这玩意最大的优势,就是只是一个工具,而不是框架,因此与标准的JDBC和preparedstatement结合的很顺畅,能大幅度下降jdbc写法的工做量,java

不过我的仍是建议,写入用orm,读取在合适的时候稍微用一下绑定数据的DTO的代码,因此提供了GenericSelect类和GenericUpdate的rawSql函数,方便你们本身拼where的sql,真遇到复杂的select查询,你们仍是别期望了,其实hibernate甚至nutzDAO也都期望不上的。mysql

这个工具包,完全贯彻了读写分离的思想,若是实现读取,须要继承GenericReader,若是实现写入,继承GenericWriter。git

并且闲的蛋疼,把自动建立表和数据库也加上了。具体代码参考GenericCreator。github


不过里面依赖的的guava,这个是用来测试的。实际用能够删掉。sql

话说回来,用异步的guava的AsyncEventBus+Executors.newFixedThreadPool()+CountDownLatch()进行多线程的测试,很是高效,强烈推荐。数据库

github地址:多线程

https://github.com/hubinix/com.kamike.db 框架

刚刚有更新了,修改了几个bug异步


依赖的包:

c3p0-0.9.2.1
cos-26Dec2008
guava-15.0
mchange-commons-java-0.2.3.4
mysql-connector-java-5.1.18-bin

下面贴一个具体的实现:代码在github的com.kami.console包下面.

关于最麻烦的Select操做,能够参考以下:

新建一个继承了BaseReader的对象,具体如何在本身的DAO用这个工具能够参考find(TestTable t)的实现,这个find(TestTable t)只是一个例子,无任何实际意义。在这里面也能够随意添加findByXXX的方法。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kami.console;

import com.kamike.db.SysDbInst;
import com.kamike.db.generic.BaseReader;
import com.kamike.db.generic.GenericSelect;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author THiNk
 */
public class TestTableReader extends BaseReader<TestTable> {

    public TestTableReader(String dbName) {
        super(dbName);
    }

    @Override
    public GenericSelect<TestTable> createSelect() {

        return new TestTableSelect();
    }

    @Override
    public GenericSelect<TestTable> createSelect(TestTable t) {
        return new TestTableSelect(t);
    }

    
    public long count(T t) {
        GenericSelect<TestTable> select = createSelect();

        ResultSet rs = null;
        PreparedStatement ps = null;
        Connection conn = null;

        long ret = 0;
        try {
            conn = SysDbInst.getInstance().getDatabase().getSingleConnection();
            ps = conn.prepareStatement(select.countSQL(select.rawSql(dbName)+ "where t.count=? "), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            ps.setLong(1, t.getCount());//这里的查询参数的绑定,只能本身动手,丰衣足食了。
            rs = ps.executeQuery();
            ret = select.count(rs);

        } catch (Exception e) {
            ret =0;
            System.out.println(this.getClass().getName() + e.toString());

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                    rs = null;
                }
                if (ps != null) {
                    ps.close();
                    ps = null;
                }
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (SQLException ex) {
                Logger.getLogger(BaseReader.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return ret;
    }

    @Override
    public ArrayList<TestTable> find(TestTable t) {
        GenericSelect<TestTable> select = createSelect();

        ResultSet rs = null;
        PreparedStatement ps = null;
        Connection conn = null;

        ArrayList<TestTable> ret = null;
        try {
            conn = SysDbInst.getInstance().getDatabase().getSingleConnection();
            ps = conn.prepareStatement(select.rawSql(dbName) + "where t.count=? ", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            ps.setLong(1, t.getCount());//这里的查询参数的绑定,只能本身动手,丰衣足食了。
            rs = ps.executeQuery();
            ret = select.fetch(rs);

        } catch (Exception e) {
            ret = new ArrayList<>();
            System.out.println(this.getClass().getName() + e.toString());

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                    rs = null;
                }
                if (ps != null) {
                    ps.close();
                    ps = null;
                }
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (SQLException ex) {
                Logger.getLogger(BaseReader.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return ret;

    }

}

而后是实体对象定义:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kami.console;

import com.kamike.db.generic.FieldLength;
import com.kamike.db.generic.FieldName;
import com.kamike.db.generic.Id;
import com.kamike.db.generic.TableName;
import java.util.Date;

/**
 *
 * @author THiNk
 */
@TableName("test_table")
public class TestTable {

    @Id
    @FieldName("id")
    @FieldLength(64)
    private String id;

    @FieldName("count")
    private long count;

    @FieldName("name")
    @FieldLength(255)
    private String name;

    @FieldName("ready")
    private boolean ready;

    @FieldName("create_date")
    private Date createDate;

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the count
     */
    public long getCount() {
        return count;
    }

    /**
     * @param count the count to set
     */
    public void setCount(long count) {
        this.count = count;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the ready
     */
    public boolean isReady() {
        return ready;
    }

    /**
     * @param ready the ready to set
     */
    public void setReady(boolean ready) {
        this.ready = ready;
    }

    /**
     * @return the createDate
     */
    public Date getCreateDate() {
        return createDate;
    }

    /**
     * @param createDate the createDate to set
     */
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
}

而后要实现GenericSelect须要的几个对象new的方法,重载一下GenericSelect。。万恶的Java半吊子泛型。。

/*
 *  就写一个new。。不过两个构造器,有参的无参的都要这么实现,还有调用父类的构造器
 */
package com.kami.console;

import com.kamike.db.generic.GenericSelect;

/**
 *
 * @author THiNk
 */
public class TestTableSelect extends GenericSelect<TestTable> {

    public TestTableSelect(TestTable t) {
        super(t);
    }

    public TestTableSelect() {
        super();
    }

    @Override
    public TestTable create() {
        return new TestTable();
    }

}

而后就是调用了。

   //查询测试
           TestTableReader tts=new TestTableReader("kamike");
           TestTable template=new TestTable();
           template.setCount(500);
           ArrayList<TestTable> testList=tts.find(template);
相关文章
相关标签/搜索