Mybatis的sql映射文件的顶级元素使用实例(超级详细)(下)

简介: Mybatis是是一款优秀的持久层框架(持久化是程序数据在瞬时状态和持久状态间转换的过程。),在dao层大量使用,使sql语句封装在配置文件中,降低程序的耦合度。

3、insert实现:注意每个字段之间一一对应就行了。

<insert id="addRum" parameterType="RegRum"> 
     insert into regnum(patientname,cardtype,cardid,socalnum,phone,sex,
     age,position,firstdiagnose,doctorid,status,remark,date) 
     VALUES(#{regnum.patientname},#{regnum.cardtype},#{regnum.cardid}
</insert>

4、update:可以使用set-if或者trim-if

set-if实例:

<update id="updateregnum">
        update regnum
        <set >
            <if test="phone!=null and phone!=''">
                phone=#{phone},
            </if>
            <if test="position!=null and position!=''">
                position=#{position},
            </if>
            <if test="firstdiagnose!=null and firstdiagnose!=''">
                firstdiagnose=#{firstdiagnose},
            </if>
            <if test="doctorid!=0">
                doctorid=#{doctorid},
            </if>
            <if test="remark!=null and remark!=''">
                remark=#{remark},
            </if>
            <if test="date!=null and date!=''">
                date=#{date},
            </if>
            <if test="age!=0">
                age=#{age},
            </if>
            <if test="status!=null and status !=''">
                status=#{status},
            </if>
            <if test="sex==0 or sex==1">
                sex=#{sex},
            </if>
        </set>
        where patientid=#{patientid}
    </update>

if的判断test必须有添加条件,否则会报错。

trim-if组合

  <update id="updateregnum">
        update regnum
        <trim prefix="set" suffixOverrides="," suffix=" where patientid=#{patientid}">
            <if test="phone!=null and phone!=''">
                phone=#{phone},
            </if>
            <if test="position!=null and position!=''">
                position=#{position},
            </if>
            <if test="firstdiagnose!=null and firstdiagnose!=''">
                firstdiagnose=#{firstdiagnose},
            </if>
            <if test="doctorid!=0">
                doctorid=#{doctorid},
            </if>
            <if test="remark!=null and remark!=''">
                remark=#{remark},
            </if>
            <if test="date!=null and date!=''">
                date=#{date},
            </if>
            <if test="age!=0">
                age=#{age},
            </if>
            <if test="status!=null and status !=''">
                status=#{status},
            </if>
            <if test="sex!=0">
                sex=#{sex},
            </if>
        </trim>
    </update>

5、delete

1. <delete id="delversion" parameterType="Integer">
2.        delete from app_version where appid=#{appid};
3. </delete>

6、foreach的实例:迭代一个集合,通常用于in条件。

(1)只传入一个参数,集合类型的。

<select id="findByids2" resultType="User">
        select * from user where userid in
        <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
</select>

collection表示传入的集合名字,item的值表示集合中每一个值的别名,open以什么开头,close以什么结尾,separator表示值之间以什么分隔。


(2)传入多个参数,我们可以用map集合传入。当然也可以用@param注解。


使用map时:dao层的map值一定要是Object,否则集合类型传不进去。

public Collection<User> findbymap(Map<String,Object> map);

dao.xml的#{phone}以及collection的值是map所对应的键。

 <select id="findbymap" resultType="User">
        select * from user where userid in
        <foreach collection="idslist" index="index" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        and phone=#{phone};
  </select>

使用@param注解的方法:dao层这样写:

public Collection<User> findByids3(@Param("ids")int [] ids,@Param("phone") String phone);

dao.xml的#{phone}以及collection的值是注解名。

目录
相关文章
|
12月前
|
SQL Java 数据库连接
【YashanDB知识库】解决mybatis的mapper文件sql语句结尾加分号";"报错
【YashanDB知识库】解决mybatis的mapper文件sql语句结尾加分号";"报错
|
6月前
|
SQL Java 数据库连接
MyBatis 的映射关系
MyBatis 核心功能之一是映射关系,支持一对一、一对多和多对多三种 ORM 映射。通过实体类与配置文件结合,开发者可灵活实现数据关联,提升数据库操作效率。
370 4
|
10月前
|
SQL XML Java
菜鸟之路Day35一一Mybatis之XML映射与动态SQL
本文介绍了MyBatis框架中XML映射与动态SQL的使用方法,作者通过实例详细解析了XML映射文件的配置规范,包括namespace、id和resultType的设置。文章还对比了注解与XML映射的优缺点,强调复杂SQL更适合XML方式。在动态SQL部分,重点讲解了`&lt;if&gt;`、`&lt;where&gt;`、`&lt;set&gt;`、`&lt;foreach&gt;`等标签的应用场景,如条件查询、动态更新和批量删除,并通过代码示例展示了其灵活性与实用性。最后,通过`&lt;sql&gt;`和`&lt;include&gt;`实现代码复用,优化维护效率。
968 5
|
12月前
|
SQL Java 数据库连接
【YashanDB 知识库】解决 mybatis 的 mapper 文件 sql 语句结尾加分号";"报错
【YashanDB 知识库】解决 mybatis 的 mapper 文件 sql 语句结尾加分号";"报错
|
12月前
|
SQL 缓存 Java
框架源码私享笔记(02)Mybatis核心框架原理 | 一条SQL透析核心组件功能特性
本文详细解构了MyBatis的工作机制,包括解析配置、创建连接、执行SQL、结果封装和关闭连接等步骤。文章还介绍了MyBatis的五大核心功能特性:支持动态SQL、缓存机制(一级和二级缓存)、插件扩展、延迟加载和SQL注解,帮助读者深入了解其高效灵活的设计理念。
|
12月前
|
Java 数据库连接 mybatis
MyBatis篇-映射关系(1-1 1-n n-n)
本文介绍了MyBatis中四种常见关系映射的配置方法,包括一对一、一对多、多对一和多对多。**一对一**通过`resultMap`实现属性与字段的映射;**一对多**以用户-角色为例,使用`&lt;collection&gt;`标签关联集合数据;**多对一**以作者-博客为例,利用`&lt;association&gt;`实现关联;**多对多**则通过引入第三方类(如UserForDept)分别在User和Dept类中添加集合属性,并配置对应的`&lt;collection&gt;`标签完成映射。这些方法解决了复杂数据关系的处理问题,提升了开发效率。
|
关系型数据库 MySQL 网络安全
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
|
SQL 存储 监控
SQL Server的并行实施如何优化?
【7月更文挑战第23天】SQL Server的并行实施如何优化?
628 13
解锁 SQL Server 2022的时间序列数据功能
【7月更文挑战第14天】要解锁SQL Server 2022的时间序列数据功能,可使用`generate_series`函数生成整数序列,例如:`SELECT value FROM generate_series(1, 10)。此外,`date_bucket`函数能按指定间隔(如周)对日期时间值分组,这些工具结合窗口函数和其他时间日期函数,能高效处理和分析时间序列数据。更多信息请参考官方文档和技术资料。
469 9
|
SQL 存储 网络安全
关系数据库SQLserver 安装 SQL Server
【7月更文挑战第26天】
311 6