Magician-JDBC 正在参加 2021 年度 OSC 中国开源项目评选,请投票支持!
Magician-JDBC 在 2021 年度 OSC 中国开源项目评选 中已获得 {{ projectVoteCount }} 票,请投票支持!
2021 年度 OSC 中国开源项目评选 正在火热进行中,快来投票支持你喜欢的开源项目!
2021 年度 OSC 中国开源项目评选 >>> 中场回顾
Magician-JDBC 获得 2021 年度 OSC 中国开源项目评选「最佳人气项目」 !
授权协议 MIT
开发语言 Java 查看源码 »
操作系统 跨平台
软件类型 开源软件
开源组织
地区 国产
投 递 者 Magician开源
适用人群 未知
收录时间 2021-05-08

软件简介

Magician-JDBC 是Magician的官方JDBC组件,支持多数据源,无sql单表操作,复杂操作可以写sql,事务管理等

文档

https://magician-io.com

示例

导入依赖

<dependency>
    <groupId>com.github.yuyenews</groupId>
    <artifactId>Magician-JDBC</artifactId>
    <version>last version</version>
</dependency>

<!-- mysql driver package -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.20</version>
</dependency>
<!-- druid connection pool -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.5</version>
</dependency>

<!-- This is the log package, which supports any package that can be bridged with slf4j -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-jdk14</artifactId>
    <version>1.7.12</version>
</dependency>

创建数据源

// 这里用druid做示例,实际上可以支持任意实现了DataSource接口的连接池
DruidDataSource dataSource = new DruidDataSource();

Properties properties = new Properties();
properties.put("druid.name", "local");
properties.put("druid.url", "jdbc:mysql://127.0.0.1:3306/martian-test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=true&useSSL=false");
properties.put("druid.username", "root");
properties.put("druid.password", "123456");
properties.put("druid.driverClassName", Driver.class.getName());

dataSource.setConnectProperties(properties);

将数据源添加到JDBC

// Create JDBC, it is recommended to execute it only once when the project starts
MagicianJDBC.createJDBC()
        .addDataSource("a", dataSource)// Add data source, this method can be called multiple times to add multiple data sources
        .defaultDataSourceName("a");// Set the name of the default data source

单表操作

按条件查询

List<Condition> conditionList = new ArrayList<>();
conditionList.add(Condition.get("id > ?", 10));
conditionList.add(Condition.get("and name = ?", 100));
conditionList.add(Condition.get("order by create_time", Condition.NOT_WHERE));

List<Map> result = JDBCTemplate.get().select("表名", conditionList, Map.class);

按条件删除

List<Condition> conditionList = new ArrayList<>();
conditionList.add(Condition.get("id > ?", 10));
conditionList.add(Condition.get("and name = ?", 100));
conditionList.add(Condition.get("order by create_time", Condition.NOT_WHERE));

JDBCTemplate.get().delete("表名", conditionList);

插入一条数据

DemoPO demoPO = new DemoPo();
demoPO.setName("bee");
demoPo.setAge(10);

JDBCTemplate.get().insert("表名", demoPO);

修改数据

DemoPO demoPO = new DemoPo();
demoPO.setName("bee");
demoPo.setAge(10);

List<Condition> conditionList = new ArrayList<>();
conditionList.add(Condition.get("id = ?", 10));
conditionList.add(Condition.get("and name = ?", 100));

JDBCTemplate.get().update("表名", demoPO, conditionList);

自己写sql

查询

DemoPO demoPO = new DemoPo();
demoPO.setName("bee");
demoPo.setAge(10);

List<Map> result = JDBCTemplate.get().selectList("select * from xxx where name={name} and age={age}", demoPO, Map.class);

增删改

DemoPO demoPO = new DemoPo();
demoPO.setName("bee");
demoPo.setAge(10);

JDBCTemplate.get().exec("update xxx set xxx = {xxx}, ccc = {ccc} where name={name} and age={age}", demoPO);

除此之外,还支持事务管理 和分页查询,详情可以查看文档

展开阅读全文

代码

的 Gitee 指数为
超过 的项目

评论

点击引领话题📣
发表了资讯
2021/05/08 14:31

Magician-JDBC 1.1.2 发布,加入SQL构造器

此次更新 主要是加入了一个新特性:SQL构造器。方便在单表操作的时候无需写sql 一、具体应用如下 查询数据 // 根据主键查询,可以这样构建sql String sql = SqlBuilder.select("表名").byPrimaryKey("主键名").builder(); // 自定义查询条件,可以这样构建 String sql = SqlBuilder.select("表名").where("表字段名 = #{参数字段名}").builder(); // 自定义查询字段,可以这样构建(column传入的类里面的属性就是要查询...

0
5
没有更多内容
加载失败,请刷新页面
点击加载更多
加载中
下一页
发表了博客
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
发表了问答
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
com.fasterxml.jackson.core:jackson-databind 存在拒绝服务漏洞
拒绝服务
com.fasterxml.jackson.core:jackson-databind 是一个库,其中包含Jackson Data Processor的通用数据绑定功能和树模型。当使用 JDK 序列化来序列化和反序列化 JsonNode 值时,此包的受影响版本容易受到拒绝服务 (DoS) 的攻击。
MPS-2022-12500
2022-08-08 20:51
没有更多内容
加载失败,请刷新页面
点击加载更多
加载中
下一页
0 评论
6 收藏
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部