为什么用mybatis不能向数据库里面插入数据?

为什么用mybatis不能向数据库里面插入数据?,第1张

mybatis不能向数据库里面插入数据原因可能是执行了插入动作,但是没有最终commit到数据库服务器导致。

mybatis插入数据的例子如下:

package com.mybatis.demo

import java.io.Reader

public class Test {

private static SqlSessionFactory sqlSessionFactory

private static Reader reader

static{

try{

reader = Resources.getResourceAsReader("Configuration.xml")

sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader)

}catch(Exception e){

e.printStackTrace()

}

}

public static SqlSessionFactory getSession(){

return sqlSessionFactory

}

//添加用户

public void addUser(){

User user = new User()

user.setId(4)

user.setUserAddress("人民广场")

user.setUserName("Birds")

user.setUserAge("102")

SqlSession session = sqlSessionFactory.openSession()

try{

IUserOperation userOperation = session.getMapper(IUserOperation.class)

session.commit()

System.out.println("当前增加的用户id为:"+user.getId())

}

finally{

session.close()

}

}

public static void main(String[] args) {

Test testUser = new Test()

testUser.addUser()

}

}

当执行到 testUser.addUser()后执行到session.commit()数据就会插入表。

oracle中用于保存位串的数据类型是RAW,LONG RAW(推荐使用BLOB)。

RAW,类似于CHAR,声明方式RAW(L),L为长度,以字节为单位,作为数据库列最大2000,作为变量最大32767字节。

LONG RAW,类似于LONG,作为数据库列最大存储2G字节的数据,作为变量最大32760字节

RAW类型的好处就是:在网络中的计算机之间传输 RAW 数据时,或者使用 Oracle 实用程序将 RAW 数据从一个数据库移到另一个数据库时,Oracle 服务器不执行字符集转换。存储实际列值所需要的字节数大小随每行大小而异,最多为 2,000 字节。可能这样的数据类型在数据库效率上会提高,而且对数据由于字符集的不同而导致的不一致的可能性在这边也排除了。

下面是官方的定义:

Note:

The LONG RAW datatype is provided for backward compatibility with existing applications. For new applications, use the BLOB and BFILEdatatypes for large amounts of binary data.

The RAW and LONG RAW datatypes are used for data that is not to be interpreted (not converted when moving data between different systems) by Oracle. These datatypes are intended for binary data or byte strings. For example, LONG RAW can be used to store graphics, sound, documents, or arrays of binary data. The interpretation depends on the use.

RAW is a variable-length datatype like the VARCHAR2 character datatype, except Oracle Net Services (which connects user sessions to the instance) and the Import and Export utilities do not perform character conversion when transmitting RAW or LONG RAW data. In contrast, Oracle Net Services and Import/Export automatically convertCHAR, VARCHAR2, and LONG data between the database character set and the user session character set (set by the NLS_LANGUAGE parameter of the ALTER SESSION statement), if the two character sets are different.

When Oracle automatically converts RAW or LONG RAW data to and from CHAR data, the binary data is represented in hexadecimal form with one hexadecimal character representing every four bits of RAW data. For example, one byte of RAW data with bits 11001011 is displayed and entered as 'CB.'

LONG RAW data cannot be indexed, but RAW data can be indexed.

常用于 *** 作raw类型的函数:UTL_RAW.CAST_TO_RAW,hextoraw.

RAW保存的为16进制数。当使用HEXTORAW时,会把字符串中数据当作16进制数。而使用UTL_RAW.CAST_TO_RAW时,直接把字符串中每个字符的ASCII码存放到RAW类型的字段中。

下面是常用到了两个函数:

utl_raw.cast_to_raw([varchar2])--将varchar2转换为raw类型

utl_raw.cast_to_varchar2([raw])--将raw转换为varchar2类型

这里varchar2的字符集一般是GB2312。

因为RAW保存的为16进制数。故下面的SQL会报错: insert into test_raw values (hextoraw('hh'))

insert into test_raw values (hextoraw('hh'))

*

ERROR 位于第 1 行:

ORA-01465: 无效的十六进制数字

raw类型数据列常用的位 *** 作:

utl_raw.bit_or()

utl_raw.bit_and()

utl_raw.bit_xor()


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9761030.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存