博客
关于我
利用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/

你可能感兴趣的文章
Orderer节点启动报错解决方案:Not bootstrapping because of 3 existing channels
查看>>
org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected subelement profile
查看>>
sql查询中 查询字段数据类型 int 与 String 出现问题
查看>>
org.apache.commons.beanutils.BasicDynaBean cannot be cast to ...
查看>>
org.apache.dubbo.common.serialize.SerializationException: com.alibaba.fastjson2.JSONException: not s
查看>>
sqlserver学习笔记(三)—— 为数据库添加新的用户
查看>>
org.apache.http.conn.HttpHostConnectException: Connection to refused
查看>>
org.apache.ibatis.binding.BindingException: Invalid bound statement错误一例
查看>>
org.apache.ibatis.exceptions.PersistenceException:
查看>>
org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
查看>>
org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
查看>>
org.apache.poi.hssf.util.Region
查看>>
org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
查看>>
org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
查看>>
org.hibernate.HibernateException: Unable to get the default Bean Validation factory
查看>>
org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
查看>>
org.springframework.boot:spring boot maven plugin丢失---SpringCloud Alibaba_若依微服务框架改造_--工作笔记012
查看>>
SQL-CLR 类型映射 (LINQ to SQL)
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>