对于sql字段非空但插入值为空,将值转换为空的字符串
2017-10-19 17:59
495 查看
第一步:实现mybatls中的接口TypeHandler,其中有四个方法
public String getResult(ResultSet arg0, String arg1) throws SQLException
public String getResult(ResultSet arg0, int arg1) throws SQLException
public String getResult(CallableStatement arg0, int arg1) throws SQLException
public void setParameter(PreparedStatement arg0, int arg1, String arg2, JdbcType arg3) throws SQLException
主要用第四个方法。
第二步:创建一个类实现这个接口
package com.rthd.controller.person;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
public class NullValueHandler implements TypeHandler<String> {
@Override
public String getResult(ResultSet arg0, String arg1) throws SQLException {
// TODO Auto-generated method stub
return arg0.getString(arg1);
}
@Override
public String getResult(ResultSet arg0, int arg1) throws SQLException {
// TODO Auto-generated method stub
return arg0.getString(arg1);
}
@Override
public String getResult(CallableStatement arg0, int arg1) throws SQLException {
// TODO Auto-generated method stub
return arg0.getString(arg1);
}
@Override
public void setParameter(PreparedStatement arg0, int arg1, String arg2, JdbcType arg3) throws SQLException {
if(arg2==null && arg3==JdbcType.VARCHAR){
arg0.setString(arg1, "");
}else{
arg0.setString(arg1, arg2);
}
}
}
第三步:在spring-mybatils配置文件中注入
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.rthd.bean"></property>
<!-- 插入为空的值是实现接口typeHandlers -->
<property name="typeHandlersPackage" value="com.rthd.controller.person"></property>
第四步:在sql语句中
#{dimiapport,jdbcType=VARCHAR,typeHandler=com.rthd.controller.person.NullValueHandler},
第
public String getResult(ResultSet arg0, String arg1) throws SQLException
public String getResult(ResultSet arg0, int arg1) throws SQLException
public String getResult(CallableStatement arg0, int arg1) throws SQLException
public void setParameter(PreparedStatement arg0, int arg1, String arg2, JdbcType arg3) throws SQLException
主要用第四个方法。
第二步:创建一个类实现这个接口
package com.rthd.controller.person;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
public class NullValueHandler implements TypeHandler<String> {
@Override
public String getResult(ResultSet arg0, String arg1) throws SQLException {
// TODO Auto-generated method stub
return arg0.getString(arg1);
}
@Override
public String getResult(ResultSet arg0, int arg1) throws SQLException {
// TODO Auto-generated method stub
return arg0.getString(arg1);
}
@Override
public String getResult(CallableStatement arg0, int arg1) throws SQLException {
// TODO Auto-generated method stub
return arg0.getString(arg1);
}
@Override
public void setParameter(PreparedStatement arg0, int arg1, String arg2, JdbcType arg3) throws SQLException {
if(arg2==null && arg3==JdbcType.VARCHAR){
arg0.setString(arg1, "");
}else{
arg0.setString(arg1, arg2);
}
}
}
第三步:在spring-mybatils配置文件中注入
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.rthd.bean"></property>
<!-- 插入为空的值是实现接口typeHandlers -->
<property name="typeHandlersPackage" value="com.rthd.controller.person"></property>
第四步:在sql语句中
#{dimiapport,jdbcType=VARCHAR,typeHandler=com.rthd.controller.person.NullValueHandler},
第
相关文章推荐
- SQL 将一串字符串转换为列插入临时表
- 往数据表中插入 "字段名 字符串" , 对于非字符字段,采用 "递增整形数字" 代替(日期字段会自动转变为对应日期)
- java多线程,将oracle中数据导入到sql到文件(包括大字段转换成字符串)
- SQL把表中某字段的所有值(字符串转换成int)
- [SQL]根据刚刚插入数据的 ID 向另外一张表插入数据(插入的其他字段数据需要对字符串进行分析)
- 在sql字符串中插入日期型字段
- sql 分隔符字符串转换成表以及多行记录列值转换成字符串
- mysql 一怪sql语句:插入字段(uid,username,level,time...),且username是据uid从另表查出,其它为常数
- 绑定SqlDataSource的Gridview字符串字段长度截取
- mysql里面如何用sql语句让字符串转换为数字
- sql server 使用for xml path 将1列多行转换为字符串连接起来,俗称 sql 合并字符
- 同时插入2个表(不同字段个数)的sql!
- 将SQL语句转换为符合VB.NET格式字符串
- sql语句在某一字段所有数据后面加上相同的字符串
- sql怎么批量替换字段里的字符串的
- mysql通过sql语句如何批量去掉某一个表中某一个字段的下面的相同部分字符串
- sql 把a表的数据插入b表中,where某一个字段相同
- Sql 将一个整型数字转换成带前导零的固定长度数字字符串
- 用sql往数据库中插入一个新字段
- SQL把做个字段组合成一个字符串