What if the field name is different from the property name (the name of the database)? Scenario One: Configure a resultmapper in a small configuration
<!-- -
<Resultmaptype= "Student"ID= "Studentmapper"> <resultcolumn= "Stuname2" Property= "Stuname"/> </Resultmap> <!--Query All - <SelectID= "FindAll"Resultmap= "Studentmapper">SELECT * FROM Student</Select>
Scenario Two: Query statements in small configurations with AS
<!---<id= "FindAll" Resulttype = "Student"> Select Stuname2 as Stuname from Student </ Select>
Second, mapper dynamic agent culling implementation class The first step of the change is the small configuration of <mapper namespace= "Cn.happy.dao.IStudentDAO" > Write to the interface
<?xml version="1.0"encoding="UTF-8"? ><!DOCTYPE mapperpublic"-//mybatis.org//dtd Mapper 3.0//en""HTTP://MYBATIS.ORG/DTD/MYBATIS-3-MAPPER.DTD"><mappernamespace="Cn.happy.dao.IStudentDAO"> <!--Scenario Two: As aliases---<SelectId="FindAll"Resulttype="Student">SelectStuname2 asStuname fromStudent</Select></mapper>
The second step is to use Getmapper to get an interface implementation class when the test class invokes the interface.
Public classMyTest {Istudentdao dao; @Before Public voidInitData ()throwsioexception{sqlsession Session=mybatisutil.getsession (); //Dynamic kick Out implementation class//first change studentdao.xml to <mapper namespace= "Cn.happy.dao.IStudentDAO" >Dao=session.getmapper (Istudentdao.class); } /*** SelectAll Students *@throwsIOException*/@Test Public voidFindAll ()throwsioexception{List<Student> list =Dao.findall (); for(Student student:list) {System.out.println (Student.getstuname ()); /*System.out.println (Student.getstuage ());*/ } } }Third, using the map set value and use index number (a) using the map collection to get the value 1 customizing a method on the interface Istudentdao
// Multi-conditional query encapsulation into map Public List<student> Findstudentmore (map<string, object> Map);
2 id= "Findstudentmore" in a small configuration to be the same as the method of the interface Dharma name
<!--multi-condition query--><SelectId="Findstudentmore"Resulttype="Student"> <!--mysql database--<!--Select* fromStudentwhereStuname like'%'#{stuname}'%'and Stuage>#{stuage}-<!--ORCL Database--Select* fromStudentwhereStuname like'%'|| #{stuname}| |'%'and stuage>#{stuage}</Select>3 in the test class
/*** Multi-criteria Query *@throwsIOException*/@Test Public voidFindstudentmore () {Map<string, object> maplist=NewHashmap<string, object>(); Maplist.put ("Stuname", "123"); Maplist.put ("Stuage", 11); List<Student> list =Dao.findstudentmore (maplist); for(Student student:list) {System.out.println (Student.getstuname ()); } }(ii) using index 1 to customize a method on the interface Istudentdao
// multi-Conditional query quotes Public List<student> findstudentbycondition (String name,int age);
2 in a small configurationid= "Findstudentmore" to be the same way as the interface Dharma name
<SelectId="findstudentbycondition"Resulttype="Student"> <!--MySQL DatabaseSelect* fromStudentwhereStuname like'%'#{0}'%'and stuage>#{1} ORCL Database-Select* fromStudentwhereStuname like'%'|| #{0}||'%'and stuage>#{1} </Select>3 in the test class
@Test Public void throws ioexception{ String name= "Person"; int age=12; List<Student> list = dao.findstudentbycondition (name,age); for (Student student:list) { System.out.println (Student.getstuname ()); } }
Small summary:
Iv. Smart Tags What they used together is the following 1 define a method in the connector
Public List<student> findstudentbyif (Student stu);
2 Test class
Public classMyTest {Istudentdao dao; @Before Public voidInitData ()throwsioexception{sqlsession Session=mybatisutil.getsession (); //Dynamic kick Out implementation class//first change studentdao.xml to <mapper namespace= "Cn.happy.dao.IStudentDAO" >Dao=session.getmapper (Istudentdao.class); } /*** Multi-criteria Query *@throwsIOException*/@Test Public voidFindstudentbycondition ()throwsioexception{String name= "Person"; intAge=12; Student Stu=NewStudent (); Stu.setstuage (age); Stu.setstuname (name); List<Student> list =Dao.findstudentbyif (STU); for(Student student:list) {System.out.println (Student.getstuname ()); } } 3 Configuring the If label in a small configuration
<!--multi-condition query-<SelectId="findstudentif"Resulttype="Student">Select* fromStudentwhere 1=1<iftest="Stuname!=null">and Stuname like'%'|| #{stuname}| |'%'</if> <iftest="Stuage!=null">and Stuage>#{stuage}</if> </Select>where labelNote If you have a <where> tag, you do not need where 1=1
<!--multi-condition query-<SelectId="Findstudentbychoose"Resulttype="Student">Select* fromStudent <!--where 1=1 If you have a where label you don't need-<where> <iftest="Stuname!=null">and Stuname like'%'|| #{stuname}| |'%'</if> <iftest="Stuage!=null">and Stuage>#{stuage}</if> </where> </Select>Choose Label
<!--multi-criteria query where1=1If you have a where label you don't need-<SelectId="Findstudentbyif"Resulttype="Student">Select* fromStudent<where> <choose> <when test="Stuname!=null">and Stuname like'%'|| #{stuname}| |'%'</when> <otherwise> </otherwise> </choose> </where> </Select>
MyBatis a series of problems (a workaround that traverses the map collection and smart tags and attributes and fields)