欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TypeHandler出现问题

程序员文章站 2022-07-14 18:45:37
...

TypeHandler出现问题

环境描述

springboot + mybatis

配置文件

#默认使用配置
spring:
  profiles:
    active: pro
  #公共配置与profiles选择无关
mybatis:
  typeAliasesPackage: com.example
  mapperLocations: classpath:mapper/*.xml
---
  #开发配置
spring:
  profiles: dev
  datasource:
    url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
---
#开发配置
spring:
  profiles: pro
  datasource:
    url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

MyTypeHandler

package com.example.common;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MyTypeHandler extends BaseTypeHandler<List<Integer>> {

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, List<Integer> params, JdbcType jdbcType) throws SQLException {
        //java到jdbc
        preparedStatement.setString(i,String.join(",", params.toString()));
    }

    @Override
    public List<Integer> getNullableResult(ResultSet resultSet, String s) throws SQLException {
        String checkbox = resultSet.getString(s);
        return getIntegers(checkbox);
    }

    @Override
    public List<Integer> getNullableResult(ResultSet resultSet, int i) throws SQLException {
        String checkbox = resultSet.getString(i);
        return getIntegers(checkbox);
    }

    @Override
    public List<Integer> getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        String checkbox = callableStatement.getString(i);
        return getIntegers(checkbox);
    }

    private List<Integer> getIntegers(String checkbox) {
        if (checkbox == null || "".equals(checkbox.trim())) {
            return null;
        } else {
            String[] box = checkbox.split(",");
            List<Integer> result = new ArrayList<>();
            for (String record : box) {
                result.add(Integer.parseInt(record));
            }
            return result;
        }
    }
}


实体类Person

package com.example.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

public class Person implements Serializable {
    private Integer id;

    private String name;

    private List<Integer> checkBox;

    private Integer age;

    private Date gmtCreate;

    private Date gmeModified;

    private static final long serialVersionUID = 1L;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public List<Integer> getCheckBox() {
        return checkBox;
    }

    public void setCheckBox(List<Integer> checkBox) {
        this.checkBox = checkBox;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getGmtCreate() {
        return gmtCreate;
    }

    public void setGmtCreate(Date gmtCreate) {
        this.gmtCreate = gmtCreate;
    }

    public Date getGmeModified() {
        return gmeModified;
    }

    public void setGmeModified(Date gmeModified) {
        this.gmeModified = gmeModified;
    }

    @Override
    public boolean equals(Object that) {
        if (this == that) {
            return true;
        }
        if (that == null) {
            return false;
        }
        if (getClass() != that.getClass()) {
            return false;
        }
        Person other = (Person) that;
        return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
            && (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName()))
            && (this.getCheckBox() == null ? other.getCheckBox() == null : this.getCheckBox().equals(other.getCheckBox()))
            && (this.getAge() == null ? other.getAge() == null : this.getAge().equals(other.getAge()))
            && (this.getGmtCreate() == null ? other.getGmtCreate() == null : this.getGmtCreate().equals(other.getGmtCreate()))
            && (this.getGmeModified() == null ? other.getGmeModified() == null : this.getGmeModified().equals(other.getGmeModified()));
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
        result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
        result = prime * result + ((getCheckBox() == null) ? 0 : getCheckBox().hashCode());
        result = prime * result + ((getAge() == null) ? 0 : getAge().hashCode());
        result = prime * result + ((getGmtCreate() == null) ? 0 : getGmtCreate().hashCode());
        result = prime * result + ((getGmeModified() == null) ? 0 : getGmeModified().hashCode());
        return result;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", id=").append(id);
        sb.append(", name=").append(name);
        sb.append(", checkBox=").append(checkBox);
        sb.append(", age=").append(age);
        sb.append(", gmtCreate=").append(gmtCreate);
        sb.append(", gmeModified=").append(gmeModified);
        sb.append(", serialVersionUID=").append(serialVersionUID);
        sb.append("]");
        return sb.toString();
    }
}

异常信息
TypeHandler出现问题

解决:
注意需要转换的字段需要全部转换,这里的问题就是出现在 update语句中的 需要转换的字段没进行转换,导致引用在启动的时候解析xml出现了异常。