首页 > 代码库 > 学习mybatis-3 step by step 篇二

学习mybatis-3 step by step 篇二

Mapper XML 文件

基本的*Mapper.xml文件配置就不熬述了具体可参考:

http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html

1、sql元素

这个元素可以被用来定义可重用的 SQL 代码段,可以包含在其他语句中。它可以被静态地(在加载参数) 参数化. 不同的属性值通过包含的实例变化. 比如:

<sql id="userColumns"> ${alias}.id,${alias}.username,${alias}.password </sql>

这个 SQL 片段可以被包含在其他语句中,例如:

<select id="selectUsers" resultType="map">
  select
    <include refid="userColumns"><property name="alias" value="t1"/></include>,
    <include refid="userColumns"><property name="alias" value="t2"/></include>
  from some_table t1
    cross join some_table t2
</select>

属性值可以用于包含的refid属性或者包含的字句里面的属性值,例如:

<sql id="sometable">
  ${prefix}Table
</sql>

<sql id="someinclude">
  from
    <include refid="${include_target}"/>
</sql>

<select id="select" resultType="map">
  select
    field1, field2, field3
  <include refid="someinclude">
    <property name="prefix" value="Some"/>
    <property name="include_target" value="sometable"/>
  </include>
</select>

上面这段SQL首先引用了someinclude然后再someinclude里面又引用了sometable。利用该功能配合if就能动态生成SQL语句了。

2、使用Mybatis优雅的解决N+1查询问题

概括地讲,N+1 查询问题可以是这样引起的:

  • 你执行了一个单独的 SQL 语句来获取结果列表(就是“+1”)。
  • 对返回的每条记录,你执行了一个查询语句来为每个加载细节(就是“N”)。

    先举一个简单的例子:客户表customer和car是有一对多的关系,假设有实体类如下:

package com.zealzhangz.entity;

import java.io.Serializable;
import java.util.List;

public class Customer implements Serializable {

    private static final long serialVersionUID = 7500918067281634276L;

    private String name;                    // 名称

    private String sex;                    // 性别

    private String idCar;

    private Car customerCar;



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getIdCar() {
        return idCar;
    }

    public void setIdCar(String idCar) {
        this.idCar = idCar;
    }

    public Car getCustomerCar() {
        return customerCar;
    }

    public void setCustomerCar(Car customerCar) {
        this.customerCar = customerCar;
    }
}

如果要获取客户和车辆的信息,一般的做法需要两次查询才能查出结果。当然可以换一种方式,把Car中字段拿到Customer中来,SQL JOIN查询也能查出结果,对于List<car>这种情况JOIN就无能为力了。但是mybatis已经提供了直接优雅

完成这种查询的功能SQL如下:

<?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="CustomerMapper">
    <resultMap type="Customer" id="customerMap">
        <association property="customerCar" column="idCar" javaType="Car" select="selectCar"/>
    </resultMap>
    <select id="selectCustomer" resultMap="customerMap" parameterType="string">
        select name,sex,cc.id_car AS idCar from customer c INNER JOIN customer_car cc ON c.pk_id=cc.id_customer where c.pk_id = CAST(#{pk_id} AS UNSIGNED)
    </select>

    <select id="selectCar" resultType="Car">
        select car_prefix AS carPrefix,car_no AS carNo,vin,car_brand_name AS carBrandName, car_series_mame AS carSeriesMame from car  where pk_id = CAST(#{id} AS UNSIGNED)
    </select>
</mapper>

结果如下:

{
  "customerCar": {
    "carBrandName": "Jeep",
    "carNo": "A666666",
    "carPrefix": "苏",
    "carSeriesMame": "Compass [指南者]",
    "vin": "1C4NJCCA9CD645865"
  },
  "name": "张先生",
  "sex": "先生"
}

更复杂的情形有多辆车的情况,这种情况我们稍后考虑。

 

学习mybatis-3 step by step 篇二