Mybatis中标签大全

article/2025/9/19 13:45:53

文章目录

一、标签分类

 二、标签总结

1. 基础SQL标签

1.1 查询select

1.2 增删改

1.3 其他基础标签

1.3.1 sql 标签

1.3.2 include 标签

1.3.3 if 标签

1.3.4 别名

2. collection与association标签

3. resultMap标签

4. foreach标签

5. where标签

6. set标签

7. trim标签

8. choose、when、otherwise标签

二、写在后面


一、标签分类

定义SQL语句

  • insert
  • delete
  • update
  • select

 配置关联关系

  • collection
  • association

 配置java对象属性与查询结果集中列名的对应关系

  • resultMap

 控制动态SQL拼接

  • foreach
  • if
  • choose

 格式化输出

  • where
  • set
  • trim

定义常量

  • sql

 其他

  • include

 二、标签总结

1. 基础SQL标签

1.1 查询select

    /*** 根据条件查询用户集合*/List<User> selectUsers(@Param("cond")Map<String, Object> map);
    <!-- 返回的是List,resultType给定的值是List里面的实体类而不是list,mybatis会自动把结果变成List --><select id="selectUsers" parameterType="map" resultType="con.it.bean.User">select id, username, password, sex, birthday, address from user u<where><trim suffixOverrides=","><if test="cond.username != null and cond.username != ''">u.username = #{cond.username},</if><if test="cond.sex != null">and u.sex = #{cond.sex},</if><if test="cond.beginTime != null"><![CDATA[  and DATE_FORMAT(u.birthday, '%Y-%m-%d %H:%T:%s') >= DATE_FORMAT(#{beginTime}, '%Y-%m-%d %H:%T:%s'),   ]]></if><if test="cond.endTime != null"><![CDATA[  and DATE_FORMAT(u.birthday, '%Y-%m-%d %H:%T:%s') <= DATE_FORMAT(#{endTime}, '%Y-%m-%d %H:%T:%s'),   ]]></if><if test="cond.address != null and cond.address != ''">and u.addrerss like '%' || #{cond.address} || '%',</if></trim></where></select>

1.2 增删改

    <insert id="insert" parameterType="com.it.bean.User"><!-- 使用序列插入oracle数据库返回主键,MYSQL数据库无需添加selectKey --><selectKey resultType="long" order="BEFORE" keyProperty="id">SELECT user_seq.NEXTVAL as id from DUAL</selectKey>insert into User (ID, USERNAME, PASSWORD, SEX, ADRESS, CREATED_BY, CREADTED_DATE)values (#{id}, #{username}, #{password}, #{sex}, #{adress}, #{createdBy}, SYSDATE)</insert>

1.3 其他基础标签

1.3.1 sql 标签

定义一些常用的sql语句片段

<sql id="selectParam">id, username, password, sex, birthday, address
</sql>

1.3.2 include 标签

引用其他的常量,通常和sql一起使用

<select>select <include refid="selectParam"></include>from user
</select>

1.3.3 if 标签

基本都是用来判断值是否为空,注意Integer的判断,mybatis会默认把0变成 ‘’

<if test="item != null and item != ''"></if><!-- 如果是Integer类型的需要把and后面去掉或是加上or-->
<if test="item != null"></if>
<if test="item != null and item != '' or item == 0"></if>

1.3.4 别名

经常使用的类型可以定义别名,方便使用,mybatis也注册了很多别名方便我们使用

<typeAliases><typeAlias type="com.it.bean.User" alias="User"/>
</typeAliases>

2. collection与association标签

collection与association的属性一样,都是用于resultMap返回关联映射使用,collection关联的是集合,而association是关联单个对象

/***问题表*/
public class Question {private Long id; //问题idprivate String question; //问题private Integer questionType; //问题类型private List<QuestionAnswer> answerList; //问题选项集合//Getter和Setter省略
}/***问题选项表*/
public class QuestionAnswer {private Long id; //选项idprivate Long questionId;  //问题idprivate String answer; //选项//Getter和Setter省略
}
<!-- 具体可参考下面ResultMap -->
<collection property="answerList" javaType="java.util.List"ofType="com.it.bean.QuestionAnswer" column="id" select="setlectQuestionAnswerByQuestionId"/>

3. resultMap标签

 

<!-- 返回关联查询的问题 -->
<resultMap id="detail_result" type="com.it.bean.Question"><id column="id" property="id" /><result column="question" property="question" /><result column="question_type" property="questionType" /><collection property="answerList" javaType="java.util.List"ofType="com.it.bean.QuestionAnswer" column="id" select="setlectQuestionAnswerByQuestionId"/>
</resultMap><!-- 查询问题集 -->
<select id="selectQuestions" parameterType="map" resultMap="detail_result">select q.id, q.question, q.question_type from question q <where><if test="cond.id != null">q.id = #{cond.id}</if><if test="cond.idList != null and cond.idList.size() != 0">q.id in <foreach collection="cond.idList" item="id" open="(" separator="," close=")">#{id}</foreach></if></where>
</select><!-- 查询对应问题的答案集 -->
<select id="setlectQuestionAnswerByQuestionId" parameterType="long" resultType="com.it.bean.QuestionAnswer">select a.id, a.answer from question_answer a where a.question_id = #{id}
</select>

4. foreach标签

<sql id="base_column">id, question_id, answer</sql><!-- oracle的批量插入 -->
<insert id="insertBatchOracle" parameterType="list">insert into question_answer ( <include refid="base_column" /> ) select question_answer_seq.NEXTVAL, A.* from (<foreach collection="list" item="item" separator="union all">select #{item.questionId}, #{item.answer} from dual</foreach>) A 
</insert><!-- Mysql的批量插入,主键自增 -->
<insert id="insertBatchMysql" parameterType="list">insert into question_answer ( <include refid="base_column" /> ) values <foreach collection="list" item="item" open="(" separator="union all" close=")">#{item.id}, #{item.questionId}, #{item.answer}</foreach>
</insert>

5. where标签

where用来去掉多条件查询时,开头多余的and

    <select id="selectUserList" parameterType="com.it.bean.User" resultType="com.it.bean.User"><!-- 引用Sql片段 -->select <include refid="selectParam"> from user u<where><!--where 可以自动去掉条件中的第一个and--><if test="id != null">and u.id = #{id}</if><if test="name != null and name != ''">and u.name = #{name}</if></where></select>

6. set标签

set是mybatis提供的一个智能标记,当在update语句中使用if标签时,如果前面的if没有执行,则或导致逗号多余错误。使用set标签可以将动态的配置SET 关键字,和剔除追加到条件末尾的任何不相关的逗号。
没有使用if标签时,如果有一个参数为null,都会导致错误,如下示例:
 

    <update id="updateUser" parameterType="com.it.bean.user">update user<set><if test="username != null and username != ''">username = #{username},</if><if test="sex != null and sex == 0 or sex == 1">sex = #{sex},</if><if test="birthday != null ">  birthday = #{birthday},</if >  <if test="address != null and address != ''">address = #{address},</if><if test="lastModifiedBy != null and lastModifiedBy != ''">last_modified_by = #{lastModifiedBy},last_modified_date = SYSDATE,</if></set><where>id = #{id}</where></update>

7. trim标签

prefix:前缀      
prefixoverride:去掉第一个and或者是or
suffixoverride:去掉最后一个逗号(也可以是其他的标记,就像是上面前缀中的and一样)suffix:后缀
<update id="test" parameterType="com.it.bean.User">update user<!-- 开头加上set,结尾去除最后一个逗号 --><trim prefix="set" suffixOverrides=","><if test="username!=null and username != ''">name= #{username},</if><if test="password!=null and password != ''">password= #{password},</if></trim><where>id = #{id}</where></update>

8. choose、when、otherwise标签

<select id="getUserList" resultType="com.it.bean.User" parameterType="com.it.bean.User">  SELECT <include refid="resultParam"></include> FROM User u   <where>  <choose>  <when test="username !=null and username != ''">  u.username LIKE CONCAT(CONCAT('%', #{username}),'%')  </when >  <when test="sex != null">  AND u.sex = #{sex}  </when >  <when test="birthday != null ">  AND u.birthday = #{birthday}  </when >  <otherwise>  </otherwise>  </choose>  </where>    
</select>  

二、写在后面


在网上看了很多标签的解释,但不是很全,我就自己总结了一份,搭配示例更好理解标签的含义,如有什么遗漏或是错误还望多多发言补充,我会继续完善。

注: 关于参数指定jdbcType,是因为当传参为null时候,mybatis无法自动判断类型,就必须要显示指定它的类型,多用于insert中
传入的参数的字段为null对象无法获取对应的jdbcType类型,而报的错误。 
你只要在insert语句中insert的对象加上jdbcType就可以了,修改如下: 
#{menuTitle,jdbcType=VARCHAR} 

 

 


http://chatgpt.dhexx.cn/article/EpZbyJ7G.shtml

相关文章

jarsigner命令行签名打包

jarsigner签名&#xff0c;之前都是通过AS进行签名&#xff0c;通过这种方式也是可以签名的&#xff0c;需要提前把需要的东西准备好&#xff0c;现在把步骤记录在下面。&#xff08;下面的操作都是在jdk的路径下进行操作&#xff09; 1.首先准备Jdk的路径&#xff08;在androi…

jarsigner: 无法打开 jar 文件: tap_unsign.apk

flutter 的 android项目上线&#xff0c;我们在想应用宝发布应用时&#xff0c;需将key.jks文件放入应用宝的空白文件中&#xff0c;在cmd中执行 jarsigner -verbose -keystore key.jks -signedjar baoming.apk tap_unsign.apk name遇到如下报错 jarsigner: 无法打开 jar 文件…

apk重签名之jarsigner命令签名

apk的签名工作可以通过两种方式来完成&#xff1a; 1&#xff09;通过ADT提供的图形化界面完成apk签名&#xff1b;2&#xff09;完全通过DOS命令来完成apk签名 1&#xff09;准备工作 我比较喜欢第2&#xff09;种方式&#xff0c;所以下面将讲解如何通过命令的方式完成apk…

jarsigner签名

上线了3年多的项目,最近为了进行市场推广,在现在主流的vivo,oppo,华为及百度和阿里进行上传应用.因为有些市场是因为有抓取包的行为(比如在oppo应用市场,以前从未在oppo市场上传过应用,但是能在他的市场中搜索到,这就是因为在其他市场比如应用宝这些市场抓取过包),因此在诸如op…

使用jarsigner工具apk签名算法问题

为 -digestalg 选项指定的SHA1 算法 被视为存在安全风险。此算法将在未来的更新中被禁用。 为 -sigalg 选项指定的SHA1withRSA 算法被视为存在安全风险。此算法将在未来的更新中被禁用。 修改&#xff1a; 将 signcmd "%sjarsigner" -keystore "%s" -st…

Metasploit远程控制安卓手机,jarsigner(工具找不到)签名使用问题zipalign(工具)及解决方法

问题点&#xff1a; 1.kali linux2020.3版本&#xff0c;在使用msf生成安卓木马时远控手机&#xff0c;签名时需要3个工具&#xff0c;分别是keytool,jarsigner,zipalign,只提示有keytool工具&#xff0c;jarsigner和zipalign工具找不到. 2.在模拟器中成功&#xff0c;但很多…

使用jarsigner对APK签名

写在文章中之前&#xff1a;网上当前很多关于jarsigner对APK签名的讲法&#xff0c;但是有些讲的很粗糙&#xff0c;有些还在就在抄袭他人的文章&#xff0c;而自己却没有去验证是否正确 一、由于自己在工作中要用到jarsigner对apk进行最后的发布签名&#xff0c;所以自己也去…

Android 应用平台加固之后操作(apk重签名「zipalign」「apksigner」「jarsigner」) 操作环境:MacOS

Android 应用平台加固之后操作&#xff08;apk重签名&#xff09;「zipalign」「apksigner」「jarsigner」 前言运行环境对齐&#xff08;zipalign&#xff09;重签名 apksigner (推荐方法)重签名 jarsigner (v1打包)具体使用对齐「zipalign」出现 "Output file ......apk…

Windows平台 使用jarsigner对Apk签名

使用的是JDK自带的jarsigner工具来完成Apk签名 1) 首先找到你的Java Jdk中bin的路径&#xff1a;C:\Program Files\Java\jdk1.8.0_152\bin jarsigner简单使用说明 #jarsigner的命令格式&#xff1a; jarsigner -verbose -keystore [您的私钥存放路径] -signedjar [签名后文件存…

jarsigner和apksigner对apk/aab签名

1、查看签名文件的信息 工具目录&#xff1a;C:\Program Files\Java\jre1.8.0_331\bin keytool -list -v -keystore E:\test.jks输入该命令&#xff0c;然后输入密码&#xff0c;即可查看签名信息 2、签名工具 签名工具说明位置jarsignerJDK提供的针对jar包签名的通用工具…

Android jarsigner和apksigner签名apk(.jks)

前言 为了增加 Apk 的安全性&#xff0c;防止反编译&#xff0c;我们会对开发的 Apk 做签名处理&#xff0c;如果要上架应用商店&#xff0c;部分还会要求 Apk 必须加固后才能上架。另外存在一部分免费加固apk后会删除AndroidStudio打包的签名&#xff0c;需要我们重新签名&am…

jarsigner命令详解

用法&#xff1a;jarsigner [选项] jar 文件别名 jarsigner -verify [选项] jar 文件 [-keystore <url>] 密钥库位置[-storepass <口令>] 用于密钥库完整性的口令[-storetype <类型>] 密钥库类型[-keypass <口令>] …

Java的安装及‘jarsigner‘ 不是内部或外部命令解决方法(附jdk8下载链接)

安装JDK 下载JDK&#xff1a; 网盘地址 , 提取码&#xff1a;y5f3 双击安装程序 一直点击下一步 到这里已经安装好了 但是执行 jarsigner命令会看到 ‘jarsigner‘ 不是内部或外部命令解决方法 的报错 解决 可能是因为在bin 目录下没有 jarsigner.exe&#xff0c;没有的…

【Android 教程系列第 28 篇】Android 分别使用 jarsigner 和 apksigner 对 APK 签名(v1、v2 签名)的详细教程

这是【Android 教程系列第 28 篇】&#xff0c;如果觉得有用的话&#xff0c;欢迎关注专栏。 为了增加 Apk 的安全性&#xff0c;防止反编译&#xff0c;我们会对开发的 Apk 做签名处理&#xff0c;如果要上架应用商店&#xff0c;部分还会要求 Apk 必须加固后才能上架。 而对…

【Java可执行命令】(十)JAR文件签名工具 jarsigner:通过数字签名及验证保证代码信任与安全,深入解析 Java的 jarsigner命令~

Java可执行命令之jarsigner 1️⃣ 概念2️⃣ 优势和缺点3️⃣ 使用3.1 语法3.1.1 可选参数&#xff1a;jarsigner -keystore < url>3.1.2 可选参数&#xff1a;jarsigner -storepass <口令>3.1.3 可选参数&#xff1a;jarsigner -keypass <口令>3.1.4 可选参…

您的凭据不工作,请输入新凭据

转载自 http://m.blog.csdn.net/article/details?id47706021 今天上班同事说远程桌面无法使用&#xff0c;然后VNC连接&#xff0c;查看情况&#xff0c;密码是正确的&#xff0c;防火墙也关了&#xff0c;百思不得其解。 上网查询问题&#xff0c;找到一个解决办法&#xff0…

远程桌面连接提示“您的凭据不工作”

本次操作的是企图远程别人的电脑配置&#xff0c;而非被远程的电脑 在域环境中&#xff0c;如果使用Vista及以上的操作系统&#xff0c;默认情况下&#xff0c;在连接到远程计算机时无法保存凭据。 当我们使用远程桌面连接时&#xff0c;即使已经勾选了“记住我的凭证”。 但是…

凭据不工作

最悲催的事情是什么&#xff1f;那就是你可以远程别人的电脑&#xff0c;但是别人不能远程自己的电脑&#xff01; 背景&#xff1a; 换了个win8.1的系统&#xff0c;刚开始可以远程上,过了几天,电脑突然不能远程了,让我很是郁闷呀. 于是在网上查了好多资料,看看他到底是什么…

win10远程桌面连接凭据怎么设置_win10远程桌面连接提示您的凭据不工作修复方法...

在win10系统中&#xff0c;使用远程桌面可以不在用户电脑前就可以帮助他人&#xff0c;轻松的解决一些自己不在场的难题&#xff0c;然而近日有用户却发现自己的win10系统在进行远程桌面连接时出现提示您的凭据不工作的问题&#xff0c;那么对此有什么方法能够解决呢&#xff1…

计算机远程连接提示凭证不工作,连接远程桌面提示您的凭证不工作怎么办

安装win10正式版&#xff0c;工作需要连接远程桌面&#xff0c;但是连接过程中&#xff0c;提示您的凭证不工作。原有保存的远程帐号密码无法使用&#xff0c;导致远程登录系统失败&#xff0c;连接远程桌面提示您的凭证不工作的解决方法其实很简单&#xff0c;下面由学习啦小编…