一对多的话,这里用到的是Customer跟LinkMan,一个customer对应多个linkman。(表的关系见hibernate映射关系中的表)
1、创建实体
LinkMan.java
package com.nieshenkuan.model;
public class LinkMan {
// CREATE TABLE linkman(
// lkm_id BIGINT(32) PRIMARY KEY auto_increment,
// lkm_name VARCHAR(32) NOT NULL,
// lkm_sex CHAR(1),
// lkm_phone VARCHAR(16),
// lkm_cus_id BIGINT(32),
// CONSTRAINT fk_cus_lkm FOREIGN KEY(lkm_cus_id) REFERENCES customer(cus_id)
// )ENGINE=INNODB CHARSET=utf8;
private Long lkm_id;
private String lkm_name;
private Character lkm_sex;
private String lkm_phone;
private Long lkm_cus_id;
public Long getLkm_id() {
return lkm_id;
}
public void setLkm_id(Long lkm_id) {
this.lkm_id = lkm_id;
}
public String getLkm_name() {
return lkm_name;
}
public void setLkm_name(String lkm_name) {
this.lkm_name = lkm_name;
}
public Character getLkm_sex() {
return lkm_sex;
}
public void setLkm_sex(Character lkm_sex) {
this.lkm_sex = lkm_sex;
}
public String getLkm_phone() {
return lkm_phone;
}
public void setLkm_phone(String lkm_phone) {
this.lkm_phone = lkm_phone;
}
public Long getLkm_cus_id() {
return lkm_cus_id;
}
public void setLkm_cus_id(Long lkm_cus_id) {
this.lkm_cus_id = lkm_cus_id;
}
@Override
public String toString() {
return "LinkMan [lkm_id=" + lkm_id + ", lkm_name=" + lkm_name + ", lkm_sex=" + lkm_sex + ", lkm_phone="
+ lkm_phone + ", lkm_cus_id=" + lkm_cus_id + "]";
}
}
Customer.java
package com.nieshenkuan.model;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Customer {
// CREATE table customer(
// cus_id BIGINT(32) PRIMARY KEY auto_increment,
// cus_name VARCHAR(32) NOT NULL,
// cus_mobile VARCHAR(32) DEFAULT NULL,
// )ENGINE=INNODB CHARSET=utf8;
private Long cus_id;
private String cus_name;
private String cus_mobile;
private List<LinkMan> linkMan;
public Long getCus_id() {
return cus_id;
}
public void setCus_id(Long cus_id) {
this.cus_id = cus_id;
}
public String getCus_name() {
return cus_name;
}
public void setCus_name(String cus_name) {
this.cus_name = cus_name;
}
public String getCus_mobile() {
return cus_mobile;
}
public void setCus_mobile(String cus_mobile) {
this.cus_mobile = cus_mobile;
}
public List<LinkMan> getLinkMan() {
return linkMan;
}
public void setLinkMan(List<LinkMan> linkMan) {
this.linkMan = linkMan;
}
@Override
public String toString() {
return "Customer [cus_id=" + cus_id + ", cus_name=" + cus_name + ", cus_mobile=" + cus_mobile + ", linkMan="
+ linkMan + "]";
}
}
2、创建CustomerMapper.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.nieshenkuan.dao.CustomerMapper">
<resultMap type="com.nieshenkuan.model.Customer" id="customerResultMap">
<id property="cus_id" column="cus_id" javaType="java.lang.Long"/>
<result property="cus_name" column="cus_name" />
<result property="cus_mobile" column="cus_mobile" />
<collection property="linkMan" ofType="com.nieshenkuan.model.LinkMan"
column="lkm_cus_id">
<id property="lkm_id" column="lkm_id" javaType="java.lang.Long"
jdbcType="INTEGER" />
<result property="lkm_name" column="lkm_name" javaType="java.lang.String"
jdbcType="VARCHAR" />
<result property="lkm_sex" column="lkm_sex" javaType="java.lang.Character"
jdbcType="CHAR" />
<result property="lkm_name" column="lkm_name" javaType="java.lang.String"
jdbcType="VARCHAR" />
<result property="lkm_cus_id" column="lkm_cus_id" javaType="java.lang.Long"
jdbcType="INTEGER" />
</collection>
</resultMap>
<select id="getCustomer" resultMap="customerResultMap" parameterType="java.lang.Long">
SELECT c.*,l.*
FROM customer c, linkman l
WHERE c.cus_id=l.lkm_cus_id and c.cus_id=#{cus_id}
</select>
</mapper>
3、创建dao接口CustomerMapper.java
package com.nieshenkuan.dao;
import com.nieshenkuan.model.Customer;
public interface CustomerMapper {
public Customer getCustomer(Long id);
}
4、将CustomerMapper.xml注册到sqlMapperConfig.xml文件中去
<mappers>
<mapper resource="com/nieshenkuan/mapper/PersonMapper.xml"></mapper>
<mapper resource="com/nieshenkuan/mapper/CustomerMapper.xml"></mapper>
</mappers>
5、测试
package com.nieshenkuan.test;
import java.io.IOException;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.nieshenkuan.dao.CustomerMapper;
import com.nieshenkuan.model.Customer;
import com.nieshenkuan.util.MyBatisUtil;
public class TestOneToMany {
@Test
public void test1() throws IOException {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
/**
* 这里是一对多的实例
*
*/
/*1、一对多就是利用collection关键字来弄得。
* 首先,这里是是从一的一方,通过查到一的一方,并把对应多的数据也查出来。
* 一个顾客对应多个联系人。即一个customer对应多个linkman
* */
/*
* 2、查找到customer,就能把所有的linkman也顺带查出来。
* 这里也是通过外键的形式,外键建在多的一方,即linkman这一方
* linkman这一数据库对应的实体有一个字段作为外键,指向customer的主键。
* 从而从linkman到customer形成多对一的关系
* 从customer到linkman形成一对多的关系。
*
* 如果我们要从customer中找到多的一方,要用collection集合带出所有的查询结果。
* 这点在mapper.xml文件中显现的很明显。
* */
CustomerMapper customerMapper=sqlSession.getMapper(CustomerMapper.class);
Customer customer=customerMapper.getCustomer(2l);
System.out.println(customer);
}
}
6、测试结果
Customer [cus_id=2, cus_name=阿里巴巴, cus_mobile=null, linkMan=[LinkMan [lkm_id=3, lkm_name=马云, lkm_sex=null, lkm_phone=null, lkm_cus_id=2], LinkMan [lkm_id=4, lkm_name=基哥, lkm_sex=null, lkm_phone=null, lkm_cus_id=2]]]