Mybatis框架中实现双向一对多关系映射
学习过 Hibernate 框架的伙伴们很容易就能简单的配置各种映射关系 (Hibernate 框架的映射关系在我的 blogs 中也有详细的讲解),但是在 Mybatis 框架中我们又如何去实现
一对多的关系映射呢? 其实很简单
首先我们照常先准备前期的环境 (具体解释请 参考初识 Mybatis 进行增、删、改、查 blogs) 这里我就直接上代码了
主配置文件:Configuration.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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="" value=""/>
</transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="username" value="practice"/>
<property name="password" value="123"/>
</dataSource>
</environment>
</environments><mappers>
<mapper resource="config/Student.xml"/>
<mapper resource="config/Grade.xml"/>
</mappers></configuration>
背景:学生和班级是一个典型的一对多的关系,一个班级可以对应着多个学生,所以我们随即创建了学生对象和班级对象
学生类:Student
package entity; /* * 学生类 * */ public class Student { //学生编号 private Integer sid; //学生名称 private String sname; //学生性别 private String sex;</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Student() { } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Student(String sname, String sex) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.sname =<span style="color: rgba(0, 0, 0, 1)"> sname; </span><span style="color: rgba(0, 0, 255, 1)">this</span>.sex =<span style="color: rgba(0, 0, 0, 1)"> sex; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Integer getSid() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> sid; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setSid(Integer sid) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.sid =<span style="color: rgba(0, 0, 0, 1)"> sid; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getSname() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> sname; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setSname(String sname) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.sname =<span style="color: rgba(0, 0, 0, 1)"> sname; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getSex() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> sex; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setSex(String sex) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.sex =<span style="color: rgba(0, 0, 0, 1)"> sex; }
}
班级类:Grade
package entity;
import java.util.HashSet;
import java.util.Set;
/*
-
班级类
-
*/
public class Grade {
//班级编号
private Integer gid;
//班级名称
private String gname;
//班级描述
private String gdesc;
//班级下的学生信息
private Set<Student> stus=new HashSet<Student>();
public Set<Student> getStus() {
return stus;
}
public void setStus(Set<Student> stus) {
this.stus = stus;
}
public Grade() {
}
public Grade(Integer gid, String gname, String gdesc) {
this.gid = gid;
this.gname = gname;
this.gdesc = gdesc;
}
public Integer getGid() {
return gid;
}
public void setGid(Integer gid) {
this.gid = gid;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public String getGdesc() {
return gdesc;
}
public void setGdesc(String gdesc) {
this.gdesc = gdesc;
}
}
实体类准备完了的话,我们就可以开始看配置文件了,也是最关键的一部分
首先讲简单点的学生实体类对应的配置文件
Student.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="Student">
<resultMap type="entity.Student" id="StudentResult">
<id column="sid" jdbcType="INTEGER" property="sid"/>
<result column="sname" jdbcType="VARCHAR" property="sname"/>
<result column="sex" jdbcType="VARCHAR" property="sex"/>
</resultMap></mapper>
然后就是最关键的班级实体的配置文件了
Grade.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="Grade">
<resultMap type="entity.Grade" id="GradeResult">
<id column="gid" jdbcType="INTEGER" property="gid"/>
<result column="gname" jdbcType="VARCHAR" property="gname"/>
<result column="gdesc" jdbcType="VARCHAR" property="gdesc"/>
<!-- 一对多关系 -->
<collection property="stus" resultMap="Student.StudentResult"></collection>
</resultMap><!-- 查询所有信息 --> <<span style="color: rgba(0, 0, 255, 1)">select</span> id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">selectAllInfo</span><span style="color: rgba(128, 0, 0, 1)">"</span> resultMap=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">GradeResult</span><span style="color: rgba(128, 0, 0, 1)">"</span>> <!-- <span style="color: rgba(0, 0, 255, 1)">select</span> sid,sname,sex,g.gid,gname,gdesc <span style="color: rgba(0, 0, 255, 1)">from</span> Student s,Grade g <span style="color: rgba(0, 0, 255, 1)">where</span> s.gid=g.gid --> <span style="color: rgba(0, 0, 255, 1)">select</span> sid,sname,sex,g.gid,gname,gdesc <span style="color: rgba(0, 0, 255, 1)">from</span> Student s left join Grade g on s.gid=<span style="color: rgba(0, 0, 0, 1)">g.gid </span></<span style="color: rgba(0, 0, 255, 1)">select</span>>
</mapper>
以上就是对配置文件的解释了
接下来我们就可以进行一道测试了
/* * 1.1 查询所有的班级和班级下的所有学生 (一对多) * */ @Test public void selectAllStu() throws Exception{ //通过配置文件获取到数据库连接信息 Reader reader = Resources.getResourceAsReader("config/Configuration.xml"); //通过配置信息构建一个 SessionFactory 工厂 SqlSessionFactory sqlsessionfactory=new SqlSessionFactoryBuilder().build(reader); //通过 SessionFaction 打开一个回话通道 SqlSession session = sqlsessionfactory.openSession(); /*SqlSession session =MybatisUtil.getSession();*/ //调用配置文件中的 sql 语句 List<Grade> list = session.selectList("Grade.selectAllInfo"); //遍历查询出来的结果 for (Grade grade : list) { System.out.println("班级:"+grade.getGname()); for (Student stu : grade.getStus()) { System.out.println("学生:"+stu.getSname());} }session.close(); } </span></pre>
执行后,查询出来的结果是
以上是第一种一对多关系映射的方式,下面是第二种一对多映射的方法,其他的所有步骤和上面的都是一样的只有相对应的配置文件不同,所以我就只贴小配置了
Grade.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="Grade">
<resultMap type="entity.Grade" id="GradeResult">
<id column="gid" jdbcType="INTEGER" property="gid"/>
<result column="gname" jdbcType="VARCHAR" property="gname"/>
<result column="gdesc" jdbcType="VARCHAR" property="gdesc"/>
<!-- 一对多关系 -->
<!-- <collection property="stus" resultMap="Student.StudentResult"></collection> -->
<collection property="stus" javaType="entity.Student">
<id property="sid" column="sid"/>
<result property="sname" column="sname"/>
<result property="sex" column="sex"/>
</collection>
</resultMap><!-- 查询所有信息 --> <<span style="color: rgba(0, 0, 255, 1)">select</span> id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">selectAllInfo</span><span style="color: rgba(128, 0, 0, 1)">"</span> resultMap=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">GradeResult</span><span style="color: rgba(128, 0, 0, 1)">"</span>> <!-- <span style="color: rgba(0, 0, 255, 1)">select</span> sid,sname,sex,g.gid,gname,gdesc <span style="color: rgba(0, 0, 255, 1)">from</span> Student s,Grade g <span style="color: rgba(0, 0, 255, 1)">where</span> s.gid=g.gid --> <span style="color: rgba(0, 0, 255, 1)">select</span> sid,sname,sex,g.gid,gname,gdesc <span style="color: rgba(0, 0, 255, 1)">from</span> Student s left join Grade g on s.gid=<span style="color: rgba(0, 0, 0, 1)">g.gid </span></<span style="color: rgba(0, 0, 255, 1)">select</span>> <!-- 新增班级并同时新增班级下的学生 --> <!--useGeneratedKeys=<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)"> 表明采用主键生成策略 keyProperty</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">gid</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> 表明将生成的主键添加到parameterType类中的那个属性值中去 </span>--> <!-- <insert id=<span style="color: rgba(128, 0, 0, 1)">""</span> useGeneratedKeys=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">true</span><span style="color: rgba(128, 0, 0, 1)">"</span> keyProperty=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">gid</span><span style="color: rgba(128, 0, 0, 1)">"</span> parameterType=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">entity.Grade</span><span style="color: rgba(128, 0, 0, 1)">"</span>> </insert> -->
</mapper>
接下来就可以在多的一方配置一的关联关系了
Student.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="Student">
<resultMap type="entity.Student" id="StudentResult">
<id column="sid" jdbcType="INTEGER" property="sid"/>
<result column="sname" jdbcType="VARCHAR" property="sname"/>
<result column="sex" jdbcType="VARCHAR" property="sex"/>
<!-- 多对一 -->
<!-- <association property="grade" resultMap="Grade.GradeResult"></association> -->
<association property="grade" javaType="entity.Grade">
<id property="gid" column="gid"/>
<result property="gname" column="gname"/>
<result property="gdesc" column="gdesc"/>
</association>
</resultMap><!-- 使用别名 --> <sql id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">cloums</span><span style="color: rgba(128, 0, 0, 1)">"</span>><span style="color: rgba(0, 0, 0, 1)"> s.sid,s.sname,s.sex ,g.gid,g.gname,g.gdesc </span></sql> <!-- 多对一查询学生的班级 --> <<span style="color: rgba(0, 0, 255, 1)">select</span> id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">selectGradeByStu</span><span style="color: rgba(128, 0, 0, 1)">"</span> resultMap=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">StudentResult</span><span style="color: rgba(128, 0, 0, 1)">"</span>> <span style="color: rgba(0, 0, 255, 1)">select</span> <include refid=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">cloums</span><span style="color: rgba(128, 0, 0, 1)">"</span>/> <span style="color: rgba(0, 0, 255, 1)">from</span> Student s ,Grade g <span style="color: rgba(0, 0, 255, 1)">where</span> s.gid=<span style="color: rgba(0, 0, 0, 1)">g.gid </span></<span style="color: rgba(0, 0, 255, 1)">select</span>> <!-- 简单查询所有信息 --> <<span style="color: rgba(0, 0, 255, 1)">select</span> id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">selectAllStu</span><span style="color: rgba(128, 0, 0, 1)">"</span> resultMap=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">StudentResult</span><span style="color: rgba(128, 0, 0, 1)">"</span>> <span style="color: rgba(0, 0, 255, 1)">select</span> sid,sname,sex,gid <span style="color: rgba(0, 0, 255, 1)">from</span><span style="color: rgba(0, 0, 0, 1)"> Student </span></<span style="color: rgba(0, 0, 255, 1)">select</span>> <!--动态拼接Sql --> <<span style="color: rgba(0, 0, 255, 1)">select</span> id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">selectAllStuByWhere</span><span style="color: rgba(128, 0, 0, 1)">"</span> parameterType=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">entity.Student</span><span style="color: rgba(128, 0, 0, 1)">"</span> resultMap=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">StudentResult</span><span style="color: rgba(128, 0, 0, 1)">"</span>> <span style="color: rgba(0, 0, 255, 1)">select</span> sid,sname,sex,gid <span style="color: rgba(0, 0, 255, 1)">from</span> Student <span style="color: rgba(0, 0, 255, 1)">where</span> <span style="color: rgba(128, 0, 128, 1)">1</span>=<span style="color: rgba(128, 0, 128, 1)">1</span> <<span style="color: rgba(0, 0, 255, 1)">if</span> test=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">sname!=null and !&quot;&quot;.equals(sname.trim())</span><span style="color: rgba(128, 0, 0, 1)">"</span>> <!-- and sname like <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">%</span><span style="color: rgba(128, 0, 0, 1)">'</span>|| #{sname}|| <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">%</span><span style="color: rgba(128, 0, 0, 1)">'</span> --> <!-- 模糊查询 --><span style="color: rgba(0, 0, 0, 1)"> and sname like </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">%${sname}%</span><span style="color: rgba(128, 0, 0, 1)">'</span><!-- 模糊查询 --> <!-- and sname = #{sname} --> </<span style="color: rgba(0, 0, 255, 1)">if</span>> </<span style="color: rgba(0, 0, 255, 1)">select</span>> <!-- 新增学生信息 --> <insert id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">InsertStuInfo</span><span style="color: rgba(128, 0, 0, 1)">"</span> parameterType=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">entity.Student</span><span style="color: rgba(128, 0, 0, 1)">"</span> ><span style="color: rgba(0, 0, 0, 1)"> insert into Student values(SEQ_NUM.Nextval,#{sname},#{sex},</span><span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">) </span></insert> <!-- 删除学生信息 --> <insert id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">DeleteStuBySid</span><span style="color: rgba(128, 0, 0, 1)">"</span> parameterType=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">int</span><span style="color: rgba(128, 0, 0, 1)">"</span>><span style="color: rgba(0, 0, 0, 1)"> delete </span><span style="color: rgba(0, 0, 255, 1)">from</span> Student <span style="color: rgba(0, 0, 255, 1)">where</span> sid=<span style="color: rgba(0, 0, 0, 1)">#{sid} </span><!--或者是 delete <span style="color: rgba(0, 0, 255, 1)">from</span> Student <span style="color: rgba(0, 0, 255, 1)">where</span> sid=#{_parameter} --> </insert> <!-- 根据SID修改学生信息 --> <update id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">UpdateStuBySid</span><span style="color: rgba(128, 0, 0, 1)">"</span> parameterType=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">entity.Student</span><span style="color: rgba(128, 0, 0, 1)">"</span> ><span style="color: rgba(0, 0, 0, 1)"> update Student </span><span style="color: rgba(0, 0, 255, 1)">set</span> sname=#{sname},sex=#{sex} <span style="color: rgba(0, 0, 255, 1)">where</span> sid=<span style="color: rgba(0, 0, 0, 1)">#{sid} </span></update>
</mapper>
这样就已经完成了 Mybatis 框架中的简单的双向一对多的配置了,怎么样,是不是没有想象中的难呢?希望对大家有点帮助吧!