MyBatis annotations use dynamic SQL

I have been using MyBatis for a long time, and I have been using SQL configured by XML. I just tried to use the annotation method to develop in the last project. The main reason is that the XML configuration is too cumbersome. The annotation can be written directly on the Mapper function, which is more convenient.

You cannot use dynamic SQL directly on annotations, you need to add <script> before and after it

copy code
@Select("<script> " +
            "SELECT id, name, email,password " +
            "FROM user " +
            " <where> " +
            " <if test=\"email != null\">id=#{email}</if> " +
            " <if test=\"name != null\"> AND name=#{name}</if> " +
            " </where> " +
            " </script> ")
copy code

Otherwise, MyBatis will report an error.

At the same time, LIKE cannot be used directly, which can be implemented with the help of the concat function.

@Select("SELECT name from user WHERE email LIKE concat(#{prefix},'%') limit 5")

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325753557&siteId=291194637