一如Hibernate中的两个对象之间的关系在MyBatis中也关于两个对象之间的关联关系的描述!好了不多说,直接进入正题。
为了测试数据简单使用表的字段较少!
student:id,name,supervisor_id
teacher:id,name
项目结构截图
项目中引入包的截图
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>();
private List<Student> students = new ArrayList<Student>();
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;
}
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> students) {
// this.students = students;
// }
}
com.iss.dao.StudentMapper
package com.iss.dao;
import com.iss.model.Student;
public interface StudentMapper {
public Student getById(int id);
public 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);
public 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">
<!--查询数据 -->
<select id="getById" parameterType="int" resultMap="studentResultMap">
select s.name,t.name from student s,teacher t where
s.supervisor_id=t.id
and s.id=#{id}
<!-- select name from student where id=#{id} -->
</select>
<!--学生实体映射 -->
<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>
<!--插入数据 -->
<insert id="addStudent" useGeneratedKeys="true" parameterType="User"
keyProperty="id">
insert into student
values(#{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">
<!--增加老师 -->
<insert id="addTeacher" useGeneratedKeys="true" parameterType="Teacher"
keyProperty="id">
insert into teacher values(#{id},#{name});
</insert>
<!--查询老师 -->
<select id="getById" parameterType="int" resultMap="supervisorResultMap">
select t.id
t_id,
t.name t_name,s.name from student s,teacher t where
s.supervisor_id=t.id and
t.id=#{id}
</select>
<!-- 教师实体映射 -->
<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>
把映射接口和映射的配置文件放在同一目录的好处是不需要在核心配置文件Mybatis中使用mappers指定映射配置文件了。
下面看一下Spring和Mybatis整合的配置文件
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>
<!-- 把数据源交给Spring管理 -->
<!-- <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">
<!-- 配置数据源 -->
<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>
<!--配置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:扫描特定的包自动创建成批地映射器。 -->
<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>
<!--配置事务处理策略,transaction-manager属性指定事务管理器。 若事务管理器bean的id即为transactionManager,则
transaction-manager的属性可以不指定 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--所有以find开头的方法都是只读的 -->
<tx:method name="get*" read-only="true" />
<tx:method name="add*" />
<!--其他方法使用默认事务策略 -->
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- AOP配置 -->
<aop:config>
<!--pointcut元素定义一个切入点,execution中的第一个星号 用以匹配方法的返回类型,这里星号表明匹配所有返回类型。 com.abc.dao.*.*(..)表明匹配com.abc.dao包下的所有类的所有
方法 -->
<aop:pointcut id="myPointcut" expression="execution(* com.iss.dao.*.*(..))" />
<!--将定义好的事务处理策略应用到上述的切入点 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" />
</aop:config>
</beans>
最后看com.iss.test.Test对表中数据进行测试
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());
}
}
}
好了!这个简单的双向一对多就完成了!