First, use annotations to configure the mapper dynamic SQL:
Use is not a lot, understand under;
Student.java Entity Bean:
PackageCom.cy.model; Public classstudent{PrivateInteger ID; PrivateString name; PrivateInteger age; PublicStudent () {} PublicStudent (String name, Integer age) { This. Name =name; This. Age =Age ; } PublicInteger getId () {returnID; } Public voidsetId (Integer id) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicInteger getage () {returnAge ; } Public voidsetage (Integer age) { This. Age =Age ; } @Override PublicString toString () {return"Student [id=" + ID + ", name=" + name + ", age=" + Age + "]"; } }
View Code
Com.cy.mapper under:
Studentdynasqlprovider.java:
Studentmapper.java:
PackageCom.cy.mapper;ImportJava.util.Map;ImportOrg.apache.ibatis.jdbc.SQL;Importcom.cy.model.Student; Public classStudentdynasqlprovider { PublicString Inertstudent (FinalStudent Student) { return NewSQL () {{Insert_into ("T_student"); if(Student.getname ()! =NULL) {VALUES ("Name", "#{name}"); } if(Student.getage ()! =NULL) {VALUES ("Age", "#{age}"); }}}.tostring (); } PublicString Updatestudent (FinalStudent Student) { return NewSQL () {{UPDATE] ("T_student"); if(Student.getname ()! =NULL) {SET ("Name=#{name}"); } if(Student.getage ()! =NULL) {SET ("Age=#{age}"); } WHERE ("Id=#{id}"); }}.tostring (); } PublicString deletestudent () {return NewSQL () {{Delete_from ("T_student"); WHERE ("Id=#{id}"); }}.tostring (); } PublicString Getstudentbyid () {return NewSQL () {{SELECT} ("*"); From ("T_student"); WHERE ("Id=#{id}"); }}.tostring (); } PublicString findstudents (FinalMap<string, object>map) { return NewSQL () {{SELECT} ("*"); From ("T_student"); StringBuffer SB=NewStringBuffer (); if(Map.get ("name")! =NULL) {sb.append ("and name Like '" +map.get ("name") + "'"); } if(Map.get ("age")! =NULL) {sb.append (' and age = ' +map.get ("age")); } if(!sb.tostring (). Equals ("") {WHERE (sb.tostring (). Replacefirst ("and", "" ")); }}}.tostring (); }}
Studentmapper.java:
Public InterfaceStudentmapper {//Insert@InsertProvider (Type=studentdynasqlprovider.class, method= "Inertstudent") Public intinertstudent (Student Student); //Update@UpdateProvider (Type=studentdynasqlprovider.class, method= "Updatestudent") Public intupdatestudent (Student stu); //Delete@DeleteProvider (Type=studentdynasqlprovider.class, method= "Deletestudent") Public intDeletestudent (intID); //find students by ID@SelectProvider (Type=studentdynasqlprovider.class, method= "Getstudentbyid") PublicStudent Getstudentbyid (Integer ID); //Find all Students@SelectProvider (Type=studentdynasqlprovider.class, method= "Findstudents") PublicList<student> findstudents (map<string,object>map); }
Test code:
Studenttest.java:
1 Public classStudenttest {2 Private StaticLogger Logger = Logger.getlogger (studenttest.class);3 4 PrivateSqlsession sqlsession=NULL;5 PrivateStudentmapper studentmapper=NULL;6 7 @Before8 Public voidSetUp ()throwsException {9sqlsession=sqlsessionfactoryutil.opensession ();TenStudentmapper=sqlsession.getmapper (Studentmapper.class); One } A - @After - Public voidTearDown ()throwsException { the sqlsession.close (); - } - - @Test + Public voidtestinsertstudent () { -Logger.info ("Test Insertstudent"); +Student stu =NewStudent ("Kiki", 12); A intCount =studentmapper.inertstudent (stu); at sqlsession.commit (); - } - - @Test - Public voidtestupdatestudent () { -Logger.info ("Test Updatestudent"); inStudent stu =NewStudent ("Kiki qi Qi", 13); -Stu.setid (16); to intCount =studentmapper.updatestudent (stu); + sqlsession.commit (); - } the * @Test $ Public voidtestdeletestudent () {Panax NotoginsengLogger.info ("Test Delete student"); - intCount = Studentmapper.deletestudent (16); the sqlsession.commit (); + } A the @Test + Public voidTestgetstudentbyid () { -Logger.info ("Find Students by ID"); $Student Student = Studentmapper.getstudentbyid (1); $ System.out.println (student); - } - the @Test - Public voidtestfindstudents () {WuyiLogger.info ("Find students on condition"); themap<string,object> map =NewHashmap<string,object>(); - //map.put ("name", "%a%"); WuMap.put ("Age", 14); -list<student> students =studentmapper.findstudents (map); About for(Student student:students) { $ System.out.println (student); - } - } -}
View Code
Xiao Feng MyBatis (5) MyBatis Configuring SQL Mapper with annotations-dynamic SQL