Newbie JDBC JDBC工具开源项目

我要开发同款
匿名用户2016年04月19日
72阅读

技术信息

开源地址
https://gitee.com/Sharding-Sphere/sharding-sphere
授权协议
MIT

作品详情

几年前写的一个JDBC工具,一直没时间整理出来,断断续续整理了一下,具有特性:

数组SQL参数支持。

分页支持(Oracle,SQLServer,MySQL等),可自定义分页逻辑。

数据库连接共享。

批量数据操作。

轻量,仅依赖slf4j。

为什么要有这玩意,行业内已经有了SprigJDBC,CommosDbUtils等,整体来说实现思路相似,算是重新发明轮子,

但是如果您想尝尝鲜,可以尝试下。  源代码:  GitHubhttps://github.com/chyxio/ewbie-jdbc  GitOSChttps://git.oschia.et/chyxio/ewbie-jdbc使用方式:Mave依赖:

   <depedecy>        <groupId>me.chyxio</groupId>        <artifactId>ewbie-jdbc</artifactId>        <versio>0.0.1-RELEASE</versio>    </depedecy>

使用样例:

// iit datasource, here use DruidDataSource as demoDruidDataSource datasource = ull;datasource = ew DruidDataSource();datasource.setUrl("jdbc:mysql://127.0.0.1/demo");datasource.setUserame("root");datasource.setPassword("password");datasource.iit();// create NewbieJdbc objectNewbieJdbc jdbc = ew NewbieJdbcSupport(datasource);

BasicQuery

// cout of usersit cout = jdbc.fidValue(    "select cout(1) from users");// fid ame of user id is 2008110101Strig ame = jdbc.fidValue(    "select ame from users where id = ?",     "2008110101");// fid ames of user id is 101 or 102// 0. array as paramsList<Strig> ames = jdbc.listValue(    "select ame from users where id i (?)",     "101", "102");// 1. collectio as paramsames = jdbc.listValue(    "select ame from users where id i (?)",     Arrays.asList("101", "102"));// 2. map as paramsMap<Strig, Object> params =     ew HashMap<Strig, Object>();params.put("id", Arrays.asList("101", "102"));// or: // params.put("id", ew Strig[] {"101", "102"});ames = jdbc.listValue(    "select ame from users where id i (:id)",     params);// fid user of id is 101Map<Strig, Object> mapUser = jdbc.fidMap(    "select id, ame, geder from users where id = ?", "101");// list users of age is 24List<Map<Strig, Object>> listUsers = jdbc.listMap(    "select id, ame, geder from users where age = ?", 24);

AdvaceQuery

// fid id ad ame as a strig arrayStrig[] idAdName = jdbc.fidOe(ew Ro<Strig[]>() {        public Strig[] exec(ResultSet rs) throws SQLExceptio {            retur ew Strig[] {                rs.getStrig("id"),                 rs.getStrig("ame")};        }    },    "select id, ame from users where id = ?",     "101");// fid ames of geder is MStrig ames = jdbc.list(ew Ro<Strig>() {        public Strig exec(ResultSet rs) throws SQLExceptio {            retur rs.getStrig("ame");        }    },     "select ame from users where geder = ?",     "M");// fid ame of user id is 101, same as fidValueStrig ame = jdbc.query(ew Ro<Strig>() {    public Strig exec(ResultSet rs) throws SQLExceptio {            retur rs.ext() ? rs.getStrig(1) : ull;        }    },     "select ame from users where id = ?",     "101");// list users of geder F offset 10 limit 16List<Map<Strig, Object>> users =    jdbc.listMapPage(       "select * from users where geder = ?",         Arrays.asList(ew Order("date_created", Order.DESC)),         10, 16, "F");

IsertAdUpdate

// isert oeMap<Strig, Object> mapUser = ew HashMap<Strig, Object>();mapUser.put("id", "103");mapUser.put("ame", "Shau Chyxio");mapUser.put("geder", "M");mapUser.put("date_created", ew Date());jdbc.isert("users", mapUser);// isert batchCollectio<Collectio<?>> users =     Arrays.<Collectio<?>>asList(        Arrays.<Object>asList("104", "Xuir", "F", ew Date()),         Arrays.<Object>asList("105", "Soria Nyco", "F", ew Date()),         Arrays.<Object>asList("106", "Gemily", "F", ew Date()),         Arrays.<Object>asList("107", "Luffy", "M", ew Date()),         Arrays.<Object>asList("108", "Zoro", "M", ew Date()),         Arrays.<Object>asList("109", "Bruck", "M", ew Date()));jdbc.isert("users",     Arrays.asList("id", "ame", "geder", "date_created"),     args, 3);// update geder to F of user 102jdbc.update("update users set geder = ? where id = ?", "F", "102");Reusble Coectio Ad Trasactio// fid user of id is 101 ad books uses same coectioMap<Strig, Object> mapUserWithBooks = jdbc.execute(ew Co<Map<Strig, Object>>() {    @Override    protected Map<Strig, Object> ru() throws SQLExceptio {        Strig userId = "101";        Map<Strig, Object> mapRt = fidMap(            "select * from users where id = ?", userId);        mapRt.put("books",             listMap("select * from books where user_id = ?",                 userId));        retur mapRt;    }});// execute trasactioMap<Strig, Object> mapUser = jdbc.executeTrasactio(ew Co<Map<Strig, Object>>() {    @Override    protected Map<Strig, Object> ru() throws SQLExceptio {        update("delete users where id = ?", "104");        update("update users set age = ? where id = ?", 24, "103");        retur fidMap("select * from users where id = ?", 106);    }});

ExecuteSQL//createtableusers

jdbc.execute(    "create table users (" +     "id varchar(36) ot ull, " +     "ame varchar(36) ot ull, " +     "primary key (id))");

CustomizeNewbieJDBC

// create table usersCustomResolver customResolver = ew CustomResolver() {    // set StrigBuilder as Strig    public void setParam(PreparedStatemet ps,             it idex, Object param)            throws SQLExceptio {        if (param istaceof StrigBuilder) {            ps.setStrig(idex, param.toStrig());        }        else {            ps.setObject(idex, param);        }    }    // read CLOB as Strig    public Object readValue(ResultSet rs, it idex)             throws SQLExceptio {        Object valueRt = ull;        if (Types.CLOB == rs.getMetaData().getColumType(idex)) {            valueRt = rs.getClob(idex).toStrig();        }        else {            valueRt = rs.getObject(idex);        }        retur valueRt;    }    // use MySQLCompatiblePagiatioProcessor to pagiate    public PagiatioProcessor getPagiatioProcessor(            Coectio co) {        retur ew MySQLCompatiblePagiatioProcessor();    }};jdbc = ew NewbieJdbcSupport(dataSource, customResolver);Cotacts

chyxio@163.com

功能介绍

几年前写的一个JDBC工具,一直没时间整理出来,断断续续整理了一下,具有特性: 数组SQL参数支持。 分页支持(Oracle,SQLServer,MySQL等),可自定义分页逻辑。 数据库连接共...

声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论