Mybatis基本使用

概述

mybatis官方文档
采用 ORM 思想解决了实体和数据库映射的问题,对jdbc进行了封装,屏蔽了jdbc api底层访问细节。

使用依赖:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.20</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.6</version>
</dependency>

使用步骤

  1. 定义与sql映射文件同名的mapper接口
  2. 设置sql映射文件的namespace属性为mapper接口的全限定名
  3. mapper接口中定义方法,方法名就是sql映射文件中sql语句的id,并保持参数类型和返回值类型一致
  4. 编码(获取mapper接口的代理对象,调用对应的方法完成sql执行)
    即:编写接口方法——>编写sql——>执行方法

注意点

  • 实体类属性名和数据库表列名不一致,不能自动封装数据。有以下两种常用解决方式

    • sql语句的表字段起别名
    • 使用resultMap
      <resultMap id="BolgDetial" type="com.it.dto.response.AuthorBlogPostVo">
          <!-- 主键字段的对应 -->
          <id property="blogId" column="id"/>
          <!--非主键字段的对应--><!-- column字段即sql查出来的字段,也可以是别名 -->
          <result property="blogTitle" column="blog_title"/>
          <!--javaType是用来指定pojo中属性的类型-->
          <association property="author" javaType="com.it.entity.Author">
              <result property="userName" column="username"/>
              <result property="password" column="password"/>
              <result property="email" column="email"/>
          </association>
          <!--ofType指定的是映射到list集合属性中pojo的类型-->
          <collection property="postTagDetailVo" ofType="com.it.dto.response.PostTagDetailVo">
              <result property="postTitle" column="title"/>
              <result property="postDesc" column="post_desc"/>
              <collection property="tags" ofType="com.it.entity.Tag">
                  <result property="tagName" column="tag_name"/>
              </collection>
              <collection property="comments" ofType="com.it.entity.Comment">
                  <result property="commentTxt" column="comment_txt" />
              </collection>
          </collection>
      </resultMap>
      

  • 编写sql的parameterType可以省略

      <select id="getPostDetailById" parameterType="int" resultType="com.it.dto.response.PostDetailVo">
          select p.title postTitle, p.post_desc postDesc from post  p where id = #{id};
      </select>
    
  • 编写sql特殊字符处理

    • 转义字符,如&lt
    • CDATA区
  • mybatis核心配置文件指定映射配置

      <!--指定映射配置文件的位置,映射配置文件指的是每个mapper接口独立的配置文件-->
      <mappers>
          <!--resource指定的话,mapper.xml可以为其他路径,但是没一个mapper.xml都要指定-->
          <!--<mapper resource="com/it/mapper/BlogMapper.xml"></mapper>-->
    
          <!-- <package>标签的使用:必须与mapper接口同一个类路径下-->
          <package name="com.it.mapper"/>
      </mappers>
    

  • 参数传递

    1. 散装参数
        User getUserInfoMapper(@Param("name") Integer id, @Param("sex") String sex);
    2. 实体类封装参数
        User getUserInfoMapper(User user);
    3. map集合
        map.put("name","zs");
        User getUserInfoMapper(Map map);
    4. collection集合
        Collection list = new ArrayList();
        list.add(1);
        List<User> getUserInfoMapper(@Param("list") Collection list);
        使用<foreach>标签     
    
  • 动态sql拼接

    <select id="findUserByCondition" resultMap="userMap">
        select * from user
        <where>
            <if test="userName != null">
                and username like #{userName}
            </if>
            <if test="usersex != null">
                and sex like #{usersex}
            </if>
            <if test="useraddress != null">
                and address like #{useraddress}
            </if>
        </where>
    </select>
    
    <select id="findUserInIds" resultMap="userMap" parameterType="QueryVo">
        select * from user
        <where>
            id in
            <if test="ids != null and ids.size()>0">
                <foreach collection="ids" item="uid" separator="," open="(" close=")">
                    #{uid}
                </foreach>
            </if>
        </where>
    </select>
    

demo源码下载-mybatisuse-blog
demo源码下载-mybatisuse-account

学习来源:https://www.bilibili.com/video/BV1Qf4y1T7Hx?p=47

posted @ 2021-12-18 21:40  扬帆去远航  阅读(55)  评论(0)    收藏  举报