MyBatis Example Condition query

Source: Internet
Author: User


Criterion is the most basic, lowest-level where condition, used for filtering at the field levels


    • Criteria

The criteria contains a collection of Cretiron, and the relationships between the cretiron contained within each criteria object are connected by and.

Oredcriteria

A member of the example, called Oredcriteria, is a set of criteria that, as the name suggests, the criteria in this set are connected by or, logical or relational. Oredcriteria is ORed Criteria.

Other

The distinct field of the example class is used to specify the distinct query.

The Orderbyclause field is used to specify an order by condition, which is not constructed directly by passing a string value.


Code

import java.io.ioexception;import java.io.reader;import java.util.arraylist;import  java.util.list;import org.apache.ibatis.io.resources;import org.apache.ibatis.session.sqlsession; import org.apache.ibatis.session.sqlsessionfactory;import  org.apache.ibatis.session.sqlsessionfactorybuilder;import  org.apache.log4j.pattern.classnamepatternconverter;import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext;import  Cn.itcast.ssm.mapper.itemsmapper;import cn.itcast.ssm.po.itemsexample;public class student  {    public static void main (String[] args)  throws  ioexception {        /* Way One   */         itemsexample itemsexample1 = new itemsexample ();        &nbSp;itemsexample1.or (). Andidequalto (5). Andnameisnotnull ();         Itemsexample1.or (). Andpicequalto ("xxx"). Andpicisnull ();         list <Integer> fieldValues = new ArrayList<Integer> ();         fieldvalues.add (8);         fieldvalues.add (11) ;         fieldvalues.add (+);         fieldvalues.add (+);         itemsexample1.or (). AndIdIn ( fieldvalues);         itemsexample1.or (). AndIdBetween (5, 9);         /*   two  criteria1 and criteria2 are or relations  */         itemsexample itemsexample2 = new itemsexample ();       &Nbsp; itemsexample.criteria criteria1 = itemsexample2.createcriteria ();         criteria1.andidisnull ();         Criteria1.andpriceequalto ((float)  3);         Itemsexample.criteria criteria2 = itemsexample2.createcriteria ();         criteria2.andnameisnull ();         Criteria2.andidgreaterthanorequalto (5);         itemsexample2.or ( CRITERIA2);         //mode one and mode two are equivalent                          //  spring Get Mapper Proxy object         ApplicationContext  Applicationcontext = new classpathxmlapplicationcontext ("ClasspaTh:applicationContext.xml ");        itemsmapper itemsmapper  =  (Itemsmapper)  applicationcontext.getbean ("Itemsmapper");         itemsmapper.countbyexample (ItemsExample2);        //  Get sqlsessionfactory        string resource =  " Sqlmapconfig.xml ";        reader reader =  Resources.getresourceasreader (Resource);         sqlsessionfactory  sqlmapper = new sqlsessionfactorybuilder (). Build (reader);         //  Get sqlsession        sqlsession  Sqlsession = sqlmapper.opensession ();     }}


the member variables of the JavaBeans class are generally called Properties. Access to each property is generally defined as private or protected, rather than as public . Note: The property name must start with a lowercase letter.

For each attribute, two public methods are generally defined , which are called access method (getter) and modification method (setter), respectively. That allows the container to access and modify the Bean 's properties.

Public String GetColor ();

public void SetColor (String);

An exception is when an accessor method should be defined as a isxxx () form when the property is a boolean type .


Object type

Although you can explicitly refer to the property name of an object, if you want to test the incoming user parameter in the IF element, you still use _parameter to refer to the actual argument passed in, because the name of the user object passed in is not testable. If you test the properties of an object, you can simply refer to the property name.

To test the user object:

<if test= "_parameter! = null" >

To test the properties of the user object:

<if test= "Name! = NULL" >


Map type

Incoming map type, directly through #{keyname} can refer to the value corresponding to the key. Multiple parameter values that use the @param annotation are also assembled into a map data structure, and there is no difference in passing the map directly.

Mapper Interface:

int Updatebyexample (@Param ("user") user user, @Param ("example") userexample example);

SQL mapping:

<update id= "updatebyexample" parametertype= "map" > update tb_user Set id = #{user.id,jdbctype=integer},..    . <if test= "_parameter! = null" > <include refid= "update_by_example_where_clause"/> </if>

Note here that the test passed in the map is empty, still using _parameter











Reference article:

http://ljhzzyx.blog.163.com/blog/static/38380312201412043525595/

Http://openwares.net/database/mybatis_generator_example.html

Http://openwares.net/database/mybatis_parametertype.html



This article is from the "bit accumulation" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1741268

MyBatis Example Condition query

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.