public class DaoTemplate extends Object
| 限定符和类型 | 字段和说明 | 
|---|---|
| protected String | primaryKeyField本表的主键字段,请在子类中覆盖或构造方法中指定,默认为id | 
| protected SqlRunner | runnerSQL运行器 | 
| protected String | tableName表名 | 
| 构造器和说明 | 
|---|
| DaoTemplate(String tableName)构造,此构造需要自定义SqlRunner,主键默认为id | 
| DaoTemplate(String tableName,
           DataSource ds) | 
| DaoTemplate(String tableName,
           String primaryKeyField)构造,使用默认的池化连接池,读取默认配置文件的空分组,适用于只有一个数据库的情况 | 
| 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()查询当前表的所有记录 | 
| List<Entity> | findBySql(String sql,
         Object... params)根据SQL语句查询结果 SQL语句可以是非完整SQL语句,可以只提供查询的条件部分(例如WHERE部分) 此方法会自动补全SELECT * FROM [tableName] 部分,这样就无需关心表名,直接提供条件即可 | 
| Entity | get(Entity where)根据条件实体查询单个记录,当有多条返回时,只显示查询到的第一条 | 
| <T> Entity | get(String field,
   T value)根据某个字段(最好是唯一字段)查询单个记录 当有多条返回时,只显示查询到的第一条 | 
| <T> Entity | get(T pk)根据主键获取单个记录 | 
| PageResult<Entity> | page(Entity where,
    Page page)分页 | 
| PageResult<Entity> | page(Entity where,
    Page page,
    String... selectFields)分页 | 
| int | update(Entity entity)更新 | 
| int | update(Entity record,
      Entity where)按照条件更新 | 
protected String tableName
protected String primaryKeyField
protected SqlRunner runner
public DaoTemplate(String tableName)
tableName - 数据库表名public DaoTemplate(String tableName, String primaryKeyField)
tableName - 数据库表名primaryKeyField - 主键字段名public DaoTemplate(String tableName, DataSource ds)
public DaoTemplate(String tableName, String primaryKeyField, DataSource ds)
tableName - 表名primaryKeyField - 主键字段名ds - 数据源public int add(Entity entity) throws SQLException
entity - 实体对象SQLException - SQL执行异常public List<Object> addForGeneratedKeys(Entity entity) throws SQLException
entity - 实体对象SQLException - SQL执行异常public Long addForGeneratedKey(Entity entity) throws SQLException
entity - 实体对象SQLException - SQL执行异常public <T> int del(T pk)
            throws SQLException
T - 主键类型pk - 主键SQLException - SQL执行异常public <T> int del(String field, T value) throws SQLException
T - 主键类型field - 字段名value - 字段值SQLException - SQL执行异常public <T> int del(Entity where) throws SQLException
T - 主键类型where - 删除条件,当条件为空时,返回0(防止误删全表)SQLException - SQL执行异常public int update(Entity record, Entity where) throws SQLException
record - 更新的内容where - 条件SQLException - SQL执行异常public int update(Entity entity) throws SQLException
entity - 实体对象,必须包含主键SQLException - SQL执行异常public int addOrUpdate(Entity entity) throws SQLException
entity - 实体,当包含主键时更新,否则新增SQLException - SQL执行异常public <T> Entity get(T pk) throws SQLException
T - 主键类型pk - 主键值SQLException - SQL执行异常public <T> Entity get(String field, T value) throws SQLException
T - 字段值类型field - 字段名value - 字段值SQLException - SQL执行异常public Entity get(Entity where) throws SQLException
where - 条件SQLException - SQL执行异常public <T> List<Entity> find(String field, T value) throws SQLException
T - 字段值类型field - 字段名value - 字段值SQLException - SQL执行异常public List<Entity> findAll() throws SQLException
SQLException - SQL执行异常public List<Entity> find(Entity where) throws SQLException
where - 查询条件SQLException - SQL执行异常public List<Entity> findBySql(String sql, Object... params) throws SQLException
sql - SQL语句params - SQL占位符中对应的参数SQLException - SQL执行异常public PageResult<Entity> page(Entity where, Page page, String... selectFields) throws SQLException
where - 条件page - 分页对象selectFields - 查询的字段列表SQLException - SQL执行异常public PageResult<Entity> page(Entity where, Page page) throws SQLException
where - 条件page - 分页对象SQLException - SQL执行异常public int count(Entity where) throws SQLException
where - 条件SQLException - SQL执行异常public boolean exist(Entity where) throws SQLException
where - 条件SQLException - SQL执行异常Copyright © 2021. All rights reserved.