MyBatis

MyBatis

1.为什么要使用MyBatis - Dao层开发,增删改查

  • 代替JDBC,减少代码冗余

  • 不在java代码中将sql语句写死

2.如何使用MyBatis?如何在springboot中使用?

  • 建库建表

  • 创建springboot项目,导入mysql、MyBatis-start

  • 创建实体类

  • Mapper接口(定义了一些抽象方法)

  • Mapper映射配置文件 - 定义了和抽象方法对应的sql语句(.xml)

    • namespace和mapper接口的全接口名一致

    • id和mapper接口的抽象方法名一致

    • parameterType和mapper接口抽象方法参数的返回值类型一致

    • resultType和mapper接口抽象方法返回值类型一致

    • mapper映射配置文件在resource目录下的相对位置和mapper接口在java目录下的相对位置保持一致

  • 配置MyBatis(application\properties\启动类上添加@MapperScann注解)

    • properties
  • 测试

3.配置:lombok、mybatisframework(???????)

4.参数绑定

  • 序号参数绑定:如果抽象方法有多个参数,则

    • 第一个参数:arg0,param1

    • 第二个参数:arg1,param2

  • 注解参数绑定:在抽象方法前添加@Param注解

    • @Param("参数名") - 表示给方法的参数起了一个别名
  • map参数绑定

    • #{}中的占位符和map中的key对应,获取key对应的value作为参数值

5.模糊查询

sql 复制代码
 concat('%,#{username},'%')

6.主键回填 - 获取自增主键

  • useGeneratedKeys设置为true,keyColumn - 主键的列名 keyProperty - 主键对应的属性名

  • last_insert_id(),order=AFTER/BEFORE

  • 通过uuid()查询主键 - 雪花算法

7.动态SQL

  • if/where
sql 复制代码
 <select id="selectByCondition" resultType="user">
     SELECT * FROM `user` WHERE 1=1
     <if test="username!=null and username.length>0">
         AND `username`=#{username}
     </if>
     <if test="age!=null">
         AND `age`=#{age}
     </if>
     <if test="gender!=null and gender.length>0">
         AND `gender`=#{gender}
     </if>
     <if test="addr!=null and addr.length>0">
         AND `addr`=#{addr}
     </if>
 </select>
sql 复制代码
 // where标签
 <select id="selectByCondition" resultType="user">
     SELECT * FROM `user` 
     <where>
     <if test="username!=null and username.length>0">
         AND `username`=#{username}
     </if>
     <if test="age!=null">
         AND `age`=#{age}
     </if>
     <if test="gender!=null and gender.length>0">
         AND `gender`=#{gender}
     </if>
     <if test="addr!=null and addr.length>0">
         AND `addr`=#{addr}
     </if>
     </where>
 </select>
  • set

    sql 复制代码
     <update id="uodate" parameterType="user">
     UPDATE `user`
     <set>
         <if test="username!=null and username.length>0">
             AND `username`=#{username}
         </if>
         <if test="age!=null">
             AND `age`=#{age}
         </if>
         <if test="gender!=null and gender.length>0">
             AND `gender`=#{gender}
         </if>
         <if test="addr!=null and addr.length>0">
             AND `addr`=#{addr}
         </if>
     </set>
     WHERE `id`=#{id}
     </update>
sql 复制代码
trim

 <update id="uodate" parameterType="user">
 UPDATE `user`
 <trim prefix="SET" suffixOverrides=",">
     <if test="username!=null and username.length>0">
         AND `username`=#{username}
     </if>
     <if test="age!=null">
         AND `age`=#{age}
     </if>
     <if test="gender!=null and gender.length>0">
         AND `gender`=#{gender}
     </if>
     <if test="addr!=null and addr.length>0">
         AND `addr`=#{addr}
     </if>
 </trim>
 WHERE `id`=#{id}
 </update>
  • foreach

    sql 复制代码
     <select closet id="selectBuIds" resultType="user">
         SELECT * FROM `user`
         <where>
             <foreach collection="list" item="id" open="id IN (" close")" separator=",">
             #{id}
             </foreach>
         </where>
    </select>
    复制代码

8.列名和属性名不一致时如何映射

1)起别名:别名和属性名一致

2)resultMap - 结果映射

  • 定义resultMap

    • id:resultMap的名字,resultMap在当前映射配置文件中的标识

    • type:映射的类的全名/别名

    • id标签:设置主键列映射

    • result:设置非主键列映射

    • column:列名

    • property:属性名

  • 使用resultMap

9.SQL片段抽取:将重复的SQL提取出来,使用时用include引用即可,最终达到SQL重用的目的,减少代码冗余

复制代码
 
sql 复制代码
  <sql id="selectAll">
         SELECT * FROM user
     </sql>
         <select id="findAll" resultType="user">
       <include refid="selectAll" />
     </select>
 ​
     <select id="findByCondition" resultType="user">
         <include refid="selectAll" />
         <where>
             <if test="age!=null">
                 and age=#{age}
             </if>
             <if test="gender!=null">
                 and gender=#{gender}
             </if>
         </where>
     </select>

10.分页查询

如何使用分页插件:

  • 导入对应的starter

  • 设置分页信息

    java 复制代码
     PageHelper.startPage(1,5);
  • 查询

    java 复制代码
    List<User> userlist = userMapper.selectAll();
  • 创建封装查询结果的对象

    java 复制代码
    PageInfo<User> pageInfo = new PageInfo<>(userlise);
    sout(pageInfo.getPages());
相关推荐
天上掉下来个程小白4 小时前
案例-14.文件上传-简介
数据库·spring boot·后端·mybatis·状态模式
sunnyday042613 小时前
MyBatis XML映射文件中的批量插入和更新
xml·java·mysql·mybatis
剑走偏锋o.O2 天前
Java四大框架深度剖析:MyBatis、Spring、SpringMVC与SpringBoot
java·spring boot·spring·mybatis
风月歌2 天前
基于springboot校园健康系统的设计与实现(源码+文档)
java·spring boot·后端·mysql·毕业设计·mybatis·源码
剑走偏锋o.O2 天前
MyBatis框架详解与核心配置解读
java·学习·mybatis
ONEPEICE-ing2 天前
快速入门Springboot+vue——MybatisPlus多表查询及分页查询
前端·vue.js·spring boot·mybatis
wolf犭良2 天前
14、《SpringBoot+MyBatis集成(2)——进阶配置XML与注解的灵活运用》
xml·spring boot·mybatis
seabirdssss2 天前
重构测试项目为spring+springMVC+Mybatis框架
java·spring·重构·mvc·mybatis
火烧屁屁啦3 天前
【JavaEE进阶】MyBatis之动态SQL
java·java-ee·mybatis