首页 > 数据库 >Mybatis:Mybatis注解实现动态SQL注意事项

Mybatis:Mybatis注解实现动态SQL注意事项

时间:2023-03-17 09:22:54浏览次数:40  
标签:selectUser name Select User SQL Mybatis 注解 select

1. 使用<script>声明动态SQL

@Select("<script>" + 
    "select * from User " + 
    "<where>" + 
    " id = #{id}" + 
    "</where>" + 
    "</script>")
User selectUser(int id);

2. 判断空字符用单引号

@Select("<script>" + 
    "select * from User " + 
    "<where>" + 
    "<if test=\"name != null and name != ''\">" + 
    " name = #{name}" + 
    "</if>" + 
    "</where>" + 
    "</script>")
User selectUser(String name);

3. if、when等判断条件拼接用and或or 

@Select("<script>" + 
    "select * from User " + 
    "<where>" + 
    "<if test=\"name != null and name != ''\">" + 
    " name = #{name}" + 
    "</if>" + 
    "</where>" + 
    "</script>")
User selectUser(String name);

4. 模糊查询使用concat()拼接函数

@Select("<script>" + 
    "select * from User " + 
    "<where>" + 
    "<if test=\"name != null and name != ''\">" + 
    " name like concat('%', #{name}, '%') " + 
    "</if>" + 
    "</where>" + 
    "</script>")
User selectUser(String name);

 

标签:selectUser,name,Select,User,SQL,Mybatis,注解,select
From: https://www.cnblogs.com/nhdlb/p/17225418.html

相关文章

  • MSSQLSERVER 存储过程debug调试
    每当我们遇到相对稍复杂的业务的时候,都会考虑写在存储过程中,这样相当于一个黑匣,方便管理。 但是如果写的行数太多,如果碰到了问题,凭经验,很难发现问题,那就要用到debug调试......
  • 修复SQLServer 2014支持 TLS 1.2
    修复原因:当把.netcore应用程序部署到linux或docker中去的时候,连接sqlserver数据库可能报错如下:Aconnectionwassuccessfullyestablishedwiththeserver,butthena......
  • SQL—对学校和性别进行分组,计算用户活跃度和发帖数量
    题目:求每个学校(university)每种性别(gender)的用户数、30天内平均平均活跃天数(active_days_within_30)和平均发帖数量(question_cnt)。我的尝试:selectcount(device_id......
  • SQL - 内置函数
         ......
  • MyBatis日志四
    使用useGeneratedKeys和keyProperty属性<insertid="insert"parameterType="com.yogurt.po.Student"useGeneratedKeys="true"keyProperty="id">INSERTINT......
  • MyBatis学习日志五
    缓存一级缓存默认开启,同一个SqlSesion级别共享的缓存,在一个SqlSession的生命周期内,执行2次相同的SQL查询,则第二次SQL查询会直接取缓存的数据,而不走数据库,当然,若第一次和第......
  • SQL—计数(count)与求平均值(avg/AVG)大小写都能识别
    题目要求:计算男生人数以及求平均gpa,而且还需要将查询后的列重新命名(注意有将平均gpa保留到小数点后一位的限制。)两个具体要求:计数与平均、重新命名selectcount(gender)......
  • MyBatis
    MyBatis1.编写流程1、编写工具类如:MybatisUtils(里面需要一个配置文件mybatis-config.xml)​ 主要是为了获取SqlSessionFactory,以及从中获得SqlSession的实例2、编......
  • MySql生成ER【StarUML】文件
    1.背景要画ER图,一个个打费时费力,StarUML文件打开是json。那么就有可能自动生成。2.效果把表结构生成好,自己只要维护关系即可。3.代码importlombok.Data;import......
  • 【项目实战】基于Python+Django+MySQL的自行车租赁系统(附完整源码)
    1、项目说明基于python+Django+Mysql的自行车租赁系统项目实战项目需要安装pycharm专业版以及MySQL环境(环境搭建和破解可以看我的B站里的视频有讲解)首先需要创建数据......