复杂对象ibatis插入,属性为list,怎么一次性插入
2012-04-06 16:47
501 查看
转自: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
Customer.xml
XML code
Orders.xml
XML code
DAO
Java code
test
Java code
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()); } } }
相关文章推荐
- 复杂对象ibatis插入,属性为list,怎么一次性插入
- Spring框架xml配置文件 复杂类型属性注入——数组 list map properties DI dependency injection 依赖注入——属性值的注入依赖于建立的对象(堆空间)
- ibatis批量插入对象list
- 使用js遍历出List数组中的对象中的属性
- List<>能按照List中对象的某个属性排序吗
- oracle里存的是number类型,java查询出来的对象属性是bigdecimal类型,该怎么解决?
- java list中的对象,按对象某属性排序
- ibatis基础(四):插入一个实体对象
- Java实现根据List中对象的相应属性进行排序
- 使用 ibatis 处理复杂对象数据关系的实例
- 元素的classList属性怎么用
- 把list(对象)集合中的(某个属性),放到数组中。
- 将同一个list对象中具有相同属性的对象放入到一个新的list中
- mint-ui Picker数据为json,怎么获取同一对象的其他属性,及显示
- List对象利用属性进行排序的通用工具类
- 将list<对象>转换成DataTable,把DataTable转换成参数传入存储过程实现批量插入数据
- 【例题】Oracle中怎么在一个多个表格中一次性插入多个insert语句;
- java将对象列表中的某个属性转换成List或Map
- 关于list.sort()中对象的属性无法调用compareTo()方法解决方案
- 使用Devexpress 的gridControl 绑定List列表(元素对象包含List属性)的Master-Detail显示