Access to public static end row in mybatis sql in mapper files

I have sql written in the Mapper MyBatis file, which looks something like this:

<select id="somesql"> select a,b,c from tbl_name where d = ? </select> 

The placeholder value for d must be a constant declared in a file called Constants.java as follows:

 public static final String d = "d_value"; 

How to replace a placeholder with a value without actually passing the parameter in the <select> build? I tried #{com.pkg.name.Constants.d} but that did not work.

No hard coding !!!

+6
source share
2 answers

With the behavior of MyBatis from the drawer, as far as I know, you cannot.

You can try to write an interceptor for the ParameterHandler parameter and enter a value there, but this is not always possible, since you can play with only two methods in the parameter handler:

  • getParameterObject , which returns the parameter that was sent to the request (it can be a constant, a string, a map, a custom object, or even zero, as is the case in your example where you do not send the parameter)
  • setParameters , where, I think, you could try setting the parameter if you know where it is in the prepared statement (in most cases you cannot).

My suggestion is to pass it as a parameter to your request. The functions of the MyBatis interceptor are not well documented, so you may not get the right combination from the start, and you may have more problems than it costs.

+2
source
 <select id="getConvenienceStoreList" resultType ="Store"> SELECT * FROM Store WHERE type = ${@foo.product.co nstant.StoreType@CONVENIENCE _STORE} ORDER BY id LIMIT #{start}, #{limit} </select> 

Link: http://qiita.com/ApplePedlar/items/12dc389cc32f3db5557a

+3
source

Source: https://habr.com/ru/post/922802/


All Articles