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

springboot-mybatis的增删改查

目录

一、准备工作

二、常用配置

 三、尝试

 四、增删改查

1、增加

2、删除

3、修改

4、查询

五、XML的映射方法


一、准备工作

实施前的准备工作:

  1. 准备数据库表

  2. 创建一个新的springboot工程,选择引入对应的起步依赖(mybatis、mysql驱动、lombok)

  3. application.properties中引入数据库连接信息

  4. 创建对应的实体类 Emp(实体类属性采用驼峰命名)

  5. 准备Mapper接口 EmpMapper

SQL文件:emp的sql文件

二、常用配置

#指定mybatis输出日志的位置, 输出控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl# 查询的时候mybatis驼峰命名法
mybatis.configuration.map-underscore-to-camel-case=true

 三、尝试

在Mybatis中提供的参数占位符有两种:${...} 、#{...}

  • #{...}

    • 执行SQL时,会将#{…}替换为?,生成预编译SQL,会自动设置参数值

    • 使用时机:参数传递,都使用#{…}

  • ${...}

    • 拼接SQL。直接将参数拼接在SQL语句中,存在SQL注入问题

    • 使用时机:如果对表名、列表进行动态设置时使用

注意事项:在项目开发中,建议使用#{...},生成预编译SQL,防止SQL注入安全。

 四、增删改查

1、增加

 // 新增@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})")int insert(Emp emp);

        测试

 // 新增@Testpublic void empAdd(){//创建员工对象Emp emp = new Emp();emp.setUserName("小明");emp.setName("小将");emp.setImage("sadasdasd.jpg");emp.setGender((short)1);emp.setJob(1);emp.setEntryDate(LocalDate.of(2000,1,1));emp.setCreateTime(LocalDate.now());emp.setUpdateTime(LocalDate.now());emp.setDeptId(1);empMapper.insert(emp);}

2、删除

  // 删除@Delete("delete from emp where id = #{id}")int delete(int id);

        test

 // 删除测试@Testpublic void empDelete() {var s =  empMapper.delete(17);System.out.printf("删除:%s\n",s);}

3、修改

  // 修改@Update("update emp set username = #{userName}, name = #{name}, gender = 3 where id = 18;")void update(Emp emp);

        Test

  // 修改@Testpublic  void update(){Emp emp = new Emp();emp.setName("大卫");emp.setUserName("daadasd");emp.setGender(2);empMapper.update(emp);}

4、查询

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

        Test

 // 查询@Testpublic void search(){List<Emp> emp =   empMapper.list("汤姆", (short) 1,LocalDate.of(2000,8,15),LocalDate.of(2023,8,5));System.out.println(emp);}

五、XML的映射方法

  • <sql>:定义可重用的SQL片段

  • <include>:通过属性refid,指定包含的SQL片段

  • <if>

    • 用于判断条件是否成立,如果条件为true,则拼接SQL

    • 形式:

      <if test="name != null"> … </if>
  • <where>

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

  • <set>

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

  • <foreach>​​​​​​​遍历deleteByIds方法中传递的参数ids集合

       <foreach collection="集合名称" item="集合遍历出来的元素/项" separator="每一次遍历使用的分隔符" open="遍历开始前拼接的片段" close="遍历结束后拼接的片段">
    </foreach>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.demo.crud.mapper.EmpMapper"><!--提取重复代码 --><sql id="commonSelect">select * from emp </sql><!--  查询-->
<!--   resultType单条记录封装的类型 --><select id="list" resultType="com.demo.crud.pojo.Emp"><include refid="commonSelect"/><where><if test="name != null">name like concat('%',#{name},'%')</if>order by update_time desc</where></select><!--删除操作--><delete id="deleteByIds">delete from emp where id in<foreach collection="ids" item="id" separator="," open="(" close=")">#{id}</foreach></delete></mapper>

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

相关文章:

  • HTML5(H5)的前生今世
  • 抽象工厂模式(Abstract Factory)
  • Java 实现下载文件工具类
  • C# 12 预览版的新功能
  • 34.利用matlab解 多变量多目标规划问题(matlab程序)
  • 暑假刷题第18天--7/30
  • 通向架构师的道路之Apache整合Tomcat
  • 如何消除“信息孤岛”对业务增长的威胁?
  • Kali部署dvwa和pikachu靶场
  • ​LeetCode解法汇总722. 删除注释
  • Linux中的firewall-cmd
  • python 最大归一化
  • Netty:ByteBuf写入数据、读出数据
  • C++(15):面向对象程序设计
  • 2023牛客暑期多校训练营6-A Tree
  • Vc - Qt - QPainter::SmoothPixmapTransform及QPainter::Antialiasing
  • 【练习】条件变量:创建三个线程 id号为ABC,三个线程循环打印自己的ID号,运行顺序为 ABCABC
  • SpringBoot项目修改中静态资源,只需刷新页面无需重启项目(附赠—热加载)
  • clear_data_code_2d_model
  • “深入剖析JVM:揭秘Java虚拟机的工作原理“
  • elementui表格table中实现内容的换行
  • java 框架
  • 死锁的发生原因和怎么避免
  • visual studio 生成dll文件以及修改输出dll文件名称操作
  • 【Leetcode】73.矩阵置零
  • zabbix监控mysql容器主从同步状态并告警钉钉/企业微信
  • ARM 常见汇编指令学习 9 - 缓存管理指令 DC 与 IC
  • 落地数字化管理,提升企业市场竞争力
  • 2023华数杯数学建模竞赛C题思路解析
  • Photon之如何解决Photon Server无法在局域网使用的bug