映射单个对象的代码,将一行记录映射成单个对象。sql可以是任意的。
Connection conn=DriverManager.getConnection(url); PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while(rs.next()) { user=(Users)ResultMapObject(rs); //TODO 可以使用user }
映射对象树。
/* sale_orders销售订单表 sale_order_details销售订单细表 products商品表 product_catalogs商品类别表 从sql中就可以分析出 id是主鍵 sale_order_details.sale_order_id===>sale_orders.id sale_order_details.product_catalog_id==>product_catalogs.id sale_order_details.product_id==>products.id */ String []sqls=new String []{ " select id, total_sum, memo, state, modify_date, creator, checker from sale_orders ", " select id, sale_order_id, product_id, product_name, product_catalog_id, model, product_unit, product_amount, product_price, product_sum, memo from sale_order_details ", " select id, name, catalog, model, price, unit, memo, createtime, state, code, productno from products ", " select id, name, memo from product_catalogs" }; //不帶XML映射的,但是無法得知一对一还是一对多还是多对多的关系,所以一律建成多对多关系。 List l=SQLMapObjects(conn,sqls); /* l的结构: sale_orders[] |-----sale_order_details[](sale_order_details.sale_order_id===>sale_orders.id) |-----products[](sale_order_details.product_id==>products.id) |-----product_catalogs[](sale_order_details.product_catalog_id==>product_catalogs.id) */
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/