代码拉取完成,页面将自动刷新
默认使用 postgresql 数据库(已附带对应驱动包)
仅使用标准库,开箱即用
转载请注明出处
基于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[] | {} | 否 | 初始化所需的其他配置 |
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(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 字符串到相应方法中。
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;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。