mybatis关系映射示例

mybatis关系映射

sql

create table emp
(
    `id`      int auto_increment primary key,
    `name`    varchar(255) unique not null,
    `age`     int                 not null,
    `sex`     varchar(8)          not null,
    `address` varchar(255)        not null
) engine = innodb
  default character set = utf8;

create table workCard
(
    `id`       int auto_increment primary key,
    `realName` varchar(255) unique not null,
    `tel`      varchar(30)         not null,
    `position` varchar(255)        not null,
    `emp_id`   int,
    constraint fk_dpt_id foreign key (emp_id) references emp (id)
        on delete cascade
) engine = innodb
  default character set = utf8;


insert into emp
values (null, '陈平安', 23, '男', '骊珠洞天泥瓶巷');
insert into emp
values (null, '宁姚', 22, '女', '剑气长城');
insert into emp
values (null, '张山峰', 25, '男', '中土神州龙虎山');
insert into emp
values (null, '董三更', 40, '男', '剑气长城');
insert into emp
values (null, '陈清都', 60, '男', '剑气长城');

insert into workCard
values (null, '陈平安', '123123123', '职员', 1);
insert into workCard
values (null, '宁姚', '12345678', '部门经理', 2);

实体类

@Data
public class WorkCard {
    private int id;
    private String realName;
    private String tel;
    private String position;
    private Emp emp;
}
@Data
public class Emp {
    private Integer id;
    private String name;
    private int age;
    private String sex;
    private String address;
}

mapper接口

public interface EmpMapper {
     Emp findById(int id);
}
public interface WorkCardMapper {
    WorkCard findById(int id);
}

EmpMapper.xml

   <select id="findById" resultType="emp">
        select *
        from emp
        where id = #{id};
    </select>

WorkCardMapper.xml的2种写法

第一种

<resultMap id="myCard" type="workCard">
        <id column="cid" property="id"/>
        <result column="realName" property="realName"/>
        <result column="tel" property="tel"/>
        <result column="position" property="position"/>
         <association property="emp" javaType="Emp" column="emp_id">
        <id column="eid" property="id"></id>
        <result column="name" property="name"></result>
        <result column="age" property="age"></result>
        <result column="sex" property="sex"></result>
        <result column="address" property="address"></result>
        </association>
    </resultMap>
     <select id="findById" resultMap="myCard">
     select
     e.id eid,name,age,sex,address,c.id cid,realName,tel,position
     from emp e inner join workCard c
     on e.id=emp_id where c.id=#{cid};
     </select>

第二种

<resultMap id="myCard" type="workCard">
        <id column="cid" property="id"/>
        <result column="realName" property="realName"/>
        <result column="tel" property="tel"/>
        <result column="position" property="position"/>
       <association property="emp" javaType="emp" column="emp_id"
                     select="com.bo.dao.EmpMapper.findById"/>
    </resultMap>
    <select id="findById" resultMap="myCard">
        select * from workCard where id=#{cid};
    </select>

image-20220911153236962

image-20220911153254893

image-20220911153309033

emp_id是外键

mybatis默认的驼峰是关闭的,那么这种方式

<resultMap id="myCard" type="workCard">
        <id column="id" property="id"/>
        <result column="realName" property="realName"/>
        <result column="tel" property="tel"/>
        <result column="position" property="position"/>
         <association property="emp" javaType="Emp" column="emp_id">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <result column="age" property="age"></result>
        <result column="sex" property="sex"></result>
        <result column="address" property="address"></result>
        </association>
    </resultMap>
    <select id="findById" resultMap="myCard">
        select * from workCard where id=#{id};
    </select>

column是数据库字段名,property是实体类名

15行的#{id}对应的是第二行的id(property)

这样查

 <resultMap id="myCard" type="workCard">
        <id column="id" property="id"/>
        <result column="realName" property="realName"/>
        <result column="tel" property="tel"/>
        <result column="position" property="position"/>
        <association property="emp" javaType="emp" column="emp_id"
                     select="com.bo.dao.EmpMapper.findById"/>
    </resultMap>
    <select id="findById" resultMap="myCard">
        select * from workCard where id=#{id};
    </select>

或者这样查

   <resultMap id="myCard" type="workCard">
        <id column="eid" property="id"/>
        <result column="realName" property="realName"/>
        <result column="tel" property="tel"/>
        <result column="position" property="position"/>
         <association property="emp" javaType="Emp" column="emp_id">
        <id column="cid" property="id"></id>
        <result column="name" property="name"></result>
        <result column="age" property="age"></result>
        <result column="sex" property="sex"></result>
        <result column="address" property="address"></result>
        </association>
    </resultMap>
     <select id="findById" resultMap="myCard">
     select
     e.id eid,name,age,sex,address,c.id cid,realName,tel,position
     from emp e inner join workCard c
     on e.id=emp_id where c.id=#{cid};
     </select>

2行的eid对应17行给emp别名e然后就是e.id;

然后7的cid对应的是18的cid

证明:

18行的cid改名为id,7行的cid也改为id,成功查出

MyBatis是一个基于Java的持久层框架,它提供了一种将数据库和对象之间进行映射的方法。在MyBatis中,关系映射是通过XML配置文件或注解来完成的。 关系映射的主要目的是将数据库表中的行数据映射Java对象上,使开发者能够通过操作对象来实现对数据库的操作,而不需要直接编写SQL语句。在MyBatis中,关系映射可以通过以下几种方式实现: 1. XML配置文件:通过在XML文件中定义SQL语句和结果映射规则,将数据库表中的数据映射Java对象上。XML配置文件中可以定义查询语句、参数映射、结果集映射等信息。 例如,以下是一个使用XML配置文件进行关系映射示例: ```xml <!-- 定义查询语句 --> <select id="getUserById" resultType="User"> SELECT * FROM user WHERE id = #{id} </select> <!-- 定义结果集映射 --> <resultMap id="userMap" type="User"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="age" column="age"/> </resultMap> ``` 2. 注解:通过在Java对象的属性或方法上添加注解,指定与数据库表中的列的映射关系。注解方式相对简洁,适用于一些简单的映射场景。 例如,以下是一个使用注解进行关系映射示例: ```java public class User { @Id private Long id; @Column(name = "name") private String userName; // 省略getter和setter方法 } ``` 在MyBatis中,还可以使用一些高级特性来处理复杂的关系映射,例如一对一、一对多、多对一、多对多的关系。此外,MyBatis还提供了动态SQL和缓存等功能,可进一步简化开发。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值