mysql basemapper_重写baseMapper遇到的问题

在实现一个基于BeetlSQL的`SaicBaseMapper`时,尝试重写`updateTemplateById`方法,但在执行过程中遇到了SQL语句生成错误。错误源于更新语句的条件部分,提示在3行存在词法错误。问题可能与自动生成的SQL模板有关,需要检查模板处理逻辑,特别是`@trim()`和`@if`条件判断的使用,确保变量正确引用。
摘要由CSDN通过智能技术生成

执行到到扩展的方法了

@Service

@Transactional

public class IndiaAvnServiceImpl implements AvnService {

@Override

public void update(Avn avn) {

avnDao.updateTemplateById(avn);

}

}

public interface SaicBaseMapper extends BaseMapper {

@Override

int updateTemplateById(T entity);

}

public class SaicUpdateTemplateByIdAmi extends AbstractSaicAmi implements MapperInvoke {

public Object call(SQLManager sm, Class cls, String sqlId, Method m, Object[] args) {

nameConversion = sm.getNc();

metadataManager = sm.getMetaDataManager();

String tableName = nameConversion.getTableName(cls);

TableDesc table = metadataManager.getTable(tableName);

ClassDesc classDesc = table.getClassDesc(cls, nameConversion);

StringBuilder sql = (new StringBuilder("update ")).append(tableName).append(" set ").append(this.lineSeparator);

String condition = this.appendIdCondition(cls);

condition = this.appendVersion(condition, classDesc);

Iterator cols = classDesc.getInCols().iterator();

Iterator properties = classDesc.getAttrs().iterator();

List idCols = classDesc.getIdCols();

while(cols.hasNext() && properties.hasNext()) {

String col = (String)cols.next();

String prop = (String)properties.next();

if (!classDesc.isUpdateIgnore(prop) && !idCols.contains(col)) {

if (prop.equals(classDesc.getVersionProperty())) {

sql.append(this.getKeyWordHandler().getCol(col)).append("=").append(this.getKeyWordHandler().getCol(col)).append("+1").append(",");

} else {

sql.append(this.appendSetColumn(cls, table, col, prop));

}

}

}

StringBuilder trimSql = new StringBuilder();

trimSql.append(STATEMENT_START).append("trim(){\n").append(STATEMENT_END).append("\n").append(sql);

trimSql.append(STATEMENT_START).append("}\n").append(STATEMENT_END);

sql = this.removeComma(trimSql, condition);

if (condition == null) {

throw new BeetlSQLException(4, "无法生成sql语句,缺少主键");

} else {

return sm.execute(sql.toString(),cls,args[0]);

}

}

protected String appendSetColumn(Class> c, TableDesc table, String colName, String fieldName) {

String sql = fieldName;

AbstractDBStyle db = new MySqlStyle();

ClassDesc classDesc = table.getClassDesc(c, this.nameConversion);

AttributeBuilderHolder handler = (AttributeBuilderHolder)classDesc.getColHandlers().get(fieldName);

if (handler != null && handler.supportPersistGen()) {

AttributePersistBuilder baseAttributeBuilder = (AttributePersistBuilder)handler.getInstance();

sql = baseAttributeBuilder.toSql(db, fieldName, colName, handler.getBeanAnnotaton(), table);

}

return this.STATEMENT_START + "if(!isEmpty(" + sql + ")){" + this.STATEMENT_END + "\t" + this.getKeyWordHandler().getCol(colName) + "=" + this.HOLDER_START + sql + this.HOLDER_END + "," + this.lineSeparator + this.STATEMENT_START + "}" + this.STATEMENT_END;

}

}

报错:update tb_

0|@trim(){

1|

2|update tb_avn set

3|@if(!isEmpty(vin)){vin=#vin#,

4|@}ROW_VERSION=ROW_VERSION+1,@if(!isEmpty(lastUpdateDate)){LAST_UPDATE_DATE=#lastUpdateDate#

5|@}@}

6| where id = #id# and row_version = #rowVersion#

99668 [http-nio-8080-exec-6] ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] log 175 - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.beetl.sql.core.BeetlSQLException: SQL Script Error:>>09:05:27:词法错误(TOKEN_ERROR):#v 位于3行 资源:auto.gen@trim(){

update tb_] with root cause

org.beetl.sql.core.BeetlSQLException: SQL Script Error:>>09:05:27:词法错误(TOKEN_ERROR):#v 位于3行 资源:auto.gen@trim(){

update tb_

at org.beetl.sql.core.engine.BeetlSQLTemplateExceptionHandler.processExcption(BeetlSQLTemplateExceptionHandler.java:79)

at org.beetl.core.Template.renderTo(Template.java:170)

at org.beetl.core.Template.renderTo(Template.java:91)

at org.beetl.core.Template.render(Template.java:79)

at org.beetl.sql.core.SQLScript.run(SQLScript.java:90)

at org.beetl.sql.core.SQLScript.run(SQLScript.java:64)

at org.beetl.sql.core.SQLScript.select(SQLScript.java:326)

at org.beetl.sql.core.SQLScript.select(SQLScript.java:381)

at org.beetl.sql.core.SQLManager.execute(SQLManager.java:1607)

at org.beetl.sql.core.SQLManager.execute(SQLManager.java:1587)

at com.ibeetl.admin.core.sql.mapper.internal.SaicUpdateTemplateByIdAmi.call(SaicUpdateTemplateByIdAmi.java:50)

at org.beetl.sql.core.mapper.MapperJavaProxy.invoke(MapperJavaProxy.java:168)

at org.beetl.sql.core.mapper.MapperJava8Proxy.invoke(MapperJava8Proxy.java:92)

at com.sun.proxy.$Proxy99.updateTemplateById(Unknown Source)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)

at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)

at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)

at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)

at com.sun.proxy.$Proxy101.updateTemplateById(Unknown Source)

at com.ibeetl.admin.console.service.avn.IndiaAvnServiceImpl.update(IndiaAvnServiceImpl.java:56)

at com.ibeetl.admin.console.service.avn.IndiaAvnServiceImpl$$FastClassBySpringCGLIB$$eb40ba9f.invoke()

at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)

at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)

at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:295)

at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)

at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)

at com.ibeetl.admin.console.service.avn.IndiaAvnServiceImpl$$EnhancerBySpringCGLIB$$6cb2ac6f.update()

at com.ibeetl.admin.console.web.AvnController.update(AvnController.java:93)

错误意思是sql错误,但是不知道怎么在执行之前把sql转换成需要的sql

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值