mybatis的类型转化器报错

要继承这个类

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));





    }








}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值