← 返回首页
Mybatis基础教程(十九)
发表时间:2022-03-13 14:14:17
一对一(基于注解)

1.一对一 在关系映射的一对一案例的基础上,我们基于注解方式实现一个学生对应一个身份证的一对一关系映射。

项目结构图如下:

1.在com.dao包下创建映射接口。

//IdCardMapper.java
package com.dao;

import com.entity.IdCard;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

public interface IdCardMapper {
    @Select("select * from idcard where id=#{id}")
    public IdCard getIdCardById(Integer id);

    @Insert("insert into idcard (code) values (#{code})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    int addIdCard(IdCard card);

    //使用内连接方法
    @Select("select s.*,c.code from students s,idcard c where s.cid = c.id and c.id=#{id}")
    @Results({@Result(property = "stu.sid",column = "sid" ),
              @Result(property = "stu.sname",column = "sname"),
              @Result(property = "stu.gender",column = "gender"),
              @Result(property = "stu.birthday",column = "birthday"),
              @Result(property = "stu.major",column = "major")
            })
    public IdCard getIdCardWithStudentById(Integer id);
}

//StudentsMapper.java

package com.dao;

import com.entity.IdCard;
import com.entity.Students;
import org.apache.ibatis.annotations.*;

public interface StudentsMapper {

    //使用级联查询方式
    @Select("select * from students where sid=#{sid}")
    @Results(@Result(column = "cid",property = "card",javaType = IdCard.class, one = @One(select = "com.dao.IdCardMapper.getIdCardById")))
    public Students getStudentBySid(Integer sid);

    //使用内连接方法
    @Select("select s.*,c.code from students s,idcard c where s.cid = c.id and s.sid=#{sid}")
    @Results({@Result(property = "card.id",column = "cid" ),@Result(property = "card.code",column = "code")})
    public Students getStudentBySid2(Integer sid);
}

2.在mybatis-config.xml添加映射接口。

    ...
    <!--映射文件-->
    <mappers>
        <mapper class="com.dao.StudentsMapper"/>
        <mapper class="com.dao.IdCardMapper"/>
   </mappers>

3.编写测试类

//IdCardMapperTest.java
public class IdCardMapperTest {
    //测试添加身份证
    @Test
    public void testAddClassRoom(){
        SqlSession session = null;
        IdCard card = new IdCard();
        card.setCode("612133197804155480");
        try {
            session = SqlSessionFactoryUtil.openSqlSession();
            IdCardMapper idCardMapper = session.getMapper(IdCardMapper.class);
            int result = idCardMapper.addIdCard(card);
            System.out.println("受影响记录条数是:" + result);
            session.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
}

//StudentsMapperTest.java
public class StudentsMapperTest {

    @Test
    public void testQueryStudentsBySid(){
        SqlSession session = null;
        try {
            session = SqlSessionFactoryUtil.openSqlSession();
            StudentsMapper studentsMapper =  session.getMapper(StudentsMapper.class);
            Students s = studentsMapper.getStudentBySid(7);
            System.out.println(s);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    @Test
    public void testQueryStudentsBySid2(){
        SqlSession session = null;
        Students s = null;
        try {
            session = SqlSessionFactoryUtil.openSqlSession();
            StudentsMapper studentsMapper =  session.getMapper(StudentsMapper.class);
            s = studentsMapper.getStudentBySid2(7);
            System.out.println(s);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

}