首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【SSM_09】Mybatis-多表操作、插件

【SSM_09】Mybatis-多表操作、插件

作者头像
用户8250147
发布2021-02-04 10:50:16
2980
发布2021-02-04 10:50:16
举报
文章被收录于专栏:黑马黑马

一、映射文件增强

1. 动态 sql
① if 标签
<select id="find" parameterType="demo" resultType="demo">

    select * from test

    <!--
    where 标签中 if 有成立的会再 sql 语句后面自动添加 where
    -->
    <where>
        <!--
        if 标签判断,条件成立后会添加标签体内容    and 底层处理 第一个自动去掉 and
        -->
        <if test="id != null">
            and id = #{id}
        </if>
        <if test="name != null">
            and name = #{name}
        </if>
        <if test="phone != null">
             and phone = #{phone}
        </if>
    </where>

</select>
----------------------------------------------------------------------------------------------------------------
② forearch 标签
<select id="in" parameterType="demo" resultType="demo">

    select * from test

    <where>
        <!--
            传入一个容器,将其中数据遍历取出
        -->
        <foreach collection="list" open="id in(" close=")" item="id" separator=",">
        	#{id}
        </foreach>
    </where>

</select>
----------------------------------------------------------------------------------------------------------------
③ sql 抽取
<!-- 抽取 sql -->
<sql id="sqlDemo" >select * from test</sql>

<select id="find" parameterType="demo" resultType="demo">
    <!-- 使用抽取的 sql -->
    <include refid="sqlDemo"></include>
</select>
2. 多表查询
① 一对一
<!--
    封装为 map
    id : 唯一标识
    type : 类的全限定名
-->
<resultMap id="demoMap" type="user" >
    <!-- 
        手动指定 字段 与 属性之间的对应关系
        property : 属性
        column : 字段
    -->
    <!-- 普通属性 -->
    <result property="id" column="uid"/>
    <result property="name" column="name"/>
    <result property="password" column="password"/>

    <!-- 引用对象 -->
    //方法一:
    <result property="role.id" column="rid"/>
    <result property="role.desc" column="desc"/>

    //方法二:
    <!-- 
        property : 属性
        javaType : 属性类型
    -->
    <association property="role" javaType="role">
        <result property="id" column="rid" />
        <result property="desc" column="desc" />
    </association>
</resultMap>

<select id="find" resultMap="demoMap">
    select *,u.id uid from user u, role r where u.rid = r.id
</select>
----------------------------------------------------------------------------------------------------------------
② 一对多
<resultMap id="demoMap" type="user" >
    <result property="id" column="uid"/>
    <result property="name" column="name"/>
    <result property="password" column="password"/>

    <collection property="role" ofType="role">
        <result property="id" column="rid" />
        <result property="desc" column="desc" />
    </collection>
</resultMap>

<select id="find" resultMap="demoMap">
    select *,u.id uid from user u left join role r on r.id = u.rid
</select>
----------------------------------------------------------------------------------------------------------------
③ 多对多
<resultMap id="demoMap" type="user" >
    <result property="id" column="uid"/>
    <result property="name" column="name"/>
    <result property="password" column="password"/>

    <collection property="role" ofType="role">
        <result property="id" column="rid" />
        <result property="desc" column="desc" />
    </collection>
</resultMap>

<select id="find" resultMap="demoMap">
    select r.*,u.*,u.id uid from user u left join user_role ur on u.id = ur.uid join role r on ur.rid = r.id
</select>
----------------------------------------------------------------------------------------------------------------
④ 注意
    - 一对多 与 多对多 的区别在于查询语句
    - 常用的多表查询为 左外 select * from 表1 left [outer] join 表2 on 查询条件

二、核心配置文件

1. typeHandles
① 写一个类继承 BaseTypeHandler<T> 并复写其中的四个方法
/*
    T 为需要转换的类型 
     一般用于 Date 存入 毫秒值 到数据库
*/
public class MyTypeHandle extends BaseTypeHandler<Date> {
    /*
         Date --->  Long
         PreparedStatement 执行对象
         i 字段的位置
         data 需要转换的对象
    */
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) 
				throws SQLException {
       preparedStatement.setString(i,date.getTime() + "");
    }

    /*
         resultSet 结果集
         s 需要转换的字段字段
    */
    public Date getNullableResult(ResultSet resultSet, String s) throws SQLException {
        return new Date(resultSet.getLong(s));
    }

    /*
         resultSet 结果集
         i 字段的位置
    */
    public Date getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return new Date(resultSet.getLong(i));
    }

    /*
         callableStatement 执行函数
         i 字段的位置
    */
    public Date getNullableResult(CallableStatement callableStatement, int i) 
			throws SQLException {
        return new Date(callableStatement.getLong(i));
    }
}
----------------------------------------------------------------------------------------------------------------
② 注册 
<!-- handler 为自定义类的全限定名 -->
<typeHandlers>
    <typeHandler handler="com.softwareMan.typeHandle.MyTypeHandle" />
</typeHandlers>
2. plugins 【分页插件】
① 导入依赖
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.2</version>
</dependency>
<dependency>
    <groupId>com.github.jsqlparser</groupId>
    <artifactId>jsqlparser</artifactId>
    <version>1.0</version>
</dependency>
----------------------------------------------------------------------------------------------------------------
② 配置插件【Mybatis.xml】
<plugins>
    <!-- 
         PageHelper 4.0 以前配置 
         需要指定为 PageHelper 并指定定方言
    -->
    <plugin interceptor="com.github.pagehelper.PageHelper">
        <property name="dialect" value="mysql"/>
    </plugin>

    <!-- 
         PageHelper 4.0 以后配置 
         需要指定为 PageInterceptor 不需要指定方言
    -->
    <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
----------------------------------------------------------------------------------------------------------------
③ 使用
public void TestC() {
    /*
        第一个参数为 当前页码; 
        第二个参数为 每页显示条数;

        # 注意
        一定要在执行查询之前设置,否则不生效
    */
    PageHelper.startPage(1,4);

    //执行查询 此处得到 lists 已经不是原来的 List 而是 PagesProxy 代理对象
    List<Demo> lists = mapper.find();

    //遍历输出
    for (Demo list : lists) {
        System.out.println(list);
    }

    // 获取分页信息对象
    PageInfo<Demo> info = new PageInfo<Demo>(lists);

    //获取分页信息方法
    System.out.println("当前页:" + info.getPageNum());
    System.out.println("总页数:" + info.getPages());
    System.out.println("总条数:" + info.getTotal());
    System.out.println("上一页:" + info.getPrePage());
    System.out.println("下一页:" + info.getNextPage());
    System.out.println("是否首页:" + info.isIsFirstPage());
    System.out.println("是否末页:" + info.isIsLastPage());
    System.out.println("每页显示条数:" + info.getPageSize());
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、映射文件增强
    • 1. 动态 sql
      • 2. 多表查询
      • 二、核心配置文件
        • 1. typeHandles
          • 2. plugins 【分页插件】
          相关产品与服务
          容器服务
          腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档