首页 MyBatis
文章
取消

MyBatis

1.什么是MyBatis

image-20240112165420835

官网:MyBatis中文网

2.利用MyBatis操作数据库

image-20240112165700046

  • 在IDEA中配置数据库

image-20240112170141392

image-20240112170327998

创建SpringBoot项目

image-20240112170712998

image-20240112184600191

创建pojo.User实体类

image-20240112184715097

创建mapper.UserMapper接口

image-20240112184724857

测试

image-20240112190710744

先写接口,然后再写测试类

image-20240112223420443

配置SQL语句

image-20240112223620225

在IDEA中配置Mysql数据库

image-20240112223751173

JDBC程序

image-20240112224143575

MyBatis程序

image-20240112224350065

image-20240112224410576

3.数据库连接池

image-20240113122425100

image-20240113131027095

如何切换连接池

  • 直接在依赖里面加入德鲁伊依赖

image-20240113131102392

成功转换成德鲁伊

image-20240113131547648

image-20240113131858873

4.lombok

问题分析

image-20240113133234768

具体使用

image-20240113133323981

springboot中已经集成了lombok的版本,并且进行了统一的管理,这里就不需要再说明版本

1
2
3
4
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

image-20240113133951266

5.MyBatis基础操作-环境准备

image-20240113150844468

注意:

实体类和数据库中定义的名称有差别

image-20240113151422113

删除操作

image-20240113155005810

image-20240113155040690

mybatis日志输出

image-20240113155520848

image-20240113160321459

预编译SQL

image-20240113161337790

image-20240113161403653

SQL注入

image-20240113161450510

image-20240113161828589

image-20240113162019519

以下列子就是预编译

image-20240113162111255

image-20240113162131257

新增员工

image-20240113163120485

更新员工

image-20240113171510117

数据查询

image-20240113175045951

  • 方案一:给字段起别名,让别名与实体类一致

    image-20240113175333789

  • 方案二:通过@Results,@Result注解手动映射封装

image-20240113175513734

  • 方案三:开启mybatis的驼峰命名的自动开关 —a Cloumn ————->aColumn
  • 模糊查询

image-20240113191711428

需要改进

image-20240113192522798

    //根据条件查询员工
    @Select("select * from emp where name like concat('%',#{name},'%')and gender = #{gender} and " +
            "entrydate between #{begin} and #{end} order by id desc ")
    public List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);
}

image-20240113192951697

6.XML映射文件

image-20240113193411254

用/斜线分隔

image-20240113193746649

image-20240113194023531

namespace的值要和映射文件中的类名相同

image-20240113194047432

image-20240113194709564

image-20240113195340535

MyBatisX下载安装

MyBatisX动态SQL

if

image-20240113200009329

如果第一个参数为空,后面参数不为空,会出现以下错误

image-20240113200652129

  • 用where标签

    image-20240113200827996

where标签的两个优点

  • where元素只会在子元素有内容的情况下才插入where子句。而且会自动去除子句的开头的AND或OR。

update

image-20240113201928767

set标签的两个有点

  • 动态的在行首插入SET关键字,并会删除额外的逗号。(用在update语句中)

image-20240113202200741

foreach

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!--批量删除员工(18,19,20    -->
<!--
    collection:遍历的集合
    iten:遍历出来的元素
    separator:分隔符
    open:遍历开始前拼接的SQL片段
    close:遍历结束后拼接的片段
    -->
<delete id="deleteByIds">
    delete from emp where id in
    <foreach collection="ids" item="id" separator="," open="(" close=")">
        #{id}
    </foreach>
</delete>

sql片段

image-20240113203757297

本文由作者按照 CC BY 4.0 进行授权
热门标签