当前位置: 代码迷 >> Web前端 >> 应用Abator自动生成ibatis的DAO.Model方法使用说明
  详细解决方案

应用Abator自动生成ibatis的DAO.Model方法使用说明

热度:495   发布时间:2012-08-21 13:00:22.0
使用Abator自动生成ibatis的DAO.Model方法使用说明

使用Abator的ibatis自动生成器很方便的可以自动生成xx-sqlmap.xml、DAO、Model(包括example)文件。

关于abator的下载和配置这里暂且不予以说明,因为网上资源多多,详细多多,有木有!!

?

主要说明的是DAO文件中自动生成的各种方法是何含义,因为主页君开始使用的时候也闹不明白是神马意思,后来仔细研究过后才搞明白,所以贴在这里希望对大家有帮助。

?

DAO文件(Model以CAR为说明):

?

public class CarDAOImpl extends SqlMapClientDaoSupport implements CarDAO {

??? 说明 :这个是构造函数,在这里就不多说明了

??? public CarDAOImpl() {
??????? super();
??? }

??
??? 说明 : 这里是insert插入方法
??? 参数为实体对象CAR (举例:CAR record = new Car(); record.setId("1");record.setCarName("奥? 迪"))
??? public void insert(Car record) {
??????? getSqlMapClientTemplate().insert("car.abatorgenerated_insert", record);
??? }

???

???
???? 说明 : 根据主键修改表(可修改多个字段
???? 参数为实体对象CAR (举例: CAR record = new Car(); record.setId("1");record.setCarName("奥迪") ; record.setCarModel(" sedan ")

相当于 update car set carname="奥迪"),carmodel("sedan") where id="1"; ???
??? public int updateByPrimaryKey(Car record) {
??????? int rows =?? getSqlMapClientTemplate().update("car.abatorgenerated_updateByPrimaryKeyWithBLOBs", record);
??????? return rows;
??? }

???
???? 说明 : 根据主键修改 (只修改单个字段)
???? 参数为实体对象CAR (会自动将其中的ID取出来作为where条件) (举例: CAR record = new Car(); record.setId("1");record.setCarName("奥迪") 相当于 update car set carname="奥迪"where id="1"; ??
??? public int updateByPrimaryKeySelective(Car record) {
??????? int rows = getSqlMapClientTemplate().update("car.abatorgenerated_updateByPrimaryKeySelective", record);
??????? return rows;
??? }

??
???? 说明 : 根据条件查询(不包括BLOB字段的)
???? 参数为CarExample的实例,通过example将where条件传入
???? (举例:CarExample example = new CarExample();Criteria? criteria = example .createCriteria();

?criteriaAddCarNameEqualto("奥迪").相当于select * from car where carname="奥迪")
???? public List selectByExampleWithoutBLOBs(CarExample example) {
??????? List list = getSqlMapClientTemplate().queryForList("car.abatorgenerated_selectByExample", example);
??????? return list;
??? }

???
???? 说明 : 根据条件查询包括BLOB字段的
???? 参数为CarExample的实例,通过example将where条件传入 (使用方法同上)
????
??? public List selectByExampleWithBLOBs(CarExample example) {
??????? List list = getSqlMapClientTemplate().queryForList("car.abatorgenerated_selectByExampleWithBLOBs", example);
??????? return list;
??? }

??
??? 说明 : 根据ID查询
???? 参数为ID字符串
????
??? public Car selectByPrimaryKey(String id) {
??????? Car key = new Car();
??????? key.setId(id);
??????? Car record = (Car) getSqlMapClientTemplate().queryForObject("car.abatorgenerated_selectByPrimaryKey", key);
??????? return record;
??? }
???

??
???? 说明 : 根据条件删除
????? 参数为example (使用方法同上)
????
????? public int deleteByExample(CarExample example) {
??????? int rows = getSqlMapClientTemplate().delete("car.abatorgenerated_deleteByExample", example);
??????? return rows;
??? }

???
???? 说明 : 根据主键删除
????? 参数为ID字符串

????
??? public int deleteByPrimaryKey(String id) {
??????? Car key = new Car();
??????? key.setId(id);
??????? int rows = getSqlMapClientTemplate().delete("car.abatorgenerated_deleteByPrimaryKey", key);
??????? return rows;
??? }

??
????? 说明 : 根据条件计数
???? 参数为example
(举例: CarExample example = new CarExample();Criteria? criteria = example .createCriteria();

?criteriaAddCarNameEqualto("奥迪").相当于select count(*) from car where carname="奥迪"


???
??? public int countByExample(CarExample example) {
??????? Integer count = (Integer)? getSqlMapClientTemplate().queryForObject("car.abatorgenerated_countByExample", example);
??????? return count.intValue();
??? }

???
??? 说明 : 根据条件选择性更新
???? 参数record为set后面的参数;example为where条件;只能更新一个字段(
举例: CAR record = new Car(); record.setId("1");record.setCarName("宝马") ;

相当于 update car set carname="奥迪"),carmodel("sedan") where id="1"; CarExample example = new CarExample();Criteria? criteria = example .createCriteria();

?criteriaAddCarNameEqualto("奥迪").相当于update car set carname="宝马" where carname="奥迪"

?


???
??? public int updateByExampleSelective(Car record, CarExample example) {
??????? UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
??????? int rows = getSqlMapClientTemplate().update("car.abatorgenerated_updateByExampleSelective", parms);
??????? return rows;
??? }

?
???? 说明 : 按条件更新无BLOBS类型的字段
????? 参数record为set后面的参数;example为where条件;(使用方法同上,不同的是,record中设置set的条件可以是多个)

????
??? public int updateByExampleWithBLOBs(Car record, CarExample example) {
??????? UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
??????? int rows = getSqlMapClientTemplate().update("car.abatorgenerated_updateByExampleWithBLOBs", parms);
??????? return rows;
??? }

??
???? 说明 : 按条件更新更新无blob类型的字段
????
???? 参数record为set后面的参数;example为where条件;(使用方法同上)

????
??? public int updateByExampleWithoutBLOBs(Car record, CarExample example) {
??????? UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
??????? int rows = getSqlMapClientTemplate().update("car.abatorgenerated_updateByExample", parms);
??????? return rows;
??? }

???
??????? 说明 : 按条件更新 (使用方法我米研究,有懂的同事们帮忙说明一下,谢谢!)
??? ??? private static class UpdateByExampleParms extends CarExample {
??????? private Object record;

??????? public UpdateByExampleParms(Object record, CarExample example) {
??????????? super(example);
??????????? this.record = record;
??????? }

??????? public Object getRecord() {
??????????? return record;
??????? }
??? }
}

?

?

PS: 总的来说,用abator自动生成的DAOImpl方法中的方法只是最基础的增删改查,结合自己的项目中的数据库操作我们往往需要更复杂的SQL操作,这就要求自己有能力在xml文件中配置SQL语句,并在DAOImpl中增设自定义方法了,当然,无论多复杂的SQL,都有sqlclient提供的强大的增删改查执行方法来执行的。

  相关解决方案