MyBatis dynamic SQL and fuzzy query

Source: Internet
Author: User
Tags dateformat

SqlXml

<?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= "Personcondition">    <!--implementation of multi-criteria query, name fuzzy matching, age between the maximum minimum value -    <SelectID= "Getperson"ParameterType= "Com.stone.bean.ConditionPerson"Resulttype= "Com.stone.bean.Person">SELECT * from person where<ifTest= ' Name! = '%null% "'>name like #{name} and</if>Age between #{minage} and #{maxage}</Select></Mapper>

Condition Java Bean

 PackageCom.stone.bean; Public classConditionperson {PrivateString name; Private intMinage; Private intMaxAge;  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; }     PublicConditionperson (String name,intMinage,intMaxAge) {        Super();  This. Name =name;  This. Minage =Minage;  This. MaxAge =MaxAge; }     PublicConditionperson () {Super(); } @Override PublicString toString () {return"Conditionperson [name=" + name + ", minage=" +Minage+ ", maxage=" + MaxAge + "]"; }    }

Java Bean

 PackageCom.stone.bean;ImportJava.text.SimpleDateFormat;Importjava.util.Date; Public classPerson {Private intID; PrivateString name; PrivateDate birthday; Private intAge ;  Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicDate Getbirthday () {returnbirthday; }     Public voidsetbirthday (Date birthday) { This. Birthday =birthday; } @Override PublicString toString () {SimpleDateFormat DateFormat=NewSimpleDateFormat ("YyyyMMdd HH:mm:SS"); return"Person [id=" + ID + ", name=" + name + ", birthday=" + dateformat.format (Birthday) + "]"; }}

Test

 PackageCom.stone.dao;Importjava.util.List;Importorg.apache.ibatis.session.SqlSession;ImportCom.stone.bean.ConditionPerson;ImportCom.stone.bean.Person;Importcom.stone.db.DBAccess; Public classDbdaoperson { Public Static voidMain (string[] args) {DBAccess DBAccess=NewDBAccess (); Sqlsession sqlsession=NULL; Try{sqlsession=dbaccess.getsqlsession (); String Statement= "Personcondition.getperson"; Conditionperson parameter=NewConditionperson ("%a%", 11, 18); //Execute SQL statements via sqlsession;list<person> list =sqlsession.selectlist (statement, parameter);            SYSTEM.OUT.PRINTLN (list); System.out.println ("======================="); } Catch(Exception e) {e.printstacktrace (); } finally {            if(Sqlsession! =NULL) {sqlsession.close (); }        }    }}

MyBatis dynamic SQL and fuzzy 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.