要继承这个类
package com.briup.bean;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
/**
* @program: mybatius
* @description:
* @author: lyc
* @create: 2022-09-29 09:34
**/
public class PhoneTypeHandler extends BaseTypeHandler<PhoneNumber> {
//遇到PhoneNumber参数的时候应该如何在ps中设置值
@Override
public void setNonNullParameter(PreparedStatement ps, int i, PhoneNumber parameter, JdbcType jdbcType)
throws SQLException {
ps.setString(i, parameter.getAsString());
}
//查询中遇到PhoneNumber类型的应该如何封装(使用列名封装)
@Override
public PhoneNumber getNullableResult(ResultSet rs, String columnName) throws SQLException {
return new PhoneNumber(rs.getString(columnName));
}
//查询中遇到PhoneNumber类型的应该如何封装(使用列的下标)
@Override
public PhoneNumber getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return new PhoneNumber(rs.getString(columnIndex));
}
//CallableStatement使用中遇到了PhoneNumber类型的应该如何封装
@Override
public PhoneNumber getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return new PhoneNumber(cs.getString(columnIndex));
}
}
一对多
package com.briup.dao;
import com.briup.bean.Student;
import org.apache.ibatis.annotations.Param;
public interface duobiao {
//
Student getStudentAndAddress(@Param("studId") Integer studId);
// 使用【嵌套结果】ResultMap,实现一对一关系映射。
Student getStudentAndAddress1(@Param("studId") Integer studId);
}
<?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.briup.dao.duobiao">
<resultMap type="Student" id="StudentWithAddressResult">
<id property="studId" column="id"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<result property="phone" column="phone" typeHandler="com.briup.bean.PhoneTypeHandler"/>
<result property="address.addrId" column="addr_id"/>
<result property="address.street" column="street"/>
<result property="address.city" column="city"/>
<result property="address.state" column="state"/>
<result property="address.zip" column="zip"/>
<result property="address.country" column="country"/>
</resultMap>
<select id="getStudentAndAddress" parameterType="int" resultMap="StudentWithAddressResult">
select
s.id , s.name, s.email,s.dob,s.phone,
a.addr_id, a.street, a.city, a.state, a.zip, a.country
from
students s
left outer join addresses a on s.addr_id = a.addr_id
where
id = #{studId}
</select>
<!-- 使用【嵌套结果】ResultMap,实现一对一关系映射。-->
<!-- 独立的Address封装映射 -->
<resultMap type="Address" id="AddressResult">
<id property="addrId" column="addr_id" />
<result property="street" column="street" />
<result property="city" column="city" />
<result property="state" column="state" />
<result property="zip" column="zip" />
<result property="country" column="country" />
</resultMap>
<!-- Student封装映射,里面关联上Address的封装映射 -->
<resultMap type="Student" id="StudentWithAddressResult1">
<id property="studId" column="id" />
<result property="name" column="name" />
<result property="email" column="email" />
<result property="dob" column="dob" />
<result property="phone" column="phone" />
<association property="address" resultMap="AddressResult" />
</resultMap>
<select id="getStudentAndAddress1" parameterType="int" resultMap="StudentWithAddressResult1">
select
s.id, s.name, s.email,s.dob,s.phone,
a.addr_id, a.street, a.city, a.state, a.zip, a.country
from
students s
left outer join addresses a on s.addr_id = a.addr_id
where
id = #{studId}
</select>
</mapper>
测试类
import com.briup.dao.duobiao;
import com.briup.until.MyBatisSqlSessionFactory;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
/**
* @program: mybatius
* @description:
* @author: lyc
* @create: 2022-09-28 14:37
**/
public class Testdat {
@Test
public void day(){
SqlSession session = MyBatisSqlSessionFactory.openSession();
duobiao mapper=session.getMapper(duobiao.class);
System.out.println(mapper.getStudentAndAddress(2));
}
@Test
public void day1(){
SqlSession session = MyBatisSqlSessionFactory.openSession();
duobiao mapper=session.getMapper(duobiao.class);
System.out.println(mapper.getStudentAndAddress1(2));
}
}