Mybatis入门--关联查询一对一

本文深入探讨了使用MyBatis框架实现文章对象与其客户对象的一对一关联映射,包括配置文件、实体类、映射文件及查询方法的详细解析。

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

一篇文章对应一个customer

public class Article {

    private int id;
    private Customer customer;
    private String title;
    private String content;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

articleMapper.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="org.lezg.web.mapper.ArticleMapper">

    <!-- 数据库字段与实体对象的映射关系 -->
    <resultMap id="customerResultMap" type="Customer">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="contact" property="contact"/>
        <result column="telephone" property="telephone"/>
        <result column="email" property="email"/>
    </resultMap>

    <!-- association标签来解决一对一的关联查询 -->
    <resultMap id="articleResultMap" type="Article">
        <id column="id" property="id"/>
        <result column="title" property="title"/>
        <result column="content" property="content"/>
        <association property="customer" column="customer_id" javaType="Customer" resultMap="customerResultMap"/>
    </resultMap>

    <!-- 查询Article by id -->
    <select id="getArticle" parameterType="int" resultMap="articleResultMap">
        SELECT * from customer c, article a WHERE c.id = a.customer_id and a.id = #{id}
    </select>
    <!-- 查询所有Article -->
    <select id="getArticles" resultMap="articleResultMap">
        SELECT * from article a, customer c where a.customer_id = c.id
    </select>
</mapper>

ArticleMapper

public interface ArticleMapper {

    /**
     * 查询Article by id
     * @param id
     * @return
     */
    Article getArticle(int id);

    /**
     * 查询所有Article
     * @return
     */
    List<Article> getArticles();
}

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>
    <!-- 设置别名 -->
    <typeAliases>
        <typeAlias type="org.lezg.web.entity.Customer" alias="Customer"/>
        <typeAlias type="org.lezg.web.entity.Article" alias="Article"/>
    </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/lezgweb"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <!-- 映射文件 -->
    <mappers>
        <mapper resource="mapper/customerMapper.xml"/>
        <mapper resource="mapper/articleMapper.xml"/>
    </mappers>
</configuration>

测试

@org.junit.Test
    public void getArticle(){
        SqlSession session = getSession();
        ArticleMapper articleMapper = session.getMapper(ArticleMapper.class);
        Article article = articleMapper.getArticle(1);
        System.out.println(article.getTitle());
        System.out.println(article.getContent());
        System.out.println(article.getCustomer().getContact());
        closeSession(session);
    }

@org.junit.Test
    public void getArticles(){
        SqlSession session = getSession();
        ArticleMapper articleMapper = session.getMapper(ArticleMapper.class);
        List<Article> articles = articleMapper.getArticles();
        for (Article article : articles) {
            System.out.println(article.getTitle());
            System.out.println(article.getContent());
            System.out.println(article.getCustomer().getContact());
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值