目录
mybatis多对一、一对多查询
/  

mybatis多对一、一对多查询

数据库备份文件

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `uid` int(11) NOT NULL,
  `uname` varchar(255) DEFAULT NULL,
  `usex` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', '张三', '男');
INSERT INTO `user` VALUES ('2', '李四', '男');
DROP TABLE IF EXISTS `friend`;
CREATE TABLE `friend` (
  `friend_id` int(11) NOT NULL,
  `friend_name` varchar(255) DEFAULT NULL,
  `friend_sex` varchar(255) DEFAULT NULL,
  `user_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`friend_id`),
  KEY `fk` (`user_id`),
  CONSTRAINT `fk` FOREIGN KEY (`user_id`) REFERENCES `user` (`uid`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- Records of friend
-- ----------------------------
INSERT INTO `friend` VALUES ('1', '小明', '男', '1');
INSERT INTO `friend` VALUES ('2', '小红', '女', '1');
INSERT INTO `friend` VALUES ('3', '小兰', '女', '2');
INSERT INTO `friend` VALUES ('4', '小光', '男', '1');
INSERT INTO `friend` VALUES ('5', '小天', '男', '2');

maven创建项目

  • 依赖jar包
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.43</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.25</version>
      <!--      <scope>test</scope>-->
    </dependency>

    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.5</version>
    </dependency>
  </dependencies>

一对多查询

多对一查询

  • User.java
package cn.lacknb.beans;

import java.util.Set;

public class User {

    private Integer uid;
    private String uname;
    private String usex;


    public User(String uname, String usex) {
        this.uname = uname;
        this.usex = usex;
    }

    public User() {
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getUsex() {
        return usex;
    }

    public void setUsex(String usex) {
        this.usex = usex;
    }


    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", uname='" + uname + '\'' +
                ", usex='" + usex + '\'' +
                '}';
    }
}


  • Friend.java
package cn.lacknb.beans;

public class Friend {

    private Integer friend_id;
    private String friend_name;
    private String friend_sex;
    private Integer user_id;
    private User user;

    public Friend() {
    }

    public Friend(String friend_name, String friend_sex, Integer user_id) {
        this.friend_name = friend_name;
        this.friend_sex = friend_sex;
        this.user_id = user_id;
    }

    public Integer getFriend_id() {
        return friend_id;
    }

    public void setFriend_id(Integer friend_id) {
        this.friend_id = friend_id;
    }

    public String getFriend_name() {
        return friend_name;
    }

    public void setFriend_name(String friend_name) {
        this.friend_name = friend_name;
    }

    public String getFriend_sex() {
        return friend_sex;
    }

    public void setFriend_sex(String friend_sex) {
        this.friend_sex = friend_sex;
    }

    public Integer getUser_id() {
        return user_id;
    }

    public void setUser_id(Integer user_id) {
        this.user_id = user_id;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String toString() {
        return "Friend{" +
                "friend_id=" + friend_id +
                ", friend_name='" + friend_name + '\'' +
                ", friend_sex='" + friend_sex + '\'' +
                ", user_id=" + user_id +
                ", user=" + user +
                '}';
    }
}

  • IFriendDao.java
package cn.lacknb.dao;

import cn.lacknb.beans.Friend;

import java.util.List;

public interface IFriendDao {

    List<Friend> selectFriend();

}

  • mapper.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="cn.lacknb.dao.IFriendDao">

    <select id="selectById" resultType="cn.lacknb.beans.User">
        select * from user where uid = #{uid}
    </select>

    <resultMap id="friendMap" type="cn.lacknb.beans.Friend">
        <id column="id" property="id"/>
        <result column="friend_name" property="friend_name"/>
        <result column="friend_sex" property="friend_sex"/>
        <result column="user_id" property="user_id"/>
        <association property="user" javaType="cn.lacknb.beans.User" column="user_id" select="selectById"/>
    </resultMap>

<!--    多对一-->

    <select id="selectFriend" resultMap="friendMap">
        select * from friend
    </select>

</mapper>
  • 其他代码不变

标题:mybatis多对一、一对多查询
作者:gitsilence
地址:http://blog.lacknb.cn/articles/2019/09/24/1577974154963.html