当前位置: 首页 > news >正文

Mybatis笔记的后续补充

返回值问题

1.返回值为单个属性的时候

//接口中方法设计,返回值为String:
String getUser(int id);
//XML中语法:
<select id="getUser" parameterType="int" resultType="String">select username from t_user where id = #{id}
</select>
//测试类执行:
String username = userDao.getUser(2);
//返回int类型
int id  =  userMapper.getuserid(336);<select id="getuserid"  resultType="int" parameterType="int">select id from t_user where id=#{id}
</select>

2.返回值为多个属性的时候

方式一:采用对象形式

//接口中方法设计,返回值为User:
User getUser(int id);
//XML中语法:
<select id="getUser" parameterType="int" resultType="com.zx.entity.User">select username,age from t_user where id = #{id}
</select>
//测试类执行:
User user = userDao.getUser(2);

方式二:采用map形式处理返回结果

//接口中方法设计,返回值为HashMap:
HashMap getUser02(int id);
//XML中语法:
<select id="getUser02" parameterType="int" resultType="map">select username,age from t_user where id = #{id}
</select>
//测试类执行:
HashMap map = userDao.getUser02(2);

返回map的时候,键是属性名称,值就是具体的值,只能返回一条数据,返回多条就会报错

resultType一般都是返回已有的类型,或者你写好的实体类型,可以直接对应的类型。若是字段或者类型,或者不是能很好的直接对应,就需要用自定义对应的类型。

多对一关联映射

resultMap 如果返回值中有其他对象的属性时使用
属性:
id:固定id
type:查询结果的返回类型
子标签:
id:表中的主键
column:数据表中的列名称
property:实体类中的属性
result:其他属性
column:数据表中的列名称
property:实体类中的属性
方式一:发送多次sql语句,每张表进行单独查询,通过select属性向对方映射文件中进行查询

association: 关联标签 出现在“多”方
column:数据表中的列名称
property:实体类中的属性
select: 需要指定命名空间.id去查询关联对象
javaType: 查询结束之后的返回类型

<select id="getUser04" parameterType="int" resultMap="userResult">select * from t_user where id = #{id}
</select>
<resultMap id="userResult" type="com.zx.entity.User"><id column="id" property="id"></id><result column="username" property="username"></result><result column="password" property="password"></result><result column="age" property="age"></result><association column="gid" property="group" select="com.zx.dao.GroupDao.getGroupByid" javaType="com.zx.entity.Group"></association>
</resultMap>

这个方法需要注意,select放的是你要二次执行的接口,(简单来说就是一个完整的查询接口,需要有mapper,有xml),查询的内容通常是用了关联字段或外键,没有中间表,但是查询完成以后,还是可以显示关联字段的所有信息。若是没有关联字段,任然需要联查

方式二:发送一次sql语句,进行联表查询,需要注意SQL语句的性能,不需要使用select属性

association: 关联标签 出现在“多”方
column:数据表中的列名称
property:实体类中的属性
javaType: 查询结束之后的返回类型

association打开,可以嵌入子标签:

id:对方表中的主键
column:数据表中的列名称
property:实体类中的属性
result:其他属性
column:数据表中的列名称
property:实体类中的属性

<select id="getUser05" parameterType="int" resultMap="userResult05">select u.id uid,u.username username,u.password password,u.age age,g.id gid,g.gname gname from t_user u,t_group g where u.gid = g.id and u.id = #{id};
</select><resultMap id="userResult05" type="com.zx.entity.User"><id column="uid" property="id"></id><result column="username" property="username"></result><result column="password" property="password"></result><result column="age" property="age"></result><association column="gid" property="group" javaType="com.zx.entity.Group"><id column="gid" property="id"></id><result column="gname" property="gname"></result></association>
</resultMap>
一对多集合映射

方式一:发送多次sql语句,每张表进行单独查询,通过select属性向对方映射文件中进行查询

<select id="getGroupByid02" resultMap="groupResult02" parameterType="int">select * from t_group where id = #{id}
</select><resultMap id="groupResult02" type="com.zx.entity.Group"><id column="id" property="id"></id><result column="gname" property="gname"></result><collection column="id" property="users" select="com.zx.dao.UserDao.getUsersByGid" ofType="com.zx.entity.User" ></collection>
</resultMap>

对象对应多个用户 用list展示

方式二:发送一次sql语句,进行联表查询,需要注意SQL语句的性能,不需要使用select属性

collection打开,可以嵌入子标签:

id:对方表中的主键
column:数据表中的列名称
property:实体类中的属性
result:其他属性
column:数据表中的列名称
property:实体类中的属性

<select id="getGroupByid03" resultMap="groupResult03" parameterType="int">select g.id gid,g.gname gname,u.id uid,u.username username,u.password password,u.age age from t_user u,t_group g where u.gid = g.id and g.id = #{gid}
</select><resultMap id="groupResult03" type="com.zx.entity.Group"><id column="gid" property="id"></id><result column="gname" property="gname"></result><collection column="gid" property="users" ofType="com.zx.entity.User" ><id column="uid" property="id"></id><result column="username" property="username"></result><result column="password" property="password"></result><result column="age" property="age"></result></collection>
</resultMap>

动态SQL

<select id="getUsersif" resultType="com.zx.entity.User" parameterType="com.zx.entity.User">select * from t_user where 1=1<if test="username != null">and username = #{username}</if><if test="age != 0">and age = #{age}</if>
</select><select id="getUserschoose" resultType="com.zx.entity.User" parameterType="com.zx.entity.User">select * from t_user where 1=1
choose就是一个分支选择标签,when就是每一个分支,不管有多少分支,只会执行一个。
<choose><when test="username!=null">and username = #{username}</when><when test="age!=0">and age = #{age}</when>
默认选项,所有选项不匹配则进入。<otherwise>and password = #{password}</otherwise></choose>
</select><select id="getUserswhere" resultType="com.zx.entity.User" parameterType="com.zx.entity.User">select * from t_user   where  1=1<where>他就是咱们的字段 where 
<where>标签会自动处理WHERE子句中的AND和OR关键字,因此在使用时应该合理安排条件语句的位置,避免出现意外结果。
Where只能去除最前面的关键字,他会自动去除第个条件的and或者or 的关键字,<if test="username != null">and username = #{username}</if><if test="age != 0">and age = #{age}</if></where>
</select><select id="getUserstrim" resultType="com.zx.entity.User" parameterType="com.zx.entity.User">select * from t_user<trim prefix="where" prefixOverrides="and" suffix="order by"><if test="username != null">and username = #{username}</if><if test="age != 0">and age = #{age}</if></trim>id desc
</select><update id="updateUser" parameterType="com.zx.entity.User">update t_user<set><if test="username != null">username = #{username},</if><if test="age != 0">age = #{age},</if><if test="password != null">password = #{password},</if></set>where id = #{id}
</update><select id="getUsersforeach" resultType="com.zx.entity.User">select * from t_user where id in <foreach collection="array" item="id" open="(" close=")" separator=",">#{id}</foreach>
</select><insert id="addUser" ><foreach collection="list" item="user" separator=";">insert into t_user values (null,#{user.username},#{user.password},#{user.age})</foreach>
</insert>

Insert into user(username,pwd,) values(username,pwd,)

标签也是Mabits动态SQL中常用的一个标签,主要用于快速生成包含WHERE、SET等关键字的SQL语句,同时还能够自动处理SQL语句中的逗号(,)和AND/OR等连接符。

标签通常包含以下属性:

prefixOverrides:表示在生成SQL语句前需要忽略的字符前缀。

suffixOverrides:表示在生成SQL语句后需要忽略的字符后缀。

prefix:表示在生成SQL语句前需要添加的字符串。

suffix:表示在生成SQL语句后需要添加的字符串。

suffixOverrides:表示在生成SQL语句中需要忽略掉的字符串。 // as

<select id="getUserstrim" resultType="com.zx.entity.User" parameterType="com.zx.entity.User">select * from t_user<trim prefix="where" prefixOverrides="and" suffix="order by"><if test="username != null">and username = #{username}</if><if test="age != 0">and age = #{age}</if></trim>id desc
</select>

通过使用标签和标签,可以根据用户传入的参数动态生成SQL语句的SET子句。如果参数不为空,则会包含相应的更新内容;否则,该更新内容会被忽略

需要注意的是,由于标签会自动处理SET子句中的逗号(,),因此在使用时应该合理安排更新内容的位置,避免出现意外结果

标签通过将SET子句中的所有条件用逗号(,)连接起来,可以根据不同的更新情况动态生成满足需求的SQL语句

<update id="updateUser" parameterType="com.zx.entity.User">update t_user<set><if test="username != null">username = #{username},</if><if test="age != 0">age = #{age},</if><if test="password != null">password = #{password},</if></set>where id = #{id}
</update>

标签是 MyBatis 中的一个迭代标签,可以对集合对象进行遍历,并生成多条 SQL 语句(如 INSERT、UPDATE、DELETE 等)。

批量添加,导入文件exsl 多条 添加多条,查询列表得到list,把list的内容添加到别的表中

在java代码中遍历添加,sql

批量删除,

使用 标签,我们可以将一个集合对象的元素依次取出,作为 SQL 语句中的参数进行插入、更新或删除操作。常用的语法如下:

其中,各个属性和元素的含义如下

collection:指定要遍历的集合对象的属性名。 参数名

item:指定在遍历过程中每个元素所对应的变量名。 起名字,对应变量名

separator:指定在生成多条 SQL 语句时,不同语句之间的分隔符,默认为英文逗号。

open:指定生成的 SQL 语句的头部。

close:指定生成的 SQL 语句的尾部。

List是单个参数

Item是单个参数的时候用单个参数,

List是对象,

则item对应的是对象名,用法是对象.内容

<select id="getUsersforeach" resultType="com.zx.entity.User">select * from t_user where id in <foreach collection="array" item="id" open="(" close=")" separator=",">#{id}</foreach>
</select><insert id="addUser" ><foreach collection="list" item="user" separator=";">insert into t_user values (null,#{user.username},#{user.password},#{user.age})</foreach>
</insert>

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 用C#写一个随机音乐播放器
  • 【现代通信技术】第八章 SDH技术
  • 探索顶级PDF水印API:PDFBlocks(2024年更新)
  • LCD 图片格式转换
  • FreeRTOS 快速入门(三)之任务管理
  • ensp小实验(ospf+dhcp+防火墙)
  • PHP模拟高并发异步请求测试+redis的setnx处理并发和防止死锁处理
  • PINCE——Linux 原生游戏内存修改器,一款替代 Cheat Engine 的强大游戏修改器,Linux 游戏玩家必备神器!
  • 【论文学习与撰写】论文中公式的编辑,Mathtype的使用,全文编号排版,智能截图识别公式,公式编号自动更新
  • 一键更换Linux优质的软件源和docker源 —— 筑梦之路
  • 超全MySQL优化清单
  • 如何在ThinkPHP6中轻松实现WebSocket通信?看这里就够了!
  • 【HarmonyOS NEXT星河版开发学习】综合测试案例-各平台评论部分
  • 【javaEE进阶1】spring简介IoC
  • 都2024年了,还在问网络安全怎么入门,气得我当场脑血栓发作
  • [rust! #004] [译] Rust 的内置 Traits, 使用场景, 方式, 和原因
  • 【跃迁之路】【699天】程序员高效学习方法论探索系列(实验阶段456-2019.1.19)...
  • 30天自制操作系统-2
  • Android 架构优化~MVP 架构改造
  • IIS 10 PHP CGI 设置 PHP_INI_SCAN_DIR
  • js ES6 求数组的交集,并集,还有差集
  • Map集合、散列表、红黑树介绍
  • Markdown 语法简单说明
  • MySQL的数据类型
  • orm2 中文文档 3.1 模型属性
  • Zsh 开发指南(第十四篇 文件读写)
  • 包装类对象
  • 关于List、List?、ListObject的区别
  • 离散点最小(凸)包围边界查找
  • 前端存储 - localStorage
  • 使用 @font-face
  • 微服务核心架构梳理
  • elasticsearch-head插件安装
  • 说说我为什么看好Spring Cloud Alibaba
  • 资深实践篇 | 基于Kubernetes 1.61的Kubernetes Scheduler 调度详解 ...
  • ​io --- 处理流的核心工具​
  • ​必胜客礼品卡回收多少钱,回收平台哪家好
  • ​软考-高级-系统架构设计师教程(清华第2版)【第20章 系统架构设计师论文写作要点(P717~728)-思维导图】​
  • # 飞书APP集成平台-数字化落地
  • #162 (Div. 2)
  • #define、const、typedef的差别
  • #laravel 通过手动安装依赖PHPExcel#
  • #NOIP 2014#day.2 T1 无限网络发射器选址
  • #传输# #传输数据判断#
  • #我与Java虚拟机的故事#连载12:一本书带我深入Java领域
  • (02)Unity使用在线AI大模型(调用Python)
  • (33)STM32——485实验笔记
  • (39)STM32——FLASH闪存
  • (C语言)strcpy与strcpy详解,与模拟实现
  • (阿里云万网)-域名注册购买实名流程
  • (论文阅读11/100)Fast R-CNN
  • (三)Honghu Cloud云架构一定时调度平台
  • (游戏设计草稿) 《外卖员模拟器》 (3D 科幻 角色扮演 开放世界 AI VR)
  • (转) SpringBoot:使用spring-boot-devtools进行热部署以及不生效的问题解决
  • (转)关于如何学好游戏3D引擎编程的一些经验