mybatis常用使用

news/2025/2/22 3:02:11/

1.xml模板

<?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.gen.mapper.StudentMapper">
</mapper>

2.普通增删改查

  • mapper文件
int insert(Student student);int delete(Integer id);int update(Student student);Student selectById(Integer id);List<Student> selectAll();
  • xml文件
<insert id="insert">INSERT INTO `student` (`name`, `sex`, `create_time`)VALUES (#{name}, #{sex}, #{createTime})
</insert><delete id="delete">delete from student where id = #{id}
</delete><update id="update">UPDATE `student` SET `name` = #{name}, `sex` = #{sex}, `create_time` = #{createTime} WHERE `id` = #{id};
</update><select id="selectById" resultType="com.gen.entity.Student">select * from student where id = #{id}
</select><select id="selectAll" resultType="com.gen.entity.Student">select * from student
</select>

3.模糊查询

  • mapper文件
List<Student> selectLikeName(String name);
  • xml文件
<select id="selectLikeName" resultType="com.gen.entity.Student">select * from student where name like concat('%', #{name}, '%')
</select>

4.驼峰字段Java对象映射

  • mapper配置文件
<settings><setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

5.获取自增主键

  • mapper文件
int insert(Student student);
  • xml文件
<insert id="insert" useGeneratedKeys="true" keyColumn="id" keyProperty="id">INSERT INTO `student` (`name`, `sex`, `create_time`)VALUES (#{name}, #{sex}, #{createTime})
</insert>

6.批量插入

  • foreach:用于循环拼接的内置标签,常用于批量新增、in查询等

    • collection:必填,值为要迭代循环的集合类型,情况有多种
      • 入参是List类型的时候,collection属性值为list
      • 入参是Map类型的时候,collection属性值为map的key值
    • item:每一个元素进行迭代时的别名
    • index:索引的属性名,在集合数组情况下值为当前索引值,当迭代对象是map时,这个值是map的key
    • open:整个循环内容的开头字符串
    • close:整个循环内容的结尾字符串
    • separator:每次循环的分隔符
  • mapper文件

int insertBatch(List<Student> list);
  • xml文件
<insert id="insertBatch">INSERT INTO `student` (`name`, `sex`, `create_time`) VALUES<foreach collection="list" item="item" separator=",">(#{item.name}, #{item.sex}, #{item.createTime})</foreach>
</insert>

7.部分更新非空字段

  • mapper文件
int update(Student student);
  • xml文件 (注意:数值类型不要加上!= ‘’,否则无法更新0值情况)
<update id="update">UPDATE `student`<trim prefix="set" suffixOverrides=","><if test="name != null and name != ''">`name` = #{name},</if><if test="sex != null and sex != ''">`sex` = #{sex},</if><if test="createTime != null">`create_time` = #{createTime},</if><if test="age != null">`age` = #{age},</if></trim>WHERE `id` = #{id}
</update>

8.转义字符

  • mapper文件
int deleteByCreateTime(@Param("startDate") LocalDateTime startDate, @Param("endDate") LocalDateTime endDate);
  • xml文件(两种方式)
<delete id="deleteByCreateTime"><![CDATA[delete from student where create_time >= #{startDate} and create_time <= #{endDate}]]>
</delete><delete id="deleteByCreateTime">delete from student where create_time >= #{startDate} and create_time &lt;= #{endDate}
</delete>

9.sql片段

  • mapper文件
List<Student> selectAll();
  • xml文件
<sql id="Base_Column">id, name</sql>
<select id="selectAll" resultType="com.gen.entity.Student">select<include refid="Base_Column"/>from student
</select>

10.一对一映射association

  • mapper文件
List<Student> selectAll();
  • xml文件
<resultMap id="BaseResultMap" type="com.gen.entity.Student"><id column="id" property="id"/><result column="name" property="name"/><result column="sex" property="sex"/><result column="create_time" property="createTime"/><result column="clazz_id" property="clazzId"/><association column="clazz_id" property="clazz" select="com.gen.mapper.ClazzMapper.selectById"/>
</resultMap>
<select id="selectAll" resultMap="BaseResultMap">select * from student
</select>

11.一对多映射collection

  • mapper文件
Clazz selectById(Integer id);
  • xml文件
<resultMap id="BaseResultMap" type="com.gen.entity.Clazz"><id column="id" property="id"/><result column="name" property="name"/><collection column="id" property="students" select="com.gen.mapper.StudentMapper.selectByClazzId"/>
</resultMap>
<select id="selectById" resultMap="BaseResultMap">select * from clazz where id = #{id}
</select>

http://www.ppmy.cn/news/1041641.html

相关文章

生活随笔,记录我的日常点点滴滴.

前言 &#x1f618;个人主页&#xff1a;曲终酣兴晚^R的小书屋&#x1f971; &#x1f615;作者介绍&#xff1a;一个莽莽撞撞的&#x1f43b; &#x1f496;专栏介绍&#xff1a;日常生活&往事回忆 &#x1f636;‍&#x1f32b;️每日金句&#xff1a;被人暖一下就高热&…

【C++初阶】string类字符串包不包含‘\0‘

&#x1f466;个人主页&#xff1a;Weraphael ✍&#x1f3fb;作者简介&#xff1a;目前学习C和算法 ✈️专栏&#xff1a;C航路 &#x1f40b; 希望大家多多支持&#xff0c;咱一起进步&#xff01;&#x1f601; 如果文章对你有帮助的话 欢迎 评论&#x1f4ac; 点赞&#x1…

ImageKit10 VCL Crack

ImageKit10 VCL Crack ImageKit10 VCL是一个允许您快速轻松地将图像处理功能添加到应用程序中的组件。使用ImageKit10 VCL&#xff0c;您可以编写从TWAIN扫描仪和数码相机检索图像的应用程序;加载和保存图像文件&#xff0c;并将图像从一种格式转换为另一种格式;编辑图像、在图…

TDD(测试驱动开发)?

01、前言 很早之前&#xff0c;曾在网络上见到过 TDD 这 3 个大写的英文字母&#xff0c;它是 Test Driven Development 这三个单词的缩写&#xff0c;也就是“测试驱动开发”的意思——听起来很不错的一种理念。 其理念主要是确保两件事&#xff1a; 确保所有的需求都能被照…

构建可远程访问的企业内部论坛

文章目录 前言1.cpolar、PHPStudy2.Discuz3.打开PHPStudy&#xff0c;安装网页论坛所需软件4.进行网页运行环境的构建5.运行Discuz网页程序6.使用cpolar建立穿透内网的数据隧道&#xff0c;发布到公网7.对云端保留的空白数据隧道进行配置8.Discuz论坛搭建完毕 前言 企业在发展…

基于JAVA的仓库管理系统java库房仓储进销存jsp源代码

本项目为前几天收费帮学妹做的一个项目&#xff0c;Java EE JSP项目&#xff0c;在工作环境中基本使用不到&#xff0c;但是很多学校把这个当作编程入门的项目来做&#xff0c;故分享出本项目供初学者参考。 一、项目描述 基于JAVA的仓库管理系统 系统有2权限&#xff1a;用户…

视频上传,限制时长,获取视频时长

使用element的upload上传文件时&#xff0c;除了类型和大小&#xff0c;需求需要限制只能长传18秒内的视频&#xff0c;这里通过upload的before-upload&#xff0c;以及创建一个音频元素对象拿到durtaion时长属性来实现。 getVideoTime(file) {return new Promise(async (resol…

密码学学习笔记(十九):密码学关键术语的解释1

数据加密标准(DES) 数据加密标准是使用最广泛的加密体制&#xff0c;它于1977年被美国国家标准和技术研究所(NIST)采纳为联邦信息处理标准FIPS PUB 46。 DES3DESAES明文分组长度&#xff08;位&#xff09;6464128密文分组长度&#xff08;位&#xff09;6464128密钥长度&…