myBatis04

1.实现一对一映射

数据库中新建表t_address ,在t_student表中加入addressId字段对应t_address 中主键。

我们实现通过学生id查询学生信息和家庭住址。

2.新建接口和对应映射,新建实体类

package com.java1234.mappers;

import java.util.List;

import com.java1234.model.Address;

public interface AddressMapper {

public Address findByid(Integer id);


}

====

<?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.java1234.mappers.AddressMapper">
<resultMap type="Address" id="AddressResult">
<result property="id" column="id" />
<result property="sheng" column="sheng" />
<result property="shi" column="shi" />
<result property="qu" column="qu" />
</resultMap>
<select id="findByid" parameterType="Integer" resultType="Address">
select * from t_address where id=#{id}
</select>

</mapper>

3.注意这里有3种方式实现一对一,推荐没注释这种

<?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.java1234.mappers.StudentMapper">
<!--定义一个resultmap用来接收所有学生 -->
<!-- <resultMap type="Student" id="StudentResult"> <id property="id" column="id"
/> <result property="name" column="name" /> <result property="age" column="age"
/> <result property="address.id" column="addressId"/> <result property="address.sheng"
column="sheng"/> <result property="address.shi" column="shi"/> <result property="address.qu"
column="qu"/> </resultMap> -->
<!--
<resultMap type="Student" id="StudentResult">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="age" column="age" />
<association property="address" resultMap="AddressResult"></association>
</resultMap>
<resultMap type="Address" id="AddressResult">
<result property="id" column="id" />
<result property="sheng" column="sheng" />
<result property="shi" column="shi" />
<result property="qu" column="qu" />
</resultMap>-->
<resultMap type="Student" id="StudentResult"> <id property="id" column="id"
/> <result property="name" column="name" /> <result property="age" column="age"
/> <association property="address" column="addressId" select="com.java1234.mappers.AddressMapper.findByid"></association>
</resultMap>

<select id="findStudentWithAddress" resultMap="StudentResult"
parameterType="Integer">
select * from t_student t1 , t_address t2 where
t1.addressId=t2.id and t1.id=#{id}
</select>
<insert id="add" parameterType="Student">
insert into t_student
values(null,#{name},#{age})
</insert>
<update id="update" parameterType="Student">
update t_student set
name=#{name},age=#{age} where id=#{id}
</update>
<delete id="delete" parameterType="Integer">
delete from t_student where
id=#{id}
</delete>
<select id="findStudentByid" parameterType="Integer" resultType="Student">
select * from t_student where id=#{id}
</select>
<select id="find" resultMap="StudentResult">
select * from t_student;
</select>
</mapper>

4.完成测试

package com.java1234.service;

import static org.junit.Assert.*;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.java1234.mappers.StudentMapper;
import com.java1234.model.Student;
import com.java1234.util.SqlSessionFactoryUtil;

public class StudentTest3 {
private static Logger logger = Logger.getLogger(StudentTest.class);
private SqlSession sqlSession = null;
private StudentMapper studentMapper = null;

@Before
public void setUp() throws Exception {
sqlSession = SqlSessionFactoryUtil.openSession();
studentMapper = sqlSession.getMapper(StudentMapper.class);
}

@After
public void tearDown() throws Exception {
// 方法执行完成后调用关闭
sqlSession.close();
}

@Test
public void testFindStudentWithAddress() {
logger.info("查询带地址!");
Student s=studentMapper.findStudentWithAddress(14);
System.out.println(s);

}
}

 5.打印结果如下

[main] INFO com.java1234.service.StudentTest - 查询带地址!
Student [id=14, name=wu1, age=30, address=Address [id=3, sheng=辽宁省, shi=沈阳市, qu=大东区]]

原文地址:https://www.cnblogs.com/pond/p/5303290.html