因为重构dao层,问题太多了,于是想着另起一个章节。
4 count的问题
使用count复用,不需要查询所有字段,否则会出现下面的错误
### SQL: SELECT COUNT( t.id,t.company_id,t.user_id,t.bind_time,t.role_type,t.job_type,t.student_class,t.is_current,t.used_time,t.is_operational,t1.name AS name,t1.type AS name,t1.tax_no AS name,t1.tel AS name,t1.province AS name,t1.city AS name,t1.area AS name,t1.address AS name,t1.status AS name,t1.creator_id AS name ) FROM eayc_company_user t INNER JOIN eayc_company t1 ON (t1.id = t.company_id) WHERE (t1.status = ? AND t.user_id = ?)
### Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't.company_id,t.user_id,t.bind_time,t.role_type,t.job_type,t.student_class,t.is_c' at line 2
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't.company_id,t.user_id,t.bind_time,t.role_type,t.job_type,t.student_class,t.is_c' at line 2
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:239)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:70)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:91)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441)
at com.sun.proxy.$Proxy161.selectOne(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:160)
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:89)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:148)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89)
at com.sun.proxy.$Proxy174.selectJoinCount(Unknown Source)
at com.github.yulichang.base.MPJBaseService.selectJoinCount(MPJBaseService.java:54)
at com.whty.eayc.user.service.base.impl.EaycCompanyUserServiceImpl.count(EaycCompanyUserServiceImpl.java:156)
处理方案,如下即可
MPJLambdaWrapper<EaycCompanyUser> mpjLambdaWrapper = new MPJLambdaWrapper<>();
mpjLambdaWrapper.select("1")
.innerJoin(EaycCompany.class,EaycCompany::getId,EaycCompanyUser::getCompanyId);
3 mybatis封装对象的问题
直接使用mybatis自身的对象,会有问题,他的列表对象会设置一个id,导致后续的工程会出问题
<select id="getStatementDefaultEquations" resultMap="defaultEquationsMap">
select
<include refid="com.whty.acc.setting.dao.AccStatementMapper.Base_Column_List_Udf">include>,
<include refid="com.whty.acc.setting.dao.AccStatementDefaultMapper.Base_Column_List_Udf">include>
from acc_statement accStatement
left join acc_statement_default accStatementDefault
on accStatement.id = accStatementDefault.sheet_id
where accStatement.accounting_standard = #{standard}
and accStatement.type = #{type}
order by accStatement.id, accStatementDefault.id
select>
<resultMap id="defaultEquationsMap" type="com.whty.acc.setting.dto.dto.StatementDefaultEquationDto" extends="com.whty.acc.setting.dao.AccStatementMapper.BaseResultMapUdf">
<collection property="accStatementDefaults" ofType="com.whty.acc.setting.pojo.AccStatementDefault" resultMap="com.whty.acc.setting.dao.AccStatementDefaultMapper.BaseResultMapUdf">
collection>
resultMap>
调整如下,按照mybatis的写法,对象才能封装正确
@Override
public List<StatementDefaultEquationDto> getStatementDefaultEquations(String type, String standard) {
MPJLambdaWrapper<AccStatement> mpjLambdaWrapper = new MPJLambdaWrapper<>();
mpjLambdaWrapper.selectAll(AccStatement.class)
.selectCollection(AccStatementDefault.class,StatementDefaultEquationDto::getAccStatementDefaults)
.leftJoin(AccStatementDefault.class,AccStatementDefault::getSheetId,AccStatement::getId);
mpjLambdaWrapper.eq(AccStatement::getAccountingStandard,standard);
mpjLambdaWrapper.eq(AccStatement::getType,type);
mpjLambdaWrapper.orderByAsc(AccStatement::getId);
mpjLambdaWrapper.orderByAsc(AccStatementDefault::getId);
return selectJoinList(StatementDefaultEquationDto.class,mpjLambdaWrapper);
}
2 看似是空指针异常,实则与mybatis-plus写法有关系。
09:54:55.293 ERROR c.w.a.s.controller.AccBalanceSheetController - null
java.lang.NullPointerException: null
at com.whty.acc.statement.service.ext.impl.AccBalanceSheetExtServiceImpl.genBalanceAndNoteBySimpleDto(AccBalanceSheetExtServiceImpl.java:1333)
at com.whty.acc.statement.service.ext.impl.AccBalanceSheetExtServiceImpl.genSheetLineBalanceBySimpleDto(AccBalanceSheetExtServiceImpl.java:1257)
at com.whty.acc.statement.service.ext.impl.AccBalanceSheetExtServiceImpl.assetsBySimpleDto(AccBalanceSheetExtServiceImpl.java:574)
at com.whty.acc.statement.service.ext.impl.AccBalanceSheetExtServiceImpl.getDataBySimpleDto(AccBalanceSheetExtServiceImpl.java:392)
at com.whty.acc.statement.service.ext.impl.AccBalanceSheetExtServiceImpl.getBalanceSheet(AccBalanceSheetExtServiceImpl.java:158)
at com.whty.acc.statement.service.ext.impl.AccBalanceSheetExtServiceImpl$$FastClassBySpringCGLIB$$4edc7667.invoke(<generated>)
java.lang.NullPointerException: null
at com.whty.acc.statement.service.ext.impl.AccCashFlowEquationServiceImpl.lambda$getEquationDtos$3(AccCashFlowEquationServiceImpl.java:170)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:174)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at com.whty.acc.statement.service.ext.impl.AccCashFlowEquationServiceImpl.getEquationDtos(AccCashFlowEquationServiceImpl.java:170)
at com.whty.acc.statement.service.ext.impl.AccCashFlowEquationServiceImpl.getCashFlowByEquation(AccCashFlowEquationServiceImpl.java:87)
at com.whty.acc.statement.service.ext.impl.AccCashFlowSheetExtServiceImpl.getCashFlowSheetDtos(AccCashFlowSheetExtServiceImpl.java:91)
at com.whty.acc.statement.service.ext.impl.AccCashFlowSheetExtServiceImpl$$FastClassBySpringCGLIB$$3b4f4002.invoke(<generated>)
1 分表后没有生产导致sql异常
下面这个语句咋一看是没有任何问题的,但实际根本没有acc_voucher这张表,因为数据库做了分表。
所以问题在为什么mybatis-plus后,delete操作分表没有生效。
下面是正常情况,ShardingSphere-SQL会做一次翻译
但下面删除操作却没有,为什么呢?
### SQL: DELETE FROM acc_voucher t WHERE (t.as_id = ? AND t.id = ?)
### Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't
WHERE
(t.as_id = 92293 AND t.id = 2)' at line 2
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't
WHERE
(t.as_id = 92293 AND t.id = 2)' at line 2
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:239)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:70)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:91)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441)
at com.sun.proxy.$Proxy169.delete(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.delete(SqlSessionTemplate.java:304)
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:69)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:148)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89)
at com.sun.proxy.$Proxy174.delete(Unknown Source)
at com.baomidou.mybatisplus.extension.service.IService.remove(IService.java:149)
at com.whty.acc.voucher.service.base.impl.AccVoucherServiceImpl.deleteByPrimaryKey(AccVoucherServiceImpl.java:89)
at com.whty.acc.voucher.service.base.impl.AccVoucherServiceImpl$$FastClassBySpringCGLIB$$f3310a26.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:793)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:708)
at com.whty.acc.voucher.service.base.impl.AccVoucherServiceImpl$$EnhancerBySpringCGLIB$$9a0ca76a.deleteByPrimaryKey(<generated>)
at com.whty.acc.voucher.service.dubbo.consume.impl.AccVoucherDubboConsumeImpl.remove(AccVoucherDubboConsumeImpl.java:101)
at com.whty.acc.voucher.service.dubbo.consume.impl.AccVoucherDubboConsumeImpl.removeByLock(AccVoucherDubboConsumeImpl.java:79)
at com.whty.acc.voucher.service.dubbo.consume.impl.AccVoucherDubboConsumeImpl$$FastClassBySpringCGLIB$$e041f606.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:793)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:49)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:58)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:708)
at com.whty.acc.voucher.service.dubbo.consume.impl.AccVoucherDubboConsumeImpl$$EnhancerBySpringCGLIB$$c79c2987.removeByLock(<generated>)
at com.whty.acc.voucher.controller.AccVoucherController.remove(AccVoucherController.java:583)
at com.whty.acc.voucher.controller.AccVoucherController$$FastClassBySpringCGLIB$$271f1de3.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:793)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
at org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:123)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:102)
at com.whty.acc.setting.annotation.WebLogAspect.around(WebLogAspect.java:60)
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.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:634)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:624)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:72)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:58)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:708)
at com.whty.acc.voucher.controller.AccVoucherController$$EnhancerBySpringCGLIB$$ca9bb02d.remove(<generated>)
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.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1071)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:964)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doDelete(FrameworkServlet.java:931)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:671)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at com.whty.framework.web.filter.CorsFilter.doFilter(CorsFilter.java:41)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:177)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:891)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1784)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
查看删除操作的代码
@Transactional
@Override
public boolean deleteByPrimaryKey(Integer asId, Integer id) {
MPJLambdaWrapper<AccVoucher> mpjLambdaWrapper = new MPJLambdaWrapper<>();
mpjLambdaWrapper.eq(AccVoucher::getAsId,asId);
mpjLambdaWrapper.eq(AccVoucher::getId,id);
return remove(mpjLambdaWrapper);
}
调整如下,问题就解决了,看来问题在MPJLambdaWrapper,对分表的删除操作有影响。
@Transactional
@Override
public boolean deleteByPrimaryKey(Integer asId, Integer id) {
LambdaQueryWrapper<AccVoucher> mpjLambdaWrapper = new LambdaQueryWrapper<>();
mpjLambdaWrapper.eq(AccVoucher::getAsId,asId);
mpjLambdaWrapper.eq(AccVoucher::getId,id);
return remove(mpjLambdaWrapper);
}