mysql使用XML实现增删改查模板(参数形式包括:String、对象、集合、数组、Map)

news/2025/2/22 15:35:31
xmlns="http://www.w3.org/2000/svg" style="display: none;">

在这里插入图片描述

说明:方便后续根据场景直接拷贝代码

文章目录

  • 一、foreach 标签的属性含义
  • 二、使用注意事项
  • 三、CRUD模板
    • 1.查询
      • 1.1根据条件id数组查询集合:(传参1为数组,形参1为String)
      • 1.2根据条件Map查询集合:(传参1为对象,形参1为Map)
      • 1.3根据条件neid查询集合:(传参为对象)
    • 2.新增
      • 2.1单条新增:(传参为对象)
      • 2.2批量新增:(传参1为对象,传参2为Map)
      • 2.3批量新增:(传参为集合)
    • 3.修改
      • 3.1批量更新:(传参为集合)
      • 3.2批量更新:(传参1为对象,传参2为Map)
      • 3.3单条更新:(传参为对象)
    • 4.删除
      • 4.1批量删除:(传参1为对象,传参2为Map)
      • 4.2根据条件nid删除一条:(传参为对象)
      • 4.3批量删除:传参1为String,传参2为集合)

一、foreach 标签的属性含义

foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。
foreach元素的属性主要有 item,index,collection,open,separator,close。
item集合中每一个元素进行迭代时的别名,
index表示在迭代过程中,每次迭代到的位置,
open该语句以什么开始,
separator在每次进行迭代之间以什么符号作为分隔 符,
close以什么结束,
在使用foreach的时候最关键的也是最容易出错的就是collection属性,
该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,
主要有一下3种情况:

  1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
    
  2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
    
  3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了
    

二、使用注意事项

xml">注意点1:条件判断使用if标签
数值型使用:<if test="phone != null  and phone != 0">phone,</if>
字符串使用:<if test="name != null  and name != ''">ip,</if>
xml">注意点2:${schema} 中$充当占位符,可以用来输入租户名
xml">注意点3:<foreach>标签在使用中有两种形式
形式1:<foreach item="item" collection="arr" separator="," open="(" close=")" index="">
形式2:<foreach collection="conditionMap.keys" item="item" index="index" separator="and">
xml">注意点4:有的 index="",有的index="index",这个我没测试过,是不是只要不用这个index属性,设置哪种都不影响?
xml">注意点5:形参是list、array、map 时,在具体使用<foreach>标签时解析方式都不太一样,需要留意。

三、CRUD模板

1.查询

语法:SELECT * FROM table_name;

1.1根据条件id数组查询集合:(传参1为数组,形参1为String)

xml">List<MailConfig> getListByIdArr(@Param("arr") String[] arr, @Param("schema") String schema);

<select id="getListByIdArr" resultMap="BaseResultMap">
	SELECT * FROM ${schema}.poc_grp_user 
	WHERE
	group_id in
	<foreach item="item" collection="arr" separator="," open="(" close=")" index="">
		#{item}
	</foreach>
</select>

1.2根据条件Map查询集合:(传参1为对象,形参1为Map)

xml">List<Map<String,String>> queryByCondition(@Param("moModel") MoModel moModel, @Param("conditionMap") Map<String, String> conditionMap);

<select id="queryByCondition" resultType="map">
	select * from `${moModel.tableName}` 
	<where> 
		<foreach collection="conditionMap.keys" item="item" index="index" separator="and">
			 ${item}=#{conditionMap.${item}}
		</foreach>
	</where>  
</select>

1.3根据条件neid查询集合:(传参为对象)

xml">List<Map<String,String>> queryAllByNeIdAndMoName(MoModel moModel);  

<select id="queryAllByNeIdAndMoName" parameterType="com.hero.lte.ems.config.entity.MoModel" resultType="map">
	select * from `${tableName}` where neid=#{neId}
</select>

2.新增

语法: 第一种形式无需指定要插入数据的列名,只需提供被插入的值即可:
INSERT INTO table_name VALUES (value1,value2,value3,…);

第二种形式需要指定列名及被插入的值:
INSERT INTO table_name (column1,column2,column3,…) VALUES (value1,value2,value3,…);

2.1单条新增:(传参为对象)

xml">int insert(MailConfig entity);
		
<!--新增返回主键ID-->
<insert id="insert" parameterType="com.hero.nms.platform.system.model.MailConfig" >
	<selectKey resultType="java.lang.Integer" keyProperty="id" order="BEFORE" >
		SELECT nextval('seq_mail_config_id'::regclass) AS id	//第一种写法
		//SELECT LAST_INSERT_ID()	//第二种写法
	</selectKey>
	insert into smconfig_mail
	<trim prefix="(" suffix=")" suffixOverrides=",">
		id,
		<if test="phone != null  and phone != 0">phone,</if>
		<if test="name != null  and name != ''">ip,</if>
	</trim>
	<trim prefix="values (" suffix=")" suffixOverrides=",">
		#{id},
		<if test="phone != null  and phone != 0">#{phone},</if>
		<if test="name != null  and name != ''">#{name},</if>      
	</trim>
</insert>

2.2批量新增:(传参1为对象,传参2为Map)

xml">int addMoByAttr(@Param("moModel") MoModel moModel, @Param("attrMap") Map<String, String> attrMap);

<insert id="addMoByAttr">
	insert into `${moModel.tableName}`
	<foreach collection="attrMap.keys" item="key" index="index" open="(" separator="," close=")">
		 ${key}
	 </foreach>
		values
	 <foreach collection="attrMap.keys" item="key" index="index" open="(" separator="," close=")">
		 #{attrMap.${key}}
	 </foreach>
</insert>

2.3批量新增:(传参为集合)

xml">void batchInsertService(@Param("logicalTopos") List<LogicalTopo> logicalTopos);
	
<insert id="batchInsertService" keyProperty="id" useGeneratedKeys="true">
	<selectKey resultType="java.lang.Integer" keyProperty="id" order="BEFORE" >
		SELECT LAST_INSERT_ID()	
	</selectKey>
       insert into logical_topo ( service_id ,x,y ) values
       <foreach collection='logicalTopos' item='item' index='index ' separator=', '>
           (#{item.serviceId}, #{item.x}, #{item.y} )
       </foreach>
   </insert>

3.修改

语法:UPDATE table_name SET column1=value1,column2=value2 WHERE some_column=some_value;

3.1批量更新:(传参为集合)

xml">void batchUpdateInstance(@Param("logicalTopos") List<LogicalTopo> logicalTopos);
	
<update id="batchUpdateInstance">
      <foreach collection='logicalTopos' item='item' index='index' separator='; '>				
          update logical_topo 
		<set>
			<if test="x != null  and x != 0">x=#{item.id},</if>
			<if test="y != null  and y != ''">y=#{item.name},</if>
		</set>	
		where 1=1
			<if test="user_id != null  and user_id != 0">AND user_id=#{item.userId}</if>						
      </foreach>
</update>

3.2批量更新:(传参1为对象,传参2为Map)

xml">int updateByCondition(@Param("moModel") MoModel moModel, @Param("attrMap") Map<String, String> attrMap);
	
<update id="updateByCondition">
 	update `${moModel.tableName}`
	<set>      
		 <foreach collection="attrMap.keys" item="key" index="index" separator=",">
			 ${key}
		 </foreach>       
	</set> 
	<where> 
		<foreach collection="attrMap.keys" item="key" index="index" separator="and">
			 ${key}=#{attrMap.${${key}}}
		</foreach>
	</where>     
</update>

3.3单条更新:(传参为对象)

xml">void modifyPwd(@Param("account")Account account);
	
<update id="modifyPwd" parameterType="com.hero.nms.pl.security.domain.Account">
       update t_user
       <set>
           <if test="account.password!=null and account.password!=''">
               password = #{account.password},
           </if>           
       </set>
       where username = #{account.username}
   </update>

4.删除

语法: DELETE FROM table_name WHERE some_column=some_value;

4.1批量删除:(传参1为对象,传参2为Map)

xml">int deleteByCondition(@Param("moModel") MoModel moModel, @Param("conditionMap") Map<String, String> conditionMap);
	
<deleteid="deleteByCondition">
	delete from `${moModel.tableName}`
	<where> 
	    <foreach collection="conditionMap.keys" item="item" index="index" separator="and">
	         ${item}=#{conditionMap.${item}}
	    </foreach>
   	</where> 
</delete>

4.2根据条件nid删除一条:(传参为对象)

xml">int deleteAllByNeIdAndMoName(MoModel moModel);
	
<delete id="deleteAllByNeIdAndMoName" parameterType="com.hero.lte.ems.config.entity.MoModel">
	delete from `${tableName}` where neid=#{neId}
</delete>

4.3批量删除:传参1为String,传参2为集合)

xml">int batchDelete(@Param("table") String table, @Param("list") List<EntryInfo> list);
	
<delete id="batchDelete">
       DELETE FROM nms.${table}
       WHERE key IN
       <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
           #{item.key}
       </foreach>
   </delete>

http://www.niftyadmin.cn/n/898190.html

相关文章

JAVA 技术方向支线任务-找到休息日

任务目标&#xff1a; 业务目标:让孙工找到当月休息日技能目标:综合运用循环判断分支结构,对程序进行格式化输出 某公司软件开发工程师孙工,作息规律为上三天班,休息一天,经常不确定休 息日是否周末,为此,请你开发一个程序,当孙工输入年及月,以日历方式显示对 应月份的休息日,用…

PSQLException: ERROR: column “xxxxx“ does not exist

文章目录说明场景1&#xff1a;表字段使用驼峰标示而不是全小写&#xff0c;比如字段叫&#xff1a;systemName 而不是叫&#xff1a;system_name或者systemname解决方案场景2&#xff1a;我们用的是双数据源(为了异地容灾)&#xff0c;导致a方法调用A库下面的表&#xff0c;b方…

Java集合Collections

Java集合Collections一、类集设置的目的二、链表与二叉树思路2.1 链表2.2 二叉树三、常见数据结构四、Collection接口4.1 概述4.2 Collection 常用功能五、List 接口5.1 接口介绍5.2 List接口特点5.3 List接口中常用方法5.4 扩充方法六、ArrayList6.1 概述6.2 范例&#xff1a;…

postgresql和mysql中的limit使用方法

文章目录区别举例说明MySQL 中PostgreSQL 中区别 在msyql中&#xff0c;limit使用如下 select *from mytable limit a,b 其中&#xff1a;a为起始索引&#xff0c;从0开始&#xff0c;b为获取数据长度 在postgresql中&#xff0c;limit使用如下 select *from mytable limit a o…

个人练习前端技术使用Bootstrap、JQuery、thymeleaf

说明&#xff1a;本代码只是为了本人练习前后端联动技术&#xff0c;包含html&#xff0c;jquery&#xff0c;thymeleaf模板、ajax请求及后端功能联动&#xff0c;方便自己查找及使用。 文章目录代码场景场景1.table批量查询功能&#xff08;有默认值&#xff09;&#xff0c;点…

SpringCloudGateway网关服务实现文件上传功能

文章目录说明SpringBoot和SpringCloudGateway项目区别说明1.SpringBoot的成功案例文件上传代码pom前端代吗Controller代码 重点在&#xff1a;RequestParam("file00") MultipartFile file2.SpringCloudGateway的成功案例文件上传代码Controller代码网上其他方案其他方…

后端MultipartFile接收文件转Base64

文章目录背景说明测试案例代码测试方法1测试方法2背景说明 最开始写的版本代码删改较多且无法运行&#xff0c;在其他博主给我指出问题及改进措施之后&#xff0c;下面的是最新汇总并测试结果有效的接口&#xff0c;编写日期&#xff1a;2022年08月12日。 下面代码是后端接收到…

SpringBoot获取文件将要上传的IP地址

说明&#xff1a; 有的项目会涉及文件上传&#xff0c;比如“更换logo业务”&#xff0c;或者“自定义任务上传脚本等业务”都会涉及上传&#xff0c;而有的项目上传成功后找不到上传地址&#xff0c;所以需要打印IP&#xff0c;方便用户知晓上传的精确地址&#xff0c;下面封装…