数据库中一对多通常使用主外键关联,外键应该在多方,即多方维护关系。
下面举一个简单实例来看看MyBatis怎么处理一对多的关系。
1.创建一个项目,导入所需jar包,导入db.properties配置文件,导入log4j.properties配置文件
2.创建一个数据库,在里面创建两张表
-- Table structure for `t_clazz` -- ---------------------------- DROP TABLE IF EXISTS `t_clazz`; CREATE TABLE `t_clazz` (`id` int(11) NOT NULL,`code` varchar(18) NOT NULL,PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- ---------------------------- -- Records of t_clazz -- ---------------------------- INSERT INTO `t_clazz` VALUES ('1', '一班'); INSERT INTO `t_clazz` VALUES ('2', '二班');
-- Table structure for `t_student` -- ---------------------------- DROP TABLE IF EXISTS `t_student`; CREATE TABLE `t_student` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(18) NOT NULL,`sex` varchar(3) NOT NULL,`age` int(11) NOT NULL,`cid` int(11) NOT NULL,PRIMARY KEY (`id`),KEY `cid` (`cid`),CONSTRAINT `cid` FOREIGN KEY (`cid`) REFERENCES `t_clazz` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;-- ---------------------------- -- Records of t_student -- ---------------------------- INSERT INTO `t_student` VALUES ('1', '张三', '男', '11', '1'); INSERT INTO `t_student` VALUES ('2', '李四', '男', '12', '2'); INSERT INTO `t_student` VALUES ('3', '小红', '女', '13', '1');
3.编写对应的实体类
public class Student {private Integer id;private String name;private String sex;private Integer age;//关联的clazz对象private Clazz clazz;
public class Clazz {private Integer id;private String code;//关联的student集合private List<Student> students;
4.编写对应的SQL映射文件
ClazzMapper.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.dj.mapper.ClazzMapper"><select id="selectClazzById" parameterType="int" resultMap="clazzResultMap">select * from t_clazz where id =#{id}</select><resultMap type="com.dj.domain.Clazz" id="clazzResultMap"><id property="id" column="id"/><resultproperty="code" column="code"/><!--property表示返回类型Clazz的属性studentscolumn表示将id作为参数进行之后的查询fetchtype表示懒加载javaType表示属性对应的类型 ofType表示集合当中的类型--><collection property="students" column="id" fetchType="lazy" javaType="ArrayList" ofType="com.dj.domain.Student" select="com.dj.mapper.StudentMapper.selectStudentByClazzId"><id property="id" column="id"/><resultproperty="name" column="name"/><resultproperty="sex" column="sex"/><resultproperty="age" column="age"/></collection></resultMap></mapper>
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"><!-- namespace指用户自定义的命名空间 --> <mapper namespace="com.dj.mapper.StudentMapper"><select id="selectStudentByClazzId" parameterType="int" resultType="com.dj.domain.Student">select * from t_student where cid=#{id}</select> </mapper>
5.编写mybatis-config.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!-- 引入 外部db.properties文件--><properties resource="db.properties"/><!-- 指定 MyBatis 所用日志的具体实现--><settings><setting name="logImpl" value="log4j"/></settings><!-- 环境配置 --><environments default="mysql"><environment id="mysql"><!-- 指定事务类型 --><transactionManager type="JDBC"/><!-- dataSource指数据源配置,POOLED是JDBC连接对象的数据源连接池的实现。 --><dataSource type="POOLED"><property name="driver" value="${driver}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/></dataSource></environment></environments><!-- SQL映射文件位置 --><mappers><mapper resource="com/dj/mapper/StudentMapper.xml"/><mapper resource="com/dj/mapper/ClazzMapper.xml"/></mappers></configuration>
6.mybatis建议通过mapper接口的代理对象访问mybatis,该对象关联了一个sqlsession对象,开发者可以通过该对象直接调用方法操作数据库。
注意: mapper接口对象的类名必须和之前的mapper.xml的namespace一致,方法名和参数名及返回类型也要与mapper.xml的配置一致。
public interface ClazzMapper {//根据id查询班级信息Clazz selectClazzById(int id);}
7.测试
public class OneToManyTest {public static void main(String[] args) throws Exception {InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession = sqlSessionFactory.openSession();//获得mapper接口的代理对象ClazzMapper mapper = sqlSession.getMapper(ClazzMapper.class);//调用接口中的方法Clazz clazz = mapper.selectClazzById(1);List<Student> students = clazz.getStudents();for (Student student : students) {System.out.println(student);}}}
在控制台可以看到如下结果:
测试成功。