首页 技术 正文
技术 2022年11月19日
0 收藏 726 点赞 4,640 浏览 4453 个字

mybatis+oracle 完成插入数据库,并将主键返回的注意事项
一条插入语句就踩了不少的坑,
首先我的建表语句是:

create table t_openapi_batch_info(
BATCH_NO VARCHAR2(200),
UM_CODE VARCHAR2(50),
BATCH_STATUS CHAR(1) DEFAULT ‘0’,
BATCH_TYPE CHAR(1),
CREATED_DATE DATE,
CREATED_BY VARCHAR(100),
UPDATED_DATE DATE
UPDATED_BY VARCHAR(100)

)

CREATE SEQUENCE SEQ_OPENAPI_BATCHNO
minvalue 0
maxvalue 999999999
start wuth 7342937
increate by 1
cache 40;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
我写的mapper.xml的sql语句为:

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE mapper PUBLIC “-//mybatis.org//DTD Mapper 3.0//EN” “http://mybatis.org/dtd/mybatis-3-mapper.dtd” >
<mapper>
<insert id=”insertBatchInfo” parameterType=”java.util.Map” useGeneratedKeys=”true” keyColumn =”batchNo”>
<selectKey resultType=”int” keyProperty=”batchNo” order=”BEFORE”>
select seq_openapi_batchno.nextval as batchNo from dual
</selectKey>
insert into t_openapi_batch_info
<include refid=”batchInfoKey”/>
VALUES
<include refid=”batchInfoVal”/>
</insert>

<sql id=”batchInfoKey”>
<trim prefix=”(” suffix=”)”>
batch_no,
<if test=”umCode!=null and umCode!=””>
um_code,
</if>
<if test=”batchStatus!=null and batchStatus!=””>
batch_status,
</if>
<if test=”batchType!=null and batchType!=””>
batch_type,
</if>
created_by,created_date,updated_by,updated_date
</trim>
</sql>

<sql id=”batchInfoVal”>
<trim prefix=”(” suffix=”)”>
#{batchNo},
<if test=”umCode!=null and umCode!=””>
#{umCode},
</if>
<if test=”batchStatus!=null and batchStatus!=””>
#{batchStatus},
</if>
<if test=”batchType!=null and batchType!=””>
#{batchType},
</if>
user,sysdate,user,sysdate
</trim>
</sql>

</mapper>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

截取上面mapper文件中的重要的部分,
1.使用useGeneratedkey,默认为false,设置为true可以将需要的值返回
2.keyColumn这个值可以指定你需要返回的值,比如我需要返回批次号,那么就可以指定keyColumn的值为batchNo,此时我可以将batchNo绑定到map,当然,如果你的参数类型是dto的话,就会绑定到对应实体类的属性上面,使用map.get(“batchNo”)就可以得到相应的值。
3.resultType=“int”,这里我踩得坑是将resultType写成了String类型**

/**
* 这个方法是对SqlSession的包装,对应insert、delete、update、select四种操作
*/
public Object execute(SqlSession sqlSession, Object[] args) {
Object result;//返回结果
   //INSERT操作
if (SqlCommandType.INSERT == command.getType()) {
//处理参数
Object param = method.convertArgsToSqlCommandParam(args);
//调用sqlSession的insert方法
result = rowCountResult(sqlSession.insert(command.getName(), param));
} else if (SqlCommandType.UPDATE == command.getType()) {
//UPDATE操作 同上
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.update(command.getName(), param));
} else if (SqlCommandType.DELETE == command.getType()) {
//DELETE操作 同上
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.delete(command.getName(), param));
} else if (SqlCommandType.SELECT == command.getType()) {
//如果返回void 并且参数有resultHandler ,则调用 void select(String statement, Object parameter, ResultHandler handler);方法
if (method.returnsVoid() && method.hasResultHandler()) {
executeWithResultHandler(sqlSession, args);
result = null;
} else if (method.returnsMany()) {
//如果返回多行结果,executeForMany这个方法调用 <E> List<E> selectList(String statement, Object parameter);
result = executeForMany(sqlSession, args);
} else if (method.returnsMap()) {
//如果返回类型是MAP 则调用executeForMap方法
result = executeForMap(sqlSession, args);
} else {
//否则就是查询单个对象
Object param = method.convertArgsToSqlCommandParam(args);
result = sqlSession.selectOne(command.getName(), param);
}
} else {
//接口方法没有和sql命令绑定
throw new BindingException(“Unknown execution method for: ” + command.getName());
}
//如果返回值为空 并且方法返回值类型是基础类型 并且不是VOID 则抛出异常
if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {
throw new BindingException(“Mapper method ‘” + command.getName()
+ ” attempted to return null from a method with a primitive return type (” + method.getReturnType() + “).”);
}
return result;
}

private Object rowCountResult(int rowCount) {
final Object result;
if (method.returnsVoid()) {
result = null;
} else if (Integer.class.equals(method.getReturnType()) || Integer.TYPE.equals(method.getReturnType())) {
result = rowCount;
} else if (Long.class.equals(method.getReturnType()) || Long.TYPE.equals(method.getReturnType())) {
result = (long) rowCount;
} else if (Boolean.class.equals(method.getReturnType()) || Boolean.TYPE.equals(method.getReturnType(http://www.my516.com))) {
result = (rowCount > 0);
} else {
throw new BindingException(“Mapper method ‘” + command.getName() + “‘ has an unsupported return type: ” + method.getReturnType());
}
return result;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
所以通过源码我们可以知道insert ,update,delete操作只能返回int,lang,boolean类型,若返回string类型,就会报错。

4.keyProperty 是 selectKey 语句结果应该被设置的目标属性。

SelectKey需要注意order属性,像Mysql一类支持自动增长类型的数据库中,order需要设置为after才会取到正确的值。

像Oracle这样取序列的情况,需要设置为before

,否则会报错。
———————

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,027
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,365
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,780
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,857