Dynamic SQL statements used by the MyBatis
A: Introduction
MyBatis Dynamic SQL statements can help us dynamically stitch SQL statements as needed. Mainly used in configuration files <where> <if><choose><when><otherwise> <set> <trim><foreach > tags to achieve.
Second: The specific use way
2.1 Where
2.1.1 function
The main purpose of the statement is to simplify the conditional judgment in the where element in the SQL statement, where the role is to output a where element, and another benefit is that you do not have to consider what the conditional output inside the where element looks like, MyBatis will intelligently help you handle, if all the conditions are not satisfied then MyBatis will find all the records, if the output is and start, MyBatis will be the first and ignore, of course, if the or start, MyBatis will ignore it; In the WHERE element you do not need to consider the question of spaces, MyBatis will intelligently help you add.
2.1.2 Mapping File configuration:
<select id= "getblogbyconditiondynamic" resultmap= "onetomanynested" > select T1.id blog_id, T1.title, t2.id post_id, t2.section from blog T1 left outer join post t2 on t1.id = t2.blog_id <where> <if test= "titl E! = null and Title! = "" > and t1.title like '% ' | | #{title}| | ' % ' </if> <if test= "section! = null and section! =" ' "> and t2.section like '% ' | | #{section}| | ' % ' </if> </where> </select>
2.1.3 Mapping Interface Method:
List<blog> getblogbyconditiondynamic (@Param ("title"), string title, @Param ("section") of string section);
2.1.4 Test Method:
@Test public void Testgetblogbyconditiondynamic () throws Exception { list<blog> blogList = Blogmapper.getblogbyconditiondynamic ("", "section"); Assert.assertnotnull (blogList); }
3.1 If
3.1.1 function
Simple conditional judgment, we can implement some simple condition selection by using the IF statement. The judgment is the concatenation, otherwise ignores.
3.1.2 Mapping File configuration:
<select id= "getblogdynamic" resultmap= "onetomanynested" > select T1.id blog_id, T1.title, T2.id post_id, T2.section from blog T1 left outer join post t2 on t1.id = t2.blog_id where 1=1 <if test= "Title! = null and Tit Le! = "" > and t1.title like '% ' | | #{title}| | ' % ' </if> <if test= "section! = null and section! =" ' "> and t2.section like '% ' | | #{section}| | ' % ' </if> </select>
3.1.3 Mapping interface Method:
List<blog> getblogdynamic (@Param ("title"), string title, @Param ("section") of string section);
3.1.4 Test Method:
public class Blogmappertest { private blogmapper blogmapper; Public Blogmappertest () { blogmapper = Mybatisutil.getsqlsession (). Getmapper (Blogmapper.class); } @Test public void Testgetblogdynamic () throws Exception { list<blog> blogList = Blogmapper.getblogdynamic ("title", "section"); Assert.assertnotnull (blogList); }
4.1 Choose
4.1.1 function
The role of the choose element is equivalent to the switch statement in Java, essentially the same as the action and usage of choose in Jstl, usually paired with when and otherwise. When the element indicates that when the conditions in the time when the content of the output, and the switch effect in Java is similar to the order of the conditions, when there is a condition to meet the time, will jump out of choose, that is, all the when and otherwise conditions, Only one will output, and when all of my conditions are not satisfied, I output the contents of otherwise.
4.1.2 Mapping File configuration:
<select id= "getblogbytitleorsection" resultmap= "onetomanynested" > select T1.id blog_id, T1.title, t2.id post_id, t2.section from blog T1 left outer join post t2 on t1.id = t2.blog_id where 1=1 <choose> <WH En test= "title! = null and Title! =" "> and t1.title like '% ' | | #{title}| | ' % ' </when> <when test= "section! = null and section! =" ' "> and t2.section like '% ' | | #{section}| | ' % ' </when> <otherwise> and t1.title are NOT null and t2.section are not null </ otherwise> </choose> </select>
4.1.3 Mapping Interface Method:
List<blog> getblogbytitleorsection (@Param ("title"), string title, @Param ("section") of string section);
4.1.4 Test Method:
@Test public void Testgetblogbytitleorsection () throws Exception { list<blog> blogList = Blogmapper.getblogbytitleorsection ("," ""); Assert.assertnotnull (blogList); }
5.1 Set
5.1.1 function
The set element is mainly used in the update operation, its main function and the where element is actually similar, mainly in the containing statement before the output of a set, and then if the containing statement is terminated with a comma, the comma will be ignored if the set contains the If the content is empty, an error occurs. With the set element, we can dynamically update those fields that have been modified.
5.1.2 Mapping File configuration:
<update id= "Updateblogtitleset" parametertype= "blog" > update blog <set> <if test= "title ! = null and Title! = "" > title = #{title} </if> <if test= "id! = NULL and ID! =" "> , id = #{id} </if> </set> where id = #{id} </update>
5.1.3 Mapping Interface Method:
int updateblogtitleset (blog blog);
5.1.4 test Method:
@Test public void Testupdateblogtitleset () throws Exception { Blog blog = new Blog (1, ""); int flag = this.blogMapper.updateBlogTitleSet (blog); Assert.assertequals (TRUE, flag > 0); }
6.1 Trim
6.1.1 function
The main function of the trim element is that you can add some prefixes to the content you include, or you can add some suffixes to it, and the corresponding properties are prefix and suffix; you can overwrite the header of the containing content The corresponding properties are prefixoverrides and suffixoverrides; Because trim has such a function, we can also use trim to replace the where element's functionality very simply.
6.1.2 Mapping File configuration:
<select id= "Getblogbyconditiondynamictrim" resultmap= "onetomanynested" > select T1.id blog_id, T1.title, T2.id post_id, t2.section from blog T1 left outer join post t2 on t1.id = t2.blog_id <trim prefix= "where" Prefi Xoverrides= "and | or "> <if test=" Title! = null and Title! = "" > and t1.title like '% ' | | #{title}| | ' % ' </if> <if test= "section! = null and section! =" ' "> and t2.section like '% ' | | #{section}| | ' % ' </if> </trim> </select>
6.1.3 Mapping Interface Method:
List<blog> Getblogbyconditiondynamictrim (@Param ("title"), string title, @Param ("section") of string section);
6.1.4 test Method:
@Test public void Testgetblogbyconditiondynamictrim () throws Exception { list<blog> blogList = Blogmapper.getblogbyconditiondynamictrim ("title", "section"); Assert.assertnotnull (blogList); }
7.1 foreach
7.1.1 function
The main use of foreach is in the build in condition, which can iterate a collection in an SQL statement. The properties of the Foreach element are mainly item,index,collection,open,separator,close. The item represents the alias of each element in the collection when it iterates, and index specifies a name that represents the position at which each iteration occurs during the iteration, and open indicates what the statement begins with, and separator indicates what symbol is used as the delimiter between each iteration. Close means the end, the most critical and error-prone when using foreach is the collection property, which must be specified, but in different cases the value of the property is not the same, there are 3 main cases:
If a single parameter is passed in and the parameter type is a list, the collection property value is List
If a single parameter is passed in and the parameter type is an array, the value of the collection property is array
If the parameters passed in are multiple, we need to encapsulate them into a map, of course, the single parameter can also be encapsulated asa map, in fact, if you pass in the parameter, in the MyBatis will also wrap it into a map, the map key is the parameter name, So this time the collection property value is the key to the incoming list or array object in its own encapsulated map
Take array for example:
7.1.2 Mapping File configuration:
<select id= "Dynamicforeach2test" resulttype= "Blog" >select * from T_blog where ID in<foreach collection= "array" index= "index" item= "Item" open= "(" separator= "," close= ")" >#{item}</foreach></select>
7.1.3 Mapping Interface Method:
Public list<blog> dynamicforeach2test (int[] IDs);
7.1.4 test Method:
@Testpublic void Dynamicforeach2test () {sqlsession session = Mybatisutil.getsqlsessionfactory (). Opensession (); Blogmapper blogmapper = Session.getmapper (Blogmapper.class); int[] ids = new int[] {1,3,6,9}; list<blog> blogs = blogmapper.dynamicforeach2test (IDs);}
Third: Supplement
More content: Mybatis directory
GitHub Address: Https://github.com/andyChenHuaYing/scattered-items/tree/master/items-mybatis
Source: http://download.csdn.net/detail/chenghuaying/8713311
Dynamic SQL statements used by the MyBatis