mybatis-关联查询

  • 一对一
    注意:
    1.如果映射的字段和对象的属性一致,则可以省略不写
    2.最后保留主键的字段信息
    3.封装单个对象使用association
    4.association中property代表封装对象的属性,javatype代表指定属性的类型,注意路径
    5.多表联查必须使用resultMap
    6.如果遇到关联封装,则必须全部配置映射关系,但可以在resultMap中使用autoMapping=true,这样就可以实现自动映射,不用写属性和字段名一致的映射啦
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class Emp implements Serializable {
    private Integer id;
    private String name;
    private Integer age;
    private Dept d; //一对一
}
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Dept implements Serializable {
    private Integer deptId;
    private String deptName;
    private List<Emp> empList; //一对多
}
<select id="finded" resultMap="EmpRM">
        select e.id,e.name,e.age,d.dept_id,d.dept_name 
        from emp e,dept d where e.dept_id = d.dept_id
    </select>
    <resultMap id="EmpRM" type="Emp" autoMapping="true">
        <id column="id" property="id"></id>
<!--        <result column="name" property="name"></result>-->
<!--        <result column="age" property="age"></result>-->
        <association property="d" javaType="Dept">
            <id property="deptId" column="dept_id"></id>
            <result column="dept_name" property="deptName"></result>
        </association>
    </resultMap>
  • 关联查询和子查询
    子查询:
    1.association标签中的column=子查询的字段信息
    2.select根据column中的数据实现子查询,select=sql的id
<select id="findde" resultMap="EmpRM1">
        select * from emp
    </select>
    <resultMap id="EmpRM1" type="Emp" autoMapping="true">
        <id column="id" property="id"></id>
        <association property="d" javaType="Dept" column="dept_id" select="finddept"/>
    </resultMap>

    <select id="finddept" resultMap="deptRM">
        select * from dept where dept_id = #{dept_id}
    </select>
    <resultMap id="deptRM" type="Dept">
        <id column="dept_id" property="deptId"></id>
        <result column="dept_name" property="deptName">  </result>
    </resultMap>

一对多:
一对多固定方法:collection标签,property值为映射的“多”属性名,oftype为“多”返回值类型

<select id="find1ton" resultMap="f1tonRM">
        select d.dept_id,d.dept_name,e.id,e.name,e.age 
        from 
        dept d left join emp e 
        on d.dept_id = e.dept_id
    </select>
    <resultMap id="f1tonRM" type="Dept">
        <id column="dept_id" property="deptId"></id>
        <result column="dept_name" property="deptName"></result>
        <collection property="emplist" ofType="Emp" autoMapping="true">
            <id property="id" column="id"></id>
        </collection>
    </resultMap>
  • 配置文件自动映射
    官网API:mapUnderscoreToCamelCase
<settings>
    <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
  • mybatis缓存机制
    作用: 可以降低用户访问物理设备的频次,提高用户的响应速度
    扩展mybatis自身缓存: 一级缓存/二级缓存
    redis: 读10万次/s,写8.6万次每秒
    二级缓存: 默认开启,但需要手动标识,二级缓存可以在同一个sqlSessionFactory内部有效
    二级缓存标识符: 在Mapper标签里面添加·</cache>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值