public
class Customer
implements Serializable{
private
static
final
long serialVersionUID = 451949797460417653L;
private
int id;
private String address;
private String postcode;
private String sex;
private String cname;
private List<Orders> orders; //一个Customer 对应N多个Orders
public
int getId() {
return id;
}
public
void setId(
int id) {
this.id = id;
}
public String getAddress() {
return address;
}
public
void setAddress(String address) {
this.address = address;
}
public String getPostcode() {
return postcode;
}
public
void setPostcode(String postcode) {
this.postcode = postcode;
}
public String getSex() {
return sex;
}
public
void setSex(String sex) {
this.sex = sex;
}
public String getCname() {
return cname;
}
public
void setCname(String cname) {
this.cname = cname;
}
public List<Orders> getOrders() {
return orders;
}
public
void setOrders(List<Orders> orders) {
this.orders = orders;
}
}
Orders.java
package com.lixing.ibatis.entity.onetomany;
import java.io.Serializable;
public
class Orders
implements Serializable {
private
static
final
long serialVersionUID = 8215977396669780567L;
private
int id;
private String code;
private
int customerId;
private Customer customer; //多个Orders对应一个Customer
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 Customer getCustomer() {
return customer;
}
public
void setCustomer(Customer customer) {
this.customer = customer;
}
}
<
insert
id
="insertCustomer"
parameterMap
="parameterCustomerMap"
>
<
selectKey
keyProperty
="id"
resultType
="int"
order
="AFTER"
>
SELECT @@IDENTITY AS ID
</
selectKey
>
INSERT INTO t_customer(address,postcode,sex,cname)
VALUES(#{address},#{postcode},#{sex},#{cname})
</
insert
>
<
resultMap
type
="Customer"
id
="resultCustomerMap"
>
<
result
property
="id"
column
="id"
/>
<
result
property
="address"
column
="address"
/>
<
result
property
="postcode"
column
="postcode"
/>
<
result
property
="sex"
column
="sex"
/>
<
result
property
="cname"
column
="cname"
/>
<collection property="orders" column="id" select="com.lixing.ibatis.onetomany.mapper.OrdersMapper.getOrdersById"/>
</
resultMap
>
<
select
id
="getCustomer"
resultMap
="resultCustomerMap"
parameterType
="int"
>
SELECT *
FROM t_customer
WHERE id=#{id}
</
select
>
</
mapper
>
OrderMapper.xml
<?
xml
version
="1.0"
encoding
="UTF-8"
?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<
mapper
namespace
="com.lixing.ibatis.onetomany.mapper.OrdersMapper"
>
<
parameterMap
type
="Orders"
id
="parameterOrdersMap"
>
<
parameter
property
="id"
/>
<
parameter
property
="code"
/>
<
parameter
property
="customerId"
/>
</
parameterMap
>
<
insert
id
="insertOrders"
parameterMap
="parameterOrdersMap"
>
<
selectKey
keyProperty
="id"
resultType
="int"
order
="AFTER"
>
SELECT LAST_INSERT_ID() AS ID
</
selectKey
>
INSERT INTO t_orders(code,customer_id)
VALUES(#{code},#{customerId})
</
insert
>
<
resultMap
type
="Orders"
id
="ordersMap"
>
<
result
property
="id"
column
="id"
/>
<
result
property
="code"
column
="name"
/>
<
result
property
="customerId"
column
="customer_id"
/>
<
association
property
="customer"
javaType
="Customer"
column
="customer_id"
select
="com.lixing.ibatis.onetomany.mapper.CustomerMapper.getCustomer"
/>
</
resultMap
>
<
select
id
="getOrders"
resultMap
="ordersMap"
parameterType
="int"
>
SELECT * FROM t_orders
WHERE id=#{id}
</
select
>
<
select
id
="getOrdersById"
resultMap
="ordersMap"
parameterType
="int"
>
SELECT * FROM t_orders
WHERE customer_id=#{customer_id}
</
select
>
</
mapper
>
5.mybatis-config.xml 文件
<?
xml
version
="1.0"
encoding
="UTF-8"
?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<
configuration
>
<
settings
>
<!--
changes from the defaults for testing -->
<
setting
name
="cacheEnabled"
value
="false"
/>
<
setting
name
="useGeneratedKeys"
value
="true"
/>
<
setting
name
="defaultExecutorType"
value
="REUSE"
/>
</
settings
>
<
typeAliases
>
<
typeAlias
type
="com.lixing.ibatis.entity.onetomany.Customer"
alias
="Customer"
/>
<
typeAlias
type
="com.lixing.ibatis.entity.onetomany.Orders"
alias
="Orders"
/>
</
typeAliases
>
<
environments
default
="development"
>
<
environment
id
="development"
>
<
transactionManager
type
="JDBC"
/>
<
dataSource
type
="POOLED"
>
<
property
name
="driver"
value
="com.mysql.jdbc.Driver"
/>
<
property
name
="url"
value
="jdbc:mysql://localhost:3306/test"
/>
<
property
name
="username"
value
="root"
/>
<
property
name
="password"
value
="7501857"
/>
</
dataSource
>
</
environment
>
</
environments
>
<
mappers
>
<
mapper
resource
="com/lixing/ibatis/onetomany/mapper/CustomerMapper.xml"
/>
<
mapper
resource
="com/lixing/ibatis/onetomany/mapper/OrdersMapper.xml"
/>
</
mappers
>
</
configuration
>