Mybatis基础操作学习

文章目录

实施前的准备工作:

  1. 准备数据库表
  2. 创建一个新的springboot工程,选择引入对应的起步依赖(mybatis、mysql驱动、lombok)
  3. application.properties中引入数据库连接信息
  4. 创建对应的实体类 Emp(实体类属性采用驼峰命名)
  5. 准备Mapper接口 EmpMapper

安装mybatis请见:mybatis入门

在Mybatis当中我们可以借助日志,查看到sql语句的执行、执行传递的参数以及执行结果。具体操作如下:

  1. 打开application.properties文件

  2. 开启mybatis的日志,并指定输出到控制台

    #指定mybatis输出日志的位置, 输出控制台
    mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

开启日志之后,我们运行,可以看到在控制台中,输出了以下的SQL语句信息:

这种SQL语句我们称为预编译SQL。

本文只演示基础操作用法,准备过程便不演示

基础操作演示

删除

根据主键删除数据

  • SQL语句

    -- 删除id=17的数据
    delete from emp where id = 17;

  • 接口方法

    @Mapper
    public interface EmpMapper {

    复制代码
      //@Delete("delete from emp where id = 17")
      //public void delete();
      //以上delete操作的SQL语句中的id值写成固定的17,就表示只能删除id=17的用户数据
      //SQL语句中的id值不能写成固定数值,需要变为动态的数值
      //解决方案:在delete方法中添加一个参数(用户id),将方法中的参数,传给SQL语句
      
      /**
       * 根据id删除数据
       * @param id    用户id
       */
      @Delete("delete from emp where id = #{id}")//使用#{key}方式获取方法中的参数值
      public void delete(Integer id);

    }

@Delete注解:用于编写delete操作的SQL语句

如果mapper接口方法形参只有一个普通类型的参数,#{...} 里面的属性名可以随便写,如:#{id}、#{value}。建议保持名字一致。

新增

SQL语句:

sql 复制代码
insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time) values ('songyuanqiao','宋远桥',1,'1.jpg',2,'2012-10-09',2,'2022-10-01 10:00:00','2022-10-01 10:00:00');

接口方法:

复制代码
@Mapper
public interface EmpMapper {

    @Insert("insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time) values (#{username}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, #{deptId}, #{createTime}, #{updateTime})")
    public void insert(Emp emp);

}

说明:#{...} 里面写的名称是对象的属性名

主键返回

概念:在数据添加成功后,需要获取插入数据库数据的主键。

  • 默认情况下,执行插入操作时,是不会主键值返回的。如果我们想要拿到主键值,需要在Mapper接口中的方法上添加一个Options注解,并在注解中指定属性useGeneratedKeys=true和keyProperty="实体类属性名"

主键返回代码实现:

复制代码
@Mapper
public interface EmpMapper {
    
    //会自动将生成的主键值,赋值给emp对象的id属性
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time) values (#{username}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, #{deptId}, #{createTime}, #{updateTime})")
    public void insert(Emp emp);

}

修改(更新)

根据id修改信息

SQL语句:

sql 复制代码
update emp set username = 'linghushaoxia', 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 = 18;

接口方法:

复制代码
@Mapper
public interface EmpMapper {
    /**
     * 根据id修改员工信息
     * @param emp
     */
    @Update("update emp set username=#{username}, name=#{name}, gender=#{gender}, image=#{image}, job=#{job}, entrydate=#{entrydate}, dept_id=#{deptId}, update_time=#{updateTime} where id=#{id}")
    public void update(Emp emp);
    
}

查询

根据id查询

SQL语句:

sql 复制代码
select id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time from emp;

接口方法:

复制代码
@Mapper
public interface EmpMapper {
    @Select("select id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time from emp where id=#{id}")
    public Emp getById(Integer id);
}

条件查询

  • 姓名:要求支持模糊匹配
  • 性别:要求精确匹配
  • 入职时间:要求进行范围查询
  • 根据最后修改时间进行降序排序
sql 复制代码
 @Mapper
public interface EmpMapper {

    @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);

}
相关推荐
我是你的开心果7785 分钟前
ai全栈应用开发学习day19
学习
SQL-First布道者9 分钟前
我为什么把 MyBatis 从项目中删了?
java·spring·tomcat·mybatis·mybatis plus·spring jdbc
爱折腾的编程老炮28 分钟前
潮玩品牌自建抽盒机小程序——商城 + 抽盒 + 一番赏 + 会员 + 社区,一套代码怎么搭
spring boot·小程序·vue·mybatis·uniapp
是隼人34 分钟前
buuctf-pwn wustctf2020_closed(文件描述符)题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
励志不掉头发的内向程序员40 分钟前
【LibreCAD 2D架构】从两个坐标到图形实体:RS_ActionDrawLine如何创建RS_Line
开发语言·c++·qt·学习·系统架构
kakawzw1 小时前
mybatis源码笔记1——JDBC和整体架构
java·mybatis
sunshine22 girl1 小时前
Java学习一 环境配置1 安装JDK,配置环境变量
java·开发语言·学习
我爱cope1 小时前
【计算机网络 | 传输层5:TCP 如何实现可靠传输?滑动窗口、累计确认与重传机制】
网络·网络协议·学习·tcp/ip·计算机网络·传输层
zfoo-framework1 小时前
kotlin技术栈各种 学习文档
学习
是隼人1 小时前
buuctf-pwn wdb_2018_3rd_soEasy(ret2shellcode)题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门