數據庫中一對多一般使用主外鍵關聯,外鍵應該在多方,即多方維護關係。java
下面舉一個簡單實例來看看MyBatis怎麼處理一對多的關係。mysql
-- 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');
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;
ClazzMapper.xmlsql
<?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的屬性students column表示將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>
<?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> <!-- 引入 外部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>
注意: mapper接口對象的類名必須和以前的mapper.xml的namespace一致,方法名和參數名及返回類型也要與mapper.xml的配置一致。session
public interface ClazzMapper { //根據id查詢班級信息 Clazz selectClazzById(int id); }
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); } } }
在控制檯能夠看到以下結果:mybatis
測試成功。app