XUtil学习之DBUtil(八)

来源:互联网 发布:java实现九九乘法表 编辑:程序博客网 时间:2024/05/17 04:36

上节 XUtil学习之DBUtil(七)中,我们罗列了对数据库操作的方法,我们这节对增删改查的具体方法做细化的分析。
没有看过 XUtil学习之DBUtil(七)的童鞋,请出门左转,或者点击以下链接:


XUtil学习之DBUtil(七)


(1)save(Object entity) 此方法用于将单个的entity对象加入到数据库中,所有对数据的操作都抛出DbException 。首先提一个概念“事件”,事件是一连串的操作行为的总称,事件有开始,也有结束。举个例子解释“事件”,取款机取钱,突然间断电,取钱的操纵只进行了开始,在没有完成取钱的操作之前该事件都算未完成 ,那么取款机就不会扣钱。对数据库的操作也是如此,
beginTransaction()用于标识事件的开始,
setTransactionSuccessful()标识事件完成,
endTransaction()标识结束事件。
createTableIfNotExist(entity.getClass())从方法名称上可以知道该方法作用在表中不存在要添加对象的情况下添加对象到数据库。方法的具体实现参考tools(工具)部分。
execNonQuery方法是执行SQL查询。见exec sql(sql执行) 部分。

public void save(Object entity) throws DbException {        try {            beginTransaction();            createTableIfNotExist(entity.getClass());            execNonQuery(SqlInfoBuilder.buildInsertSqlInfo(this, entity));            setTransactionSuccessful();        } finally {            endTransaction();        }    }

(2)saveAll(List< > entities)
内容和save一样,只是传入的是对象的集合,需要做的操作就是循环,逐一添加,不再赘述。

 public void saveAll(List<?> entities) throws DbException {        if (entities == null || entities.size() == 0) return;        try {            beginTransaction();            createTableIfNotExist(entities.get(0).getClass());            for (Object entity : entities) {                execNonQuery(SqlInfoBuilder.buildInsertSqlInfo(this, entity));            }            setTransactionSuccessful();        } finally {            endTransaction();        }    }

(3)saveBindingId(Object entity)

 将当前的对象按id自增的方式添加到表中
private boolean saveBindingIdWithoutTransaction(Object entity) throws DbException {        Class<?> entityType = entity.getClass();        //根据对象和注解获取到表名         Table table = Table.get(this, entityType);        Id idColumn = table.id;       //id是否自增长         if (idColumn.isAutoIncrement()) {            execNonQuery(SqlInfoBuilder.buildInsertSqlInfo(this, entity));            //获取表最后一条数据的id            long id = getLastAutoIncrementId(table.tableName);            if (id == -1) {                return false;            }            idColumn.setAutoIncrementId(entity, id);            return true;        } else {            execNonQuery(SqlInfoBuilder.buildInsertSqlInfo(this, entity));            return true;        }    }

(4)saveBindingIdAll(List< > entities)

  public void saveBindingIdAll(List<?> entities) throws DbException {        if (entities == null || entities.size() == 0) return;        try {            beginTransaction();            createTableIfNotExist(entities.get(0).getClass());            for (Object entity : entities) {                if (!saveBindingIdWithoutTransaction(entity)) {                    throw new DbException("saveBindingId error, transaction will not commit!");                }            }            setTransactionSuccessful();        } finally {            endTransaction();        }    }

(5)saveOrUpdate(Object entity)
如果添加对象数据库中存在就更新数据,否则就添加

  public void saveOrUpdate(Object entity) throws DbException {        try {            beginTransaction();            createTableIfNotExist(entity.getClass());            saveOrUpdateWithoutTransaction(entity);            setTransactionSuccessful();        } finally {            endTransaction();        }    }

(6)saveOrUpdateAll(List< > entities)
如果添加对象数据库中存在就更新数据,否则就添加,操作对象是集合,循环,赋值。

  public void saveOrUpdateAll(List<?> entities) throws DbException {        if (entities == null || entities.size() == 0) return;        try {            beginTransaction();            createTableIfNotExist(entities.get(0).getClass());            for (Object entity : entities) {                saveOrUpdateWithoutTransaction(entity);            }            setTransactionSuccessful();        } finally {            endTransaction();        }    }
1 0
原创粉丝点击