ibatis 实体映射(一对多)

来源:互联网 发布:mac flac 编辑:程序博客网 时间:2024/06/05 08:05
可以通过ibatis建立一对多的映射

POJO
Java code

public class Customer {
private Long id;
private String name;
private String address;
private String postcode;
private String sex;
private List<Orders> orderlist = newArrayList<Orders>();


public class Orders {
private Long id;
private String code;
private Long customerId;
private Customer customer;



Customer.xml
XML code

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTDSQL 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 onc.id=o.customerId
</select>


</sqlMap>




Orders.xml
XML code

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTDSQL 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

public interface CustomerDAO {
public Long insert(Customer c);
public List<Customer> getById(Longid);
public List<Customer>getWithCashById(Long id);
public List<Customer>getWithCashByIdInnerjoin();
}

public class CustomerDAOImpl extends SqlMapClientDaoSupportimplements CustomerDAO {

public Long insert(Customer c) {
return (Long)getSqlMapClientTemplate().insert("customer.insert",c);
}

public List<Customer> getById(Longid) {
returngetSqlMapClientTemplate().queryForList("customer.getById",id);
}

public List<Customer>getWithCashById(Long id) {
returngetSqlMapClientTemplate().queryForList("customer.getWithCashById",id);
}
public List<Customer>getWithCashByIdInnerjoin(){
returngetSqlMapClientTemplate().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 SqlMapClientDaoSupportimplements 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) {
returngetSqlMapClientTemplate().queryForList("orders.findByCustomerId",cid);
}

public List<Orders>getByIdWithCash(Long id) {
return (List<Orders>)getSqlMapClientTemplate().queryForList("orders.getByIdWithCash",id);
}
}






test
Java code


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(Longid)--------");
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(Longid)--------");
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(Longcid)--------");
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(Longid)----------");
List<Orders> list =ordersDAO.getByIdWithCash(1L);
for(Orders o : list){
System.out.println(o +"\n\t"+o.getCustomer().toString());
}
}

}
0 0