当前位置: 首页 > news >正文

Mybatis(黑马)

入门

概念

:是一款优秀的持久层框架,用于简化JDBC的开发

          就是通过java程序来操作数据库

快速入门

:1.准备工作(创建springboot工程、数据库表user、实体类user)

                  2.引入Mybatis的相关依赖,配置Mybatis(数据库连接信息)

                  3.编写SQL语句(注解/XML)

数据库连接池

概念:数据库连接池是个容器,负责分配、管理数据库连接(Connection)

          它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个

          释放空闲时间超过最大空闲时间的连接,来避免因为没有释放连接而引起的数据库连接遗漏

优势:资源重用、提升系统响应速度、避免数据库连接遗漏

切换Druid数据库连接池:

  1.先引入依赖

<dependency>

   <groupld>com.alibaba</groupld>

   <artifactld>druid-spring-boot-starter</artifactld>

   <version>1.2.8</version>

</dependency>

 2.配置属性

spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Drive

spring.datasource.druid.url=jdbc:mysql://localhost:3306/mybatis

spring.datasource.druid.username=root

spring.datasource.druid.password=1234

lombok

Lombok是一个实用的java类库,能通过注解的形式自动生成构造器、getter/setter、equals、hashcode、tostring.等方法,并可以自动化生成日志变量,简化java开发、提高效率。

要引入依赖

<dependency>

    <groupld>org.projectlombok</groupld>

    <artifactld>lombok</artifactld>

</dependency>

基础操作

删除

1.方法(根据主键删除)

SQL语句:   delete from emp where id = 17;

接口方法:

@Delete("delete from emp where id = #{id}")

public void delete(integer id);

注:

配置日志:可以在application.properties中,打开mybatis的日志,并指定输出到控制台#指定

mybatis输出日志的位置,输出控制台

新增

1.方法

SQL语句:

insert into emp(username, name, gender, image, job, entrydate, dept id, create time, update time) values ('songyuanqiao'“宋远桥',1,1jpg'2,'2012-10-09'2,2022-10-0110:00:00','2022-10-01 10:00:00');

接口方法:

@Insert("insert into emp(username, name, gender, image, job, entrydate, dept id, create time, update time) " + "values(#username, #name}, #{gender), #{image), #ljob), #entrydatel, #deptld), #{createTime), #{updateTime)")

public void insert(Emp emp);

注:红色的Emp是一个实体类将多个数据进行了封装,在数据传输时可以使用一个对象传输

更新(修改)

1.方法  (根据ID更新员工信息)

SOL语句

update emp set username = 'songdaxia', name = '宋大侠', gender = 1 ,image = '1.jpg', job =2, entrydate ='2012-01-01',dept id=2,update time='2022-10-01 12:12:12' where id = 19;

接口方法

@Update("update emp set username=#fusernamel, name=#{namel, gender=#genderl, image=#imagel, iob=#ioblentrydate=#{entrydate}, dept id=#{deptld), update time=#{updateTime} where id=#{id}")

public void update(Emp emp);

查询

1.根据id查询

SOL语句

select * from emp where id = 19;

接口方法

@Select("select * from emp where id = #{id}")

public Emp getByld(integer id);

注:


数据封装:实体类属性名 和 数据库表查询返回的字段名一致,mybatis会自动封装。

                   如果实体类属性名 和 数据库表查询返回的字段名不一致,不能自动封装。.
例:

解决办法:
1.起别名:在SQL语句中,对不一样的列名起别名,别名和实体类属性名一样。

@select("select id, username, password, name, gender, image, job, entrydate, dept id deptid, create time createTime, update timeupdateTime from emp where id = #{id} ")

public Emp getByld(Integer id);

   2.手动结果映射:通过 @Results及@Result 进行手动结果映射。.

@Select("select * from emp where id = #{id}")
@Results({
@Result(column = "dept id", property= "deptld"),
@Result(column ="create time", property = "createTime"),
@Result(column="update time", property="updateTime")})
public Emp getByld(integer id);

   3.开启驼峰命名:如果字段名与属性名符合驼峰命名规则,mybatis会自动通过驼峰命名规则映射

2.根据条件查询

SQL语句
select * from emp where name like "%;k%' and gender= 1 and entnydate between'2010-01-01' and '2020-01-01 'order by update time desc;

接口方法

1.  @Select("select* from emp where name like '%${name}%' and gender #gender} and entrydate between #{begin} and #{end} order by update time desc")

public List<Emp> list(String name,,Short gender,LocalDate begin,LocalDate end;


2.  @select"select * from emp where name like concat('%',#name,’%') and gender = #{gender} and entrydate between #{begin} and #{end} order by update _ time desc")

public List<Emp> list(String name, Short gender , LocalDate begin , LocalDate end);
推荐


XML映射文件

规范

XML映射文件的名称与Mapper接口名称一致,并且将XML映射文件和Mapper接口放置在相同包下(同包同名)

XML映射文件的namespace属性为Mapper接口全限定名一致。。

XML映射文件中sql语句的id与Mapper接口中的方法名一致,并保持返回类型一致。

<mapper namespace="com.itheima.mapper.EmpMapper">

   <select id="list"  resultType="com.itheima.pojo.Emp">

          select * from emp where name like concat('%',#{name},'%’) and gender = #{gender} and

                entrydate between #{begin} and #{end} order by update time desc

   </select>

</mapper>


注: 使用Mybatis的注解,主要是来完成一些简单的增删改查功能。如果需要实现复杂的SQL功能,建议使用XML来配置映射语句。

动态SQL

动态SQL:随着用户的输入或外部条件的变化而变化的SQL语句,我们称为 动态SQL。

<if>

<if>:用于判断条件是否成立。使用test属性进行条件判断,如果条件为true,则拼接SQL。

<where>:where 元素只会在子元素有内容的情况下才插入where子句。而且会自动去除子句的开头

的AND 或OR。


<foreach>

SOL语句

delete from emp where id in (1,2,3);

接口方法

public void deleteBylds(List<integer> ids);


XML映射文件

<delete id="deleteBylds">

      delete from emp where id in

      <foreach collection="ids" item="id" separator="," open="(" close=")">

           #{id}

      </foreach>

</delete>

sql&include

如果在文件中存在大量重复的SQL语句片段,后续改动时会很繁琐,可以使用sql&include来解决。

http://www.lryc.cn/news/588473.html

相关文章:

  • 网络传输过程
  • 理解Linux文件系统:从物理存储到统一接口
  • 小波变换 | 离散小波变换
  • 学习笔记——农作物遥感识别与大范围农作物类别制图的若干关键问题
  • rsyslog简单应用
  • Linux中的系统日志(Rsyslog)
  • 算法训练营day17 654.最大二叉树、617.合并二叉树、700.二叉搜索树中的搜索、98.验证二叉搜索树
  • Linux —— A / 基础指令
  • 深入解析Hadoop YARN架构设计:从原理到实践
  • 019 进程控制 —— 进程程序替换
  • SpringMVC2
  • 力扣-138.随机链表的复制
  • 一分钟K线实时数据数据接口,逐笔明细数据接口,分时成交量数据接口,实时五档委托单数据接口,历史逐笔明细数据接口,历史分时成交量数据接口
  • 深入理解MyBatis延迟加载:原理、配置与实战优化
  • 美丽田园发布盈喜公告,预计净利增长超35%该咋看?
  • 现场设备无法向视频汇聚EasyCVR视频融合平台推流的原因排查与解决过程
  • CA-IS3082W 隔离485 收发器芯片可能存在硬件BUG
  • 第十五节:Vben Admin 最新 v5.0 (vben5) + Python Flask 快速入门 - vue前端 生产部署
  • Laravel 中 chunk 分页漏掉数据?深度解析原因与解决方案
  • Unity3D + VS2022连接雷电模拟器调试
  • 4、qt窗口(沉淀中)
  • iOS APP 上架流程:跨平台上架方案的协作实践记录
  • ConcurrentHashMap 原子操作详解:computeIfAbsent、computeIfPresent和putIfAbsent
  • C语言-数据输入与输出
  • 《甘肃棒球》国家级运动健将标准·棒球1号位
  • c#进阶之数据结构(动态数组篇)----Queue
  • Javaweb使用websocket,请先连上demo好吧!很简单的!
  • Vim库函数
  • 【DOCKER】-4 dockerfile镜像管理
  • 纯C++11实现!零依赖贝叶斯情感分析系统,掌握机器学习系统工程化的秘密!