In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
这篇文章给大家介绍Mybatis中怎么使用if语句,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
一个参数情况DAO层Map selectPeriodByDataType(String dataType);XML文件
一个参数时,在if条件判断时,需要使用 _parameter 如果使用dataType,会报错如下
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'dataType' in 'class java.lang.String' at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446) at com.sun.proxy.$Proxy115.selectOne(Unknown Source) at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166) at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:82) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59) at com.sun.proxy.$Proxy155.selectPeriodByDataType(Unknown Source) at com.bjbde.trade.service.impl.WmsDictionaryDataServiceImpl.getBuyerRightsPeriod(WmsDictionaryDataServiceImpl.java:47) at com.bjbde.trade.service.impl.WmsDictionaryDataServiceImpl$$FastClassBySpringCGLIB$$b29775c3.invoke()解决方案
方案一
${alias}.DATA_VALUE dataValue SELECT FROM WMS_DICTIONARY_DATA t1 WHERE t1.IS_VALID = '1' AND t1.DATA_NAME = 'data_period' AND t1.DATA_NAME = 'api_period' AND t1.DATA_NAME = 'analysis_period' AND t1.DATA_NAME = 'model_period'
if中的test使用单引号,dataType参数对应的为字符串,需要使用""来引起来。
方案二
${alias}.DATA_VALUE dataValue SELECT FROM WMS_DICTIONARY_DATA t1 WHERE t1.IS_VALID = '1' /*AND t1.DATA_NAME = 'data_period'*/ AND t1.DATA_NAME = 'data_period' AND t1.DATA_NAME = 'api_period' AND t1.DATA_NAME = 'analysis_period' AND t1.DATA_NAME = 'model_period'
dataType对应的参数需要加上toString()方法,否则会将该值当做数字,从而找不到满足要求的条件。
多个参数情况DAO层 List selectDataProductList(DataProductListVO vo);POJO类DataProductListVO@Builder@Datapublic class DataProductListVO implements Serializable { private static final long serialVersionUID = -6905484517733433984L; //页码 默认值0 private Integer page = 0; //页长 默认值0 private Integer size = 0; private String industry; private String ownership; private String salNum; private String wiVal; private String name; private String tag; private String address; private String pubTime; private String comment; private String type;}
多个参数时,常将参数封装成对象来处理。
xml文件 ${alias}.DATA_ID,${alias}.NAME,${alias}.SH_IMG,${alias}.TYP PRODUCT_TYP,${alias}.INDUSTRY,${alias}.RANGE,${alias}.OWNERSHIP,${alias}.TRADING,${alias}.LABELS,${alias}.LINK_MAN,${alias}.LINK_PHONE,${alias}.PRICE,${alias}.HAS_SPEC,${alias}.SPEC,${alias}.WDESC,${alias}.PUB_TIME,${alias}.COMMENT_SCORE,${alias}.SALED_NUM,${alias}.VIEW_NUM,${alias}.FAVORITY_NUM,${alias}.WSTATE,${alias}.MBR_ID,${alias}.DEFAULT_WI select from PSM_DATA_BASE t1 where t1.WSTATE = '01' AND t1.NAME like '%${name}%' AND t1.TYP = '%${type}%' AND t1.RANGE like '%${RANGE}%' AND t1.INDUSTRY like '%${industry}%' AND t1.LABELS like '%${tag}%' AND t1.OWNERSHIP like '%${ownership}%' order by t1.SALED_NUM asc t1.SALED_NUM desc t1.DEFAULT_WI asc t1.DEFAULT_WI desc t1.PUB_TIME asc t1.PUB_TIME desc t1.COMMENT_SCORE asc t1.COMMENT_SCORE desc t1.DEFAULT_WI asc 关于Mybatis中怎么使用if语句就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.