Skip to content

Commit

Permalink
完善README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ffpy committed Nov 13, 2017
1 parent 86e3bb7 commit b735931
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 27 deletions.
65 changes: 61 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
SqlPal是一款封装了JDBC的对象关系映射(ORM)框架,参考了LitePal的API设计,追求简洁的数据库操作,在这里先感谢一下郭神
SqlPal是一款封装了JDBC API的对象关系映射(ORM)框架,
参考了[LitePal](https://github.com/LitePalFramework/LitePal)的API设计,
目的是追求简洁的数据库操作

### 使用示例
```
// 初始化
SqlPal.init("sample/sqlpal.xml");
// 创建一条记录
User user = new User("admin", "123");
try {
// 保存到数据库
user.save();
} catch (SQLException e) {
e.printStackTrace();
}
// 销毁
SqlPal.destroy();
```

### 添加依赖包
- sqlpal.jar
Expand Down Expand Up @@ -82,6 +100,21 @@ public class News extends DataSupport {
- @PrimaryKey注解用于指定主键
- @AutoIncrement注解表示主键是自增的

### 初始化
```
SqlPal.init("sqlpal.xml");
```
- 放在程序的初始化的位置,只能执行一次
- init方法接收一个String参数,表示配置文件的路径
- 初始化资源

### 销毁
```
SqlPal.destroy();
```
- 放在程序结束的位置
- 释放占用的资源

### 存储数据
#### 单个存储
```
Expand Down Expand Up @@ -153,6 +186,17 @@ try {
```
#### 批量修改
类似于批量存储
#### 约束修改
```
// 将age大于10的所有行的密码修改为aaa
User user = new User();
user.setPassword("aaa");
try {
user.updateAll("age > ?", "10");
} catch (SQLException e) {
e.printStackTrace();
}
```

### 删除数据
#### 单个删除
Expand All @@ -162,13 +206,22 @@ User user = new User();
user.setUsername("admin");
try {
// 删除
user.delete();````````
user.delete();
} catch (SQLException e) {
e.printStackTrace();
}
```
#### 批量删除
类似于批量存储
#### 约束删除
```
// 删除age大于10的行
try {
DataSupport.deleteAll(User.class, "age > ?", "10");
} catch (SQLException e) {
e.printStackTrace();
}
```

### 查询数据
#### 查询user表的所有数据
Expand Down Expand Up @@ -271,7 +324,7 @@ int max = DataSupport.max(User.class, "age", int.class);
int min = DataSupport.min(User.class, "age", int.class);
```

### 事务回滚
### 事务操作
```
try {
SqlPal.begin();
Expand All @@ -285,6 +338,7 @@ try {
// 提交事务
SqlPal.commit();
} catch (SQLException e) {
// 回滚事务
SqlPal.rollback();
e.printStackTrace();
} finally {
Expand All @@ -293,4 +347,7 @@ try {
} catch (SQLException e) {
e.printStackTrace();
}
```
```
把CRUD操作放在SqlPal.begin()和SqlPal.end()之间,因为begin方法会请求连接,而end方法会释放连接。
这样的话就让其中的CRUD操作使用同一个Connection,省去的请求连接的时间,
所以如果要同时执行多个CRUD操作的话建议把它们放在SqlPal.begin()和SqlPal.end()之间
Binary file added downloads/sqlpal-0.0.1.jar
Binary file not shown.
Binary file added sample/lib/mysql-connector-java-3.1.14-bin.jar
Binary file not shown.
4 changes: 1 addition & 3 deletions sample/src/org/sqlpal/sample/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.sqlpal.sample;

import org.sqlpal.SqlPal;
import org.sqlpal.sample.crud.QueryTest;
import org.sqlpal.sample.crud.SaveTest;
import org.sqlpal.sample.crud.UpdateTest;
import org.sqlpal.sample.crud.*;

public class Main {

Expand Down
2 changes: 1 addition & 1 deletion sample/src/org/sqlpal/sample/crud/DeleteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void deleteAll() {
}

/**
* 查询删除
* 约束删除
*/
public static void deleteByConditions() {
// 删除age大于10的行
Expand Down
2 changes: 1 addition & 1 deletion sample/src/org/sqlpal/sample/crud/UpdateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static void updateAll() {
}

/**
* 查询更新
* 约束更新
*/
public static void updateByCondictions() {
// 将age大于10的所有行的密码修改为aaa
Expand Down
2 changes: 1 addition & 1 deletion sqlpal/src/org/sqlpal/crud/DataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ <T> T execute(@NotNull ExecuteCallback<T> callback, DataSupport model) throws SQ
}
}

void executeBatch(@NotNull ExecuteCallback callback, @NotNull List<? extends DataSupport> models) throws SQLException {
void executeBatch(@NotNull final ExecuteCallback callback, @NotNull final List<? extends DataSupport> models) throws SQLException {
if (EmptyUtils.isEmpty(models)) return;

execute(new DefaultExecuteCallback<Void>() {
Expand Down
2 changes: 1 addition & 1 deletion sqlpal/src/org/sqlpal/crud/DeleteHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void deleteAll(@NotNull List<? extends DataSupport> models) throws SQLException
* @return 返回删除的行数
* @throws SQLException
*/
int deleteAll(@NotNull Class<? extends DataSupport> modelClass, @NotNull String... conditions) throws SQLException {
int deleteAll(@NotNull final Class<? extends DataSupport> modelClass, @NotNull final String... conditions) throws SQLException {
Integer row = new DataHandler().execute(new DefaultExecuteCallback<Integer>() {
@Override
public PreparedStatement onCreateStatement(Connection connection, DataSupport model) throws SQLException {
Expand Down
12 changes: 6 additions & 6 deletions sqlpal/src/org/sqlpal/crud/QueryHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ <T extends DataSupport> List<T> findAll(@NotNull Class<? extends DataSupport> mo
* @return 返回查询结果
* @throws SQLException 数据库错误
*/
<T extends DataSupport> List<T> find(@NotNull Class<? extends DataSupport> modelClass, @Nullable String[] columns,
@Nullable String[] conditions, @Nullable String[] orderBy,
int limit, int offset) throws SQLException {
<T extends DataSupport> List<T> find(@NotNull final Class<? extends DataSupport> modelClass, @Nullable final String[] columns,
@Nullable final String[] conditions, @Nullable final String[] orderBy,
final int limit, final int offset) throws SQLException {
List<T> list = new DataHandler().execute(new DefaultExecuteCallback<List<T>>() {
@Override
public PreparedStatement onCreateStatement(Connection connection, DataSupport model) throws SQLException {
Expand Down Expand Up @@ -98,7 +98,7 @@ public List<T> onExecute(PreparedStatement statement) throws SQLException {
return models;
}
});
return list == null ? Collections.emptyList() : list;
return list == null ? Collections.<T>emptyList() : list;
}

/**
Expand All @@ -110,8 +110,8 @@ public List<T> onExecute(PreparedStatement statement) throws SQLException {
* @return 返回查询结果
* @throws SQLException 数据库错误
*/
<T extends Number> T aggregate(@NotNull Class<? extends DataSupport> modelClass, @NotNull Class<T> columnType,
@Nullable String[] columns, @Nullable String[] conditions) throws SQLException {
<T extends Number> T aggregate(@NotNull final Class<? extends DataSupport> modelClass, @NotNull final Class<T> columnType,
@Nullable final String[] columns, @Nullable final String[] conditions) throws SQLException {
return new DataHandler().execute(new DefaultExecuteCallback<T>() {
@Override
public PreparedStatement onCreateStatement(Connection connection, DataSupport model) throws SQLException {
Expand Down
4 changes: 2 additions & 2 deletions sqlpal/src/org/sqlpal/crud/UpdateHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ void updateAll(@NotNull List<? extends DataSupport> models) throws SQLException
* @return 返回更新的行数
* @throws SQLException 数据库错误
*/
int updateAll(@NotNull DataSupport model, @NotNull String... conditions) throws SQLException {
int updateAll(@NotNull DataSupport model, @NotNull final String... conditions) throws SQLException {
if (EmptyUtils.isEmpty(conditions)) return 0;
List<ModelField> allFields = new ArrayList<>();
final List<ModelField> allFields = new ArrayList<>();
ModelManager.getAllFields(model, allFields);
if (EmptyUtils.isEmpty(allFields)) return 0;

Expand Down
24 changes: 16 additions & 8 deletions sqlpal/src/org/sqlpal/manager/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,14 @@ private static void listNotNullFields(@NotNull DataSupport model, @NotNull Field
* @param model 要读取的Model类
* @param allFields 在这里保存读取结果
*/
public static void getAllFields(@NotNull DataSupport model, @NotNull List<ModelField> allFields) {
public static void getAllFields(@NotNull DataSupport model, @NotNull final List<ModelField> allFields) {
allFields.clear();
listNotNullFields(model, (field, name, obj) -> allFields.add(new ModelField(name, obj)));
listNotNullFields(model, new FieldListCallback() {
@Override
public void onList(Field field, String name, Object obj) {
allFields.add(new ModelField(name, obj));
}
});
}

/**
Expand Down Expand Up @@ -163,14 +168,17 @@ public static void getPrimaryKeyFields(@NotNull DataSupport model, @NotNull List
* @param primaryKeyFields 这里保存主键字段列表
* @param notPrimaryKeyFields 这里保存非主键字段列表
*/
public static void getFields(@NotNull DataSupport model, @NotNull List<ModelField> primaryKeyFields, @NotNull List<ModelField> notPrimaryKeyFields) {
public static void getFields(@NotNull DataSupport model, @NotNull final List<ModelField> primaryKeyFields, @NotNull final List<ModelField> notPrimaryKeyFields) {
primaryKeyFields.clear();
notPrimaryKeyFields.clear();
listNotNullFields(model, (field, name, obj) -> {
if (isPrimaryKeyField(field)) {
primaryKeyFields.add(new ModelField(name, obj));
} else {
notPrimaryKeyFields.add(new ModelField(name, obj));
listNotNullFields(model, new FieldListCallback() {
@Override
public void onList(Field field, String name, Object obj) {
if (isPrimaryKeyField(field)) {
primaryKeyFields.add(new ModelField(name, obj));
} else {
notPrimaryKeyFields.add(new ModelField(name, obj));
}
}
});
}
Expand Down

0 comments on commit b735931

Please sign in to comment.