Mybatis 一对一关联关系

一、创建用户表和信息表

CREATE TABLE `t_person` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(40) DEFAULT NULL,
  `age` int DEFAULT NULL,
  `cardno` varchar(18) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
CREATE TABLE `t_info` (
  `id` int NOT NULL AUTO_INCREMENT,
  `cardno` varchar(18) DEFAULT NULL,
  `address` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

二、身份信息保存的实现
Info.class

public class Info {
    
    
    private Integer id;
    private String cardno;
    private String address;
	//getXXX() setXXX()
}

InfoDAO.interface

public interface InfoDAO {
    
    
    //保存身份信息
    int save(Info info);
}

InfoDAO.xml

<mapper namespace="com.xxx.dao.InfoDAO">
    <insert id="save" parameterType="com.xxx.entity.Info" keyProperty="id" useGeneratedKeys="true" >
    insert into t_info values (#{id},#{cardno},#{address})
</insert>
</mapper>

Mybatis主配置文件中注册mapper.xml文件

 <mappers>
        <mapper resource="com/xxx/dao/InfoDAO.xml"></mapper>
</mappers>

测试

  @Test
    public void testInfoDAO(){
    
    
        Info info = new Info(1,"1653478938456","美国纽约中央大道256号");
        //重写了构造函数
        infoDAO.save(info);
    }

用户信息保存类似这里就没有记

三、用户信息查询
Person .class

public class Person {
    
    
    private Integer id;
    private String name;
    private Integer age;
    private String cardno;

    //关系属性
    private Info info;//定义了一个身份对象接收当前用户身份信息
    //set() get()
}

PersonDAO .interface

public interface PersonDAO {
    
    
    int save(Person person);
    //查询所有
    List<Person> queryAll();
}
<mapper namespace="com.xxx.dao.PersonDAO">
	<!--保存用户信息-->
    <insert id="save" parameterType="com.xxx.entity.Person" keyProperty="id" useGeneratedKeys="true">
           insert into t_person values (#{id},#{name},#{age},#{cardno})
    </insert>
    <!--用于结果封装-->
    <resultMap id="personMap" type="com.xxx.entity.Person">
        <id column="id" property="id"></id>
        <result column="name" property="name"/>
        <result column="age" property="age"/>
        <result column="cardno" property="cardno"/>
<!--        association处理一对一关联关系的标签 property:用于书写封装的额关系属性名 javaType:关系属性的Java类型-->
        <association property="info" javaType="com.xxx.entity.Info">
            <id column="iid" property="id"></id>
            <result column="address" property="address"/>
            <result column="icardno" property="cardno"/>
        </association>
    </resultMap>
    <!--查询所有-->
    <select id="queryAll" resultMap="personMap">
       select p.id,name,age,p.cardno,i.address ,i.id iid,i.cardno icardno from t_person p
        left join t_info i
        on i.cardno=p.cardno
    </select>
</mapper>

注册配置文件

   <mappers>
        <mapper resource="com/xxx/dao/PersonDAO.xml"></mapper>
    </mappers>

测试


public class TestPerson {
    
    
    private InputStream in;

    private SqlSession session;
    private PersonDAO personDAO;

    @Before
    public void init() throws IOException {
    
    
        //1.读取配置文件
        in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2创建工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        session = factory.openSession();
        personDAO = session.getMapper(PersonDAO.class);
    }
    @After
    public void destory()  throws IOException {
    
    
        //提交事務
        session.commit();
        session.close();
        in.close();

    }
    //测试保存用户信息
    @Test
    public void testPersonDAO(){
    
    
        Person person=new Person();
        person.setAge(45);
        person.setCardno("123456");
        person.setName("Tiom");
        personDAO.save(person);
    }
    //测试关联查询
    @Test
    public void testQueryAll(){
    
    
        personDAO.queryAll().forEach(person-> System.out.println("当前用户信息"+person+person.getInfo()));

    }
}

输出

当前用户信息Person{id=1, name='Joey', age=25, cardno='1653478938456'}Info{id=1, cardno='1653478938456', address='美国纽约中央大道256号'}

猜你喜欢

转载自blog.csdn.net/weixin_49035356/article/details/112258646