mybatis怎样向oracle插入blob字段,设置了jdbcType=BLOB还是不行,报ORA-01460: 转换请求无法实施或不合理

一叶葬目 2017-05-16 05:36:45
mybatis向oracle插入数据存在blob字段时直接报错:
SQL: insert into AAQY_TEST_BLOB(id, name, image) values ( ?, ?, ? )
Cause: java.sql.SQLException: ORA-01460: 转换请求无法实施或不合理
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:172)
at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:157)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:52)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)
at $Proxy15.insertUser(Unknown Source)
at com.per.qy.blob.TestBlob.test1(TestBlob.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
……

下面是测试代码

//实体类
public class User {
private int id;
private String name;
private byte[] image;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
}

//mapper.xml
<mapper namespace="com.per.qy.blob.UserDao">
<resultMap type="com.per.qy.blob.User" id="userMap">
<id property="id" column="id" />
<result property="name" column="name" jdbcType="VARCHAR" />
<result property="image" column="image" jdbcType="BLOB" />
</resultMap>

<select id="selectUserById" parameterType="java.lang.Integer" resultMap="userMap">
select * from AAQY_TEST_BLOB where id=#{id}
</select>

<insert id="insertUser" parameterType="com.per.qy.blob.User">
insert into AAQY_TEST_BLOB(id, name, image)
values (
#{id},
#{name, jdbcType=VARCHAR},
#{image, jdbcType=BLOB}
)
</insert>
</mapper>

//测试代码
@Test
public void test1() {
SqlSession session = sqlSessionFactory.openSession();
try {
InputStream in = new FileInputStream(new File("D://opt//files//1.jpg"));
byte[] image = new byte[in.available()];
System.out.println("========================" + image.toString());

User user = new User();
user.setId(1);
user.setName("呵呵哒");
user.setImage(image);

UserDao userDao = session.getMapper(UserDao.class);
userDao.insertUser(user);

User result = userDao.selectUserById(1);
System.out.println("========"+result.getId()+"=="+result.getName()+"=="+result.getImage());
} catch (Exception e) {
e.printStackTrace();
}
}

求大神帮忙看看
...全文
1333 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
辉泽 2020-03-03
  • 打赏
  • 举报
回复
Csdn为啥搜不到这个文档
我是一道坎 2017-05-27
  • 打赏
  • 举报
回复
学习了,感谢楼主
一叶葬目 2017-05-23
  • 打赏
  • 举报
回复
困扰了那么久原来是驱动版本问题,把ojdbc14.jar换成ojdbd6.jar就可以了
一叶葬目 2017-05-16
  • 打赏
  • 举报
回复
引用 1 楼 Danishlyy1995的回复:
是因为数据类型的问题吧,你的image是byte 但是数据库你搞的是blob
数据类型应该是这个的,mysql这样是可以成功入数据,但对oracle我不知道mybatis应该怎样操作blob
墨笙弘一 2017-05-16
  • 打赏
  • 举报
回复
是因为数据类型的问题吧,你的image是byte 但是数据库你搞的是blob

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧