As in hibernate, the relationship between the two objects in MyBatis also describes the association relationship between two objects! Well, not much, just go straight to the point.
In order to test the data simple Use table field less!
student:id,name,supervisor_id
Teacher:id,name
Project structure
The package is introduced in the project
Com,iss.model.student
public class Student {private int id;private String name;private Teacher supervisor;public Teacher getsupervisor () {return Supervisor;} public void Setsupervisor (Teacher supervisor) {this.supervisor = supervisor;} public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}
Com.iss.model.Teacher
Package Com.iss.model;import Java.util.arraylist;import Java.util.list;public class Teacher {private int id;private String name;//Private set<student> students = new hashset<student> ();p rivate list<student> students = New Arraylist<student> ();p ublic int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public list<student> getstudents () {return students;} public void Setstudents (list<student> students) {this.students = students;} Public set<student> getstudents () {//return students;//}////public void setstudents (set<student> studen TS) {//this.students = students;//}}
Com.iss.dao.StudentMapper
Package Com.iss.dao;import Com.iss.model.student;public Interface Studentmapper {public Student getById (int id);p ublic void Addstudent (Student Student);}
Com.iss.dao.TeacherMapper
Package Com.iss.dao;import Com.iss.model.teacher;public interface Teachermapper {public void Addteacher (Teacher Teacher);p ublic teacher getById (int id);}
Com,iss,dao,studentmapper.xml
<?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" >< Mapper namespace= "Com.iss.dao.StudentMapper" ><!--query data--><select id= "getById" parametertype= "int" resultmap= "Studentresultmap" >select s.name,t.name from student s,teacher T Wheres.supervisor_id=t.idand s.id=#{id} <!--select name from student where Id=#{id}--></select><!--Student entity mapping--><resultmap type= "student" id = "Studentresultmap" ><result property= "id" column= "id"/><result property= "name" column= "name"/><! --<association property= "supervisor" javatype= "Teacher" > <result property= "id" column= "id"/> <result property= "name" column= "name"/> </association>--><association property= "supervisor" resultmap= " Com.iss.dao.TeacherMapper.supervisorResultMap "></association></resultMap><!--inserting data-->< Insert Id= "Addstudent" UsegeneratedkeYs= "true" parametertype= "User" keyproperty= "id" >insert into studentvalues (#{id},#{name},#{supervisor.id}) </ Insert></mapper>
Com.iss.dao,teachermapper.xml
<?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" >< Mapper namespace= "Com.iss.dao.TeacherMapper" ><!--increase teacher--><insert id= "Addteacher" usegeneratedkeys= " True "parametertype=" Teacher "keyproperty=" id ">insert into Teacher values (#{id},#{name});</insert><!-- Query teacher--><select id= "getById" parametertype= "int" resultmap= "Supervisorresultmap" >select t.idt_id,t.name t_ Name,s.name from student S,teacher T wheres.supervisor_id=t.id andt.id=#{id}</select><!--teacher entity mapping-->< Resultmap type= "Teacher" id= "Supervisorresultmap" ><result property= "id" column= "t_id"/><result property = "Name" column= "T_name"/><collection property= "students" resultmap= " Com.iss.dao.StudentMapper.studentResultMap "></collection></resultMap></mapper>
The advantage of putting the mapping interface and the mapped configuration file in the same directory is that you do not need to specify the mapping profile in the core profile MyBatis using Mappers.
Here's a look at spring and MyBatis consolidated configuration files
Mybatis-config,xml
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE configuration Public "-//mybatis.org//dtd Config 3.0//en" "http://mybatis.org/dtd/ Mybatis-3-config.dtd "><configuration><!--give the data source to spring management--><!--<mappers><mapper Resource= "Com/iss/dao/usermapper.xml"/></mappers>--></configuration>
Applicationcontext.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:tx = "Http://www.springframework.org/schema/tx" xmlns:p= "http://www.springframework.org/schema/p" xmlns:aop= "http// Www.springframework.org/schema/aop "xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX http://www.springframework.org/schema/tx/spring-tx-3.0.xsd/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "><!-- Configure Data source--><bean id= "Jdbcdatasource" class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name= "Driverclassname" ><value>org.gjt.mm.mysql.Driver</value></property>< Property name= "url" ><value>jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding= Utf-8</valuE></property><property name= "username" ><value>root</value></property>< Property name= "Password" ><value>root</value></property></bean><!-- Configure Sqlsessionfactory--><bean id= "sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" > <property name= "DataSource" ref= "Jdbcdatasource"/><property name= "Typealiasespackage" value= " Com.iss.model "></property><!--<property name=" configlocation "value=" Classpath:mybatis-config.xml "></property>--></bean><!--<bean id=" Usermapper "class=" Org.mybatis.spring.mapper.MapperFactoryBean "> <property name=" mapperinterface "value=" Com.iss.dao.UserMapper "></property> <property name=" sqlsessionfactory "ref=" Sqlsessionfactory "> </property> </bean>--><!--mappperscannerconfigure: Scan a specific package to automatically create a batch mapper. --><bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" ><property name= "basepAckage "value=" Com.iss.dao "></property></bean><bean id=" TransactionManager "class=" Org.springframework.jdbc.datasource.DataSourceTransactionManager "><property name=" datasource "ref=" Jdbcdatasource "/></bean><!--Configure the transaction handling policy, Transaction-manager property specifies the transaction manager. If the ID of the transaction manager Bean is TransactionManager, the Transaction-manager property can not be specified--><tx:advice id= "Txadvice" Transaction-manager= "TransactionManager" ><tx:attributes><!--all methods that begin with find are read-only--><tx:method Name= "get*" read-only= "true"/><tx:method name= "add*"/><!--Other methods use the default transaction policy--><tx:method name= "*"/ ></tx:attributes></tx:advice><!--AOP configuration--><aop:config><!--pointcut element defines a pointcut, The first asterisk in execution matches the return type of the method, where the asterisk indicates that all return types are matched. Com.abc.dao.*.* (..) Indicates all methods that match all classes under the Com.abc.dao package--><aop:pointcut id= "mypointcut" expression= "Execution (* com.iss.dao.*.* (..))"/ ><!--apply a defined transaction processing policy to the above pointcut--><aop:advisor advice-ref= "Txadvice" pointcut-ref= "Mypointcut"/></aop:config></beans>
Finally see Com.iss.test.Test test the data in the table
Package Com.iss.test;import Java.util.list;import Java.util.set;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.iss.dao.studentmapper;import Com.iss.dao.teachermapper;import Com.iss.dao.usermapper;import Com.iss.model.student;import Com.iss.model.Teacher Import Com.iss.model.user;public class Test {public static void main (string[] args) {ApplicationContext CTX = Null;ctx = New Classpathxmlapplicationcontext ("Applicationcontext.xml"); Studentmapper Studentmapper = (studentmapper) ctx.getbean ("Studentmapper"); Teachermapper Teachermapper = (teachermapper) ctx.getbean ("Teachermapper"); Teacher Teacher = Teachermapper.getbyid (1); list<student> students = teacher.getstudents (); System.out.println (Students.size ()); for (Student stu:students) {System.out.println (Stu.getsupervisor (). GetName ()) ;}}}
All right! This simple two-way pair is done!
Spring-mybatis integration in a one-to-many