mybatis----动态Sql

这篇具有很好参考价值的文章主要介绍了mybatis----动态Sql。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.if标签

通过if标签构建动态条件,通过其test属性的true或false来判断该添加语句是否执行。

mapper接口

public interface AccountMapper {
    List<Account> selectAllByCondition(Account account);
}

映射文件

<select id="selectAllByCondition" resultMap="AccountMap">
    select *
    from account
    where 1=1
    <if test="id!=null and id!=''">
        and id=#{id}
    </if>
    <if test="accountType!=null and accountType!=''">
        and account_type=#{accountType}
    </if>
</select>

这里test中的属性名与Account类中的属性名一致。例如:上述文件的第一个if标签中的id严格与Account中属性名id一致。

测试

@Test
public void sqlTest(){
    AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
    Account account=new Account();
    account.setId(1);
    account.setAccountType("Checking");
    List<Account> accounts = accountMapper.selectAllByCondition(account);
    System.out.println(accounts);
}

结果:

mybatis----动态Sql,java,mybatis,数据库

执行效果等同于select * from account where 1=1 and id=1 and account_type="Checking"

现在修改查询的id属性值,将其赋值为空,在测试类中注释掉account.setId方法

mybatis----动态Sql,java,mybatis,数据库

test属性是boolean类型,通过表达式判断true,false,决定此条件是否会被执行

注意其中的1=1,这样就可以解决if标签可能会带来的sql语法错误例如:

select * from account where and id=1;

当Mapper接口传递的是@Param参数指定的形参时,if标签中的test属性名应与Param参数指定的值一致

例如接口参数修改为如下形式:

public interface AccountMapper {
    List<Account> selectAllByCondition(@Param("AccountId") int id,@Param("accountType") String accountType);
}

映射文件应修改如下(注意AccountId的修改):

<select id="selectAllByCondition" resultMap="AccountMap">
    select *
    from account
    where 1=1
    <if test="AccountId!=null and AccountId!=''">
        and id=#{AccountId}
    </if>
    <if test="accountType!=null and accountType!=''">
        and account_type=#{accountType}
    </if>
</select>

这中映射规则与#{}值的映射类似

2.where标签

where标签是对if标签的升级版,这里用where标签替代where语句,而且它还能消除条件语句中可能出现的多余sql字段,例如and,or。

映射文件修改如下:

<select id="selectAllByCondition" resultMap="AccountMap">
    select *
    from account
    <where>
    <if test="id!=null and id!=''">
        and id=#{id}
    </if>
    <if test="accountType!=null and accountType!=''">
        and account_type=#{accountType}
    </if>
    </where>
</select>

这里测试时id为null,此时where语句会自动去除if标签中属性值多余的and或者or,如果and或or在属性值后面则不会生效。

mybatis----动态Sql,java,mybatis,数据库

3.trim标签

使用trim标签可以添加在指定sql语句前添加删除字段

映射文件修改

<select id="selectAllByCondition" resultMap="AccountMap">
    select *
    from account
    <trim prefix="where" prefixOverrides="and">
        <if test="id!=null and id!=''">
            and id=#{id}
        </if>
        <if test="accountType!=null and accountType!=''">
            and account_type=#{accountType}
        </if>
    </trim>
</select>

测试结果

mybatis----动态Sql,java,mybatis,数据库

当测试id值为null时,prefixOverrides属性值的设定取出了多余的and,prefix值的设定在trim标签前添加了where。如下,trim标签还用suffix,suffixOverrides属性,与前两个属性相对,分别是在trim标签中语句后添加值,在if标签中对and或or相对属性值后的动态处理,防止sql语句出错。

mybatis----动态Sql,java,mybatis,数据库

4.choose标签

如下choose标签中的when,otherwise类似与if else,它只会执行其中的一个

<select id="selectAllByCondition" resultMap="AccountMap">
    select *
    from account
    where
    <choose>
        <when test="id!=null and id!=''">
            id=#{id}
        </when>
        <otherwise>
            account_type=#{accountType}
        </otherwise>
    </choose>
</select>

测试方法中只设置id属性

@Test
    public void sqlTest(){
        AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
        Account account=new Account();
        account.setId(2);
//        account.setAccountType("Checking");
        List<Account> accounts = accountMapper.selectAllByCondition(account);
        System.out.println(accounts);
    }

结果:

mybatis----动态Sql,java,mybatis,数据库

再将id属性设置注释掉,测试

mybatis----动态Sql,java,mybatis,数据库

它只会执行其中一个条件,而且一定会执行其中一个。

5.foreach标签

foreach标签是针对于集合,列表的类似与for循环

范围查询:

接口方法:

List<Account> selectByIds(@Param("ids") int [] s);

映射文件

<select id="selectByIds" resultMap="AccountMap">
    select * from account where id in(1,2);
</select>

测试

@Test
public void sqlTest2(){
    AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
    int [] ids={1,2};
    List<Account> accounts = accountMapper.selectByIds(ids);
    System.out.println(accounts);
}

mybatis----动态Sql,java,mybatis,数据库

修改xml,使用foreach标签

<select id="selectByIds" resultMap="AccountMap">
    select * from account where id in(
    <foreach collection="ids" item="id" separator=",">
        id
    </foreach><!--遍历ids数组,将生成的id以,隔开--> 
    );
</select>

结果

mybatis----动态Sql,java,mybatis,数据库文章来源地址https://www.toymoban.com/news/detail-818229.html

到了这里,关于mybatis----动态Sql的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 使用mybatis和dynamic-datasource-spring-boot-starter动态切换数据源操作数据库

    记录 :415 场景 :使用mybatis和dynamic-datasource-spring-boot-starter动态切换数据源操作数据库。 版本 :JDK 1.8,Spring Boot 2.6.3,dynamic-datasource-spring-boot-starter-3.3.2,mybatis-3.5.9。 源码 :https://github.com/baomidou/dynamic-datasource-spring-boot-starter dynamic-datasource-spring-boot-starter :一个基于springboot的快

    2023年04月19日
    浏览(32)
  • 在IDEA中配置MySQL数据库连接以及在使用mybatis时设置sql语句的代码提示功能

    在IDEA中配置MySQL数据库连接以及在使用mybatis 时设置 sql语句的代码提示功能 一:在IDEA中配置MySQL数据库连接 第一步:在IDEA右侧区域有database选项,点击进去 第二步:database  - data soucre - mysql   第三步:配置连接信息,连接数据库   第四步:显示的数据库以及表的信息  第

    2024年02月14日
    浏览(36)
  • 使用dynamic-datasource-spring-boot-starter动态切换数据源操作数据库(MyBatis-3.5.9)

    记录 :383 场景 :使用dynamic-datasource-spring-boot-starter动态切换数据源,使用MyBatis操作数据库。提供三种示例:一,使用@DS注解作用到类上。二,使用@DS注解作用到方法上。三,不使用注解,使用DynamicDataSourceContextHolder类在方法内灵活切换不同数据源。 源码: https://github.com/

    2024年01月20日
    浏览(40)
  • Java EE 突击 13 - MyBatis 查询数据库(2)

    这个专栏给大家介绍一下 Java 家族的核心产品 - SSM 框架 JavaEE 进阶专栏 Java 语言能走到现在 , 仍然屹立不衰的原因 , 有一部分就是因为 SSM 框架的存在 接下来 , 博主会带大家了解一下 Spring、Spring Boot、Spring MVC、MyBatis 相关知识点 并且带领大家进行环境的配置 , 让大家真正用好

    2024年02月11日
    浏览(37)
  • 解决IntelliJ IDEA在Mybatis 编写mapper.xml SQL语句时不自动提示SQL语句和数据库表的问题

    在Idea中链接数据库后,发现在MySql 的console中有SQL语句提示和数据表的提示,但是在编写mapper.xml中发现并没有提示,很烦,觉得效率下降。 在百度搜索后,发现了解决方法,出现了SQL语句的提示. 解决办法: 按下alt + enter,选择Language injection settings 然后选择SQL即可,但是这种

    2024年02月16日
    浏览(34)
  • MyBatis实现 Java 对象和数据库中日期类型之间的转换(超详细)

    数据库存储的时间字段的类型是datetime Java实体类的时间字段类型是Date 需求:响应前端的时间字段格式为”yyyy-MM-dd HH:mm:ss“ 1、定义resultMap 定义 Java 对象和数据库表字段的对应关系,在 mapper.xml 文件中使用 #{属性名,jdbcType=数据库字段类型} 来进行参数传递和结果集映射,例如

    2024年02月15日
    浏览(35)
  • 利用java.sql包--访问和处理数据库数据

    The java.sql package in Java provides the API for interacting with relational databases using JDBC (Java Database Connectivity). JDBC is a standard Java API that allows Java programs to connect to and interact with various database management systems (DBMS) using SQL (Structured Query Language). The java.sql package contains several important interfaces and

    2024年02月10日
    浏览(29)
  • 【Java】Mybatis查询数据库返回JSON格式的字段映射到实体类属性

    今天遇到了一个bug,大概就是数据库(Mysql)中有一个 type 类型字段,数据类型为json,大概是这样的:[“苹果”,“香蕉”,“葡萄”]的数据格式,这个bug的问题所在呢就是查询后这个json格式的数据无法映射到我们实体类的属性上,解决方案如下: 实体类的配置: @TableField

    2024年02月15日
    浏览(34)
  • 通过Java连接Sql Server数据库

    JDBC是Java DateBase Connectivity的简写,翻译过来就是java连接数据库,或者更通俗地说就是java语言操作数据库。JDBC的本质其实是官方定义的一套操作所有关系型数据库的规则,也就是接口。 微软官方jdbc下载地址:https://www.microsoft.com/zh-cn/download/details.aspx?id=11774 下载之后解压到相

    2024年01月17日
    浏览(37)
  • MyBatis实现 Java 实体类和数据库中日期类型之间的转换(超详细)

    数据库存储的时间字段的类型是datetime Java实体类的时间字段类型是Date 需求:响应前端的时间字段格式为”yyyy-MM-dd HH:mm:ss“ 1、定义resultMap 定义 Java 对象和数据库表字段的对应关系,在 mapper.xml 文件中使用 #{属性名,jdbcType=数据库字段类型} 来进行参数传递和结果集映射,例如

    2024年02月20日
    浏览(42)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包