mbatis--Dynamic SQL

Source: Internet
Author: User

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><Mappernamespace= "COM.NUCH.EDU.MAPPER.EMPLOYEEDYNAMICSQL">    <!--if: Judge choose (when, otherwise): branch selection, with a break swtich-case if the ID with ID, if the LastName with LastName to check; will only enter one Trim string interception (where (package query condition), set (package modify condition)) foreach Traversal collection -    <!--query employees, ask, which field to bring the query criteria with the value of this field -    <SelectID= "Getempsbyconditionif"Resulttype= "Com.nuch.edu.domain.Employee">SELECT * from Tbl_employee<where>            <!--test: Judgment expression (OGNL) OGNL refer to PPT or official document.  -            <ifTest= "id! = NULL">id = #{id}</if>            <!--OGNL will convert the string and the number to judge "0" ==0 -            <ifTest= "gender==0 or gender ==1">and gender = #{gender}</if>            <ifTest= "Email! = null and Email.trim ()! =" ">and email = #{email}</if>            <ifTest= "LastName! = null and Lastname.trim ()! =" ">and last_name like #{lastname}</if>        </where>    </Select>    <SelectID= "Getempsbyconditiontrim"Resulttype= "Com.nuch.edu.domain.Employee">SELECT * from Tbl_employee<!--The later and or or where tags do not resolve the prefix= "": prefix: the trim tag body is the result of the entire string.                 Prefix adds a prefix to the entire string after the prefixoverrides= "": Prefix Overwrite: Remove the extra characters in front of the entire string suffix= "": Suffix Suffix the entire string followed by a suffix suffixoverrides= "" suffix overwrite: Remove extra characters from the entire string -        <!--interception rules for custom strings -        <Trimprefix= "where"Prefixoverrides= "and"suffix=""Suffixoverrides= "and">            <ifTest= "id! = NULL">id = #{id} and</if>            <ifTest= "gender==0 or gender ==1">gender = #{gender} and</if>            <ifTest= "Email! = null and Email.trim ()! =" ">email = #{email} and</if>            <ifTest= "LastName! = null and Lastname.trim ()! =" ">last_name like #{lastname} and</if>        </Trim>    </Select>    <SelectID= "Getempsbyconditionchoose"Resulttype= "Com.nuch.edu.domain.Employee">SELECT * from Tbl_employee<where>            <!--if ID is taken with ID, if you bring LastName, you can use LastName to check; only one of them will enter . -            <Choose>                < whenTest= "id! = NULL" >id = #{id}</ when>                < whenTest= "Email! = null and Email.trim ()! =" ">email = #{email}</ when>                < whenTest= "LastName! = null and Lastname.trim ()! =" " >last_name = #{lastname}</ when>                <otherwise>gender = 1</otherwise>            </Choose>        </where>    </Select>    <UpdateID= "Updateempsbyset">Update Tbl_employee<!--use of Set tags -        <Set>            <ifTest= "gender==0 or gender ==1">gender = #{gender},</if>            <ifTest= "Email! = null and Email.trim ()! =" ">email = #{email},</if>            <ifTest= "LastName! = null and Lastname.trim ()! =" ">last_name = #{lastname},</if>        </Set>        <where>            <ifTest= "id! = NULL">id = #{id}</if>        </where>    </Update>    <SelectID= "Getempsbyconditioncollection"Resulttype= "Com.nuch.edu.domain.Employee">SELECT * from Tbl_employee WHERE ID in<!--Collection: Specifies the collection to traverse: The parameter of the list type is encapsulated in the map, and the map key is called list item: assigns the currently traversed element to the specified Variable separator: delimiter between each element open: Iterates through all results stitching a starting character close: Traverse out all results stitching an end character index Index When you traverse the list, index is the index, item is the current value when traversing the map, index indicates that map Key,item is the value of map #{variable name} can be taken out of the value of the variable is The currently traversed element -          <foreachCollection= "IDs"Item= "id"Open="("Close=")"Separator=",">#{id}</foreach>    </Select>    <!--Bulk Save -    <!--MySQL under Batch Save: can be foreach traversal MySQL support values (), (), () syntax -   <!--<insert id= "Addemps" > INSERT into Tbl_employee (gender,email,last_name,dept_id) VALUES <for Each collection= "Emps" item= "emp" separator= "," > (#{emp.gender},#{emp.email},#{emp.lastname},#{emp.dept.i D}) </foreach> </insert> -    <!--database connection properties are required to execute multiple SQL at once allowmultiqueries=true; this semicolon separates multiple SQL can be used for other bulk operations (delete, modify) -    <!--url=jdbc:mysql://localhost:3306/mybatis?allowmultiqueries=true -    <InsertID= "Addemps">        <foreachCollection= "Emps"Item= "EMP"Separator=";">INSERT into Tbl_employee (gender,email,last_name,dept_id) VALUES (#{emp.gender},#{emp.email}, #{emp.lastname},#{emp.dept.id})</foreach>    </Insert></Mapper>

mbatis--Dynamic SQL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.