这篇文章给大家介绍Mybatis中怎么使用if语句,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
Map selectPeriodByDataType(String dataType);
一个参数时,在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(<generated>)
方案一
<sql id="selectPeriodByDataTypeField">${alias}.DATA_VALUE dataValue</sql>
<select id="selectPeriodByDataType" resultType="java.util.Map">
SELECT
<include refid="selectPeriodByDataTypeField">
<property name="alias" value="t1"></property>
</include>
FROM WMS_DICTIONARY_DATA t1
WHERE t1.IS_VALID = '1'
<if test='_parameter != null and _parameter == "1"'>
AND t1.DATA_NAME = 'data_period'
</if>
<if test='_parameter != null and _parameter == "2"'>
AND t1.DATA_NAME = 'api_period'
</if>
<if test='_parameter != null and _parameter == "3"'>
AND t1.DATA_NAME = 'analysis_period'
</if>
<if test='_parameter != null and _parameter == "4"'>
AND t1.DATA_NAME = 'model_period'
</if>
</select>
if中的test使用单引号,dataType参数对应的为字符串,需要使用“”来引起来。
方案二
<sql id="selectPeriodByDataTypeField">${alias}.DATA_VALUE dataValue</sql>
<select id="selectPeriodByDataType" resultType="java.util.Map">
SELECT
<include refid="selectPeriodByDataTypeField">
<property name="alias" value="t1"></property>
</include>
FROM WMS_DICTIONARY_DATA t1
WHERE t1.IS_VALID = '1'
/*AND t1.DATA_NAME = 'data_period'*/
<if test="_parameter != null and _parameter == '1'.toString()">
AND t1.DATA_NAME = 'data_period'
</if>
<if test="_parameter != null and _parameter == '2'.toString()">
AND t1.DATA_NAME = 'api_period'
</if>
<if test="_parameter != null and _parameter == '3'.toString()">
AND t1.DATA_NAME = 'analysis_period'
</if>
<if test="_parameter != null and _parameter == '4'.toString()">
AND t1.DATA_NAME = 'model_period'
</if>
</select>
dataType对应的参数需要加上toString()方法,否则会将该值当做数字,从而找不到满足要求的条件。
List<DataProductListDTO> selectDataProductList(DataProductListVO vo);
@Builder
@Data
public 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;
}
多个参数时,常将参数封装成对象来处理。
<sql id="selectDataProductListField"> ${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 </sql>
<select id="selectDataProductList" parameterType="com.bjbde.trade.model.DataProductListVO" resultMap="DataProductListDTO" useCache="false">
select
<include refid="selectDataProductListField">
<property name="alias" value="t1"/>
</include>
from PSM_DATA_BASE t1
where t1.WSTATE = '01'
AND t1.NAME like '%${name}%'
<if test="type != null and type neq 0">
AND t1.TYP = '%${type}%'
</if>
<if test="address != null and address neq 0">
AND t1.RANGE like '%${RANGE}%'
</if>
<if test="industry != null and industry neq 0">
AND t1.INDUSTRY like '%${industry}%'
</if>
<if test="tag != null and tag neq 0">
AND t1.LABELS like '%${tag}%'
</if>
<if test="ownership != null and ownership neq 0">
AND t1.OWNERSHIP like '%${ownership}%'
</if>
order by
<choose>
<when test="salNum == 1">
t1.SALED_NUM asc
</when>
<when test="salNum == 2">
t1.SALED_NUM desc
</when>
<when test="wiVal == 1">
t1.DEFAULT_WI asc
</when>
<when test="wiVal == 2">
t1.DEFAULT_WI desc
</when>
<when test="pubTime == 1">
t1.PUB_TIME asc
</when>
<when test="pubTime == 2">
t1.PUB_TIME desc
</when>
<when test="comment == 1">
t1.COMMENT_SCORE asc
</when>
<when test="comment == 2">
t1.COMMENT_SCORE desc
</when>
<otherwise>
t1.DEFAULT_WI asc
</otherwise>
</choose>
</select>
关于Mybatis中怎么使用if语句就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/yuhuashang/blog/3121635