MyBatis

MyBatis

下载插件

MySQL是根据你使用什么数据库就下什么

配置

这个文件是需要自己创建

1.创建一个与数据库内的表相同配置的类

2.实现一个接口

3.创建一个符合规范的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">
<!--                  接口路径-->
<mapper namespace="org.mx.mybatis.mapper.UserMapper">
<!--               接口方法名称            返回的类型 -->
    <select id="getAll" resultType="org.mx.mybatis.model.User">
        select * from User
    </select>
</mapper>

单元测试

点击接口 选中接口后点击

选中后点击Test

之后会在test文件下形成相同路径的测试类

使用

点击测试类运行 查看是否成功

添加数据

1.在接口类中提供方法

2.在xml中实现语句

取别名后的类名取到对应的属性

测试

删除

1.接口类内提供方法

2.实现方法

测试

修改

1.接口提供方法

2.xml内实现方法

测试

小项目运行逻辑

先进入Controller控制器

之后进入server服务

服务内调用数据库

#{}这个会预处理 如 int不加‘’ string就加‘’ 大部分都用这个#{}

${}这个是原生的 不会添加'' 需要'${}' 这个一般用来传关键字 如 sort desc asc这种

虽然${}可以进行传关键字 但会面临着有SQL注入的风险 也就是本来密码是一串字符串,现在某个人穿了个SQL语句 or 1=‘1’ 就把某条数据取出来了,这就是SQL注入

如果查询时发生了隐式类型转换 就不会走索引 性能会极大的降低

id='1' int类型是1 但加了'1' 变成了字符 发生隐式类型转换 性能会降低

MyBatis内模糊匹配 需要用到MySQL内置函数的concat的字符串拼接

lick concat('%',#{username},'%') 

当类的变量名和数据库字段名不相同时

可以使用字典映射

    <resultMap id="Base" type="org.mx.mybatis.model.User">
<!--       当类内的类型名和数据库的字段名不一致时,进行映射-->
<!--            数据库字段名    类的类型铭-->
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <result column="age" property="a"></result>
    </resultMap>
<!--               接口方法名称            返回的类型 -->
<!--                        把返回类型改为映射的类-->
    <select id="getAll" resultMap="Base">-->
        select * from User
    </select>

动态SQL

if标签

当某个字段有传值时,就连接上

测试1 添加id

测试2 不添加id

trim标签

  • prefix:表示整个语句块,以prefix的值作为前缀

  • suffix:表示整个语句块,以suffix的值作为后缀

  • prefixOverrides:表示整个语句块要去除掉的前缀

  • suffixOverrides:表示整个语句块要去除掉的后缀

<insert id="insert" parameterType="org.example.model.User" useGeneratedKey
 s="true" keyProperty="id">
  insert into user
 <trim prefix="(" suffix=")" suffixOverrides=",">
 <if test="username != null">
          username,
 </if>
 <if test="password != null">
          password,
 </if>
 <if test="nickname != null">
          nickname,
 </if>
 <if test="sex != null">
          sex,
 </if>
 <if test="birthday != null">
          birthday,
 </if>
 <if test="head != null">
          head,
 </if>
 <if test="createTime != null">
          create_time,
 </if>
 </trim>
    
 <trim prefix="values (" suffix=")" suffixOverrides=",">
 <if test="username != null">
          #{username},
 </if>
 <if test="password != null">
          #{password},
 </if>
 <if test="nickname != null">
          #{nickname},
 </if>
 <if test="sex != null">
          #{sex},
 </if>
 <if test="birthday != null">
          #{birthday},
 </if>
 <if test="head != null">
          #{head},
 </if>
 <if test="createTime != null">
          #{createTime},
 </if>
 </trim>
 </insert>

where标签

<select id="selectByCondition" parameterType="org.example.model.User" resu
 ltMap="BaseResultMap">
    select id, username, password, nickname, sex, birthday, head, create_t
 ime
        from user
 <where>
 <if test="username != null">
            and username=#{username}
 </if>
 <if test="password != null">
            and password=#{password}
 </if>
 <if test="nickname != null">
            and nickname=#{nickname}
 </if>
 <if test="sex != null">
            and sex=#{sex}
 </if>
 <if test="birthday != null">
            and birthday=#{birthday}
 </if>
 <if test="head != null">
            and head=#{head}
 </if>
 <if test="createTime != null">
            and create_time=#{createTime}
 </if>
 </where>
 </select>

set标签

 <update id="updateById" parameterType="org.example.model.User">
    update user
 <set>
 <if test="username != null">
                username=#{username},
 </if>
 <if test="password != null">
                password=#{password},
 </if>
 <if test="nickname != null">
                nickname=#{nickname},
 </if>
 <if test="sex != null">
                sex=#{sex},
 </if>
 <if test="birthday != null">
                birthday=#{birthday},
 </if>
 <if test="head != null">
                head=#{head},
 </if>
 <if test="createTime != null">
                create_time=#{createTime},
 </if>
 </set>
    where id=#{id}
 </update>

foreach标签

  • collection:绑定⽅法参数中的集合,如 List,Set,Map或数组对象

  • item:遍历时的每⼀个对象

  • open:语句块开头的字符串

  • close:语句块结束的字符串

  • separator:每次遍历之间间隔的字符串

<!--传一个列表-->
int deleteByIds(List<Integer> ids);

   <!--批量删除-->
<delete id="deleteByIds">
    delete from article
    where id in
    <!--         列表名        一个列表每个元素   前缀  后缀   分隔符-->
 <foreach collection="ids" item="item" open="(" close=")" separator=",">
        #{item}
 </foreach>
 </delete>

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值