8.mybatis Dynamic SQL Fuzzy query (multi-parameter query, using ParameterType)

Source: Internet
Author: User

Multi-parameter query, using ParameterType. Instance:

User User[id, name, age]

1.mysql build tables and insert data

2.Java Solid Class
 Public classUser { PublicUser () {} PublicUser (intID, String name,intAge ) {        Super();  This. ID =ID;  This. Name =name;  This. Age =Age ; }    Private intID; PrivateString name; Private intAge ;  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; } @Override PublicString toString () {return"User [id=" + ID + ", name=" + name + ", age=" + Age + "]"; }    }
3. Create a query parameter entity class
/*** Fuzzy query for user class *@authorAdministrator **/ Public classselectusercondition {PrivateString name;//name    Private intMinage;//Minimum Age    Private intMaxAge;//Maximum Age             Publicselectusercondition () {Super(); }     PublicSelectusercondition (String name,intMinage,intMaxAge) {        Super();  This. Name =name;  This. Minage =Minage;  This. MaxAge =MaxAge; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetminage () {returnMinage; }     Public voidSetminage (intminage) {         This. Minage =Minage; }     Public intGetmaxage () {returnMaxAge; }     Public voidSetmaxage (intMaxAge) {         This. MaxAge =MaxAge; }    }
configuration file for 4.sql queries Selectusermapper.xml
<namespace= "Com.mlxs.mybatis.test7.selectUserMapper">     <!--          Dynamic SQL, fuzzy query parametertype as query condition entity  
  - 
   <SelectID= "Getconditionuser"ParameterType= "Selectusercondition"Resulttype= "User">
SELECT * from Users WHERE <if test= ' name! = '%null% '> name like #{name} and c11></if> Age between #{minage} and #{maxage}
</Select>

</mapper>
5. Test class:
/*** Dynamic SQL Fuzzy Query * * NOTE: when name=null, "%" +name+ "%" =%null% *@authorCharm _ Niche **/ Public class_test7selectconditionuser {@Test Public voidGetconditionuser () {//Create session, set transaction to Truesqlsession session = Mybatisutil.getsessionfactory (). Opensession (true); String Statement= "Com.mlxs.mybatis.test7.selectUserMapper.getConditionUser";  // define Query condition entity selectusercondition condition = new selectusercondition ("%a%", 100); List<User> ulist =session.selectlist (statement, condition); System.out.println ("UserList--->" +ulist);    Session.close (); }}
6. Results:

UserList--->[user [id=3, Name=update2, age=100], User [id=5, NAME=ADD1, age=23]]

8.mybatis Dynamic SQL Fuzzy query (multi-parameter query, using ParameterType)

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.