Orm 如何在带注释的MyBatis中使用动态SQL查询(如何使用selectProvider)?
我试图避免在mybatis3中使用额外的xml来定义映射器。注释正好合适 我对@SelectProvider/@InsertProvider/等的用法感到有点困惑。不要认为网上有太多的资源指导我完成这项工作 基本上,我希望在mybatis3中找到alternative for的注释版本 例如,我有一个xml映射器,我想将其转换为使用注释Orm 如何在带注释的MyBatis中使用动态SQL查询(如何使用selectProvider)?,orm,annotations,mapping,mybatis,Orm,Annotations,Mapping,Mybatis,我试图避免在mybatis3中使用额外的xml来定义映射器。注释正好合适 我对@SelectProvider/@InsertProvider/等的用法感到有点困惑。不要认为网上有太多的资源指导我完成这项工作 基本上,我希望在mybatis3中找到alternative for的注释版本 例如,我有一个xml映射器,我想将其转换为使用注释 <select ...> <where> <if cause.....> </if>
<select ...>
<where>
<if cause.....>
</if>
<if cause......>
</if>
</where>
</select>
@Update("<script>
update Author
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio}</if>
</set>
where id=#{id}
</script>")
有人能提供一个具体的答案/解决方案,包括代码吗
提前谢谢
@SelectProvider(type=MyClass.class, method="myMethod")
public Object selectById(int id);
public static String myMethod() {
return "select * from MyTable where id=#{id}";
}
您的替代解决方案可以是: 在@annotation开头添加
<select ...>
<where>
<if cause.....>
</if>
<if cause......>
</if>
</where>
</select>
@Update("<script>
update Author
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio}</if>
</set>
where id=#{id}
</script>")
@更新(“
更新作者
用户名=#{username},
密码=#{password},
电子邮件=#{email},
bio=#{bio}
其中id=#{id}
")
另外,我们在项目中将.groovy编译为.class,这样我们就可以像上面那样在@annotation中编写SQL了这真是太酷了。但有一点需要注意:
元素必须是注释值中的第一个字符,前面不能有任何其他字符(包括空格),否则它将无法正常工作。@Phate 3.2+