Mapper.xml映射文件

Mapper.xml映射文件:

<select> resultType如果返回的是集合,那么应该设置为集合包含的类型而不是集合本身的类型

如果参数类型是pojo类型,参数名必须是pojo中的属性名

<insert>

session.commit();

session = factory.openSession();

driver:com.mysql.jdbc.Driver

url:jdbc:mysql://127.0.0.1:3306/test

动态sql:

小于号

<![CDATA[<=]]>

:

XML 复制代码
<where>:
<if test="sal != null">
and sal <![CDATA[<=]]>#{sal}         //and,or会自动给去掉
</where>
XML 复制代码
choose when otherwise:
when otherwise中只走一个
select * from emp where
<choose>
    <when test="sal != null">
        sal <![CDATA[<=]]> #{sal}
    </when>
    <when test="ename != null">
        ename like concat('%',#{ename},'%')
    </when>
    <otherwise>
        deptno=#{deptno}
    <otherwise>
</choose>

trim标签 组合 if标签 主要功能 可以在自己包含的内容前面加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;

可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides

XML 复制代码
insert into emp
<trim prefix="("  suffix=")" suffixOverrides = ",">
    <if test = "empno != null">
        empno,
    <if test = "ename != null">
        ename,
    <if test = "mgr != null">
        mgr,
    <if test = "sal != null">
        sal,
    <if test = "deptno != null">
        deptnol,
</trim>
values
<trim prefix="("  suffix=")" suffixOverrides = ",">
    <if test = "empno !=null">
        #{empno},
    <if test = "ename != null">
        #{ename},
    <if test = "mgr != null">
        #{mgr},
    <if test = "sal != null">
        #{sal},
    <if test = "deptno != null">
        #{deptnol},
</trim>

<set>标签: 如果包含的语句以逗号结束会把逗号省略掉

XML 复制代码
update   emp
<set>
    <if test="sal != null">
        sal=#{sal},
    </if>
    <if test="mgr != null">
        mgr=#{mgr},
    </if>
    <if test="ename != null">
        ename=#{ename},
    </if>
</set>
<where>
    <if test="empno != null">
        and empno = #{empno}
    </if>
    <if test="job != null">
        and job =  #{job}
    </if>
</where>

<foreach>主要用在构建in条件中,它可以在SQL语句中迭代一个集合

item 表示集合中每一个元素进行迭代时的别名

index指定一个名字,用于表示在迭代过程中,每次迭代到的位置

LIST\]: ```XML select * from emp empno=#{empno} select * from emp where empno in #{empno} ``` \[array\]: paramaterType = List ```XML select * from emp where empno in #{empno} [map]: ``` (Map\ map\>) 多条件复杂查询的时候 List\ selectEmpByMapinfo(Map\); ```XML . ``` map.put("deptnokey",10) map.put("enamekey","enamekey") map.put(empnos,list) \[SQL片段\]: ```XML select * from emp ``` \[bind\]标签 ```XML ``` resultType实现一对一 多对多: ```XML ``` SqlSessionFactory factory; Inputstream stream = Resources.getResourceasStream("SqlMapConfig.xml"); factory = SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder().build(stream) page工具类实现分页:page工具类: PageHelper.startPage((page- 1)\* size,size)

相关推荐
Java小白程序员7 小时前
MyBatis基础到高级实践:全方位指南(中)
数据库·mybatis
山楂树下懒猴子8 小时前
ChatAI项目-ChatGPT-SDK组件工程
人工智能·chatgpt·junit·https·log4j·intellij-idea·mybatis
Mr_hwt_12318 小时前
基于mybatis-plus动态数据源实现mysql集群读写分离和从库负载均衡教程(详细案例)
数据库·spring boot·mysql·mybatis·mysql集群
Z_z在努力1 天前
【杂类】Spring 自动装配原理
java·spring·mybatis
little_xianzhong1 天前
关于对逾期提醒的定时任务~改进完善
java·数据库·spring boot·spring·mybatis
MadPrinter2 天前
SpringBoot学习日记 Day11:博客系统核心功能深度开发
java·spring boot·后端·学习·spring·mybatis
奔跑吧邓邓子2 天前
【Java实战㉟】Spring Boot与MyBatis:数据库交互的进阶之旅
java·spring boot·实战·mybatis·数据库交互
lunzi_fly2 天前
【源码解读之 Mybatis】【基础篇】-- 第1篇:MyBatis 整体架构设计
java·mybatis
摸鱼仙人~2 天前
深入理解 MyBatis-Plus 的 `BaseMapper`
java·开发语言·mybatis
隔壁阿布都2 天前
spring boot + mybatis 使用线程池异步修改数据库数据
数据库·spring boot·mybatis