Next, this time, only the nested results, the same as the last three sheets
The last time I forgot to introduce resultmap, fill in:
Resultmap Conceptual View
- When the constructor– class is instantiated, it is used to inject the result into the constructor method
- Idarg–id parameters; Marking results as IDs can help improve overall performance
- A common result of arg– injection into the construction method
- ID – an ID result; Marking results as IDs can help improve overall performance
- result– normal results injected into a field or JavaBean property
- association– a complex type association; many results will be wrapped into this type
- collection– sets of complex types
- discriminator– use result values to determine which result mapping to use
- case– result mappings based on certain values
The request for this query is:
--通过教师 id 找班级和教师的信息:select * from teacher t join classes c on t.teacherId =5001 and t.teacherId =c.teacherId join student s on s.classId=c.clsId
1. Let's take a look at the configuration of three Mapper.xml
<!--studentmapper.xml --<mapper namespace="Com.yc.mybatis.mapper.StudentMapper"> <resultmap type="Student" id="Studentmap"> <ID Column= "stuid" property ="Stuid"/> <result column= "stuname" property ="Stuname"/> <result column= "stusex" property ="Stusex"/> <result column= "stubirthday" property ="Stubirthday" /> <result column= "classId" property ="ClassId"/> </resultmap></mapper>
<!--classesmapper.xml --<mapper namespace="Com.yc.mybatis.mapper.ClassesMapper"> <resultmap type="Classes" id="Classesmap"> <ID Column= "clsid" property ="clsid"/> <result column= "clsname" property ="Clsname"/> <collection property ="Stus" ofType="Student" Resultmap="Com.yc.mybatis.mapper.StudentMapper.StudentMap"></Collection> </resultmap></mapper>
<!---teachermapper.xml --<mapper namespace="Com.yc.mybatis.mapper.TeacherMapper"> <resultmap type="Teacher" id="Teachermap"> <ID Column= "teacherid" property ="Teacherid"/> <result column= "teachername" property ="TeacherName"/> <result column= "workyear" property ="Workyear"/> <result column= "Professional" property ="Professional" /> <!--call Resultmap in Classesmapperxml need to have namespace + ID-- <Association Property ="Classes" resultmap=" Com.yc.mybatis.mapper.ClassesMapper.ClassesMap "></Association> </resultmap> <select id="Getteacherbyid" parametertype="int" Resultmap="Teachermap">SELECT * FROM teacher T joins classes C on T.teacherid =#{teacherid} and T.teacherid =c.teacherid join student s on S.classid=c.clsid</Select></mapper>
2. Mybatis.xml
<configuration> <!--profile and database connection Properties file association You can then invoke the contents of the properties file through the OGNL expression- <properties Resource="Jdbc.properties"></Properties> <!--define type aliases-- <typealiases> <!--can only specify an alias for one type-- <!--<typealias type= "Com.yc.mybitis.entity.User" alias= "User"/>--> <!--defines aliases for all classes of the specified package, with the same alias as the class name without the package- - < package name="com/yc/mybitis/entity"/> </typealiases> <!--environment can be configured with more than one, the default property indicates that the environment that is currently being used by defaults needs to be aware that the environment specified in the <environments Default="Development"> <environment ID="Development"> <!--TransactionManager: Transaction management, TYPE:JDBC own management, or Type:manager to third party management such as spring-- <transactionmanager type="JDBC"/> <!--data source, pooled use database connection pool; Unpooled not used; JNDI uses a connection pool of other containers-- <dataSource type="Pooled"> <!--properties has been configured with the resource file above, associated with the database connection, and directly by the expression-- < property name="Driver" value="${jdbc.driver}"/> < property name="url" value="${jdbc.url}"/> < property name="username" value="${jdbc.user}"/ > < property name="password" value="${jdbc.password}"/> </dataSource> </Environment> </environments><mappers> <mapper Resource="Com/yc/mybitis/entity/classesmapper.xml"/> <mapper Resource="Com/yc/mybitis/entity/studentmapper.xml"/> <mapper Resource="Com/yc/mybitis/entity/teachermapper.xml"/> </mappers> </configuration>
3. Interface
4. Test class
PackageTestImportOrg.junit.After;ImportOrg.junit.Before;ImportOrg.junit.Test;ImportCom.yc.mybatis.mapper.TeacherMapper;ImportCom.yc.mybitis.entity.Teacher;ImportCom.yc.mybitis.util.MybatisUtil; Public class teachermappertest { PrivateTeachermapper Tmapper;@Before Public void setUp()throwsException {tmapper=mybatisutil.getsession (). Getmapper (Teachermapper.class); }@After Public void TearDown()throwsException {tmapper=NULL; }@Test Public void Testgetteacherbyid() {Teacher T=tmapper.getteacherbyid (5001); System.out.println (t); }}
5. Test results
Well, the nesting results are over.
MyBatis explain how to configure and use, and do not understand the underlying implementation, interested people can find information.
Copyright NOTICE: This article for Bo Master original article, thank you for reference! Where there is a problem, you are welcome to correct and progress together.
MyBatis Advanced Association and set Mapping (Ii. nested results synthesis case)