MyBatis one to one 映射

本文详细介绍了在MyBatis中使用关联映射实现Card类与Person类之间的复杂关系查询,包括通过XML映射文件定义关联关系,以及在测试类中调用DAO接口进行数据查询的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Card类

public class Card {

    private int id;
    private String cardNo;
    private String address;

    private Person person;

....

Person类

public class Person {

    private int id;
    private String name;

    private Card card;
    ......

PersonDao类

public interface PersonDao {

    List<Person> getPersons() throws Exception;

    Person getPersonById(int id) throws Exception;

}

CardDao类

public interface CardDao {

    List<Card> getCards() throws Exception;

    Card getCardById(int id) throws Exception;

}

CardDao.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.hsj.dao.CardDao">


    <select id="getCards" resultMap="resultMap_Card">
        select * from t_card c inner join t_person p on c.person_id=p.p_id
    </select>

    <resultMap type="card" id="resultMap_Card">
        <id column="c_id" property="id"/>
        <result column="c_cardno" property="cardNo"/>
        <result column="c_address" property="address"/>
        <association property="person" column="person_id" javaType="person">
            <id column="p_id" property="id"/>
            <result column="p_name" property="name"/>
        </association>
    </resultMap>


    <select id="getCardById" parameterType="int" resultMap="resultMap_Card">
        select * from t_card c inner join t_person p on c.person_id=p.p_id and c_id=#{id}
    </select>
</mapper>

PersonDao.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.hsj.dao.PersonDao">


    <select id="getPersons" resultMap="resultMap_Person">
        select * from t_person p inner join t_card c on p.p_id=c.person_id
    </select>

    <resultMap type="person" id="resultMap_Person">
        <id column="p_id" property="id"/>
        <result column="p_name" property="name"/>
        <association property="card" column="person_id" javaType="card">
            <id column="c_id" property="id"/>
            <result column="c_cardno" property="cardNo"/>
            <result column="c_address" property="address"/>
        </association>
    </resultMap>


    <select id="getPersonById"  parameterType="int" resultMap="resultMap_Person">
        select * from t_person p inner join t_card c on p.p_id=c.person_id and p_id=#{id}
    </select>
</mapper>

其中的一个测试类
public class PersonDaoTest {

private PersonDao personDao;
@Before
public void setUp() throws Exception {
    SqlSession sqlSession=MyBatisUtils.getSqlSession();
    this.personDao=sqlSession.getMapper(PersonDao.class);
}

@Test
public void testGetPersons() {

    try {
        List<Person> persons=this.personDao.getPersons();
        for(Person p:persons){
            System.out.println(p+"==>"+p.getCard());

        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Test
public void testGetPerson() {
    try {
        int id=1;
        Person person=this.personDao.getPersonById(id);
        System.out.println(person+"==>"+person.getCard());

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值