创建一对一关系的数据表,我们在这里用丈夫和妻子的关系来表示数据库中的一对一关系,
下面我们通过实例来表示Mybatis中丈夫和妻子一对一关联关系。
需求:查询丈夫和妻子数据信息。
首先创建表结构t_husband,t_wife
CREATE TABLE `t_husband` (
`id` bigint(30) NOT NULL AUTO_INCREMENT,
`husband_name` varchar(55) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`wife_id` bigint(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
CREATE TABLE `t_wife` (
`id` bigint(30) NOT NULL AUTO_INCREMENT,
`wife_name` varchar(30) NOT NULL,
`age` int(11) DEFAULT NULL,
`hus_id` bigint(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
创建Husband、Wife实体类
package com.example.springbootmybatis.domain;
public class Husband {
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getHusbandName() {
return husbandName;
}
public void setHusbandName(String husbandName) {
this.husbandName = husbandName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Long getWifeId() {
return wifeId;
}
public void setWifeId(Long wifeId) {
this.wifeId = wifeId;
}
public Wife getWife() {
return wife;
}
public void setWife(Wife wife) {
this.wife = wife;
}
private String husbandName;
private Integer age;
private Long wifeId;
private Wife wife;
@Override
public String toString() {
return "Husband{" +
"id=" + id +
", husbandName='" + husbandName + '\'' +
", age=" + age +
", wifeId=" + wifeId +
", wife=" + wife +
'}';
}
}
package com.example.springbootmybatis.domain;
public class Wife {
private Long id;
private String wifeName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getWifeName() {
return wifeName;
}
public void setWifeName(String wifeName) {
this.wifeName = wifeName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Long getHusBandId() {
return HusBandId;
}
public void setHusBandId(Long husBandId) {
HusBandId = husBandId;
}
public Husband getHusband() {
return husband;
}
public void setHusband(Husband husband) {
this.husband = husband;
}
private Integer age;
private Long HusBandId;
private Husband husband;
@Override
public String toString() {
return "Wife{" +
"id=" + id +
", wifeName='" + wifeName + '\'' +
", age=" + age +
", HusBandId=" + HusBandId +
'}';
}
}
HusBandService接口
package com.example.springbootmybatis.service;
import com.example.springbootmybatis.domain.Husband;
import java.util.List;
public interface HusBandService {
List<Husband> one2oneMapping();
}
HusBandServiceImpl实现类
package com.example.springbootmybatis.service.impl;
import com.example.springbootmybatis.domain.Husband;
import com.example.springbootmybatis.mapper.HusbandMapper;
import com.example.springbootmybatis.service.HusBandService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class HusbandServiceImpl implements HusBandService {
@Resource
HusbandMapper husbandMapper;
@Override
public List<Husband> one2oneMapping() {
return husbandMapper.one2oneMapping();
}
}
HusBandServiceMapper接口
package com.example.springbootmybatis.service.impl;
import com.example.springbootmybatis.domain.Husband;
import com.example.springbootmybatis.mapper.HusbandMapper;
import com.example.springbootmybatis.service.HusBandService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class HusbandServiceImpl implements HusBandService {
@Resource
HusbandMapper husbandMapper;
@Override
public List<Husband> one2oneMapping() {
return husbandMapper.one2oneMapping();
}
}
HusBandMapper.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.example.springbootmybatis.mapper.HusbandMapper">
<!-- 该resultMap用于实体对象User与表映射-->
<resultMap id="HusbandResult" type="com.example.springbootmybatis.domain.Husband">
<id property="id" column="id"/>
<result property="husbandName" column="husband_name"/>
<result property="age" column="age"/>
<result property="wifeId" column="wife_id"/>
<!--使用association标签来表示一对一关系映射中嵌套的对象信息 -->
<association property="wife">
<result property="id" column="id" />
<result property="wifeName" column="wife_name" />
<result property="age" column="age" />
<result property="husbandId" column="hus_id" />
</association>
</resultMap>
<!--查询丈夫和妻子的信息 一对一关系-->
<select id="one2oneMapping" resultMap="HusbandResult">
select h.*,w.wife_name,w.age,w.hus_id from t_husband h left join t_wife w on h.wife_id = w.id
</select>
</mapper>
测试类
/**
* 一对一关联关系查询
*/
@Test
public void one2oneMapping(){
List<Husband> list = husBandService.one2oneMapping();
for (Husband husbandRes:list) {
System.out.println(husbandRes.toString());
}
}
执行测试类,返回结果
Husband{id=1, husbandName='zhangsan', age=25, wifeId=1, wife=Wife{id=1, wifeName='zhang wife', age=25, husBandId=1}}
Husband{id=2, husbandName='wangwu', age=23, wifeId=2, wife=Wife{id=2, wifeName='lily', age=23, husBandId=2}}