2 Star 4 Fork 1

yuye / Magician-JDBC

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

Magician-JDBC ·

Magician-JDBC is the official JDBC component of Magician, supporting multiple data sources, no sql single table operations, complex operations can write sql, transaction management, etc.

Documentation

https://magician-io.com

Example

Importing dependencies

<dependency>
    <groupId>com.github.yuyenews</groupId>
    <artifactId>Magician-JDBC</artifactId>
    <version>2.0.2</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>

Creating a data source

// Here is an example using druid, which can actually support any connection pool that implements the DataSource interface
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);

Adding a data source to 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

Single Table Operations

Search by condition

List<Condition> conditionList = ConditionBuilder.createCondition()
            .add("id > ?", 10)
            .add("and (name = ? or age > ?)", "bee", 10))
            .add("order by create_time", Condition.NOT_WHERE))
            .build();

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

Delete by condition

List<Condition> conditionList = ConditionBuilder.createCondition()
        .add("id = ?", 10)
        .build();

JDBCTemplate.get().delete("table name", conditionList);

Insert a piece of data

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

JDBCTemplate.get().insert("table name", demoPO);

Modify data

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

List<Condition> conditionList = ConditionBuilder.createCondition()
        .add("id > ?", 10)
        .add("and name = ?", "bee"))
        .build();

JDBCTemplate.get().update("table name", demoPO, conditionList);

Write your own sql

Select

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);

insert, delete, update

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);

In addition, transaction management and paging are also supported, see the documentation for details

The MIT License (MIT) Copyright 2021, Yuye 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.

简介

Magician-JDBC 是Magician的官方JDBC组件,可以很快捷的实现数据库操作 展开 收起
Java
MIT
取消

发行版 (1)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/yuyenews/Magician-JDBC.git
git@gitee.com:yuyenews/Magician-JDBC.git
yuyenews
Magician-JDBC
Magician-JDBC
master

搜索帮助