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();
}
}
异常信息
解决:
注意需要转换的字段需要全部转换,这里的问题就是出现在 update语句中的 需要转换的字段没进行转换,导致引用在启动的时候解析xml出现了异常。
上一篇: 2.3 H264数据封装RTP包
下一篇: ffmpeg一键安装
推荐阅读
-
vs2015/vs2013中mvc5 viewbag总是出现问题该怎么办?
-
搞Jedis案例出现问题,有大佬帮我看看怎么解决吗?先感谢大佬点进来看了---Day31
-
电脑中RunDll提示Funkoala64.dll出现问题找不到指定的程序的解决方法
-
打开word时出现了出现问题需要关闭是否发送错误报告
-
关于FusionCharts需要第一次点击切换才出现问题
-
企业营销型网站做不好 可能在这三方面出现问题
-
iPad 4出现问题怎么办 苹果iPad 4怎么换iPad Air 2
-
打飞机导致射精出现问题,别边看岛国片边搞动作
-
mysql安装出现问题(The service already exists)
-
Apache源码包在LINUX(CENTOS6.8)中的安装(出现问题及解决)