转自:http://mzhj.iteye.com/blog/1030359

public class PfStuffInfoVo implements Serializable {
/** 信息Id */
  private String infoId;
  /** 项目Id */
  private String proid;
/** 附件信息 */
  private List<PfFileVo> fileList;

...

这是我的对象 该怎么把fileList属性已插入,PfFileVo 有对应的表

貌似iBatis没有提供多个关联对象的同时插入

你需要再Service层调用多个DAO去做多个关联对象的插入操作

批处理在iBatis里是肯定有的

貌似叫batchUpdate方法

不记得了,这个你可以上网搜搜,答案很多的

iBatis3里貌似确实木有批量插入,很郁闷的说

回答者:clarck_913 - 2011-05-05 09:24:52
可以通过ibatis建立一对多的映射

POJO
Java code

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> public class Customer { private Long id; private String name; private String address; private String postcode; private String sex; private List<Orders> orderlist = new ArrayList<Orders>(); public class Orders { private Long id; private String code; private Long customerId; private Customer customer;

Customer.xml
XML code

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" > <sqlMap namespace="customer"> <typeAlias alias="customer" type="com.lavasoft.ssi.domain.Customer"/> <resultMap id="result_base" class="customer"> <result property="id" column="id"/> <result property="name" column="name"/> <result property="address" column="address"/> <result property="postcode" column="postcode"/> <result property="sex" column="sex"/> </resultMap> <resultMap id="result" class="customer" extends="result_base"> <result property="orderlist" column="id" select="orders.findByCustomerId"/> </resultMap> <insert id="insert" parameterClass="customer"> insert into customer(address,postcode,sex,name) values(#address#,#postcode#,#sex#,#name#) <selectKey keyProperty="id" resultClass="long"> select LAST_INSERT_ID() </selectKey> </insert> <select id="getById" parameterClass="long" resultMap="result_base"> select * from customer where id = #value# </select> <select id="getWithCashById" parameterClass="long" resultMap="result"> select * from customer where id = #value# </select> <select id="getWithCashByIdInnerjoin" parameterClass="long" resultClass="customer" resultMap="result"> select c.* from customer c inner join orders o on c.id=o.customerId </select> </sqlMap>

Orders.xml
XML code

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" > <sqlMap namespace="orders"> <typeAlias alias="orders" type="com.lavasoft.ssi.domain.Orders"/> <resultMap id="result_base" class="orders"> <result property="id" column="id"/> <result property="code" column="code"/> <result property="customerId" column="customerId"/> </resultMap> <resultMap id="result" class="orders" extends="result_base"> <result property="customer" column="customerId" select="customer.getById"/> </resultMap> <insert id="insert" parameterClass="orders"> insert into orders(id,code,customerId) values(#id#,#code#,#customerId#) <selectKey keyProperty="id" resultClass="long"> select LAST_INSERT_ID() </selectKey> </insert> <select id="findByCustomerId" resultMap="result_base" parameterClass="long"> select * from orders where customerId = #value# </select> <select id="getById" parameterClass="long" resultMap="result_base"> select * from orders where id = #value# </select> <select id="getByIdWithCash" resultMap="result" resultClass="orders" parameterClass="long"> select * from orders where id = #value# </select> </sqlMap>

DAO
Java code

<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> public interface CustomerDAO { public Long insert(Customer c); public List<Customer> getById(Long id); public List<Customer> getWithCashById(Long id); public List<Customer> getWithCashByIdInnerjoin(); } public class CustomerDAOImpl extends SqlMapClientDaoSupport implements CustomerDAO { public Long insert(Customer c) { return (Long) getSqlMapClientTemplate().insert("customer.insert",c); } public List<Customer> getById(Long id) { return getSqlMapClientTemplate().queryForList("customer.getById",id); } public List<Customer> getWithCashById(Long id) { return getSqlMapClientTemplate().queryForList("customer.getWithCashById",id); } public List<Customer> getWithCashByIdInnerjoin(){ return getSqlMapClientTemplate().queryForList("customer.getWithCashByIdInnerjoin"); } } public interface OrdersDAO { public Long insert(Orders o); public Orders getById(Long id); public List<Orders> findByCustomerId(Long cid); public List<Orders> getByIdWithCash(Long id); } public class OrdersDAOImpl extends SqlMapClientDaoSupport implements OrdersDAO { public Long insert(Orders o) { return (Long) getSqlMapClientTemplate().insert("orders.insert", o); } public Orders getById(Long id) { return (Orders) getSqlMapClientTemplate().queryForObject("orders.getById", id); } public List<Orders> findByCustomerId(Long cid) { return getSqlMapClientTemplate().queryForList("orders.findByCustomerId", cid); } public List<Orders> getByIdWithCash(Long id) { return (List<Orders>) getSqlMapClientTemplate().queryForList("orders.getByIdWithCash",id); } }

test
Java code

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/-->
/**
* Created by IntelliJ IDEA.<br>
* <b>User</b>: leizhimin<br>
* <b>Date</b>: 2008-6-15 22:50:15<br>
* <b>Note</b>: 客户订单一对多模型:客户
*/
public class CustomerDAOTest { private CustomerDAO customerDAO = (CustomerDAO) ApplicationContextUtils.getApplicationContext().getBean("customerDAO"); public void testInsert() { System.out.println("--------insert(Customer c)--------"); Customer c = new Customer(); //fuck!竟然不支持级联保存!
//        Orders order1 = new Orders("o1");
//        Orders order2 = new Orders("o2");
//        c.getOrderlist().add(order1);
//        c.getOrderlist().add(order2);
        c.setName("多对一"); c.setSex("M"); c.setPostcode("450003"); c.setAddress("郑州市花园路"); Long pk = customerDAO.insert(c); System.out.println("插入数据的ID=" + pk); } public void testGetById() { System.out.println("--------getById(Long id)--------"); Long pk = 1L; List<Customer> list = customerDAO.getById(pk); for (Customer c : list) { System.out.println(c); } } public void testGetWithCashById() { System.out.println("--------getWithCashById(Long id)--------"); Long pk = 1L; List<Customer> list = customerDAO.getWithCashById(pk); for (Customer c : list) { System.out.println(c); } } public void testGetWithCashByIdInnerjoin() { System.out.println("--------getWithCashByIdInnerjoin()--------"); List<Customer> list = customerDAO.getWithCashByIdInnerjoin(); for (Customer c : list) { System.out.println(c); } } public static void main(String args[]) { System.out.println("正在测试CustomerDAO"); CustomerDAOTest customerDAOTest = new CustomerDAOTest(); customerDAOTest.testInsert(); customerDAOTest.testGetById(); customerDAOTest.testGetWithCashById(); customerDAOTest.testGetWithCashByIdInnerjoin(); }
}public class OrdersDAOTest { OrdersDAO ordersDAO = (OrdersDAO) ApplicationContextUtils.getApplicationContext().getBean("ordersDAO"); public void testInsert() { System.out.println("--------getWithCashById(Long id)--------"); Orders o = new Orders("o1"); o.setCustomerId(1L); Long pk = ordersDAO.insert(o); System.out.println("所插入数据ID=" + pk); } public void testGetById() { System.out.println("--------getById(Long id)--------"); Orders o = ordersDAO.getById(1L); System.out.println("查询结果:" + o.toString()); } public void testFindByCustomerId() { System.out.println("--------findByCustomerId(Long cid)--------"); List<Orders> list = ordersDAO.findByCustomerId(1L); for(Orders o : list){ System.out.println(o); } } public static void main(String args[]){ System.out.println("正在测试OrderDAO"); OrdersDAOTest ordersDAOTest = new OrdersDAOTest(); ordersDAOTest.testInsert(); ordersDAOTest.testGetById(); ordersDAOTest.testFindByCustomerId(); ordersDAOTest.testGetByIdWithCash(); } public void testGetByIdWithCash(){ System.out.println("------------getByIdWithCash(Long id)----------"); List<Orders> list = ordersDAO.getByIdWithCash(1L); for(Orders o : list){ System.out.println(o +"\n\t"+o.getCustomer().toString()); } } }

转载于:https://www.cnblogs.com/jubincn/archive/2012/04/06/3381214.html

复杂对象ibatis插入,属性为list,怎么一次性插入相关推荐

  1. dom对象常用的属性和方法有哪些?

    dom对象常用的属性和方法有哪些? 一.总结 一句话总结: 1.document属性和方法:document的属性有head,body之类,方法有各种获取element的方法 2.element的属性 ...

  2. js给数组添加数据的方式/js 向数组对象中添加属性和属性值

    参考:https://www.cnblogs.com/ayaa/p/14732349.html js给数组添加数据的方式有以下几种: 直接利用数组下标赋值来增加(数组的下标起始值是0) 例,先存在一个 ...

  3. selenium提取数据之driver对象的常用属性和方法

    selenium提取数据之driver对象的常用属性和方法 在使用selenium过程中,实例化driver对象后,driver对象有一些常用的属性和方法 driver.page_source 当前标 ...

  4. JS基础 -- 枚举对象中的属性

    /** 什么事枚举对象中的属性?* 下面以一个例子来慢慢解释*///创建一个对象var obj = {name: '唐一彩',age: 4000,gender: '男',address: '白马寺'} ...

  5. 【js细节剖析】通过=操作符为对象添加新属性时,结果会受到原型链上的同名属性影响...

    在使用JavaScript的过程中,通过"="操作符为对象添加新属性是很常见的操作:obj.newProp = 'value';.但是,这个操作的结果实际上会受到原型链上的同名属性 ...

  6. 利用BeanUtils在对象间复制属性

    commons-beanutils是jakarta commons子项目中的一个软件包,其主要目的是利用反射机制对JavaBean的属性进行处理.我们知道,一个JavaBean通常包含了大量的属性,很 ...

  7. python3 打印对象的全部属性

    __dict__方法 遇到这样一个情况,要打印出一个对象的各种属性.但是不同对象的属性名都不一样,结构也不同,无法写一个代码来实现.然后我找到了__dict__,使用这个属性,可以动态获取到对象的所有 ...

  8. SpringMVC自动将请求参数和入参对象的属性进行一一绑定;要求请求参数的名字和javaBean入参的对象里面的属性名是一样的||员工的增删改查案例

    SpringMVC自动将请求参数和入参对象的属性进行一一绑定:要求请求参数的名字和javaBean入参的对象里面的属性名是一样的 1.SpringMVC中配置HiddenHttpMethodFilte ...

  9. 给Python的类和对象动态增加属性和方法

    通常我们会将编程语言分为静态和动态.静态语言的变量是在内存中的有类型的且不可变化的,除非强制转换它的类型:动态语言的变量是指向内存中的标签或者名称,其类型在代码运行过程中会根据实际的值而定.Pytho ...

  10. ZedGraph的曲线的LineItem对象的Tag属性存储信息进而在鼠标悬浮时进行显示

    场景 Winform中设置ZedGraph鼠标悬浮显示距离最近曲线上的点的坐标值和X轴与Y轴的标题: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article ...

最新文章

  1. 软件项目管理0717:开发一定要了解客户
  2. Codeforces 1479A. Searching Local Minimum(注意输入+二分)
  3. unity 草 可以一棵棵种吗?_俗语“门前一棵柳,金银财宝往家走”啥意思?房前可以种柳树吗?...
  4. 【python】画一个爱心
  5. windows配置gvim高效率编程(cc++)带自动补全代码
  6. python神经网络实例_Python编程实现的简单神经网络算法示例
  7. iOS原生混合RN开发最佳实践
  8. 业余草教你简单 4 步搞定 SpringBoot 整合 Shiro!
  9. h5 右下角浮动按钮_Flutter 浮动按钮-FloatingActionButton的使用
  10. Qt_QTableWidget用法 添加、删除、添加控件、获取控件在表格中位置
  11. 好程序员Python培训分享numpy简介
  12. 安利一个excel对比
  13. 常用的140个Windows XP设置 [转]
  14. 3DMAX2020 材质编辑器为物理材质的问题
  15. python:利用pandas进行绘图(总结)绘图格式
  16. 苹果11蓝牙配对不成功怎么办_iphone11蓝牙搜不到设备怎么办
  17. Go中的SSRF攻防战
  18. 测试网页版淘宝购物车
  19. SQL常用函数及使用案例
  20. 制作各种docker镜像

热门文章

  1. Tipard Mac Video Converter Ultimate如何在Mac上转换DVD 视频?
  2. Vue:列表渲染 v-for on a template
  3. Neutron Vlan Network 原理- 每天5分钟玩转 OpenStack(92)
  4. [javase] 1.请从键盘随机输入10个整数保存到List中,并按倒序、从大到小的顺序显示出来...
  5. 获取本机IP可区分系统可区分虚拟机和本机java程序跨平台
  6. 利用数据绑定(DataBinding)简化多线程数据展示
  7. C语言中类型转换那些事儿
  8. sever client
  9. Android 应用程序获得系统权限
  10. 超全面的权限系统设计方案!(万能通用)