Normal mapping [html] View plain copy @Select ("select * from Mybatis_student where Id=#{id}") Public Student getstudent ( int id); @Insert ("Insert into Mybatis_student (name, age, remark, pic,grade_id,address_id) VALUES (#{name},#{age},#{ Remark}, #{pic},#{grade.id},#{address.id}) ") public int insert (Student Student); @Update (" Update mybatis_student set Name=#{name},age=#{age} where Id=#{id} ") public int update (Student Student), @Delete (" Delete from Mybatis_student where Id=#{id} ") public int delete (int id); 2. Result set mapping [html] View plain copy @Select (" SELECT * from MyBatis _student ") @Results ({@Result (id=true,property=" id ", column=" id "), @Result (property=" name ", column=" name "), @Result ( Property= ' age ', column= ' age ')}) public list<student> getallstudents (); 3. Relationship map 1), one to one [ HTML] View plain copy @Select ("SELECT * from Mybatis_student") @Results ({@Result (id=true,property= "id", column= " ID "), @Result (property=" name ", column=" name "), @Result (property=" age ", column=" age"), @Result (property=" Address ", column=" address_id ", [email protected] (select=" Com.skymr.mybatis.mappers.AddressMapper.getAddress ")}) public list<student> getallstudents (); 2) One-to-many [html] View plain copy package com.skymr.mybatis.mappers; import Org.apache.ibatis.annotations.Many ; Import Org.apache.ibatis.annotations.result;import Org.apache.ibatis.annotations.results;import Org.apache.ibatis.annotations.select; import Com.skymr.mybatis.model.grade; public Interface Grade2mapper { @Select ("select * from Mybatis_grade where Id=#{id}") @Results ({@Result (id=true,column= "id", property= "id"), @Result (column= "Grade_name", property= "Gradename"), @Result (property= "Students", column= "id", [ Email protected] (select= "Com.skymr.mybatis.mappers.Student2Mapper.getStudentsByGradeId"))}) public Grade Getgrade (int id);} [html] View plain copy package com.skymr.mybatis.mappers; import Java.util.list; import Org.apache.ibatis.annotations.DeletE;import Org.apache.ibatis.annotations.insert;import Org.apache.ibatis.annotations.one;import Org.apache.ibatis.annotations.result;import Org.apache.ibatis.annotations.results;import Org.apache.ibatis.annotations.select;import Org.apache.ibatis.annotations.update; import Com.skymr.mybatis.model.student; public interface Student2mapper { @Select ("SELECT * from Mybatis_student Where Id=#{id} ") Public Student getstudent (int id); @Insert (" Insert to Mybatis_student (name, age, remark, pic,grade_id, ADDRESS_ID) VALUES (#{name},#{age},#{remark}, #{pic},#{grade.id},#{address.id}) "public int insert (Student Student); @Update ("Update mybatis_student set Name=#{name},age=#{age} where Id=#{id}") public int Update (Student Student); @Delete ("Delete from mybatis_student where Id=#{id}") the public int delete (int id); @Select ("SELECT * from Mybatis_student") @ Results ({@Result (id=true,property= "id", column= "id"), @Result (property= "name", column= "name"), @Result (property= " Age ", column=" Age "), @ResulT (property= "address", column= "address_id", [email protected] (select= " Com.skymr.mybatis.mappers.AddressMapper.getAddress ")}) public list<student> getallstudents (); @Select (" SELECT * from Mybatis_student where Grade_id=#{gradeid} ") @Results ({@Result (id=true,property=" id ", column=" id "), @ Result (property= "name", column= "name"), @Result (property= "age", column= "Age"), @Result (property= "Address", column= " Address_id ", [email protected] (select=" com.skymr.mybatis.mappers.AddressMapper.getAddress ")}) public List <Student> Getstudentsbygradeid (int gradeid);} 4. Dynamic SQL Annotation mapping provider class [html] View plain copy package Com.skymr.mybatis.mappers.provider; import Java.util.map; import org.apache.ibatis.jdbc.sql; Import Com.skymr.mybatis.model.student; public class Studentdynasqlprovider { public String insertStudent ( Final Student Student) {return new SQL () {{Insert_into ("mybatis_student"), if (Student.getname ()! = null) {VALUES ("name", "#{name}");} IF (student.getage () > 0) {VALUES ("age", "#{age}");}}. ToString ();} public String updatestudent (final Student Student) {return new SQL () {{UPDATE ("mybatis_student"); if ( Student.getname ()! = null) {SET ("name=#{name}");} if (student.getage () > 0) {SET ("age=#{age}");} WHERE ("Id=#{id}");}}. ToString ();} public String getstudent (final map<string,object> Map) {return new SQL () {{Select ("*"); From ("Mybatis_student"), if (Map.containskey ("name")) {WHERE ("name like #{name}");} if (Map.containskey ("Age")) {WHERE ("age=#{age}");}}}. ToString ();} public String deletestudent () {return new SQL () {{Delete_from ("mybatis_student"); WHERE ("Id=#{id}");}}. ToString ();}} mapper interface [HTML] view plain copy @SelectProvider (type=studentdynasqlprovider.class,method= "Getstudent") Public list<student> getstudents (map<string,object> Map);
MyBatis (10) Annotation mappings