首页 技术 正文
技术 2022年11月21日
0 收藏 926 点赞 3,565 浏览 2612 个字

插入数据的时候,往往需要获取主键值。但是有时候主键是自增长的那么,就不太适用手动添加主键值了,此时需要一种可以回显主键参数的方法,

下面以jdbc、mybatis的实现举例

此时使用的是jdbc的话或许能简单?too young too simple !

 package hellxz; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public class Jdbc {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rsKey = null;
try {
Class.forName("com.mysql.jdbc.Driver");
//使用&是为了避免框架中可能遇到的bug,养成好习惯
String url = "jdbc:mysql://localhost/article?characterEncoding=utf8&useSSL=false";
//获取连接
conn = DriverManager.getConnection(url,"root","root");
// 更改自动提交为false,保证事务完整性:要么全完成,要么全失败
conn.setAutoCommit(false);
String sql = "insert into article vaules(null,?)"; // 主键id自增可以插null
pstmt.setString(2, "name");
pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
rsKey = pstmt.getGeneratedKeys(); // 获取插入的主键值结果集
rsKey.next();
int rootId = rsKey.getInt(1); //拿到主键值
//打印看看
System.out.println(rootId);
pstmt.executeUpdate();
conn.commit();
} catch (Exception e) {
//捕获异常回滚数据
try {
conn.rollback();
} catch (SQLException e2) {
e2.printStackTrace();
}
e.printStackTrace();
} finally {
try {
//开启自动提交
conn.setAutoCommit(true);
//关闭连接
if (rsKey != null) {
rsKey.close();
rsKey = null;
}
if(pstmt != null){
pstmt.close();
pstmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e3) {
e3.printStackTrace();
}
}
}
}

那我们接下来看看mybatis,mybatis的配置文件这里就不多说了,我们需要改的只有一个地方:xxxMapper.xml (其中xxx代表你用到的mapper),但有两种修改方法

具体实现如下:

方法一: 使用useGeneratedKeys

 <insert id="insert" parameterType="com.taotao.pojo.TbContentCategory" useGeneratedKeys="true" keyProperty="id">
insert into tb_content_category (id, parent_id, name,
status, sort_order, is_parent,
created, updated)
values (#{id,jdbcType=BIGINT}, #{parentId,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR},
#{status,jdbcType=INTEGER}, #{sortOrder,jdbcType=INTEGER}, #{isParent,jdbcType=BIT},
#{created,jdbcType=TIMESTAMP}, #{updated,jdbcType=TIMESTAMP})
</insert>

方法二:在mapper.xml中加入一条同事务的查询语句:select last_insert_id;

 <insert id="insert" parameterType="com.taotao.pojo.TbContentCategory" >
<!-- keyProperty:主键pojo的名字,returnType:使用的是mybatis对java.lang.Long类型的别称
order:对应的是这条语句是在插入前查询还是之后 -->
<selectkey keyProperty="id" returnType="long" order="AFTER">
SELECT LAST_INSERT_ID
</selectKey>
insert into tb_content_category (id, parent_id, name,
status, sort_order, is_parent,
created, updated)
values (#{id,jdbcType=BIGINT}, #{parentId,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR},
#{status,jdbcType=INTEGER}, #{sortOrder,jdbcType=INTEGER}, #{isParent,jdbcType=BIT},
#{created,jdbcType=TIMESTAMP}, #{updated,jdbcType=TIMESTAMP})
</insert>

这样我们在程序里就可以插入完成后就直接取主键id就可以了

参考转载请写明出处:http://www.cnblogs.com/hellxz/p/mybatis_studynote.html

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