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