public abstract class Model<M extends Model> extends Object implements com.litongjava.model.db.IRow<M>, Serializable
A clever person solves a problem. A wise person avoids it A stupid person makes it.
| 限定符和类型 | 字段和说明 |
|---|---|
static int |
FILTER_BY_SAVE |
static int |
FILTER_BY_UPDATE |
| 构造器和说明 |
|---|
Model() |
| 限定符和类型 | 方法和说明 |
|---|---|
String[] |
_getAttrNames()
Return attribute names of this model.
|
Map<String,Object> |
_getAttrs()
Return attribute Map.
|
Set<Map.Entry<String,Object>> |
_getAttrsEntrySet()
Return attribute Set.
|
Object[] |
_getAttrValues()
Return attribute values of this model.
|
protected Set<String> |
_getModifyFlag() |
protected abstract String |
_getPrimaryKey() |
protected Config |
_getReadConfig() |
protected Table |
_getTable() |
protected abstract String |
_getTableName() |
protected Class<? extends Model> |
_getUsefulClass() |
protected Config |
_getWriteConfig() |
M |
_setAttrs(M model)
Set attributes with other model.
|
M |
_setAttrs(Map<String,Object> attrs)
Set attributes with Map.
|
M |
_setOrPut(Map<String,Object> map) |
M |
_setOrPut(Model model) |
M |
clear()
Remove all attributes of this model.
|
M |
dao()
将本 model 对象转化为线程安全的 dao 对象.
|
boolean |
delete()
Delete model.
|
boolean |
deleteByField(String field,
String value) |
protected boolean |
deleteByField(Table table,
String field,
String value) |
boolean |
deleteById()
Delete model.
|
boolean |
deleteById(Object idValue)
Delete model by id.
|
protected boolean |
deleteById(Table table,
Object... idValues) |
boolean |
deleteByIds(Object... idValues)
Delete model by composite id values.
|
protected com.litongjava.model.page.Page<M> |
doPaginate(int pageNumber,
int pageSize,
Boolean isGroupBySql,
String select,
String sqlExceptSelect,
Object... paras) |
protected com.litongjava.model.page.Page<M> |
doPaginateByCache(String cacheName,
Object key,
int pageNumber,
int pageSize,
Boolean isGroupBySql,
String select,
String sqlExceptSelect,
Object... paras) |
protected com.litongjava.model.page.Page<M> |
doPaginateByFullSql(Config config,
Connection conn,
int pageNumber,
int pageSize,
Boolean isGroupBySql,
String totalRowSql,
StringBuilder findSql,
Object... paras) |
protected com.litongjava.model.page.Page<M> |
doPaginateByFullSql(int pageNumber,
int pageSize,
Boolean isGroupBySql,
String totalRowSql,
String findSql,
Object... paras) |
void |
each(java.util.function.Function<M,Boolean> func,
String sql,
Object... paras)
迭代处理每一个查询出来的 Model 对象
例子:
Db.each(model -> {
// 处理 model 的代码在此
// 返回 true 继续循环处理下一条数据,返回 false 立即终止循环
return true;
}, sql, paras);
|
boolean |
equals(Object o) |
boolean |
exists(String fields,
Object... paras) |
boolean |
existsById(Object... paras) |
boolean |
existsBySql(String sql,
Object... paras) |
protected void |
filter(int filterBy)
filter () 方法将被 save()、update() 两个方法回调,
子类可通过覆盖此方法,实现类似于过滤 XSS 攻击脚本的功能
|
List<M> |
find() |
protected List<M> |
find(Config config,
Connection conn,
String sql,
Object... paras)
Find model.
|
protected List<M> |
find(Config config,
Connection conn,
String tableName,
String columns,
Row record) |
protected List<M> |
find(Config config,
String sql,
Object... paras) |
List<M> |
find(Config config,
String tableName,
Row record) |
List<M> |
find(com.litongjava.db.SqlPara sqlPara) |
List<M> |
find(String sql) |
List<M> |
find(String sql,
Object... paras)
Find model.
|
List<M> |
find(String tableName,
String columns,
Row record) |
List<M> |
findAll() |
List<M> |
findByCache(String cacheName,
Object key,
String sql) |
List<M> |
findByCache(String cacheName,
Object key,
String sql,
Object... paras)
Find model by cache.
|
M |
findById(Object idValue)
Find model by id.
|
M |
findByIdLoadColumns(Object[] idValues,
String columns)
Find model by composite id values and load specific columns only.
|
M |
findByIdLoadColumns(Object idValue,
String columns)
Find model by id and load specific columns only.
|
M |
findByIds(Object... idValues)
Find model by composite id values.
|
M |
findColumnsFirst(String columns) |
M |
findFirst() |
M |
findFirst(com.litongjava.db.SqlPara sqlPara) |
M |
findFirst(String sql) |
M |
findFirst(String sql,
Object... paras)
Find first model.
|
M |
findFirst(String tableName,
Row record) |
M |
findFirst(String tableName,
String columns,
Row record) |
M |
findFirstByCache(String cacheName,
Object key,
String sql) |
M |
findFirstByCache(String cacheName,
Object key,
String sql,
Object... paras)
Find first model by cache.
|
M |
fromRow(Row row) |
<T> T |
get(String attr)
Get attribute of any mysql type
|
<T> T |
get(String attr,
Object defaultValue)
Get attribute of any mysql type.
|
BigDecimal |
getBigDecimal(String attr)
Get attribute of mysql type: decimal, numeric
|
BigInteger |
getBigInteger(String attr)
Get attribute of mysql type: unsigned bigint
|
Boolean |
getBoolean(String attr)
Get attribute of mysql type: bit, tinyint(1)
|
Byte |
getByte(String attr) |
byte[] |
getBytes(String attr)
Get attribute of mysql type: binary, varbinary, tinyblob, blob, mediumblob, longblob
|
Date |
getDate(String attr)
Get attribute of mysql type: date, year
|
Double |
getDouble(String attr)
Get attribute of mysql type: real, double
|
Float |
getFloat(String attr)
Get attribute of mysql type: float
|
Integer |
getInt(String attr)
Get attribute of mysql type: int, integer, tinyint(n) n > 1, smallint, mediumint
|
java.time.LocalDateTime |
getLocalDateTime(String attr) |
Long |
getLong(String attr)
Get attribute of mysql type: bigint, unsign int
|
Number |
getNumber(String attr)
Get attribute of any type that extends from Number
|
Short |
getShort(String attr) |
String |
getSql(String key) |
com.litongjava.db.SqlPara |
getSqlPara(String key,
Map data)
可以在模板中利用 Model 自身的属性参与动态生成 sql,例如:
select * from user where nickName = #(nickName)
new Account().setNickName("James").getSqlPara(...)
|
com.litongjava.db.SqlPara |
getSqlPara(String key,
Model model) |
com.litongjava.db.SqlPara |
getSqlPara(String key,
Object... paras) |
com.litongjava.db.SqlPara |
getSqlParaByString(String content,
Map data) |
com.litongjava.db.SqlPara |
getSqlParaByString(String content,
Model model) |
com.litongjava.db.SqlPara |
getSqlParaByString(String content,
Object... paras) |
String |
getStr(String attr)
Get attribute of mysql type: varchar, char, enum, set, text, tinytext, mediumtext, longtext
|
Time |
getTime(String attr)
Get attribute of mysql type: time
|
Timestamp |
getTimestamp(String attr)
Get attribute of mysql type: timestamp, datetime
|
int |
hashCode() |
M |
keep(String... attrs)
Keep attributes of this model and remove other attributes.
|
M |
keep(String attr)
Keep attribute of this model and remove other attributes.
|
com.litongjava.model.page.Page<M> |
paginate(int pageNumber,
int pageSize,
boolean isGroupBySql,
com.litongjava.db.SqlPara sqlPara) |
com.litongjava.model.page.Page<M> |
paginate(int pageNumber,
int pageSize,
boolean isGroupBySql,
String select,
String sqlExceptSelect,
Object... paras)
指定分页 sql 最外层以是否含有 group by 语句
举例:
paginate(1, 10, true, "select *", "from user where id>?
|
com.litongjava.model.page.Page<M> |
paginate(int pageNumber,
int pageSize,
com.litongjava.db.SqlPara sqlPara) |
com.litongjava.model.page.Page<M> |
paginate(int pageNumber,
int pageSize,
String select,
String sqlExceptSelect) |
com.litongjava.model.page.Page<M> |
paginate(int pageNumber,
int pageSize,
String select,
String sqlExceptSelect,
Object... paras)
Paginate.
|
com.litongjava.model.page.Page<M> |
paginateByCache(String cacheName,
Object key,
int pageNumber,
int pageSize,
boolean isGroupBySql,
String select,
String sqlExceptSelect,
Object... paras) |
com.litongjava.model.page.Page<M> |
paginateByCache(String cacheName,
Object key,
int pageNumber,
int pageSize,
String select,
String sqlExceptSelect) |
com.litongjava.model.page.Page<M> |
paginateByCache(String cacheName,
Object key,
int pageNumber,
int pageSize,
String select,
String sqlExceptSelect,
Object... paras)
Paginate by cache.
|
com.litongjava.model.page.Page<M> |
paginateByFullSql(int pageNumber,
int pageSize,
boolean isGroupBySql,
String totalRowSql,
String findSql,
Object... paras) |
com.litongjava.model.page.Page<M> |
paginateByFullSql(int pageNumber,
int pageSize,
String totalRowSql,
String findSql,
Object... paras) |
M |
put(Map<String,Object> map)
Put map to the model without check attribute name.
|
M |
put(Model model)
Put other model to the model without check attribute name.
|
M |
put(Row row)
Put record to the model without check attribute name.
|
M |
put(String key,
Object value)
Put key value pair to the model without check attribute name.
|
<T> List<T> |
query(String sql,
Object... paras) |
<T> T |
queryFirst(String sql,
Object... paras)
Execute sql query and return the first result.
|
Integer |
queryInt(String sql,
Object... paras) |
Long |
queryLong(String sql,
Object... paras) |
String |
queryString(String sql,
Object... paras) |
M |
remove(String... attrs)
Remove attributes of this model.
|
M |
remove(String attr)
Remove attribute of this model.
|
M |
removeNullValueAttrs()
Remove attributes if it is null.
|
boolean |
save()
Save model.
|
M |
set(String attr,
Object value)
Set attribute to model.
|
M |
setOrPut(String attrOrNot,
Object value)
如果 attrOrNot 是表中的字段则调用 set(...)
|
int |
size() |
DaoTemplate<M> |
template(String key,
Map data)
使用 sql 模板进行查询,可以省去 getSqlPara(...)
|
DaoTemplate<M> |
template(String key,
Model model) |
DaoTemplate<M> |
template(String key,
Object... paras)
使用 sql 模板进行查询,可以省去 getSqlPara(...)
|
DaoTemplate<M> |
templateByString(String content,
Map data)
使用字符串变量作为 sql 模板进行查询,可省去外部 sql 文件来使用
sql 模板功能
例子:
String sql = "select * from blog where id = #para(id)";
dao.templateByString(sql, Kv.by("id", 123)).find();
|
DaoTemplate<M> |
templateByString(String content,
Model model) |
DaoTemplate<M> |
templateByString(String content,
Object... paras)
使用字符串变量作为 sql 模板进行查询,可省去外部 sql 文件来使用
sql 模板功能
例子:
String sql = "select * from blog where id = #para(0)";
dao.templateByString(sql, 123).find();
|
Map<String,Object> |
toMap() |
Row |
toRow()
Convert model to record.
|
String |
toString() |
boolean |
update()
Update model.
|
M |
use(String configName)
Switching data source, dialect and all config by configName
|
public static final int FILTER_BY_SAVE
public static final int FILTER_BY_UPDATE
protected abstract String _getPrimaryKey()
protected abstract String _getTableName()
public M dao()
强烈建议通过 static 修饰过的 dao 对象都要调用一次 dao() 方法,
以免新手误用造成线程安全问题,示例如下:
public class UserService {
private static User dao = new User().dao();
public User getUserById(long userId) {
return dao.findFirst("select * from `user` where id = ? limit 1", userId);
}
}
protected void filter(int filterBy)
filterBy - 0 表示当前正被 save() 调用, 1 表示当前正被 update() 调用public Map<String,Object> _getAttrs()
Danger! The update method will ignore the attribute if you change it directly. You must use set method to change attribute that update method can handle it.
public String[] _getAttrNames()
public Object[] _getAttrValues()
public M _setAttrs(M model)
model - the Modelpublic M _setAttrs(Map<String,Object> attrs)
attrs - attributes of this modelprotected Config _getWriteConfig()
protected Config _getReadConfig()
protected Table _getTable()
public M set(String attr, Object value)
set 在接口中 com.litongjava.model.db.IRow<M extends Model>attr - the attribute name of the modelvalue - the value of the attributeActiveRecordException - if the attribute is not exists of the modelpublic M put(String key, Object value)
public M setOrPut(String attrOrNot, Object value)
public Row toRow()
public <T> T get(String attr)
public <T> T get(String attr, Object defaultValue)
public String getStr(String attr)
public Integer getInt(String attr)
public BigInteger getBigInteger(String attr)
public java.time.LocalDateTime getLocalDateTime(String attr)
public Timestamp getTimestamp(String attr)
public BigDecimal getBigDecimal(String attr)
public byte[] getBytes(String attr)
public com.litongjava.model.page.Page<M> paginate(int pageNumber, int pageSize, String select, String sqlExceptSelect, Object... paras)
pageNumber - the page numberpageSize - the page sizeselect - the select part of the sql statementsqlExceptSelect - the sql statement excluded select partparas - the parameters of sqlpublic com.litongjava.model.page.Page<M> paginate(int pageNumber, int pageSize, String select, String sqlExceptSelect)
public com.litongjava.model.page.Page<M> paginate(int pageNumber, int pageSize, boolean isGroupBySql, String select, String sqlExceptSelect, Object... paras)
举例: paginate(1, 10, true, "select *", "from user where id>? group by age", 123);
protected com.litongjava.model.page.Page<M> doPaginate(int pageNumber, int pageSize, Boolean isGroupBySql, String select, String sqlExceptSelect, Object... paras)
protected com.litongjava.model.page.Page<M> doPaginateByFullSql(Config config, Connection conn, int pageNumber, int pageSize, Boolean isGroupBySql, String totalRowSql, StringBuilder findSql, Object... paras) throws Exception
Exceptionprotected com.litongjava.model.page.Page<M> doPaginateByFullSql(int pageNumber, int pageSize, Boolean isGroupBySql, String totalRowSql, String findSql, Object... paras)
public com.litongjava.model.page.Page<M> paginateByFullSql(int pageNumber, int pageSize, String totalRowSql, String findSql, Object... paras)
public com.litongjava.model.page.Page<M> paginateByFullSql(int pageNumber, int pageSize, boolean isGroupBySql, String totalRowSql, String findSql, Object... paras)
public boolean save()
public boolean deleteById()
public boolean delete()
public boolean deleteById(Object idValue)
idValue - the id value of the modelpublic boolean deleteByIds(Object... idValues)
idValues - the composite id values of the modelpublic boolean update()
protected List<M> find(Config config, Connection conn, String sql, Object... paras)
protected List<M> find(Config config, Connection conn, String tableName, String columns, Row record)
public List<M> find(String sql, Object... paras)
sql - an SQL statement that may contain one or more '?' IN parameter placeholdersparas - the parameters of sqlpublic List<M> find(String sql)
find(String, Object...)public M findFirst(String sql, Object... paras)
sql - an SQL statement that may contain one or more '?' IN parameter placeholdersparas - the parameters of sqlpublic M findFirst(String sql)
sql - an SQL statementfindFirst(String, Object...)public M findFirst()
public <T> T queryFirst(String sql, Object... paras)
sql - an SQL statement that may contain one or more '?' IN parameter
placeholdersparas - the parameters of sqlpublic boolean existsById(Object... paras)
public M findById(Object idValue)
Example: User user = User.dao.findById(123);
idValue - the id value of the modelpublic M findByIds(Object... idValues)
Example: User user = User.dao.findByIds(123, 456);
idValues - the composite id values of the modelpublic M findByIdLoadColumns(Object idValue, String columns)
Example: User user = User.dao.findByIdLoadColumns(123, "name, age");
idValue - the id value of the modelcolumns - the specific columns to loadpublic M findByIdLoadColumns(Object[] idValues, String columns)
Example:
User user = User.dao.findByIdLoadColumns(new Object[]{123, 456}, "name, age");
idValues - the composite id values of the modelcolumns - the specific columns to loadpublic M remove(String attr)
attr - the attribute name of the modelpublic M remove(String... attrs)
attrs - the attribute names of the modelpublic M removeNullValueAttrs()
public M keep(String... attrs)
attrs - the attribute names of the modelpublic M keep(String attr)
attr - the attribute name of the modelpublic M clear()
public List<M> findByCache(String cacheName, Object key, String sql, Object... paras)
cacheName - the cache namekey - the key used to get data from cachefind(String, Object...)public M findFirstByCache(String cacheName, Object key, String sql, Object... paras)
cacheName - the cache namekey - the key used to get data from cachesql - an SQL statement that may contain one or more '?' IN parameter placeholdersparas - the parameters of sqlfindFirst(String, Object...)public com.litongjava.model.page.Page<M> paginateByCache(String cacheName, Object key, int pageNumber, int pageSize, String select, String sqlExceptSelect, Object... paras)
cacheName - the cache namekey - the key used to get date from cachepaginate(int, int, String, String, Object...)public com.litongjava.model.page.Page<M> paginateByCache(String cacheName, Object key, int pageNumber, int pageSize, String select, String sqlExceptSelect)
public com.litongjava.model.page.Page<M> paginateByCache(String cacheName, Object key, int pageNumber, int pageSize, boolean isGroupBySql, String select, String sqlExceptSelect, Object... paras)
protected com.litongjava.model.page.Page<M> doPaginateByCache(String cacheName, Object key, int pageNumber, int pageSize, Boolean isGroupBySql, String select, String sqlExceptSelect, Object... paras)
public com.litongjava.db.SqlPara getSqlPara(String key, Map data)
public com.litongjava.db.SqlPara getSqlParaByString(String content, Object... paras)
public M findFirst(com.litongjava.db.SqlPara sqlPara)
public com.litongjava.model.page.Page<M> paginate(int pageNumber, int pageSize, com.litongjava.db.SqlPara sqlPara)
public com.litongjava.model.page.Page<M> paginate(int pageNumber, int pageSize, boolean isGroupBySql, com.litongjava.db.SqlPara sqlPara)
public void each(java.util.function.Function<M,Boolean> func, String sql, Object... paras)
例子:
Db.each(model -> {
// 处理 model 的代码在此
// 返回 true 继续循环处理下一条数据,返回 false 立即终止循环
return true;
}, sql, paras);
public DaoTemplate<M> template(String key, Map data)
例子:
dao.template("blog.find", Kv.by("id", 123)).find();
public DaoTemplate<M> template(String key, Object... paras)
例子:
dao.template("blog.find", 123).find();
public DaoTemplate<M> template(String key, Model model)
public DaoTemplate<M> templateByString(String content, Map data)
例子:
String sql = "select * from blog where id = #para(id)";
dao.templateByString(sql, Kv.by("id", 123)).find();
public DaoTemplate<M> templateByString(String content, Object... paras)
例子: String sql = "select * from blog where id = #para(0)"; dao.templateByString(sql, 123).find();
public DaoTemplate<M> templateByString(String content, Model model)
Copyright © 2025. All rights reserved.