mybatis 关联数据的查询 多对一 一对多

本文介绍了一个简单的Java程序设计案例,通过两个类User和Article实现用户与文章的一对多关联关系。同时展示了数据库表的创建及初始化数据,并提供了MyBatis中一对多和多对一的数据映射配置。

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

创建两个类  User 和 article


package entity;


public class User {
    
    private int id;
    public User(){
        
    }
    public User(int id, String userName, String userAge, String userAddress) {
        super();
        this.id = id;
        this.userName = userName;
        this.userAge = userAge;
        this.userAddress = userAddress;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    private String userName;
    private String userAge;
    private String userAddress;
    
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getUserAge() {
        return userAge;
    }
    public void setUserAge(String userAge) {
        this.userAge = userAge;
    }
    public String getUserAddress() {
        return userAddress;
    }
    public void setUserAddress(String userAddress) {
        this.userAddress = userAddress;
    }
   
}



package entity;

public class Article {
private int id;
private User user;
private String title;
private String content;
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public User getUser() {
    return user;
}
public void setUser(User user) {
    this.user = user;
}
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;
}
public Article(){
    
}

public Article(int id, User user, String title, String content) {
    super();
    this.id = id;
    this.user = user;
    this.title = title;
    this.content = content;
}

}

数据库创建代码

Drop TABLE IF EXISTS `article`;
Create TABLE `article` (
  `id` int(11) NOT NULL auto_increment,
  `userid` int(11) NOT NULL,
  `title` varchar(100) NOT NULL,
  `content` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

-- ----------------------------
-- 添加几条测试数据
-- ----------------------------
Insert INTO `article` VALUES ('1', '1', 'test_title', 'test_content');
Insert INTO `article` VALUES ('2', '1', 'test_title_2', 'test_content_2');
Insert INTO `article` VALUES ('3', '1', 'test_title_3', 'test_content_3');
Insert INTO `article` VALUES ('4', '1', 'test_title_4', 'test_content_4');




CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `userName` varchar(50) DEFAULT NULL COMMENT '名称',
  `userAge` int(11) DEFAULT NULL COMMENT '年龄',
  `userAddress` varchar(200) DEFAULT NULL COMMENT '地址',
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;


INSERT INTO `user` VALUES (1, '查看', 23, '精彩内容');
INSERT INTO `user` VALUES (2, '查看', 23, '精彩内容');
INSERT INTO `user` VALUES (3, '查看', 23, '精彩内容');
INSERT INTO `user` VALUES (4, '查看', 23, '精彩内容');
INSERT INTO `user` VALUES (5, '查看', 23, '精彩内容');

多对一的配置文件编写方式 :  引入了association元素

<resultMap id="resultUserArticleList"   type="entity.Article">
    <id property="id"   column="aid"/>
    <result property="title"   column="title"/>
    <result property="content"  column="content"/>
    <association property="user"  javaType="entity.User">
    <id property="id" column="id"/>
    <result property="userName" column="userName"/>
    <result property="userAddress" column="userAddress"/>
    </association>
    </resultMap>

<select id="getUserArticles"  parameterType="int" resultMap="resultUserArticleList">
        select user.id , user.userName , user.userAddress , article.id aid ,article.title ,article.content from
        user,article where user.id = article.userid and user.id=#{id}
    </select>

一对多的文件配置方式:

<resultMap type="entity.User"  id="resultListUser">

<id column = "id"  property="id"/>

<result column="userName" property="userName"/>

<result column="userAge" property="userAge"/>

<result column="userAddress" property="userAddress"/>

</resultMap>

<!-- User联合文件进行查询 方法之二的配置(多对一的方式)--》

<resultMap id="resultUserArticleList-2" type="Article">

<id property="id"  column="aid"/>

<result property="title" column="title"/>

<result property="content" column="content"/>

<association property="user" javaType="User" resultMap="resultListUser"/>

</resultMap>


<select id="getUserArticles" parameterType="int" resultMap="resultUserArticleList">

select user.id , user.userName ,user.userAddress ,article.id aid , article.title , article.content from user , article where user.id = article.userid  and user.id=#{id}

</select>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值