mybatis常用标签

article/2025/9/19 13:46:31

在这里插入图片描述

一.定义sql语句

1.select 标签

属性介绍:
(1)id :唯一的标识符.
(2)parameterType:传给此语句的参数的全路径名或别名 例:com.test.poso.User或user
(3)resultType :语句返回值类型或别名。注意,如果是集合,那么这里填写的是集合的泛型,而不是集合本身(resultType 与resultMap 不能并用)

<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="Object">select * from student where id=#{id}
</select>

2.insert标签

属性介绍:
(1)id :唯一的标识符
(2)parameterType:传给此语句的参数的全路径名或别名 例:com.test.poso.User

<insert id="insertAgent" parameterType="Object">INSERT  INTO t_node_agent (host_name, os_type, created_date)VALUES (#{hostName}, #{osType}, #{createdDate})
</insert>

3.delete标签
属性同 insert

<delete id="deleteByPrimaryKey" parameterType="Object">delete from student where id=#{id}
</delete>

4.update标签
属性同 insert

<update id="updateOne"  parameterType="com.inspur.search.data.EntityRelation">update ENTITY_RELATION SET SRC_ID=#{srcId},SRC_TYPE=#{srcType},DEST_ID=#{destId},DEST_TYPE=#{destType},REL_TYPE=#{relType},STATUS=#{status},SN_ID=#{snId}where id=#{id}
</update>

二.配置对象属性与查询结果集

1.resultMap 标签的使用

基本作用:建立SQL查询结果字段与实体属性的映射关系信息

查询的结果集转换为java对象,方便进一步操作

将结果集中的列与java对象中的属性对应起来并将值填充进去

注意:与java对象对应的列不是数据库中表的列名,而是查询后结果集的列名

<resultMap id="getStudentRM" type="EStudnet"><id property="id" column="ID"/><result property="studentName" column="Name"/><result property="studentAge" column="Age"/>
</resultMap>
<select id="getStudent" resultMap="getStudentRM">SELECT ID, Name, AgeFROM TStudent
</select>

标签说明:

主标签
(1)id:该resultMap的标志
(2)type:返回值的类名,此例中返回EStudnet类

子标签:
(1)id:用于设置主键字段与领域模型属性的映射关系,此处主键为ID,对应id。
(2)result:用于设置普通字段与领域模型属性的映射关系

三.动态拼接SQL

1.if 标签的使用
  if标签通常用于WHERE语句中,通过判断参数值来决定是否使用某个查询条件, 他也经常用于UPDATE语句中判断是否更新某一个字段,还可以在INSERT语句中用来判断是否插入某个字段的值

<select id="getStudentListLikeName" parameterType="StudentEntity" resultMap="studentResultMap">     SELECT * from STUDENT_TBL ST       
WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')      
</select> 

  但是此时如果studentName是null或空字符串,此语句很可能报错或查询结果为空。此时我们使用if动态sql语句先进行判断,如果值为null或等于空字符串,我们就不进行此条件的判断。

修改为:

<select id=" getStudentListLikeName " parameterType="StudentEntity" resultMap="studentResultMap">     SELECT * from STUDENT_TBL ST      <if test="studentName!=null and studentName!='' ">     WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')      </if>     
</select>  

2.foreach 标签的使用
foreach标签主要用于构建in条件,他可以在sql中对集合进行迭代。如下:

<delete id="deleteBatch"> delete from user where id in<foreach collection="array" item="id" index="index" open="(" close=")" separator=",">#{id}</foreach>
</delete>

我们假如说参数为---- int[] ids = {1,2,3,4,5} ----那么打印之后的SQL如下:
delete form user where id in (1,2,3,4,5)

释义:

(1)collection :collection属性的值有三个分别是list、array、map三种,分别对应的参数类型为:List、数组、map集合,我在上面传的参数为数组,所以值为array
item : 表示在迭代过程中每一个元素的别名
(2)index :表示在迭代过程中每次迭代到的位置(下标)
(3)open :前缀
(4)close :后缀
(5)separator :分隔符,表示迭代时每个元素之间以什么分隔
    
我们通常可以将之用到批量删除、添加等操作中。

3.choose 标签的使用
  有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。MyBatis提供了choose 元素,按顺序判断when中的条件是否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满则时,则执行 otherwise中的sql。类似于Java 的switch 语句,choose为switch,when为case,otherwise则为default。
if是与(and)的关系,而choose是或(or)的关系。
  例如下面例子,同样把所有可以限制的条件都写上,方面使用。选择条件顺序,when标签的从上到下的书写顺序:

<select id="getStudentListChooseEntity" parameterType="StudentEntity" resultMap="studentResultMap">     SELECT * from STUDENT_TBL ST      <where>     <choose>     <when test="studentName!=null and studentName!='' ">     ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')      </when>     <when test="studentSex!= null and studentSex!= '' ">     AND ST.STUDENT_SEX = #{studentSex}      </when>     <when test="studentBirthday!=null">     AND ST.STUDENT_BIRTHDAY = #{studentBirthday}      </when>     <when test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' ">     AND ST.CLASS_ID = #{classEntity.classID}      </when>     <otherwise>     </otherwise>     </choose>     </where>     
</select>  

四.格式化输出

1.where

当if标签较多时,这样的组合可能会导致错误。例如,like姓名,等于指定性别等:

<!-- 查询学生list,like姓名,=性别 -->     
<select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">     SELECT * from STUDENT_TBL ST      WHERE      <if test="studentName!=null and studentName!='' ">     ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')      </if>     <if test="studentSex!= null and studentSex!= '' ">     AND ST.STUDENT_SEX = #{studentSex}      </if>     
</select>  

如果上面例子,参数studentName为null或’’,则或导致此sql组合成“WHERE AND”之类的关键字多余的错误SQL。
这时我们可以使用where动态语句来解决。这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。

<!-- 查询学生list,like姓名,=性别 -->     
<select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">     SELECT * from STUDENT_TBL ST      <where>     <if test="studentName!=null and studentName!='' ">     ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')      </if>     <if test="studentSex!= null and studentSex!= '' ">     AND ST.STUDENT_SEX = #{studentSex}      </if>     </where>     
</select>    

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

<!-- 更新学生信息 -->     
<update id="updateStudent" parameterType="StudentEntity">     UPDATE STUDENT_TBL      SET STUDENT_TBL.STUDENT_NAME = #{studentName},      STUDENT_TBL.STUDENT_SEX = #{studentSex},      STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},      STUDENT_TBL.CLASS_ID = #{classEntity.classID}      WHERE STUDENT_TBL.STUDENT_ID = #{studentID};      
</update>   

使用set+if标签修改后,如果某项为null则不进行更新,而是保持数据库原值。如下示例:

<!-- 更新学生信息 -->     
<update id="updateStudent" parameterType="StudentEntity">     UPDATE STUDENT_TBL      <set>     <if test="studentName!=null and studentName!='' ">     STUDENT_TBL.STUDENT_NAME = #{studentName},      </if>     <if test="studentSex!=null and studentSex!='' ">     STUDENT_TBL.STUDENT_SEX = #{studentSex},      </if>     <if test="studentBirthday!=null ">     STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},      </if>     <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">     STUDENT_TBL.CLASS_ID = #{classEntity.classID}      </if>     </set>     WHERE STUDENT_TBL.STUDENT_ID = #{studentID};      
</update>  

3.trim
trim是更灵活的去处多余关键字的标签,他可以实践where和set的效果。
where例子的等效trim语句:

<!-- 查询学生list,like姓名,=性别 -->     
<select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">     SELECT * from STUDENT_TBL ST      <trim prefix="WHERE" prefixOverrides="AND|OR">     <if test="studentName!=null and studentName!='' ">     ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')      </if>     <if test="studentSex!= null and studentSex!= '' ">     AND ST.STUDENT_SEX = #{studentSex}      </if>     </trim>     
</select>     

set例子的等效trim语句:

<!-- 更新学生信息 -->     
<update id="updateStudent" parameterType="StudentEntity">     UPDATE STUDENT_TBL      <trim prefix="SET" suffixOverrides=",">     <if test="studentName!=null and studentName!='' ">     STUDENT_TBL.STUDENT_NAME = #{studentName},      </if>     <if test="studentSex!=null and studentSex!='' ">     STUDENT_TBL.STUDENT_SEX = #{studentSex},      </if>     <if test="studentBirthday!=null ">     STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},      </if>     <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">     STUDENT_TBL.CLASS_ID = #{classEntity.classID}      </if>     </trim>     WHERE STUDENT_TBL.STUDENT_ID = #{studentID};      
</update>   

五.配置关联关系

1.collection
一对一
association通常用来映射一对一的关系,例如,有个类user,对应的实体类如下:(getter,setter方法省略)

private String id;//主键
private String userName;//用户姓名

有个类Article,对应的实体类如下:

private String id;//主键
private String articleTitle;//文章标题
private String articleContent;//文章内容

如果我想查询一个用户的时候,也查到他写的一篇文章,可以怎样写呢?在类user加入一个属性article

private String id;//主键
private String userName;//用户姓名
private Article article;//新增的文章属性

mapper.xml 我在user类的mapper.xml这样配置

<resultMap id="userResultMap" type="test.mybatis.entity.User"><id column="id" property="id" jdbcType="VARCHAR" javaType="java.lang.String"/><result column="userName" property="userName" jdbcType="VARCHAR" javaType="java.lang.String"/>
//这里把user的id传过去<association property="article" column="id"                       select="test.mybatis.dao.articleMapper.selectArticleByUserId" />//test.mybatis.dao.articleMapper为命名空间</resultMap>

同时,我的article对应的xml这样写:

<resultMap id="articleResultMap" type="test.mybatis.entity.Article"><id column="id" property="id" jdbcType="VARCHAR" javaType="java.lang.String"/><result column="articleTitle" property="articleTitle" jdbcType="VARCHAR" javaType="java.lang.String"/><result column="articleContent" property="articleContent" jdbcType="VARCHAR" javaType="java.lang.String"/></resultMap>(当然,这里还有查询user表的语句,省略)

同时,在article对应的xml有这样的select语句:

<select id="selectArticleByUserId"
parameterType="java.lang.String"
resultMap="ArticleResultMap" >
select * from
tb_article where userId=#{userId} </select>

2.association
一对多
实体类增加对应属性

private String id;//主键
private String userName;//用户姓名
private List<Article> articleList;

userMapper.xml这样配置

<resultMap id="userResultMap" type="test.mybatis.entity.User"><id column="id" property="id" jdbcType="VARCHAR" javaType="java.lang.String"/><result column="userName" property="userName" jdbcType="VARCHAR" javaType="java.lang.String"/>
//这里把user的id传过去<collection property="articleList" column="id"                       select="test.mybatis.dao.articleMapper.selectArticleListByUserId" />
</resultMap>
以下省略,类同,Mybatis会把结果封装成List类型。

如果我还想通过Article表另一张表,比如文章中有个fk_id,也可以像上面这样重复配置,把fk_id当做与另一张表关联的参数,那时就可以通过用户查到文章,查到文章关联的另一张表了

六.SQL标签

更多用于写sql语句的一部分,写在配置文件中的常量

七.include标签

用于引用常量


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

相关文章

Mybatis中标签大全

文章目录 一、标签分类 二、标签总结 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.…

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…