mybatis高级查询,关系映射

mybatis映射器

MyBatis基于动态代理机制,无需再编写Dao的实现,名称统一以Mapper结尾

映射器配置文件

配置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">
<!--namespace 指定该配置文件是那个映射器的配置文件
要和映射器的全限定名一致-->
<mapper namespace="com.wal.mybatis.mapper.ProductDirMapper">
</mapper>

ProductDriMapper

public interface ProductDirMapper {
    List<ProductDir> queryAll();
    List<ProductDir> queryAll2();
}

通过sqlSession.getMapper(ProductDirMapper.class)获得对应接口ProductDirMapper的代理对象

mybatis高级查询

映射器配置Productmapper.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">
<!--namespace 指定该配置文件是那个映射器的配置文件
要和映射器的全限定名一致-->
<mapper namespace="com.wal.mybatis.mapper.ProductMapper">
//条件查询相同的代码块
<sql id="whereSql">
       <where>
            <if test="name!=null and name!='' ">
                <!--
                    concat用作字符串拼接*/
                -->
                and name like  concat('%',#{name},'%')
            </if>
            <if test="minAge!=null">
                and age>=#{minAge}
            </if>
            <if test="maxAge!=null">
                and age &lt;= #{maxAge}
            </if>
        </where>
    </sql>
    <!--id和接口中的方法名一致-->
    <select id="findAll" resultType="com.wal.mybatis.domain.Product">
        select * from t_product
    </select>

    <select id="findAllByQuery" parameterType="com.wal.mybatis.query.ProductQuery" resultType="com.wal.mybatis.domain.Product">
        select * from t_product
        <include refid="whereSql" />
    </select>
</mapper>

query 条件查询对象

public class ProductQuery {
    private String name;
    private Integer minAge;
    private Integer maxAge;

ProductMapper

public interface ProductMapper {
    List<Product> findAllByQuery(ProductQuery query);

条件查询测试

@Test
    public void test2Name() {
        SqlSession sqlSession = SqlsessionUtils.getSqlSession();
        //mybatis 通过代理产生接口的代理对象
        ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
        ProductQuery productQuery = new ProductQuery();
        //productQuery.setName("1");
        productQuery.setMinAge(12);
        productQuery.setMaxAge(30);
        mapper.findAllByQuery(productQuery).forEach(product -> System.out.println(product));
    }

mybatis 关系映射

多对一 嵌套结果

ProductMapper

public interface ProductMapper {
    List<Product> findAll();
    List<Product> findAllByQuery(ProductQuery query);
    //多对一 嵌套结果
    List<Product> queryAll1();
    //多对一 嵌套查询
    List<Product> queryAll2();
}

ProductMapper.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">
<!--namespace 指定该配置文件是那个映射器的配置文件
要和映射器的全限定名一致-->
<mapper namespace="com.wal.mybatis.mapper.ProductMapper">
	<!--关联查询 多对一-->
  <resultMap id="productResultMap" type="com.wal.mybatis.domain.Product">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <association property="dir" column="dir_id" javaType="com.wal.mybatis.domain.ProductDir">
            <id property="id" column="did"/>
            <result property="name" column="dname"/>
        </association>
    </resultMap>
    <select id="queryAll1" resultMap="productResultMap">
        select p.id id,p.name name,p.age age,dp.id did,dp.name dname from t_product p left join t_productdir dp on p.dir_id=dp.id
    </select>
</mapper>

多对一 嵌套查询

ProductMapper.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">
<!--namespace 指定该配置文件是那个映射器的配置文件
要和映射器的全限定名一致-->
<mapper namespace="com.wal.mybatis.mapper.ProductMapper">
  <!--关联查询 多对一-->
    <resultMap id="productResultMap2" type="com.wal.mybatis.domain.Product">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <association property="dir" column="dir_id" 
                     javaType="com.wal.mybatis.domain.ProductDir"
                     select="findDirById">
            <id property="id" column="id"/>
            <result property="name" column="name"/>
        </association>
                     
    </resultMap>
    <!--
        根据id查询类型
        此处的parameterType="long"参数来自于column="dir_id"
    -->
    <select id="findDirById" parameterType="long" resultType="com.wal.mybatis.domain.ProductDir">
        select * from t_productdir where id=#{id}
    </select>
    <select id="queryAll2" resultMap="productResultMap2">
        select * from t_product p
    </select>
</mapper>

一对多 嵌套结果

ProductDirMapper

public interface ProductDirMapper {
	//嵌套结果 一对多
    List<ProductDir> queryAll();
    //嵌套查询 一对多
    List<ProductDir> queryAll2();
}

ProductDirMapper.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">
<!--namespace 指定该配置文件是那个映射器的配置文件
要和映射器的全限定名一致-->
<mapper namespace="com.wal.mybatis.mapper.ProductDirMapper">
    <resultMap id="productResultMap" type="com.wal.mybatis.domain.ProductDir">
        <id property="id" column="pid"/>
        <result property="name" column="pname"/>
        <collection property="products" ofType="com.wal.mybatis.domain.Product">
            <id property="id" column="id"/>
            <result property="name" column="name"/>
            <result property="age" column="age"/>
        </collection>
    </resultMap>
    <!--方式一 一对多-->
    <select id="queryAll" resultMap="productResultMap">
        select pd.name pname,pd.id pid,p.id id,p.name name,p.age age from t_productdir pd left join t_product p on p.dir_id=pd.id
    </select>
</mapper>

一对多 嵌套查询

<?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">
<!--namespace 指定该配置文件是那个映射器的配置文件
要和映射器的全限定名一致-->
<mapper namespace="com.wal.mybatis.mapper.ProductDirMapper">
    <!--方式二 一对多-->
    <resultMap id="productdirResultMap" type="com.wal.mybatis.domain.ProductDir">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <collection property="products" column="id" ofType="com.wal.mybatis.domain.Product"
                    select="findProductByTypeId">

        </collection>
    </resultMap>
    <select id="findProductByTypeId" resultType="com.wal.mybatis.domain.Product">
        select * from t_product where dir_id = #{id}
    </select>
    <select id="queryAll2" resultMap="productdirResultMap">
        select * from t_productdir
    </select>
</mapper>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值