public class DaoTemplate extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
protected String |
primaryKeyField
本表的主键字段,请在子类中覆盖或构造方法中指定,默认为id
|
protected SqlRunner |
runner |
protected String |
tableName
表名
|
| 构造器和说明 |
|---|
DaoTemplate(String tableName)
构造,此构造需要自定义SqlRunner,主键默认为id
|
DaoTemplate(String tableName,
String primaryKeyField)
构造,此构造需要自定义SqlRunner
|
DaoTemplate(String tableName,
String primaryKeyField,
DataSource ds)
构造
|
DaoTemplate(String tableName,
String primaryKeyField,
SqlRunner runner)
构造
|
| 限定符和类型 | 方法和说明 |
|---|---|
int |
add(Entity entity)
添加
|
Long |
addForGeneratedKey(Entity entity)
添加
|
List<Object> |
addForGeneratedKeys(Entity entity)
添加
|
int |
addOrUpdate(Entity entity)
增加或者更新实体
|
int |
count(Entity where)
满足条件的数据条目数量
|
<T> int |
del(Entity where)
删除
|
<T> int |
del(String field,
T value)
删除
|
<T> int |
del(T pk)
删除
|
boolean |
exist(Entity where)
指定条件的数据是否存在
|
List<Entity> |
find(Entity where)
根据某个字段值查询结果
|
<T> List<Entity> |
find(String field,
T value)
根据某个字段值查询结果
|
List<Entity> |
findAll()
查询当前表的所有记录
|
Entity |
get(Entity where)
根据条件实体查询单个记录,当有多条返回时,只显示查询到的第一条
|
<T> Entity |
get(String field,
T value)
根据某个字段(最好是唯一字段)查询单个记录
当有多条返回时,只显示查询到的第一条 |
<T> Entity |
get(T pk)
根据主键获取单个记录
|
int |
update(Entity entity)
更新职位信息
|
protected SqlRunner runner
protected String tableName
protected String primaryKeyField
public DaoTemplate(String tableName)
tableName - 数据库表名public DaoTemplate(String tableName, String primaryKeyField)
tableName - 数据库表名primaryKeyField - 主键字段名public DaoTemplate(String tableName, String primaryKeyField, DataSource ds)
tableName - 表名primaryKeyField - 主键字段名ds - 数据源public int add(Entity entity) throws SQLException
entity - 实体对象SQLExceptionpublic List<Object> addForGeneratedKeys(Entity entity) throws SQLException
entity - 实体对象SQLExceptionpublic Long addForGeneratedKey(Entity entity) throws SQLException
entity - 实体对象SQLExceptionpublic <T> int del(T pk)
throws SQLException
T - 主键类型pk - 主键SQLExceptionpublic <T> int del(String field, T value) throws SQLException
T - 主键类型field - 字段名value - 字段值SQLExceptionpublic <T> int del(Entity where) throws SQLException
T - 主键类型where - 删除条件,当条件为空时,返回0(防止误删全表)SQLExceptionpublic int update(Entity entity) throws SQLException
entity - 实体对象,必须包含主键SQLExceptionpublic int addOrUpdate(Entity entity) throws SQLException
entity - 实体,当包含主键时更新,否则新增SQLExceptionpublic <T> Entity get(T pk) throws SQLException
T - pk - 主键值SQLExceptionpublic <T> Entity get(String field, T value) throws SQLException
T - field - 字段名value - 字段值SQLExceptionpublic Entity get(Entity where) throws SQLException
where - 条件SQLExceptionpublic <T> List<Entity> find(String field, T value) throws SQLException
T - field - 字段名value - 字段值SQLExceptionpublic List<Entity> findAll() throws SQLException
SQLExceptionpublic List<Entity> find(Entity where) throws SQLException
where - 查询条件SQLExceptionpublic int count(Entity where) throws SQLException
where - 条件SQLExceptionpublic boolean exist(Entity where) throws SQLException
where - 条件SQLExceptionCopyright © 2015. All rights reserved.