mybatis3.0 添加了association和collection标签专门用于对多个相关实体类数据进行级联查询,但仍不支持多个相关实体类数据的级联保存和级联删除操作。因此在进行实体类多对多映射表设计时,需要专门建立一个关联对象类对相关实体类的关联关系进行描述。
(1)首先创建数据库manytomany,并在数据库下创建三个表t_classes、t_classteacher、t_teacher
create database manytomany; use manytomany; CREATE TABLE `t_classes` ( `cid` int(10) unsigned NOT NULL AUTO_INCREMENT, `cname` varchar(64) NOT NULL DEFAULT '', PRIMARY KEY (`cid`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; Insert into t_classes(cid,cname) values(1,'大一'); Insert into t_classes(cid,cname) values(2,'大二'); Insert into t_classes(cid,cname) values(3,'大三'); CREATE TABLE `t_teacher` ( `tid` int(10) unsigned NOT NULL AUTO_INCREMENT, `tname` varchar(64) NOT NULL DEFAULT '', PRIMARY KEY (`tid`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; Insert into t_teacher(tid,tname) values(1,'王老师'); Insert into t_teacher(tid,tname) values(2,'李老师'); Insert into t_teacher(tid,tname) values(3,'张老师'); CREATE TABLE `t_classTeacher` ( `id` int(10) unsigned Not NULL AUTO_INCREMENT, `cid` int(10) unsigned NOT NULL, `tid` int(10) unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; insert into t_classTeacher values(1,1,1); insert into t_classTeacher values(2,2,1); insert into t_classTeacher values(3,1,3); insert into t_classTeacher values(4,2,3);
(2)创建mybatis_007项目,项目结构如下:
(3)配置两个实体类Classes.java、Teacher.java
Classes.java
package mybatis.model; import java.util.List; public class Classes { private Integer cid; private String cname; private List<Teacher> teachers; public Integer getCid() { return cid; } public void setCid(Integer cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public List<Teacher> getTeachers() { return teachers; } public void setTeachers(List<Teacher> teachers) { this.teachers = teachers; } @Override public String toString() { return "Classes [cid=" + cid + ", cname=" + cname + ", teachers=" + teachers + "]"; } }
Teacher.java
package mybatis.model; import java.util.List; import javax.security.auth.Subject; public class Teacher { private Integer tid; private String tname; private List<Classes> classes; public Integer getTid() { return tid; } public void setTid(Integer tid) { this.tid = tid; } public String getTname() { return tname; } public void setTname(String tname) { this.tname = tname; } public List<Classes> getClasses() { return classes; } public void setClasses(List<Classes> classes) { this.classes = classes; } @Override public String toString() { return "Teacher [tid=" + tid + ", tname=" + tname + "]"; } }
(4)配置数据库连接文件config/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> <typeAliases> <typeAlias alias="Classes" type="mybatis.model.Classes" /> <typeAlias alias="Teacher" type="mybatis.model.Teacher" /> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/manytomany"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mybatis/model/ClassMapper.xml"/> </mappers> </configuration>
(5)配置ClassMapper.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.mapper.ClassesMapper"> <!-- 自定义结果映射 --> <resultMap type="mybatis.model.Classes" id="ClassesWithTeacherResult"> <id property="cid" column="cid"/> <result property="cname" column="cname"/> <!-- 多表关联映射 --> <collection property="teachers" ofType="mybatis.model.Teacher"> <id property="tid" column="tid"/> <result property="tname" column="tname"/> </collection> </resultMap> <select id="findClassesWithTeacher" parameterType="Integer" resultMap="ClassesWithTeacherResult"> select * from t_classes c,t_teacher t,t_classteacher ct where ct.cid=c.cid and ct.tid=t.tid and c.cid=#{cid} </select> </mapper>
(6)最后执行Main
package Main; import java.io.Reader; import mybatis.model.Classes; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; public class Main { private static SqlSessionFactory sqlSessionFactory; private static Reader reader; static { try { reader = Resources.getResourceAsReader("config/config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } catch (Exception e) { e.printStackTrace(); } } public static SqlSessionFactory getSession() { return sqlSessionFactory; } @Test public void findClassesWithTeacher() { SqlSession session = sqlSessionFactory.openSession(); for(int i=1;i<=2;i++){ Classes classess=session.selectOne("com.mapper.ClassesMapper.findClassesWithTeacher", i); System.out.println(classess); } } }
程序结果如下: