其实一对多关联跟我们之前的一对一关联在实现上没有区别,这里只是简单举例子说明一下。
我们以客户对订单为例。
首先创建数据库
- CREATE TABLE `customer` (
- `id` int ( 11 ) NOT NULL auto_increment,
- `name` varchar(50 ) default NULL,
- `address` varchar(200 ) default NULL,
- PRIMARY KEY (`id`)
- )
- CREATE TABLE `orders` (
- `id` int ( 11 ) NOT NULL auto_increment,
- `code` varchar(50 ) default NULL,
- `customerid` int ( 11 ) default NULL,
- PRIMARY KEY (`id`),
- KEY `sda` (`customerid`),
- CONSTRAINT `sda` FOREIGN KEY (`customerid`) REFERENCES `customer` (`id`) ON DELETE CASCADE
- )
CREATE TABLE `customer` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) default NULL,
`address` varchar(200) default NULL,
PRIMARY KEY (`id`)
)
CREATE TABLE `orders` (
`id` int(11) NOT NULL auto_increment,
`code` varchar(50) default NULL,
`customerid` int(11) default NULL,
PRIMARY KEY (`id`),
KEY `sda` (`customerid`),
CONSTRAINT `sda` FOREIGN KEY (`customerid`) REFERENCES `customer` (`id`) ON DELETE CASCADE
)
订单通过一个外建与客户表关联
实体Bean
-
- public class Customer {
- private int id;
- private String name;
- private String address;
- private List<Orders> orders;
- public int getId() {
- return id;
- }
- public void setId( int id) {
- this .id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this .name = name;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this .address = address;
- }
- public List<Orders> getOrders() {
- return orders;
- }
- public void setOrders(List<Orders> orders) {
- this .orders = orders;
- }
-
- }
-
- public class Orders {
- private int id;
- private String code;
- private int customerid;
-
- public int getId() {
- return id;
- }
- public void setId( int id) {
- this .id = id;
- }
- public String getCode() {
- return code;
- }
- public void setCode(String code) {
- this .code = code;
- }
- public int getCustomerid() {
- return customerid;
- }
- public void setCustomerid( int customerid) {
- this .customerid = customerid;
- }
- }
//客户类
public class Customer {
private int id;
private String name;
private String address;
private List<Orders> orders;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public List<Orders> getOrders() {
return orders;
}
public void setOrders(List<Orders> orders) {
this.orders = orders;
}
}
//订单类
public class Orders {
private int id;
private String code;
private int customerid;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public int getCustomerid() {
return customerid;
}
public void setCustomerid(int customerid) {
this.customerid = customerid;
}
}
下面是对应的配置文件
客户实体的配置文件
- <?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="customerVO" type= "com.bean.onetomany.Customer" />
- <parameterMap class = "customerVO" id= "customerVOmp" >
- <parameter property="id" jdbcType= "int" />
- <parameter property="name" jdbcType= "varchar" />
- <parameter property="address" jdbcType= "varchar" />
- </parameterMap>
- <insert id="createCustomer" parameterMap= "customerVOmp" >
- <selectKey keyProperty="id" type= "post" resultClass= "int" >
- select @@IDENTITY as value
- </selectKey>
- insert into customer(id,name,address) values(?,?,?)
- </insert>
- <resultMap class = "customerVO" id= "getCustomer" >
- <result column="id" property= "id" jdbcType= "int" />
- <result column="name" property= "name" jdbcType= "varchar" />
- <result column="address" property= "address" jdbcType= "varchar" />
- <result property="orders" column= "id" select= "orders.getOrdersByCid" />
- </resultMap>
- <select id="getCustomerByName" parameterClass= "java.lang.String" resultMap= "getCustomer" >
- select * from customer where name=#value#
- </select>
- </sqlMap>
<?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="customerVO" type="com.bean.onetomany.Customer"/>
<parameterMap class="customerVO" id="customerVOmp">
<parameter property="id" jdbcType="int"/>
<parameter property="name" jdbcType="varchar"/>
<parameter property="address" jdbcType="varchar"/>
</parameterMap>
<insert id="createCustomer" parameterMap="customerVOmp">
<selectKey keyProperty="id" type="post" resultClass="int">
select @@IDENTITY as value
</selectKey>
insert into customer(id,name,address) values(?,?,?)
</insert>
<resultMap class="customerVO" id="getCustomer">
<result column="id" property="id" jdbcType="int"/>
<result column="name" property="name" jdbcType="varchar"/>
<result column="address" property="address" jdbcType="varchar"/>
<result property="orders" column="id" select="orders.getOrdersByCid"/>
</resultMap>
<select id="getCustomerByName" parameterClass="java.lang.String" resultMap="getCustomer">
select * from customer where name=#value#
</select>
</sqlMap>
订单实体的配置文件
- <?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="ordersVO" type= "com.bean.onetomany.Orders" />
- <parameterMap class = "ordersVO" id= "ordersVOmp" >
- <parameter property="id" jdbcType= "int" />
- <parameter property="code" jdbcType= "varchar" />
- <parameter property="customerid" jdbcType= "int" />
- </parameterMap>
- <insert id="createOrders" parameterMap= "ordersVOmp" >
- <selectKey>
- select @@IDENTITY as value
- </selectKey>
- insert into orders(id,code,customerid) values(?,?,?)
- </insert>
- <select id="getOrdersByCid" parameterClass= "int" resultClass= "ordersVO" >
- select * from orders where customerid = #value#
- </select>
- </sqlMap>
<?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="ordersVO" type="com.bean.onetomany.Orders"/>
<parameterMap class="ordersVO" id="ordersVOmp">
<parameter property="id" jdbcType="int"/>
<parameter property="code" jdbcType="varchar"/>
<parameter property="customerid" jdbcType="int"/>
</parameterMap>
<insert id="createOrders" parameterMap="ordersVOmp">
<selectKey>
select @@IDENTITY as value
</selectKey>
insert into orders(id,code,customerid) values(?,?,?)
</insert>
<select id="getOrdersByCid" parameterClass="int" resultClass="ordersVO">
select * from orders where customerid = #value#
</select>
</sqlMap>
测试方法新增操作 与onetoone的新增没有本质区别
预习了一下之前的批次处理的方式
- Reader reader = Resources.getResourceAsReader( "SqlMapConfig.xml" );
- SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
-
- Customer c1 = new Customer();
- c1.setName("customer1" );
- c1.setAddress("c1_address1" );
-
- sqlMap.startTransaction();
- sqlMap.insert("customer.createCustomer" , c1);
- sqlMap.startBatch();
- for ( int i = 0 ;i< 10 ;i++){
- Orders o = new Orders();
- o.setCustomerid(c1.getId());
- o.setCode("code_c1 " +i);
- sqlMap.insert("orders.createOrders" , o);
- }
- sqlMap.executeBatch();
- sqlMap.commitTransaction();
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
Customer c1 = new Customer();
c1.setName("customer1");
c1.setAddress("c1_address1");
sqlMap.startTransaction();
sqlMap.insert("customer.createCustomer", c1);
sqlMap.startBatch();
for(int i = 0;i<10;i++){
Orders o = new Orders();
o.setCustomerid(c1.getId());
o.setCode("code_c1 "+i);
sqlMap.insert("orders.createOrders", o);
}
sqlMap.executeBatch();
sqlMap.commitTransaction();
查询方法 实现了customer 到orders 的一对多查询
- Reader reader = Resources.getResourceAsReader( "SqlMapConfig.xml" );
-
- SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
-
- Customer customer = (Customer)sqlMap.queryForObject("customer.getCustomerByName" , "customer1" );
- System.out.println(customer.getAddress());
- List<Orders> orders = customer.getOrders();
- for ( int i = 0 ; i<orders.size();i++){
- Orders o = orders.get(i);
- System.out.println(o.getCode());
- }