sql xml模板

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.example.mapper.UserMapper"><!-- 结果映射 --><resultMap id="UserResultMap" type="com.example.entity.User"><id property="id" column="id"/><result property="username" column="username"/><result property="email" column="email"/><result property="age" column="age"/><result property="status" column="status"/><result property="createTime" column="create_time"/></resultMap><!-- 公共字段:SELECT 字段列表 --><sql id="columns">id, username, email, age, status, create_time</sql><!-- 公共字段:INSERT 列名 --><sql id="insertColumns">username, email, age, status, create_time</sql><!-- 公共字段:INSERT 值 --><sql id="insertValues">#{username, jdbcType=VARCHAR},#{email, jdbcType=VARCHAR},#{age, jdbcType=INTEGER},#{status, jdbcType=TINYINT},#{createTime, jdbcType=TIMESTAMP}</sql><!-- 公共字段:UPDATE set 片段 --><sql id="updateSet"><set><if test="username != null">username = #{username, jdbcType=VARCHAR},</if><if test="email != null">email = #{email, jdbcType=VARCHAR},</if><if test="age != null">age = #{age, jdbcType=INTEGER},</if><if test="status != null">status = #{status, jdbcType=TINYINT},</if><if test="createTime != null">create_time = #{createTime, jdbcType=TIMESTAMP},</if></set></sql><!-- 公共条件:WHERE 动态条件(含防转义) --><sql id="whereCondition"><where><!-- 使用 &lt; 和 &gt; 转义简单比较 --><if test="status != null">AND status = #{status, jdbcType=TINYINT}</if><if test="username != null and username != ''">AND username LIKE CONCAT('%', #{username, jdbcType=VARCHAR}, '%')</if><if test="email != null and email != ''">AND email = #{email, jdbcType=VARCHAR}</if><!-- 使用 &gt;= 和 &lt;= 转义 --><if test="age != null">AND age &gt;= #{age, jdbcType=INTEGER}</if><!-- 使用 CDATA 包裹复杂时间范围条件 --><if test="startTime != null or endTime != null">AND <![CDATA[create_time >= #{startTime, jdbcType=TIMESTAMP}AND create_time <= #{endTime, jdbcType=TIMESTAMP}]]></if><!-- 使用 OGNL 逻辑判断 + 转义 --><if test="score != null and score gt 90">AND score &gt; 90</if><if test="score != null and score le 60">AND score &lt;= 60</if></where></sql><!-- 公共分页 --><sql id="limitClause"><if test="offset != null and limit != null">LIMIT #{offset, jdbcType=INTEGER}, #{limit, jdbcType=INTEGER}</if></sql><!-- 查询所有用户 --><select id="selectAll" resultMap="UserResultMap">SELECT<include refid="columns"/>FROM userWHERE status = #{value, jdbcType=TINYINT}ORDER BY id DESC</select><!-- 根据 ID 查询 --><select id="selectById" parameterType="java.lang.Long" resultMap="UserResultMap">SELECT<include refid="columns"/>FROM userWHERE id = #{id, jdbcType=BIGINT}</select><!-- 条件查询 --><select id="selectByCondition" parameterType="java.util.Map" resultMap="UserResultMap">SELECT<include refid="columns"/>FROM user<include refid="whereCondition"/>ORDER BY create_time DESC<include refid="limitClause"/></select><!-- 查询总数 --><select id="countByCondition" parameterType="java.util.Map" resultType="int">SELECT COUNT(*)FROM user<include refid="whereCondition"/></select><!-- 插入用户 --><insert id="insertUser" parameterType="com.example.entity.User">INSERT INTO user (<include refid="insertColumns"/>) VALUES (<include refid="insertValues"/>)</insert><!-- 更新用户 --><update id="updateUser" parameterType="com.example.entity.User">UPDATE user<include refid="updateSet"/>WHERE id = #{id, jdbcType=BIGINT}</update><!-- 删除用户 --><delete id="deleteUserById" parameterType="java.lang.Long">DELETE FROM userWHERE id = #{id, jdbcType=BIGINT}</delete></mapper>

<resultMap> 有嵌套对象

public class User {private Long id;private String username;private Address address; // 嵌套对象// getter 和 setter
}public class Address {private String street;private String city;private String zipCode;// getter 和 setter
}
<resultMap id="UserWithAddressResultMap" type="com.example.entity.User"><id property="id" column="id"/><result property="username" column="username"/><!-- 嵌套对象:来自 addresses 表 --><association property="address" javaType="com.example.entity.Address"><id property="id" column="address_id"/>        <!-- address 的主键 --><result property="street" column="street"/><result property="city" column="city"/><result property="zipCode" column="zip_code"/></association>
</resultMap><select id="selectUserWithAddress" resultMap="UserWithAddressResultMap">SELECT u.id,u.username,u.address_id,a.street,a.city,a.zip_codeFROM users uLEFT JOIN addresses a ON u.address_id = a.idWHERE u.id = #{id}
</select>

不用写<resultMap id="UserResultMap" type="com.example.entity.User">

<select id="selectUser" resultType="com.example.entity.User">SELECTid,username,email,age,status,create_time AS createTimeFROM users
</select>

循环sql

<select id="selectUsersByIds" resultType="com.example.entity.User">SELECT id, username, email, create_time AS createTimeFROM usersWHERE id IN<foreach item="id" index="index" collection="list" open="(" separator="," close=")">#{id}</foreach>
</select>
<insert id="batchInsert">INSERT INTO users (username, email, create_time)VALUES<foreach collection="list" item="user" separator=",">(#{user.username}, #{user.email}, #{user.createTime})</foreach>
</insert>

<if>:条件判断

<select id="selectUsers" resultType="User">SELECT id, username, email, create_time AS createTimeFROM usersWHERE 1=1<if test="username != null and username != ''">AND username LIKE CONCAT('%', #{username}, '%')</if><if test="email != null and email != ''">AND email = #{email}</if><if test="age != null">AND age >= #{age}</if>
</select>

<where>:智能 WHERE(自动处理 AND/OR

<select id="selectUsers" resultType="User">SELECT id, username, email, create_time AS createTimeFROM users<where><if test="username != null and username != ''">AND username LIKE CONCAT('%', #{username}, '%')</if><if test="status == 1">AND status = 1</if><if test="minAge != null">AND age >= #{minAge}</if></where>
</select>

<set>:用于 UPDATE,自动处理逗号 (自动去掉最后一个逗号)

<update id="updateUser">UPDATE users<set><if test="username != null">username = #{username},</if><if test="email != null">email = #{email},</if><if test="age != null">age = #{age},</if><if test="status != null">status = #{status}</if></set>WHERE id = #{id}
</update>

<choose><when><otherwise>:类似 Java 的 switch

<select id="selectUsersByCondition" resultType="User">SELECT id, username, email, statusFROM users<where><choose><when test="type == 'admin'">AND status = 1 AND username LIKE 'admin%'</when><when test="type == 'active'">AND status = 1</when><otherwise>AND status IN (0, 1)</otherwise></choose></where>
</select>

补充:mybatis-plus 配置

spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver

mybatis-plus:
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: com.example.entity
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl  # 打印 SQL

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/news/920143.shtml
繁体地址,请注明出处:http://hk.pswp.cn/news/920143.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

docker在自定义网络中安装ElasticSearch和Kibana

创建自定义网络 创建一个名为 es-net 的桥接网络。这将作为 Elasticsearch 和 Kibana 的私有通信通道。 # 创建网络 docker network create es-net # 查看网络是否创建成功 docker network ls启动 Elasticsearch 容器 安装命令 docker run -d \--name elasticsearch \--net…

基于51单片机射频RFID停车刷卡计时收费系统设计

1 系统功能介绍 本设计题目为 基于51单片机射频RFID停车刷卡计时收费系统设计&#xff0c;旨在实现停车场车辆的刷卡计时和收费管理。系统通过单片机控制&#xff0c;结合 RFID 射频识别技术、LCD1602 显示以及蜂鸣器报警&#xff0c;实现停车时间的智能计时、累加及超时提醒功…

Netty源码—性能优化和设计模式

1.Netty的两大性能优化工具 (1)FastThreadLocal FastThreadLocal的作用与ThreadLocal相当&#xff0c;但比ThreadLocal更快。ThreadLocal的作用是多线程访问同一变量时能够通过线程本地化的方式避免多线程竞争、实现线程隔离。 Netty的FastThreadLocal重新实现了JDK的ThreadLoc…

Linux网络设备分析

🐧 Linux 网络设备驱动深入分析 本文将详细分析 Linux 网络设备驱动的工作原理、实现机制和代码框架,并通过一个虚拟网卡实例展示其实现,最后介绍常用的工具和调试手段。 1️⃣ Linux 网络设备驱动概述 Linux 网络设备驱动是内核中负责管理网络硬件(如以太网卡、Wi-Fi …

计算机视觉:从 “看见” 到 “理解”,解锁机器感知世界的密码

早上醒来&#xff0c;你拿起手机&#xff0c;人脸识别瞬间解锁屏幕&#xff1b;开车上班时&#xff0c;车载系统通过摄像头实时识别车道线&#xff0c;提醒你不要偏离&#xff1b;去医院做检查&#xff0c;医生用 AI 辅助的医学影像系统快速定位肺部微小结节&#xff1b;逛超市…

深入了解linux系统—— 线程封装

C11线程库 C11也提供了对应的线程库&#xff0c;在头文件<thread>中&#xff1b;C11将其封装成thread类&#xff0c;通过类实例化出对象&#xff0c;调用类内成员方法进行线程控制。 #include <iostream> #include <thread> #include <unistd.h> using…

安全防御-SCDN如何保护网站安全

随着互联网的快速发展&#xff0c;越来越多的企业依赖在线服务来运行其核心业务。与此同时&#xff0c;网络攻击的频率和复杂性也在不断增加&#xff0c;恶意流量成为许多企业头疼的问题。为了有效地提高网站的安全性和稳定性&#xff0c;德迅云安全加速SCDN被许多用户关注。今…

运筹优化(OR)-在机器学习(ML)浪潮中何去何从?

在如今机器学习的浪潮中&#xff0c;机器学习相关的岗位日益增多&#xff0c;而运筹优化的岗位却相对较少。这是今年我秋招过程中看到的现象。企业越来越希望候选人不仅能建模求解&#xff0c;还能理解如何用数据驱动优化。需要我们有一个完整的技术栈。那么我们就来看看OR与ML…

GitHub Copilot 在 VS Code 上的终极中文指南:从安装到高阶玩法

GitHub Copilot 在 VS Code 上的终极中文指南&#xff1a;从安装到高阶玩法 前言 GitHub Copilot 作为 AI 编程助手&#xff0c;正在彻底改变开发者的编码体验。本文将针对中文开发者&#xff0c;深度解析如何在 VS Code 中高效使用 Copilot&#xff0c;涵盖基础设置、中文优化…

安全测试、web探测、httpx

&#x1f4a2; 简介 httpx 是一个快速且多用途的HTTP工具包&#xff0c;允许使用retryablehttp库运行多个探测器。它旨在通过增加线程数量来保持结果的可靠性。 功能 &#x1f92a; 发送 GET、POST、PUT、DELETE 等 HTTP 请求支持流式传输支持重定向支持身份验证支持代理支持 …

CNN 中 3×3 卷积核等设计背后的底层逻辑

为什么卷积核爱用 33&#xff1f;CNN 设计 “约定俗成” 的底层逻辑 做深度学习的同学&#xff0c;对 CNN 里 33 卷积核、最大池化、BN 层这些设计肯定不陌生&#xff0c;但你有没有想过&#xff1a;为啥卷积核总选 33&#xff1f;池化层为啥默认最大池化&#xff1f;BN 层又是…

税务岗位职场能力解析与提升路径规划

税务岗位作为企业运营的核心环节之一&#xff0c;对从业者的专业能力与综合素质要求极高。从基础税务核算到战略税务筹划&#xff0c;职场能力的提升需要系统化的路径规划。以下从核心能力、阶段化提升路径及证书价值三个维度展开分析。核心能力体系构建专业税务能力是基础&…

MySQL 索引:结构、对比与操作实践指南

MySQL系列 文章目录MySQL系列前言案例一、认识MySQL与磁盘1.1 MySQL与存储1.2 MySQL 与磁盘交互基本单位二、 MySQL 数据交互核心&#xff1a;BufferPool 与 IO 优化机制三、索引的理解3.1 测试案例3.2 page3.3 页目录3.3 对比其他结构四、聚簇索引 VS 非聚簇索引五、索引操作5…

GitHub 热榜项目 - 日榜(2025-08-24)

GitHub 热榜项目 - 日榜(2025-08-24) 生成于&#xff1a;2025-08-24 统计摘要 共发现热门项目&#xff1a;20 个 榜单类型&#xff1a;日榜 本期热点趋势总结 本期GitHub热榜呈现三大技术热点&#xff1a;1&#xff09;AI应用爆发式创新&#xff0c;包括神经拟真伴侣&#…

纯净Win11游戏系统|24H2专业工作站版,预装运行库,无捆绑,开机快,游戏兼容性超强!

哈喽&#xff0c;大家好&#xff01; 今天给大家带来一款 Windows 11 游戏版本系统镜像&#xff0c;软件已放在文章末尾&#xff0c;记得获取。 一、软件获取与启动 解压后双击exe即可直接运行&#xff0c;无需额外安装。首次启动界面简洁&#xff0c;引导清晰。 二、系统选…

CI/CD 学习之路

目录 简介&#xff1a; 1、工具介绍&#xff1a; 2、搭建jenkins 1&#xff09;创建一个文件Dockerfile&#xff0c;文件无后缀&#xff0c;写入以下代码 2&#xff09;在Dockerfile文件所在目录执行&#xff08;my-jenkins-android 未自定义镜像名称&#xff09; 3&#xf…

马斯克宣布开源Grok 2.5:非商业许可引争议,模型需8×40GB GPU运行,Grok 3半年后开源

昨晚&#xff0c;马斯克在 X 平台连续发布多条消息&#xff0c;宣布其人工智能公司 xAI 已正式开源 Grok 2.5 模型。这款模型是 xAI 在 2024 年的主力模型&#xff0c;如今完全向公众开放。与此同时&#xff0c;马斯克还预告了下一代模型 Grok 3 的开源计划&#xff0c;预计将在…

DMP-Net:面向脑组织术中成像的深度语义先验压缩光谱重建方法|文献速递-深度学习人工智能医疗图像

Title题目DMP-Net: Deep semantic prior compressed spectral reconstruction methodtowards intraoperative imaging of brain tissueDMP-Net&#xff1a;面向脑组织术中成像的深度语义先验压缩光谱重建方法01文献速递介绍脑肿瘤可分为原发性和继发性两类。原发性脑肿瘤多发生…

【nl2sql综述】2025最新综述解读

论文地址&#xff1a;https://arxiv.org/pdf/2408.05109 解读&#xff1a;迈向数据民主化——大型语言模型时代下的Text-to-SQL技术综述 近期&#xff0c;一篇名为《A Survey of Text-to-SQL in the Era of LLMs》的综述论文系统性地梳理了自然语言到SQL查询&#xff08;Text-t…

logback-spring.xml 文件

一.概述这是一个日志文件&#xff0c;主要用来对应用程序的日志进行记录&#xff0c;并且可以配置日志的一些格式和规则。二.读取机制1.SpingBoot自动识别进行文件扫描时&#xff0c;当在 classpath 下发现名为 logback-spring.xml 的文件时&#xff0c;Spring Boot 会自动加载…