论坛首页 入门技术论坛

请教ibatis中如何实现一对多的插入,谢谢!

浏览 3617 次
该帖已经被评为新手帖
作者 正文
   发表时间:2008-05-18  
public class TaskRecord {
    private String taskId;
    private List<TaskProductTypeRelation> tpRelations;
    ...//getter and setter
}
public class TaskProductType {
    private String typeId;
    private String typeName;
    ...//getter and setter
}
public class TaskProductTypeRelation {
    private String id;
    private String taskId;
    private TaskProductType productType;
    ...//getter and setter
}

请教大家如何实现taskRecord的sqlmap的映射实现,来实现insert 一个TaskRecord对象时,自动也insert它的tpRelations属性呢?
   发表时间:2008-05-19  
既然是新手贴,那就自己解决
ibatis不维护orm,select可以通过手工编写sql来维护orm,但是insert就不行,希望以后可以在升级版本中实现该功能
通过如下的方法可以实现insert的 orm

The SqlMapClientTemplate also offers a generic execute method, taking a custom SqlMapClientCallback implementation as argument. This can, for example, be used for batching:

public class SqlMapAccountDao extends SqlMapClientDaoSupport implements AccountDao {

    public void insertAccount(Account account) throws DataAccessException {
        getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                executor.startBatch();
                executor.update("insertAccount", account);
                executor.update("insertAddress", account.getAddress());
                executor.executeBatch();
            }
        });
    }
}
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics