MyBatis Dynamic SQL Example

Source: Internet
Author: User

where

SelectId="Findactivebloglike"ParameterType="Blog"Resulttype="Blog">SELECT*From BLOG<where><iftest="State! = null"> State=#{state}</if> <iftest="title! = NULL">and title like #{title}</if> <iftest="Author! = null and Author.name! = null">and author_name like #{author.name}</if> </where></Select>

Set

<update id="updateauthorifnecessary"ParameterType="Domain.blog.Author">Update Author<set><iftest="username! = NULL">username=#{username},</if> <iftest="password! = null">password=#{password},</if> <iftest="email! = NULL">email=#{email},</if> <iftest="bio! = NULL">bio=#{bio}</if> </Set>whereId=#{id}</update>

Traverse List

<select id= "Selectpostin" resulttype= "Domain.blog.Post" >  * from  Post P  WHERE ID in   <foreach item= "item" index= "index" collection= "list"      open= "(" separator= "," close= ")" >        # {Item}   </foreach></select>

Example:

public class Employees {
Private Integer employeeId;
Private String FirstName;
Private String LastName;
Private String Email;
Private String PhoneNumber;
Private Date hiredate;
Private String jobId;
Private BigDecimal salary;
Private BigDecimal commissionpct;
Private Integer ManagerID;
Private short DepartmentID;
}

Interface:

Public interface Employeesmapper {

List<employees> getemployeeslistparams (list<string> employeeids);

List<employees> getemployeesarrayparams (string[] employeeids);

List<employees> Getemployeesmapparams (map<string,object> params);
}

Xml:

<!--The collection property type in List:forech is the value of list,collection must be: List, the value of item can be arbitrary, DAO interface parameter name is arbitrary--<select id= "Getemployeeslistparams" resulttype= "Employees" >Select*From EMPLOYEES e where e.employee_id in<foreach collection= "list" item= "EmployeeId" index= "index"Open= "(" close= ")" separator= "," >#{employeeid}</foreach> </select> <!-The collection property type in-array:forech is the value of array,collection must be: List, the value of item can be arbitrary, DAO interface parameter name is arbitrary--<select id= "Getemployeesarrayparams" resulttype= "Employees" >Select*From EMPLOYEES e where e.employee_id in<foreach collection= "Array" item= "EmployeeId" index= "index"Open= "(" close= ")" separator= "," >#{employeeid}</foreach> </select> <!--Map: Not only the collection attribute in Forech is Map.key, all other properties are map.key, such as the DepartmentID--& below    Gt <select id= "Getemployeesmapparams" resulttype= "Employees" >Select*From EMPLOYEES e<where> <ifTest= "Departmentid!=null and departmentid!=" >e.department_id=#{departmentid}</if> <ifTest= "Employeeidsarray!=null and Employeeidsarray.length!=0" >and e.employee_id in<foreach collection= "Employeeidsarray" item= "EmployeeId"Index= "Index" open= "(" close= ")" separator= "," >#{employeeid}</foreach> </if> </where> </select>
    1. If a single parameter is passed in and the parameter type is a list, the collection property value is list.
    2. If a single parameter is passed in and the parameter type is an array, the value of the collection property is array.
    3. If the parameters passed in are multiple, we need to encapsulate them into a map, of course, the single parameter can also be encapsulated as a 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.

MyBatis Dynamic SQL Example

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.