下面的这个写法为什么不能成功:
<select id="findUserByFuzzyEmail" resultMap="BaseResultMap"
parameterType="com.keymen.entity.User">
<bind name="email" value="'%' +email + '%'" />
select id,username,email,status,createtime,updatetime from tb_user
where
<if test="email != null and email != ''">
email like ${email}
</if>
</select>
异常提示:
Exception in thread "main" org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'email' in 'class java.lang.String'
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:371)
at com.sun.proxy.$Proxy16.selectList(Unknown Source)
后续代码省略...
为什么呢?
问题需要这样来解决:
在user.xml文件中做出如下改的:
注意:
parameterType使用了java.lang.String类型
bind标签的value值得写法:value="'%'+_parameter.getEmail() +'%'"
SQL语句中使用: email like #{pattern}
,注意是#,而不是$
最后,测试时使用的代码也需要注意。
<select id="findUserByFuzzyEmail" resultMap="BaseResultMap"
parameterType="java.lang.String">
select id,username,email,status,createtime,updatetime from tb_user
<bind name="pattern" value="'%'+_parameter.getEmail() +'%'" />
<where>
<if test="email != null and email != ''">
email like #{pattern}
</if>
</where>
</select>
调用时,使用的测试代码:
userTest是User类的一个对象
List<User> userListByEmail = userServiceImpl.findUserByFuzzyEmail(userTest);
查询获得了正确的结果。