手写框架探险系列-dao重新设计

之前自己做的dao

  • 源码地址

改进:

  • 当初写的仅仅只是支持了jndi的操作,没有对jta的支持,同时需要在业务代码中显式的编码。

public interface SqlSession {
    int[] batch(String configId, Object[][] params) throws SQLException;

    int[] batch(SqlConfig config, Object[][] params) throws SQLException;

    int[] batch(SqlConfig config, List parameter)
            throws SQLException;

    int[] batch0(SqlConfig config, List parameter) throws SQLException;

    int[] batch0(String configId, List parameter) throws SQLException;

    int[] batch(String configId, List parameter)
            throws SQLException;

    //
    int update(SqlConfig config, Map parameter) throws SQLException;

    int update(SqlConfig config, Object parameter) throws SQLException;

    int update(String configId, Object parameter) throws SQLException;

    int update(String configId, Map parameter) throws SQLException;

    // //
    // 这里需要使用
    // ////////////////
    Future selectResultSet(SqlConfig config, Map parameter)
            throws SQLException;

    Future selectResultSet(SqlConfig config, Object parameter)
            throws SQLException;

    Future selectResultSet(String configId, Object parameter)
            throws SQLException;

    Future selectResultSet(String configId, Map parameter)
            throws SQLException;

     Future select(SqlConfig config, Map parameter) throws SQLException;

     Future select(SqlConfig config, Object parameter)
            throws SQLException;

     Future select(String configId, Object parameter) throws SQLException;

     Future select(String configId, Map parameter) throws SQLException;

    /**
     * 这里没有SqlConfig,就利用配置里面的默认的SqlConfig, 如对应的数据源,事务级别,事务传播类型。 这里可以看做是声明式的配置管理。
     * @param config该配置写在代码里面就是强制性配置的。
     * @param parameter
     * @return
     */
    Future selectValue(SqlConfig config, Map parameter)
            throws SQLException;

    Future selectValue(SqlConfig config, Object parameter)
            throws SQLException;

    Future selectValue(String configId, Object parameter)
            throws SQLException;

    Future selectValue(String configId, Map parameter)
            throws SQLException;

    Future selectRecord(SqlConfig config, Map parameter)
            throws SQLException;

    Future selectRecord(SqlConfig config, Object parameter)
            throws SQLException;

    Future selectRecord(String configId, Object parameter)
            throws SQLException;

    Future selectRecord(String configId, Map parameter)
            throws SQLException;

    Future selectTableData(SqlConfig config, Map parameter)
            throws SQLException;

    Future selectTableData(SqlConfig config, Object parameter)
            throws SQLException;

    Future selectTableData(String configId, Object parameter)
            throws SQLException;

    Future selectTableData(String configId, Map parameter)
            throws SQLException;

    Future selectColumnSet(SqlConfig config, Map parameter)
            throws SQLException;

    Future selectColumnSet(SqlConfig config, Object parameter)
            throws SQLException;

    Future selectColumnSet(String configId, Object parameter)
            throws SQLException;

    Future selectColumnSet(String configId, Map parameter)
            throws SQLException;
}
public interface SqlConfig {
    public static final String SQL_DEFAULT_CONFIG = "dao-SqlDefaultConfig";
    String SQL = "SQL"; 
    String RESULT_SET_HANDLER = "org.apache.commons.dbutils.ResultSetHandler";
    String IS_FUTURE = "Future";
    String COLUMN_NAME = "columnName";
    String COLUMN_MAX = "columnMax";
    String ROW_INDEXS = "RowIndexs"; // 第几行的数据可以被放到list里面。
    String CONNECTION = "java.sql.Connection";
    /**
     * 用于设置输入的map或bean对应设置值
     * stmt.setObject(i + 1, object);
     */
    String FILL_STATEMENT_KEYS = "fillStatementKeys";
    SqlConfig setConfig(String key , Object value);
     T getConfig(String key);
}
  • 没有SqlConfig就是在配置里的声明式配置。有SqlConfig就是强制性配置。
  • 也就是说调用方只需要知道:sql语句的标识,参数,配置,就可以返回最后的结果集,具体的什么结果集就需要用户来指定了。
    至于是具体怎么执行的(jdbc,jpa,jdni,mycat,jta,分布式事务。。。),一些细节全部由配置来指定和实现。
  • 指定对应的方法名标识来进行反射,从而使得用户不知道具体实现是sql还是nosql、newSql。也就是说只要可以符合这种封装的过程就可以了,我们也就可以把参数的设置和结果集的公共部分给提取出来。
    这样提取了代码,性能肯定不好。
  • 注意的是,这里只是对sql的实现,nosql各种客户端的api不同,需要自己实现。
  • 同时利用Future模式,实现dao层查询的多线程。当然也是可以配置是不使用的

整合Derby


            org.apache.derby
            derby


public class DerbyTest {
     private static String driver = "org.apache.derby.jdbc.EmbeddedDriver";
        private static String protocol = "jdbc:derby:db3;create=true"; // 在工程目录下创建数据库
        // private static String protocol = "jdbc:derby:db/db3;create=true";    //在工程目录下db目录中创建数据库
        // private static String protocol = "jdbc:derby:D:/mydbs/db3;create=true";    //在D:/mydbs/目录下创建数据库

        public static void main(String[] args) {
            try {
                Class.forName(driver).newInstance();
                System.out.println("Loaded the appropriate driver");
                Connection conn = DriverManager.getConnection(protocol);
                Statement stmt = conn.createStatement();
                stmt.executeUpdate("create table stu(id int not null generated by default as identity,stuname varchar(20),email varchar(30))");

                for (String str : "one,two,three,four,five".split(",")) {
                    String sql = "insert into stu(stuname,email) values('" + str + "','" + str + "@test.com')";
                    System.out.println(sql);
                    stmt.addBatch(sql);
                }
                stmt.executeBatch();
                System.out.println("insert over");
                conn.commit();
                while(true){
                    Thread.sleep(10000);
                } 
//              stmt.close();
//              conn.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
}

  • JavaDB数据库使用笔记

你可能感兴趣的:(手写框架探险系列-dao重新设计)