博客
关于我
利用Mybaties注解动态Sql实现查询功能(表名不固定)
阅读量:254 次
发布时间:2019-03-01

本文共 352 字,大约阅读时间需要 1 分钟。

数据库开发中,经常需要处理表名动态拼接SQL的问题。在我的项目中,数据库表名为日表,每次查询都需要动态传入表名,因此需要对SQL进行拼接处理。

首先,考虑使用Mapper接口。Mapper接口需要调用Provider类,Provider类需要自己定义一个新的类来返回SQL。这类可以通过StringBuider动态拼接SQL,并将结果返回,与Mapper.xml中的SQL语句保持一致。

其次,Provider类采用StringBuider拼接SQL。这种方式可以灵活控制拼接逻辑,适合需要复杂条件拼接SQL的场景。但需要注意SQL注入问题,确保参数安全性。

这种方法的核心在于通过动态拼接实现表名的灵活性,同时保持与Mapper.xml一致的SQL格式。这种设计方式在处理多个表名或动态条件时非常有用。

转载地址:http://yykt.baihongyu.com/

你可能感兴趣的文章
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>