1 Star 0 Fork 1

yours / JDBC simple framework

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

JDBC simple framework

默认使用 postgresql 数据库(已附带对应驱动包)

仅使用标准库,开箱即用

转载请注明出处

注意事项:

  1. 接口内注解方法仅支持 @Aggregated 和 @Update 类型的sql
  2. 接口内注解方法返回值仅支持基本类型和 String,且仅有一个返回值
  3. 接口内注解方法定义后即可直接使用,无需进行具体实现
  4. 若不满足上述条件仍可进行手动配置,使用SqlFactory中一系列静态方法定制所需sql操作

基于jdk代理,反射等相关原理实现。

使用示例:

  • 数据库连接
import utils.JdbcUtil;
import utils.annotations.Aggregated;
import utils.annotations.SqlSupport;

@SqlSupport(PASSWORD = "123456")
public class Main {
    public static void main(String[] args) {
        JdbcUtil.connectByConfig(Main.class);
        //...
    }
}
参数名 默认值 是否必须 解释
DRIVER String org.postgresql.Driver 驱动类名
USERNAME String postgres 数据库连接用户名
PASSWORD String "" 密码
URL String jdbc:postgresql://127.0.0.1:5432/postgres 连接URL
otherConfigs String[] {} 初始化所需的其他配置

使用自定义Mapper接口

public interface MY {
    // 一些接口方法示例
    @Aggregated(sql = "select count(id) from city")
    int count();

    @Aggregated(sql = """
           select
           (
               (select city_id from undertake
               where record_id = (select id from record where item_name = ?) and type = ?)
               =
               (select city_id from staff where name = ?)
           );
          """)
    boolean checkCourier(String itemName,int type,String name);
    
    @Update(sql = "insert into record(item_name, item_class, item_price, state, company_id) values (?,?,?,?,?)")
    boolean addRecord(String item_name,String item_class,double item_price,int state,int company_id);

    @Update(sql = "update record set state = ? where item_name = ?")
    boolean updateRecord(Integer state,String item_name);
};

//main 方法中使用
static MY mapper = JdbcUtil.getMapper(Main.class,MY.class);

public static void main(String[] args) {
    JdbcUtil.connectByConfig(Main.class);
    System.out.println(mapper.count());
}

将 sql 语句配置在 Mapper接口方法的注解中,无需具体实现即可直接使用。

注解名 解释
@Aggregated 返回值只有一行的查询
@Update 除查询以外的语句
@Multiple 返回值有多行的查询

手动配置

  • 在普通方法中继续使用上述注解,需传入当前方法对象以及收集查询结果的方式
@Aggregated(sql = """
            select s.name,c.name ,s.state from ship s
            join company c on s.company_id = c.id
            where s.name = ?
            """)
    public ShipInfo getShipInfo(LogInfo log, String name) {
            try {
                return SqlFactory.query(
                        this.getClass().getMethod("getShipInfo", LogInfo.class, String.class),
                        r -> new ShipInfo(r.getString(1),
                                r.getString(2),
                                r.getInt(3) == 1),
                        name
                );
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        } 
    }
  • @Multiple 注解,只能使用于普通方法。需传入一个映射函数和一个收集函数。
// 返回多行后求平均值
@Multiple(sql = """
          select u.tax , r.item_price from undertake u
          join record r on u.record_id = r.id
          where u.type = ? and r.item_class = ? and u.city_id =
          (select c.id from city c where c.name = ?)
          """)
  public double getTaxRate(String city, String itemClass, int type,String format) {
      try {
          return SqlFactory.query(
                  this.getClass().getMethod("getTaxRate", String.class, String.class, int.class,String.class),
                  r -> r.getDouble(1) / r.getLong(2),
                  res -> Double.parseDouble(String.format(format, res.stream()
                          .collect(Collectors.summarizingDouble(Double::doubleValue))
                          .getAverage())),
                  type, itemClass, city);
      } catch (Exception e) {
          e.printStackTrace();
          return -1;
      }
  }
  • 不使用注解,适用于一个方法内需要执行多个sql操作。

直接传入 Sql 字符串到相应方法中。

private int handleCount(String type) {
        String sql = String.format("select count(id) from %s where trim(name) != '' ", type);
        try {
            return SqlFactory.handleSingleResult(
                    SqlFactory.handleQuery(sql, (Object) null),
                    r -> r.getInt(1)
            );
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }
MIT License Copyright (c) 2023 yours Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

JDBC 的轻量封装框架 展开 收起
Java
MIT
取消

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/you-juntao/jdbc-simple-framework.git
git@gitee.com:you-juntao/jdbc-simple-framework.git
you-juntao
jdbc-simple-framework
JDBC simple framework
master

搜索帮助