-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add a new annotation @Sql
#3000
Conversation
Co-authored-by: hssy252 <[email protected]>
Hello @Lambert-Rao , I apologize if I wasn't clear enough, but (I thought) I wrote that we do not plan to support it at the moment. We are deliberate in adding new features to keep the project simple. |
Okay, thanks for your reply, @harawata. I'm sorry to disturb you again, but we do need this feature for reasons:
multiple line annotation with @Update({"<script>",
"update",
RESOURCE_TABLE,
"<set>",
"<if test = '#{roleId} != null'>role_id=#{roleId},</if>",
"<if test = '#{roleName} != null'>role_name=#{roleName},</if>",
"<if test = '#{roleDescription} != null'>role_description=#{roleDescription},</if>",
"</set>",
"where role_id = #{roldId}",
"</script>"
}) multiple line annotation with @Update("""
<script>
update
<include refid="RESOURCE_TABLE"/>
<set>
<if test = '#{roleId} != null'>role_id=#{roleId},</if>
<if test = '#{roleName} != null'>role_name=#{roleName},</if>
<if test = '#{roleDescription} != null'>role_description=#{roleDescription},</if>
</set>
where role_id = #{roldId}
</script>
"""
) if we don't add
Sincerely thank you for taking time reading my explanation, please considering review out code. We do need this feature, and we are glad that we can participate in this excellent project. |
The limitation (i.e. unable to use I honestly don't understand what's so hard about maintaining XML, but if you so desperately want to avoid it, you should consider using SQL providers. Just to give you an idea, you can add custom annotations to your mapper classes/methods. @MyVar(id = "__COLUMNS__", value = "id, name, ...")
@MyVar(id = "__TABLE__", value = "student")
public interface StudentMapper {
@SelectProvider // assuming defaultSqlProviderType is specified
@Lang(XMLLanguageDriver.class)
@MySql("select __COLUMNS__ from __TABLE__ where id = #{id}")
Student selectStudentById(Integer id);
@InsertProvider // assuming defaultSqlProviderType is specified
@Lang(XMLLanguageDriver.class)
@MySql("insert into __TABLE__ (__COLUMNS__) values (#{id}, #{name})")
int insert(Student student);
} And when the statement is called, the provider performs the variable substitution. public class SubstrProvider implements ProviderMethodResolver {
@Override
public Method resolveMethod(ProviderContext context) {
return this.getClass().getDeclaredMethod("substr", ProviderContext.class);
}
public String substr(ProviderContext context) {
String sql = // get value of @MySql
Map<String, String> myVars = // collect id/value of @MyVar
// replace variables in the sql
return "<script>" + finalSql + "</script>";
}
} For the provider implementation (accessing annotations, etc.), there are some examples in this test. If you have difficulty with provider implementation, I would be happy to help, but it might take a while for me to reply. |
@harawata There are many solutions, and how to make them lightweight, simple, comprehensive,and easy to use is the most important.I hope the official can provide a solution, which will reduce maintenance costs, learning costs, management costs, and migration costs Developers don't even want a demo |
I add an
@Sql
annotation to allow user reuse Sql statement in Mapper interface.This example shows using
@Sql
annotation to define a statement that can be reuse in<include>
:reference to issue #2986