parent
e6da5230e9
commit
9212aabd95
@ -1,10 +0,0 @@ |
|||||||
package com.arialyy.aria.core.inf; |
|
||||||
|
|
||||||
public abstract class AbsHttpFileLenAdapter implements IHttpFileLenAdapter { |
|
||||||
|
|
||||||
@Override public Object clone() throws CloneNotSupportedException { |
|
||||||
//AbsHttpFileLenAdapter newAdapter = super.clone();
|
|
||||||
|
|
||||||
return super.clone(); |
|
||||||
} |
|
||||||
} |
|
@ -1,290 +1,291 @@ |
|||||||
/* |
/* |
||||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
* |
* |
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
* you may not use this file except in compliance with the License. |
* you may not use this file except in compliance with the License. |
||||||
* You may obtain a copy of the License at |
* You may obtain a copy of the License at |
||||||
* |
* |
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
* |
* |
||||||
* Unless required by applicable law or agreed to in writing, software |
* Unless required by applicable law or agreed to in writing, software |
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
* See the License for the specific language governing permissions and |
* See the License for the specific language governing permissions and |
||||||
* limitations under the License. |
* limitations under the License. |
||||||
*/ |
*/ |
||||||
|
|
||||||
package com.arialyy.aria.orm; |
package com.arialyy.aria.orm; |
||||||
|
|
||||||
import java.util.ArrayList; |
import java.util.ArrayList; |
||||||
import java.util.List; |
import java.util.List; |
||||||
|
|
||||||
/** |
/** |
||||||
* Created by lyy on 2015/11/2. 所有数据库实体父类 |
* Created by lyy on 2015/11/2. 所有数据库实体父类 |
||||||
*/ |
*/ |
||||||
public abstract class DbEntity { |
public abstract class DbEntity { |
||||||
private static final Object LOCK = new Object(); |
private static final Object LOCK = new Object(); |
||||||
protected long rowID = -1; |
protected long rowID = -1; |
||||||
|
|
||||||
protected DbEntity() { |
protected DbEntity() { |
||||||
|
|
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 查询关联数据 |
* 查询关联数据 |
||||||
* <code> |
* <code> |
||||||
* DbEntity.findRelationData(DGEntityWrapper.class, "downloadUrl=?", downloadUrl); |
* DbEntity.findRelationData(DGEntityWrapper.class, "downloadUrl=?", downloadUrl); |
||||||
* </code> |
* </code> |
||||||
* |
* |
||||||
* @param expression 查询条件 |
* @param expression 查询条件 |
||||||
*/ |
*/ |
||||||
public static <T extends AbsDbWrapper> List<T> findRelationData(Class<T> clazz, |
public static <T extends AbsDbWrapper> List<T> findRelationData(Class<T> clazz, |
||||||
String... expression) { |
String... expression) { |
||||||
return DelegateWrapper.getInstance().findRelationData(clazz, expression); |
return DelegateWrapper.getInstance().findRelationData(clazz, expression); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 分页查询关联数据 |
* 分页查询关联数据 |
||||||
* |
* |
||||||
* <code> |
* <code> |
||||||
* DbEntity.findRelationData(DGEntityWrapper.class, 0, 10, "downloadUrl=?", downloadUrl); |
* DbEntity.findRelationData(DGEntityWrapper.class, 0, 10, "downloadUrl=?", downloadUrl); |
||||||
* </code> |
* </code> |
||||||
* |
* |
||||||
* @param expression 查询条件 |
* @param expression 查询条件 |
||||||
* @param page 需要查询的页数,从1开始,如果page小于1 或 num 小于1,返回null |
* @param page 需要查询的页数,从1开始,如果page小于1 或 num 小于1,返回null |
||||||
* @param num 每页返回的数量 |
* @param num 每页返回的数量 |
||||||
* @return 没有数据返回null,如果页数大于总页数,返回null |
* @return 没有数据返回null,如果页数大于总页数,返回null |
||||||
*/ |
*/ |
||||||
public static <T extends AbsDbWrapper> List<T> findRelationData(Class<T> clazz, int page, int num, |
public static <T extends AbsDbWrapper> List<T> findRelationData(Class<T> clazz, int page, int num, |
||||||
String... expression) { |
String... expression) { |
||||||
if (page < 1 || num < 1) { |
if (page < 1 || num < 1) { |
||||||
return null; |
return null; |
||||||
} |
} |
||||||
return DelegateWrapper.getInstance().findRelationData(clazz, page, num, expression); |
return DelegateWrapper.getInstance().findRelationData(clazz, page, num, expression); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 检查某个字段的值是否存在 |
* 检查某个字段的值是否存在 |
||||||
* |
* |
||||||
* @param expression 字段和值"downloadPath=?" |
* @param expression 字段和值"downloadPath=?" |
||||||
* @return {@code true}该字段的对应的value已存在 |
* @return {@code true}该字段的对应的value已存在 |
||||||
*/ |
*/ |
||||||
public static boolean checkDataExist(Class clazz, String... expression) { |
public static boolean checkDataExist(Class clazz, String... expression) { |
||||||
return DelegateWrapper.getInstance().checkDataExist(clazz, expression); |
return DelegateWrapper.getInstance().checkDataExist(clazz, expression); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 清空表数据 |
* 清空表数据 |
||||||
*/ |
*/ |
||||||
public static <T extends DbEntity> void clean(Class<T> clazz) { |
public static <T extends DbEntity> void clean(Class<T> clazz) { |
||||||
DelegateWrapper.getInstance().clean(clazz); |
DelegateWrapper.getInstance().clean(clazz); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 直接执行sql语句 |
* 直接执行sql语句 |
||||||
*/ |
*/ |
||||||
public static void exeSql(String sql) { |
public static void exeSql(String sql) { |
||||||
DelegateWrapper.getInstance().exeSql(sql); |
DelegateWrapper.getInstance().exeSql(sql); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 查询所有数据 |
* 查询所有数据 |
||||||
* |
* |
||||||
* @return 没有数据返回null |
* @return 没有数据返回null |
||||||
*/ |
*/ |
||||||
public static <T extends DbEntity> List<T> findAllData(Class<T> clazz) { |
public static <T extends DbEntity> List<T> findAllData(Class<T> clazz) { |
||||||
return DelegateWrapper.getInstance().findAllData(clazz); |
return DelegateWrapper.getInstance().findAllData(clazz); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 查询第一条数据 |
* 查询第一条数据 |
||||||
*/ |
*/ |
||||||
public static <T extends DbEntity> T findFirst(Class<T> clazz) { |
public static <T extends DbEntity> T findFirst(Class<T> clazz) { |
||||||
List<T> list = findAllData(clazz); |
List<T> list = findAllData(clazz); |
||||||
return (list == null || list.size() == 0) ? null : list.get(0); |
return (list == null || list.size() == 0) ? null : list.get(0); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 查询一组数据 |
* 查询一组数据 |
||||||
* <code> |
* <code> |
||||||
* DbEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl); |
* DbEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl); |
||||||
* </code> |
* </code> |
||||||
* |
* |
||||||
* @return 没有数据返回null |
* @return 没有数据返回null |
||||||
*/ |
*/ |
||||||
public static <T extends DbEntity> List<T> findDatas(Class<T> clazz, String... expression) { |
public static <T extends DbEntity> List<T> findDatas(Class<T> clazz, String... expression) { |
||||||
return DelegateWrapper.getInstance().findData(clazz, expression); |
return DelegateWrapper.getInstance().findData(clazz, expression); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 分页查询数据 |
* 分页查询数据 |
||||||
* <code> |
* <code> |
||||||
* DbEntity.findFirst(DownloadEntity.class, 0, 10, "downloadUrl=?", downloadUrl); |
* DbEntity.findFirst(DownloadEntity.class, 0, 10, "downloadUrl=?", downloadUrl); |
||||||
* </code> |
* </code> |
||||||
* |
* |
||||||
* @param page 需要查询的页数,从1开始,如果page小于1 或 num 小于1,返回null |
* @param page 需要查询的页数,从1开始,如果page小于1 或 num 小于1,返回null |
||||||
* @param num 每页返回的数量 |
* @param num 每页返回的数量 |
||||||
* @return 没有数据返回null,如果页数大于总页数,返回null |
* @return 没有数据返回null,如果页数大于总页数,返回null |
||||||
*/ |
*/ |
||||||
public static <T extends DbEntity> List<T> findDatas(Class<T> clazz, int page, int num, |
public static <T extends DbEntity> List<T> findDatas(Class<T> clazz, int page, int num, |
||||||
String... expression) { |
String... expression) { |
||||||
if (page < 1 || num < 1) { |
if (page < 1 || num < 1) { |
||||||
return null; |
return null; |
||||||
} |
} |
||||||
return DelegateWrapper.getInstance().findData(clazz, page, num, expression); |
return DelegateWrapper.getInstance().findData(clazz, page, num, expression); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 模糊查询一组数据 |
* 模糊查询一组数据 |
||||||
* <code> |
* <code> |
||||||
* DbEntity.findDataByFuzzy(DownloadEntity.class, "downloadUrl like http://"); |
* DbEntity.findDataByFuzzy(DownloadEntity.class, "downloadUrl like http://"); |
||||||
* </code> |
* </code> |
||||||
* |
* |
||||||
* @return 没有数据返回null |
* @return 没有数据返回null |
||||||
*/ |
*/ |
||||||
public static <T extends DbEntity> List<T> findDataByFuzzy(Class<T> clazz, String conditions) { |
public static <T extends DbEntity> List<T> findDataByFuzzy(Class<T> clazz, String conditions) { |
||||||
return DelegateWrapper.getInstance().findDataByFuzzy(clazz, conditions); |
return DelegateWrapper.getInstance().findDataByFuzzy(clazz, conditions); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 模糊查询一组数据 |
* 模糊查询一组数据 |
||||||
* <code> |
* <code> |
||||||
* DbEntity.findDataByFuzzy(DownloadEntity.class, 0, 10, "downloadUrl like http://"); |
* DbEntity.findDataByFuzzy(DownloadEntity.class, 0, 10, "downloadUrl like http://"); |
||||||
* </code> |
* </code> |
||||||
* |
* |
||||||
* @param page 需要查询的页数,从1开始,如果page小于1 或 num 小于1,返回null |
* @param page 需要查询的页数,从1开始,如果page小于1 或 num 小于1,返回null |
||||||
* @param num 每页返回的数量 |
* @param num 每页返回的数量 |
||||||
* @return 没有数据返回null,如果页数大于总页数,返回null |
* @return 没有数据返回null,如果页数大于总页数,返回null |
||||||
*/ |
*/ |
||||||
public static <T extends DbEntity> List<T> findDataByFuzzy(Class<T> clazz, int page, int num, |
public static <T extends DbEntity> List<T> findDataByFuzzy(Class<T> clazz, int page, int num, |
||||||
String conditions) { |
String conditions) { |
||||||
return DelegateWrapper.getInstance().findDataByFuzzy(clazz, page, num, conditions); |
return DelegateWrapper.getInstance().findDataByFuzzy(clazz, page, num, conditions); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 查询一行数据 |
* 查询一行数据 |
||||||
* <code> |
* <code> |
||||||
* DbEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl); |
* DbEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl); |
||||||
* </code> |
* </code> |
||||||
* |
* |
||||||
* @return 没有数据返回null |
* @return 没有数据返回null |
||||||
*/ |
*/ |
||||||
public static <T extends DbEntity> T findFirst(Class<T> clazz, String... expression) { |
public static <T extends DbEntity> T findFirst(Class<T> clazz, String... expression) { |
||||||
DelegateWrapper util = DelegateWrapper.getInstance(); |
DelegateWrapper util = DelegateWrapper.getInstance(); |
||||||
List<T> datas = util.findData(clazz, expression); |
List<T> datas = util.findData(clazz, expression); |
||||||
return datas == null ? null : datas.size() > 0 ? datas.get(0) : null; |
return datas == null ? null : datas.size() > 0 ? datas.get(0) : null; |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 插入多条数据 |
* 插入多条数据 |
||||||
*/ |
*/ |
||||||
public static void insertManyData(List<DbEntity> entities) { |
public static <T extends DbEntity> void insertManyData(List<T> entities) { |
||||||
checkListData(entities); |
checkListData(entities); |
||||||
DelegateWrapper.getInstance().insertManyData(entities); |
DelegateWrapper.getInstance().insertManyData(entities); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 修改多条数据 |
* 修改多条数据 |
||||||
*/ |
*/ |
||||||
public static void updateManyData(List<DbEntity> entities) { |
public static <T extends DbEntity> void updateManyData(List<T> entities) { |
||||||
checkListData(entities); |
checkListData(entities); |
||||||
DelegateWrapper.getInstance().updateManyData(entities); |
DelegateWrapper.getInstance().updateManyData(entities); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 保存多条数据,通过rowID来判断记录存在以否,如果数据库已有记录,则更新该记录;如果数据库中没有记录,则保存该记录 |
* 保存多条数据,通过rowID来判断记录存在以否,如果数据库已有记录,则更新该记录;如果数据库中没有记录,则保存该记录 |
||||||
*/ |
*/ |
||||||
public static <T extends DbEntity> void saveAll(List<T> entities) { |
public static <T extends DbEntity> void saveAll(List<T> entities) { |
||||||
checkListData(entities); |
checkListData(entities); |
||||||
List<DbEntity> insertD = new ArrayList<>(); |
List<T> insertD = new ArrayList<>(); |
||||||
List<DbEntity> updateD = new ArrayList<>(); |
List<T> updateD = new ArrayList<>(); |
||||||
DelegateWrapper wrapper = DelegateWrapper.getInstance(); |
DelegateWrapper wrapper = DelegateWrapper.getInstance(); |
||||||
for (DbEntity entity : entities) { |
for (T entity : entities) { |
||||||
if (entity.rowID == -1) { |
if (entity.rowID == -1) { |
||||||
insertD.add(entity); |
insertD.add(entity); |
||||||
continue; |
continue; |
||||||
} |
} |
||||||
if (wrapper.isExist(entity.getClass(), entity.rowID)) { |
if (wrapper.isExist(entity.getClass(), entity.rowID)) { |
||||||
updateD.add(entity); |
updateD.add(entity); |
||||||
} else { |
} else { |
||||||
insertD.add(entity); |
insertD.add(entity); |
||||||
} |
} |
||||||
} |
} |
||||||
if (!insertD.isEmpty()) { |
if (!insertD.isEmpty()) { |
||||||
wrapper.insertManyData(insertD); |
wrapper.insertManyData(insertD); |
||||||
} else { |
} |
||||||
wrapper.updateManyData(updateD); |
if (!updateD.isEmpty()) { |
||||||
} |
wrapper.updateManyData(updateD); |
||||||
} |
} |
||||||
|
} |
||||||
/** |
|
||||||
* 检查批量操作的列表数据,如果数据为空,抛出{@link NullPointerException} |
/** |
||||||
*/ |
* 检查批量操作的列表数据,如果数据为空,抛出{@link NullPointerException} |
||||||
private static <T extends DbEntity> void checkListData(List<T> entities) { |
*/ |
||||||
if (entities == null || entities.isEmpty()) { |
private static <T extends DbEntity> void checkListData(List<T> entities) { |
||||||
throw new NullPointerException("列表数据为空"); |
if (entities == null || entities.isEmpty()) { |
||||||
} |
throw new NullPointerException("列表数据为空"); |
||||||
} |
} |
||||||
|
} |
||||||
/** |
|
||||||
* 删除当前数据 |
/** |
||||||
*/ |
* 删除当前数据 |
||||||
public void deleteData() { |
*/ |
||||||
deleteData(getClass(), "rowid=?", rowID + ""); |
public void deleteData() { |
||||||
} |
deleteData(getClass(), "rowid=?", rowID + ""); |
||||||
|
} |
||||||
/** |
|
||||||
* 根据条件删除数据 |
/** |
||||||
* <code> |
* 根据条件删除数据 |
||||||
* DbEntity.deleteData(DownloadEntity.class, "downloadUrl=?", downloadUrl); |
* <code> |
||||||
* </code> |
* DbEntity.deleteData(DownloadEntity.class, "downloadUrl=?", downloadUrl); |
||||||
*/ |
* </code> |
||||||
public static <T extends DbEntity> void deleteData(Class<T> clazz, String... expression) { |
*/ |
||||||
DelegateWrapper util = DelegateWrapper.getInstance(); |
public static <T extends DbEntity> void deleteData(Class<T> clazz, String... expression) { |
||||||
util.delData(clazz, expression); |
DelegateWrapper util = DelegateWrapper.getInstance(); |
||||||
} |
util.delData(clazz, expression); |
||||||
|
} |
||||||
/** |
|
||||||
* 修改数据 |
/** |
||||||
*/ |
* 修改数据 |
||||||
public void update() { |
*/ |
||||||
DelegateWrapper.getInstance().updateData(this); |
public void update() { |
||||||
} |
DelegateWrapper.getInstance().updateData(this); |
||||||
|
} |
||||||
/** |
|
||||||
* 保存自身,如果表中已经有数据,则更新数据,否则插入数据 只有 target中checkEntity成功后才能保存,创建实体部分也不允许保存 |
/** |
||||||
*/ |
* 保存自身,如果表中已经有数据,则更新数据,否则插入数据 只有 target中checkEntity成功后才能保存,创建实体部分也不允许保存 |
||||||
public void save() { |
*/ |
||||||
synchronized (LOCK) { |
public void save() { |
||||||
if (thisIsExist()) { |
synchronized (LOCK) { |
||||||
update(); |
if (thisIsExist()) { |
||||||
} else { |
update(); |
||||||
insert(); |
} else { |
||||||
} |
insert(); |
||||||
} |
} |
||||||
} |
} |
||||||
|
} |
||||||
/** |
|
||||||
* 查找数据在表中是否存在 |
/** |
||||||
*/ |
* 查找数据在表中是否存在 |
||||||
private boolean thisIsExist() { |
*/ |
||||||
DelegateWrapper util = DelegateWrapper.getInstance(); |
private boolean thisIsExist() { |
||||||
return rowID != -1 && util.isExist(getClass(), rowID); |
DelegateWrapper util = DelegateWrapper.getInstance(); |
||||||
} |
return rowID != -1 && util.isExist(getClass(), rowID); |
||||||
|
} |
||||||
/** |
|
||||||
* 表是否存在 |
/** |
||||||
* |
* 表是否存在 |
||||||
* @return {@code true} 存在 |
* |
||||||
*/ |
* @return {@code true} 存在 |
||||||
public static boolean tableExists(Class<DbEntity> clazz) { |
*/ |
||||||
return DelegateWrapper.getInstance().tableExists(clazz); |
public static boolean tableExists(Class<DbEntity> clazz) { |
||||||
} |
return DelegateWrapper.getInstance().tableExists(clazz); |
||||||
|
} |
||||||
/** |
|
||||||
* 插入数据,只有 target中checkEntity成功后才能插入,创建实体部分也不允许操作 |
/** |
||||||
*/ |
* 插入数据,只有 target中checkEntity成功后才能插入,创建实体部分也不允许操作 |
||||||
public void insert() { |
*/ |
||||||
DelegateWrapper.getInstance().insertData(this); |
public void insert() { |
||||||
} |
DelegateWrapper.getInstance().insertData(this); |
||||||
|
} |
||||||
} |
} |
Loading…
Reference in new issue