今天遇到的问题就是和下面帖子的一样,
数据库里面 localname字段长度是32,使用下面的参数查不到数据,是null.
subinput.setMuid("110028");
subinput.setWumark("01");
subinput.setLocalname("Passwd");
where MUID = #{muid,jdbcType=CHAR}
and WUMARK = #{wumark,jdbcType=CHAR}
and trim(LOCALNAME) = #{localname,jdbcType=CHAR}
然后使用写死的sql是能查询到数据的,
where MUID = '110028' and WUMARK = '01' and LOCALNAME = 'Passwd'
把jdbcType改成varchar也不行,手工把LOCALNAME补到32位就可以
subinput.setMuid("110028");
subinput.setWumark("01");
subinput.setLocalname("Passwd ");
然后就开始找原因,解决方案就是加trim,这个很恶心,或者mybaits下就不要用char了
where MUID = #{muid,jdbcType=CHAR}
and WUMARK = #{wumark,jdbcType=CHAR}
and trim(LOCALNAME) = #{localname,jdbcType=CHAR}
http://blog.youkuaiyun.com/zsw12013/article/details/51405398
oracle数据库Project表中的NAME字段是char类型
ProjectMapper.xml配置如下:
<select id="getProject" resultMap="BaseResultMap" parameterType="Java.lang.String" >
select
<include refid="Base_Column_List" />
from PROJECT
where NAME = #{name,jdbcType=CHAR}
</select>
ProjectMapper.java 中方法如下:
public Project getProject(String name);
测试的结果得到的Project对象总是null。
原来这种情况下char存储的时候没达到指定长度空出部分用空格填充。解决办法就是先去掉多余的空格。
修改如下:
<select id="getProject" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from PROJECT
where trim(NAME) = #{name,jdbcType=CHAR}
</select>
今天碰到一个很奇怪的问题,发现mybatis中对char与varchar的处理是不一样的,如果数据库中定义的是char类型则需要字符长短也一致才能查询到。
<!--Oracle CHAR fix -->
<typeHandler handler="cn.com.git.cbs.mybatis.typehandler.OracleCharStringTypeHandler"
javaType="String" jdbcType="CHAR" />
</typeHandlers>
@Override
public void setParameter(PreparedStatement ps, int i, String parameter,
JdbcType jdbcType) throws SQLException {
OraclePreparedStatement ops = ps.unwrap(OraclePreparedStatement.class);
ops.setFixedCHAR(i,parameter);
}