public abstract class Model<M extends Model> extends Object implements Serializable
A clever person solves a problem. A wise person avoids it. A stupid person makes it.
Modifier and Type | Field and Description |
---|---|
static int |
FILTER_BY_SAVE |
static int |
FILTER_BY_UPDATE |
Constructor and Description |
---|
Model() |
Modifier and Type | Method and Description |
---|---|
String[] |
_getAttrNames()
Return attribute names of this model.
|
protected 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 Config |
_getConfig() |
protected Set<String> |
_getModifyFlag() |
protected Table |
_getTable() |
protected Class<? extends Model> |
_getUsefulClass() |
M |
_setAttrs(M model)
Set attributes with other model.
|
M |
_setAttrs(Map<String,Object> attrs)
Set attributes with Map.
|
M |
clear()
Remove all attributes of this model.
|
M |
dao()
将本 model 对象转化为线程安全的 dao 对象.
|
boolean |
delete()
Delete model.
|
boolean |
deleteById(Object... idValues)
Delete model by composite id values.
|
boolean |
deleteById(Object idValue)
Delete model by id.
|
boolean |
equals(Object o) |
protected void |
filter(int filterBy)
filter () 方法将被 save()、update() 两个方法回调,
子类可通过覆盖此方法,实现类似于过滤 XSS 攻击脚本的功能
|
List<M> |
find(SqlPara sqlPara) |
List<M> |
find(String sql) |
List<M> |
find(String sql,
Object... paras)
Find model.
|
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... idValues)
Find model by composite id values.
|
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 |
findFirst(SqlPara sqlPara) |
M |
findFirst(String sql) |
M |
findFirst(String sql,
Object... paras)
Find first model.
|
M |
findFirstByCache(String cacheName,
Object key,
String sql) |
M |
findFirstByCache(String cacheName,
Object key,
String sql,
Object... paras)
Find first model by cache.
|
<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
|
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) |
SqlPara |
getSqlPara(String key,
Map data) |
SqlPara |
getSqlPara(String key,
Model model)
可以在模板中利用 Model 自身的属性参与动态生成 sql,例如:
select * from user where nickName = #(nickName)
new Account().setNickName("James").getSqlPara(...)
注意:由于 dao 对象上的 attrs 不允许读写,不要调用其 getSqlPara(String) 方法
public SqlPara getSqlPara(String key) {
return getSqlPara(key, this.attrs);
}
|
SqlPara |
getSqlPara(String key,
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.
|
Page<M> |
paginate(int pageNumber,
int pageSize,
boolean isGroupBySql,
String select,
String sqlExceptSelect,
Object... paras)
指定分页 sql 最外层以是否含有 group by 语句
|
Page<M> |
paginate(int pageNumber,
int pageSize,
SqlPara sqlPara) |
Page<M> |
paginate(int pageNumber,
int pageSize,
String select,
String sqlExceptSelect) |
Page<M> |
paginate(int pageNumber,
int pageSize,
String select,
String sqlExceptSelect,
Object... paras)
Paginate.
|
Page<M> |
paginateByCache(String cacheName,
Object key,
int pageNumber,
int pageSize,
boolean isGroupBySql,
String select,
String sqlExceptSelect,
Object... paras) |
Page<M> |
paginateByCache(String cacheName,
Object key,
int pageNumber,
int pageSize,
String select,
String sqlExceptSelect) |
Page<M> |
paginateByCache(String cacheName,
Object key,
int pageNumber,
int pageSize,
String select,
String sqlExceptSelect,
Object... paras)
Paginate by cache.
|
Page<M> |
paginateByFullSql(int pageNumber,
int pageSize,
boolean isGroupBySql,
String totalRowSql,
String findSql,
Object... paras) |
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(Record record)
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.
|
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.
|
String |
toJson()
Return json string of this model.
|
Record |
toRecord()
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
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() 调用protected 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 _getConfig()
protected Table _getTable()
public M set(String attr, Object value)
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 Record toRecord()
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 Timestamp getTimestamp(String attr)
public BigDecimal getBigDecimal(String attr)
public byte[] getBytes(String attr)
public 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 Page<M> paginate(int pageNumber, int pageSize, String select, String sqlExceptSelect)
public 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);
public Page<M> paginateByFullSql(int pageNumber, int pageSize, String totalRowSql, String findSql, Object... paras)
public Page<M> paginateByFullSql(int pageNumber, int pageSize, boolean isGroupBySql, String totalRowSql, String findSql, Object... paras)
public boolean save()
public boolean delete()
public boolean deleteById(Object idValue)
idValue
- the id value of the modelpublic boolean deleteById(Object... idValues)
idValues
- the composite id values of the modelpublic boolean update()
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 findById(Object idValue)
Example: User user = User.dao.findById(123);
idValue
- the id value of the modelpublic M findById(Object... idValues)
Example: User user = User.dao.findById(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 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 Page<M> paginateByCache(String cacheName, Object key, int pageNumber, int pageSize, String select, String sqlExceptSelect)
public Page<M> paginateByCache(String cacheName, Object key, int pageNumber, int pageSize, boolean isGroupBySql, String select, String sqlExceptSelect, Object... paras)
public String toJson()
public SqlPara getSqlPara(String key, Model model)
Copyright © 2018. All rights reserved.