mirror of
https://github.com/CarmJos/EasySQL.git
synced 2024-09-19 21:35:47 +00:00
修改代码演示的位置
This commit is contained in:
parent
85d2ee8984
commit
2d384bdd15
65
README.md
65
README.md
@ -8,6 +8,7 @@
|
|||||||
```
|
```
|
||||||
|
|
||||||
# EasySQL
|
# EasySQL
|
||||||
|
|
||||||
[![version](https://img.shields.io/github/v/release/CarmJos/EasySQL)](https://github.com/CarmJos/EasySQL/releases)
|
[![version](https://img.shields.io/github/v/release/CarmJos/EasySQL)](https://github.com/CarmJos/EasySQL/releases)
|
||||||
[![License](https://img.shields.io/github/license/CarmJos/EasySQL)](https://opensource.org/licenses/GPL-3.0)
|
[![License](https://img.shields.io/github/license/CarmJos/EasySQL)](https://opensource.org/licenses/GPL-3.0)
|
||||||
[![workflow](https://github.com/CarmJos/EasySQL/actions/workflows/maven.yml/badge.svg?branch=master)](https://github.com/CarmJos/EasySQL/actions/workflows/maven.yml)
|
[![workflow](https://github.com/CarmJos/EasySQL/actions/workflows/maven.yml/badge.svg?branch=master)](https://github.com/CarmJos/EasySQL/actions/workflows/maven.yml)
|
||||||
@ -37,69 +38,7 @@
|
|||||||
|
|
||||||
### 示例代码
|
### 示例代码
|
||||||
|
|
||||||
```java
|
您可以 [点击这里](easysql-demo/src/main/java/EasySQLDemo.java) 查看部分代码演示,更多演示详见 [开发介绍](.documentation/INDEX.md) 。
|
||||||
public class EasySQLDemo {
|
|
||||||
|
|
||||||
public void createTable(SQLManager sqlManager) {
|
|
||||||
// 同步创建表
|
|
||||||
sqlManager.createTable("users")
|
|
||||||
.addColumn("id", "INT(11) AUTO_INCREMENT NOT NULL PRIMARY KEY")
|
|
||||||
.addColumn("username", "VARCHAR(16) NOT NULL UNIQUE KEY")
|
|
||||||
.addColumn("age", "INT(3) NOT NULL DEFAULT 1")
|
|
||||||
.addColumn("email", "VARCHAR(32)")
|
|
||||||
.addColumn("phone", "VARCHAR(16)")
|
|
||||||
.addColumn("registerTime", "DATETIME NOT NULL")
|
|
||||||
.build().execute(null /* 不处理错误 */);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sqlQuery(SQLManager sqlManager) {
|
|
||||||
// 同步SQL查询
|
|
||||||
try (SQLQuery query = sqlManager.createQuery()
|
|
||||||
.inTable("users") // 在users表中查询
|
|
||||||
.selectColumn("id", "name") // 选中 id 与 name列~~~~
|
|
||||||
.addCondition("age", ">", 18) // 限定 age 要大于5
|
|
||||||
.addCondition("email", null) // 限定查询 email 字段为空
|
|
||||||
.addNotNullCondition("phone") // 限定 phone 字段不为空
|
|
||||||
.addTimeCondition("registerTime", // 时间字段
|
|
||||||
System.currentTimeMillis() - 100000, //限制开始时间
|
|
||||||
-1//不限制结束时间
|
|
||||||
).build().execute()) {
|
|
||||||
ResultSet resultSet = query.getResultSet();
|
|
||||||
//do something
|
|
||||||
|
|
||||||
} catch (SQLException exception) {
|
|
||||||
exception.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sqlQueryAsync(SQLManager sqlManager) {
|
|
||||||
// 异步SQL查询
|
|
||||||
sqlManager.createQuery()
|
|
||||||
.inTable("users") // 在users表中查询
|
|
||||||
.addCondition("id", 5) // 限定 id 为 5
|
|
||||||
.setLimit(1).build().executeAsync(success -> {
|
|
||||||
ResultSet resultSet = success.getResultSet();
|
|
||||||
//do something
|
|
||||||
}, exception -> {
|
|
||||||
//do something
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sqlInsert(SQLManager sqlManager) {
|
|
||||||
// 同步SQL插入 (不使用try-catch的情况下,返回的数值可能为空。)
|
|
||||||
Integer id = sqlManager.createInsert("users")
|
|
||||||
.setColumnNames("username", "phone", "email", "registerTime")
|
|
||||||
.setParams("CarmJos", "18888888888", "carm@carm.cc", TimeDateUtils.getCurrentTime())
|
|
||||||
.setKeyIndex(1) // 设定自增主键的index,将会在后续返回自增主键
|
|
||||||
.execute(exception -> {
|
|
||||||
// 处理异常
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
更多演示详见开发介绍。
|
|
||||||
|
|
||||||
### 依赖方式 (Maven)
|
### 依赖方式 (Maven)
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ import java.sql.SQLException;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
public interface SQLAction<T> {
|
public interface SQLAction<T> {
|
||||||
|
|
||||||
@ -22,6 +23,13 @@ public interface SQLAction<T> {
|
|||||||
|
|
||||||
@NotNull T execute() throws SQLException;
|
@NotNull T execute() throws SQLException;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
default <R> R execute(@NotNull Function<T, R> function, @Nullable BiConsumer<SQLException, SQLAction<T>> exceptionHandler) {
|
||||||
|
T value = execute(exceptionHandler);
|
||||||
|
if (value == null) return null;
|
||||||
|
else return function.apply(value);
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
default T execute(@Nullable BiConsumer<SQLException, SQLAction<T>> exceptionHandler) {
|
default T execute(@Nullable BiConsumer<SQLException, SQLAction<T>> exceptionHandler) {
|
||||||
if (exceptionHandler == null) exceptionHandler = defaultExceptionHandler();
|
if (exceptionHandler == null) exceptionHandler = defaultExceptionHandler();
|
||||||
|
80
easysql-demo/pom.xml
Normal file
80
easysql-demo/pom.xml
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>easysql-parent</artifactId>
|
||||||
|
<groupId>cc.carm.lib</groupId>
|
||||||
|
<version>0.2.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>easysql-demo</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>99-EasySQL-Demo</name>
|
||||||
|
<description>EasySQL的演示部分</description>
|
||||||
|
<url>https://github.com/CarmJos/EasySQL</url>
|
||||||
|
|
||||||
|
<developers>
|
||||||
|
<developer>
|
||||||
|
<id>CarmJos</id>
|
||||||
|
<name>Carm Jos</name>
|
||||||
|
<email>carm@carm.cc</email>
|
||||||
|
<url>https://www.carm.cc</url>
|
||||||
|
<roles>
|
||||||
|
<role>Main Developer</role>
|
||||||
|
</roles>
|
||||||
|
</developer>
|
||||||
|
</developers>
|
||||||
|
|
||||||
|
<licenses>
|
||||||
|
<license>
|
||||||
|
<name>GNU General Public License v3.0</name>
|
||||||
|
<url>https://opensource.org/licenses/GPL-3.0</url>
|
||||||
|
</license>
|
||||||
|
</licenses>
|
||||||
|
|
||||||
|
<issueManagement>
|
||||||
|
<system>GitHub Issues</system>
|
||||||
|
<url>${project.url}/issues</url>
|
||||||
|
</issueManagement>
|
||||||
|
|
||||||
|
<ciManagement>
|
||||||
|
<system>GitHub Actions</system>
|
||||||
|
<url>${project.url}/actions/workflows/maven.yml</url>
|
||||||
|
</ciManagement>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||||
|
<maven.javadoc.skip>true</maven.javadoc.skip>
|
||||||
|
<maven.deploy.skip>true</maven.deploy.skip>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>${project.parent.groupId}</groupId>
|
||||||
|
<artifactId>easysql-impl</artifactId>
|
||||||
|
<version>${project.parent.version}</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
100
easysql-demo/src/main/java/EasySQLDemo.java
Normal file
100
easysql-demo/src/main/java/EasySQLDemo.java
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
|
import cc.carm.lib.easysql.api.SQLQuery;
|
||||||
|
import cc.carm.lib.easysql.api.util.TimeDateUtils;
|
||||||
|
import cc.carm.lib.easysql.api.util.UUIDUtil;
|
||||||
|
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class EasySQLDemo {
|
||||||
|
|
||||||
|
public void createTable(SQLManager sqlManager) {
|
||||||
|
// 同步创建表
|
||||||
|
sqlManager.createTable("users")
|
||||||
|
.addColumn("id", "INT(11) AUTO_INCREMENT NOT NULL PRIMARY KEY")
|
||||||
|
.addColumn("uuid", "VARCHAR(32) NOT NULL UNIQUE KEY")
|
||||||
|
.addColumn("username", "VARCHAR(16) NOT NULL UNIQUE KEY")
|
||||||
|
.addColumn("age", "INT(3) NOT NULL DEFAULT 1")
|
||||||
|
.addColumn("email", "VARCHAR(32)")
|
||||||
|
.addColumn("phone", "VARCHAR(16)")
|
||||||
|
.addColumn("registerTime", "DATETIME NOT NULL")
|
||||||
|
.build().execute(null /* 不处理错误 */);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sqlQuery(SQLManager sqlManager) {
|
||||||
|
// 同步SQL查询
|
||||||
|
try (SQLQuery query = sqlManager.createQuery()
|
||||||
|
.inTable("users") // 在users表中查询
|
||||||
|
.selectColumns("id", "name") // 选中 id 与 name列
|
||||||
|
.addCondition("age", ">", 18) // 限定 age 要大于5
|
||||||
|
.addCondition("email", null) // 限定查询 email 字段为空
|
||||||
|
.addNotNullCondition("phone") // 限定 phone 字段不为空
|
||||||
|
.addTimeCondition("registerTime", // 时间字段
|
||||||
|
System.currentTimeMillis() - 100000, //限制开始时间
|
||||||
|
-1//不限制结束时间
|
||||||
|
).build().execute()) {
|
||||||
|
ResultSet resultSet = query.getResultSet();
|
||||||
|
//do something
|
||||||
|
|
||||||
|
} catch (SQLException exception) {
|
||||||
|
exception.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
UUID userUUID = sqlManager.createQuery()
|
||||||
|
.inTable("users") // 在users表中查询
|
||||||
|
.selectColumns("uuid")
|
||||||
|
.addCondition("id", 5) // 限定 id 为 5
|
||||||
|
.setLimit(1) // 只取出一个数据
|
||||||
|
.build().execute(query -> {
|
||||||
|
//可以直接进行数据处理
|
||||||
|
ResultSet result = query.getResultSet();
|
||||||
|
try {
|
||||||
|
if (result != null && result.next()) {
|
||||||
|
return UUIDUtil.toUUID(result.getString("uuid"));
|
||||||
|
}
|
||||||
|
} catch (SQLException ignored) {
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}, (exception, action) -> {
|
||||||
|
// 处理异常,不想处理直接填null
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sqlQueryAsync(SQLManager sqlManager) {
|
||||||
|
// 异步SQL查询
|
||||||
|
sqlManager.createQuery()
|
||||||
|
.inTable("users") // 在users表中查询
|
||||||
|
.addCondition("id", 5) // 限定 id 为 5
|
||||||
|
.setLimit(1) // 只取出一个数据
|
||||||
|
.build().executeAsync(success -> {
|
||||||
|
ResultSet resultSet = success.getResultSet();
|
||||||
|
try {
|
||||||
|
if (resultSet != null && resultSet.next()) {
|
||||||
|
String username = resultSet.getString("username");
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}, (exception, action) -> {
|
||||||
|
//do something
|
||||||
|
long createTIme = action.getCreateTime();
|
||||||
|
String shortID = action.getShortID();
|
||||||
|
String sqlContent = action.getSQLContent();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sqlInsert(SQLManager sqlManager) {
|
||||||
|
// 同步SQL插入 (不使用try-catch的情况下,返回的数值可能为空。)
|
||||||
|
Integer id = sqlManager.createInsert("users")
|
||||||
|
.setColumnNames("username", "phone", "email", "registerTime")
|
||||||
|
.setParams("CarmJos", "18888888888", "carm@carm.cc", TimeDateUtils.getCurrentTime())
|
||||||
|
.setKeyIndex(1) // 设定自增主键的index,将会在后续返回自增主键
|
||||||
|
.execute((exception, action) -> {
|
||||||
|
// 处理异常
|
||||||
|
System.out.println(action);
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,5 +1,7 @@
|
|||||||
package cc.carm.lib.easysql.action.query;
|
package cc.carm.lib.easysql.action.query;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.SQLAction;
|
||||||
|
import cc.carm.lib.easysql.api.SQLQuery;
|
||||||
import cc.carm.lib.easysql.api.action.query.PreparedQueryAction;
|
import cc.carm.lib.easysql.api.action.query.PreparedQueryAction;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
||||||
import cc.carm.lib.easysql.query.SQLQueryImpl;
|
import cc.carm.lib.easysql.query.SQLQueryImpl;
|
||||||
@ -13,6 +15,7 @@ import java.sql.ResultSet;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class PreparedQueryActionImpl extends QueryActionImpl implements PreparedQueryAction {
|
public class PreparedQueryActionImpl extends QueryActionImpl implements PreparedQueryAction {
|
||||||
@ -66,5 +69,4 @@ public class PreparedQueryActionImpl extends QueryActionImpl implements Prepared
|
|||||||
|
|
||||||
return new SQLQueryImpl(getManager(), this, connection, preparedStatement, resultSet);
|
return new SQLQueryImpl(getManager(), this, connection, preparedStatement, resultSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package cc.carm.lib.easysql.action.query;
|
package cc.carm.lib.easysql.action.query;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.action.AbstractSQLAction;
|
import cc.carm.lib.easysql.action.AbstractSQLAction;
|
||||||
|
import cc.carm.lib.easysql.api.SQLAction;
|
||||||
import cc.carm.lib.easysql.api.SQLQuery;
|
import cc.carm.lib.easysql.api.SQLQuery;
|
||||||
import cc.carm.lib.easysql.api.action.query.QueryAction;
|
import cc.carm.lib.easysql.api.action.query.QueryAction;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
||||||
@ -11,6 +12,7 @@ import java.sql.Connection;
|
|||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class QueryActionImpl extends AbstractSQLAction<SQLQuery> implements QueryAction {
|
public class QueryActionImpl extends AbstractSQLAction<SQLQuery> implements QueryAction {
|
||||||
@ -20,7 +22,7 @@ public class QueryActionImpl extends AbstractSQLAction<SQLQuery> implements Quer
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull SQLQuery execute() throws SQLException {
|
public @NotNull SQLQueryImpl execute() throws SQLException {
|
||||||
Connection connection = getManager().getConnection();
|
Connection connection = getManager().getConnection();
|
||||||
Statement statement = connection.createStatement();
|
Statement statement = connection.createStatement();
|
||||||
|
|
||||||
@ -32,4 +34,14 @@ public class QueryActionImpl extends AbstractSQLAction<SQLQuery> implements Quer
|
|||||||
|
|
||||||
return query;
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void executeAsync(Consumer<SQLQuery> success, BiConsumer<SQLException, SQLAction<SQLQuery>> failure) {
|
||||||
|
try (SQLQueryImpl query = execute()) {
|
||||||
|
if (success != null) success.accept(query);
|
||||||
|
} catch (SQLException exception) {
|
||||||
|
(exceptionHandler == null ? defaultExceptionHandler() : exceptionHandler).accept(exception, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user