接口方法:
public interface PlayerDao {
int insertOnePlayer(Player player);
int insertOnePlayer2(Player player);
}
public void testInsertGenerateId1() throws IOException {
// 2.获取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 3.获取对应mapper
PlayerDao mapper = sqlSession.getMapper(PlayerDao.class);
// 4.执行查询语句并返回结果
Player player = new Player();
player.setPlayName("Allen Iverson");
player.setPlayNo(3);
player.setTeam("76ers");
player.setHeight(1.83F);
mapper.insertOnePlayer(player);
sqlSession.commit();
System.out.println(player.getId());
}
Mapper文件:
<insert id="insertOnePlayer" parameterType="Player" useGeneratedKeys="true" keyProperty="id">
insert into tb_player (id, playName, playNo,team, height)
values (
#{id,jdbcType=INTEGER},
#{playName,jdbcType=VARCHAR},
#{playNo,jdbcType=INTEGER},
#{team,jdbcType=VARCHAR},
#{height,jdbcType=DECIMAL}
)
</insert>
方式一配置:useGeneratedKeys=“true” keyProperty=“id”
即可
public void testInsertGenerateId2() throws IOException {
// 2.获取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 3.获取对应mapper
PlayerDao mapper = sqlSession.getMapper(PlayerDao.class);
// 4.执行查询语句并返回结果
Player player = new Player();
player.setPlayName("Tony Parker");
player.setPlayNo(9);
player.setTeam("spurs");
player.setHeight(1.88F);
mapper.insertOnePlayer2(player);
sqlSession.commit();
System.out.println(player.getId());
}
Mapper文件:
<insert id="insertOnePlayer2" parameterType="Player">
<selectKey keyProperty="id" order="AFTER" resultType="int">
select LAST_INSERT_ID()
</selectKey>
insert into tb_player (id, playName, playNo,team, height)
values (
#{id,jdbcType=INTEGER},
#{playName,jdbcType=VARCHAR},
#{playNo,jdbcType=INTEGER},
#{team,jdbcType=VARCHAR},
#{height,jdbcType=DECIMAL}
)
</insert>
方式二通过 selectKey 标签完成
,selectKey 更加灵活,支持一定程度的自定义
Java文件省略了,这里直接给出Mapper文件, Mapper 文件如下,其实就是:useGeneratedKeys=“true” keyProperty=“id”,其中id是JavaBean的主键id
<insert id="insertBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
INSERT INTO partition_info (id, node_ip_id, init_schema_info_id,
prefix_table_index, partition_num, start_time,
end_time, create_time, is_delete
)
values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.id,jdbcType=INTEGER}, #{item.nodeIpId,jdbcType=INTEGER}, #{item.initSchemaInfoId,jdbcType=INTEGER},
#{item.prefixTableIndex,jdbcType=VARCHAR}, #{item.partitionNum,jdbcType=VARCHAR}, #{item.startTime,jdbcType=TIMESTAMP},
#{item.endTime,jdbcType=TIMESTAMP}, #{item.createTime,jdbcType=TIMESTAMP}, #{item.isDelete,jdbcType=TINYINT}
)
</foreach>
</insert>
Java代码
System.out.println("before insert ...");
for (PartitionInfo p: list) {
System.out.println(p);
}
PartitionInfoMapper mapper = sqlSession.getMapper(PartitionInfoMapper.class);
int i = mapper.insertBatch(list);
System.out.println("The rows be affected :" + i);
System.out.println("after insert ...");
for (PartitionInfo p: list) {
System.out.println(p);
}
输出
before insert ...
PartitionInfo(id=null, nodeIpId=1, initSchemaInfoId=1, prefixTableIndex=1, partitionNum=null, startTime=null, endTime=null, createTime=Fri Dec 13 18:26:46 CST 2019, isDelete=null)
PartitionInfo(id=null, nodeIpId=2, initSchemaInfoId=2, prefixTableIndex=2, partitionNum=null, startTime=null, endTime=null, createTime=Fri Dec 13 18:26:46 CST 2019, isDelete=null)
PartitionInfo(id=null, nodeIpId=3, initSchemaInfoId=3, prefixTableIndex=3, partitionNum=null, startTime=null, endTime=null, createTime=Fri Dec 13 18:26:46 CST 2019, isDelete=null)
The rows be affected :3
after insert ...
PartitionInfo(id=701, nodeIpId=1, initSchemaInfoId=1, prefixTableIndex=1, partitionNum=null, startTime=null, endTime=null, createTime=Fri Dec 13 18:26:46 CST 2019, isDelete=null)
PartitionInfo(id=702, nodeIpId=2, initSchemaInfoId=2, prefixTableIndex=2, partitionNum=null, startTime=null, endTime=null, createTime=Fri Dec 13 18:26:46 CST 2019, isDelete=null)
PartitionInfo(id=703, nodeIpId=3, initSchemaInfoId=3, prefixTableIndex=3, partitionNum=null, startTime=null, endTime=null, createTime=Fri Dec 13 18:26:46 CST 2019, isDelete=null)
这里其他的代码都省略了,基本上就是: useGeneratedKeys=“true” keyProperty=“id” 这两个标签起作用
另外我用的mybatis版本是 3.4.1
注意Mapper文件中的 insert into tb_player (id, playName, playNo,team, height),这里不要多了一个逗号,之前height后面还有一个逗号导致一直空指针的错误。
————————————————
版权声明:本文为CSDN博主「惑边」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/my_momo_csdn/article/details/88535993
文章浏览阅读1.3k次。斯蒂夫•保罗•乔布斯(Steve Paul Jobs,1955年2月24日出生-)是蘋果電腦的現任首席執行長(首席执行官)兼創辦人之一。同時也是Pixar動畫公司的董事長及首席執行長。这是他2005在斯坦福大学做的毕业演讲。。。很鼓舞人。。。也许精彩就在平实之间。。。Thank you.I'm honored to be with you today for your commenceme
文章浏览阅读377次,点赞4次,收藏4次。探索 grunt-electron-installer:构建高效、自定义化的 Electron 应用安装器项目地址:https://gitcode.com/electron-archive/grunt-electron-installer在开发基于 Electron 的跨平台桌面应用时,一个整洁、易于使用的安装程序是用户体验的重要组成部分。grunt-electron-installer 是一...
文章浏览阅读297次。[转]手机游戏测试精华版上一篇 /下一篇 2013-05-31 15:23:44/ 个人分类:经验之谈手机游戏测试一、游戏的硬性BUG,此BUG不光是指一些游戏中出现的死机或者脚本错误之类的直接导致游戏无法运行下去的BUG,还包括那些字体出格,错字,来电没声音之类的,虽然不能导致游戏无法运行,但是错误明显需要必须改正的错误。二、勇于提出你所认为的瑕疵。瑕疵这些属于一些对别人来说无关紧要的事情。但..._检测手机游戏
文章浏览阅读1.6k次,点赞3次,收藏22次。此代码提供了以下几个功能: - 提取某个PDF中的全部文字和全部表格并输出 - 提取某个PDF中全部的图片并依序输出 - 提取某个PDF中某页的文字和表格并输出_python图片表格提取
文章浏览阅读714次。A - 18岁生日Gardon的18岁生日就要到了,他当然很开心,可是他突然想到一个问题,是不是每个人从出生开始,到达18岁生日时所经过的天数都是一样的呢?似乎并不全都是这样,所以他想请你帮忙计算一下他和他的几个朋友从出生到达18岁生日所经过的总天数,让他好来比较一下。 Input一个数T,后面T行每行有一个日期,格式是YYYY-MM-DD。如我的生日是1988-03-07。_g最近想给女友送两个精美的小礼品:两个底面半径分别为r1和r2的圆柱形宝石,并想装
文章浏览阅读2k次。HUAWEI华为笔记本平板电脑MateBook E 2019款 高通850版8GB+256GB(PAK-AL09)原装出厂WIN10ARM系统恢复原厂系统1809。_pak-al09 怎么装系统
文章浏览阅读1.2w次,点赞8次,收藏16次。原因一:keras的后端是theano,默认channels_first,即他的图像形状是input_shape=(img_channels,img_rows, img_cols )。而在tensorflow中则是默认channels_last,即input_shape=(img_rows, img_cols, img_channels)。解决方法:可添加这两行代码,使其变为ch..._valueerror: negative dimension size caused by subtracting 3 from 2 for '{{no
文章浏览阅读1.8k次。在Linux中自动运行程序有以下几种方式:1.开机启动时自动运行程序 Linux加载后, 它将初始化硬件和设备驱动, 然后运行第一个进程init。init根据配置文件继续引导过程,启动其它进程。通常情况下,修改放置在 /etc/rc或 /etc/rc.d 或 /etc/rc?.d 目录下的脚本文件,可以使init自动启动其它程序。例如:编辑 /etc/rc.d/rc.local 文件,在文件
文章浏览阅读1.9k次,点赞2次,收藏10次。进来踩坑吧_bootstrap怎么全局加载
文章浏览阅读808次。Spring:AOP面向方面编程、IoC控制反转、事务、Bean是构成应用进程主干、由IoC容器创建的对象。_j2ee和ssm
文章浏览阅读2.1k次。1x1卷积核虽然不能进行上/下采样,但在CNN中仍有很重要的作用1x1卷积核的作用升维、降维首先一个很简单的例子,feature map 大小为16x16 channel = 8,通过一个1x1 数量为4的卷积核,得到的输出就为16x16x4降维最明显的影响就是增加了网络的深度,但是并不会增加网络的参数量,通过不同大小卷积核的组合,同时也可以大幅度减小计算量输入56 * 56 * 192,使用5 * 5 * 32的卷积核。最终的计算量是:56 * 56 * 32 * 5 * 5 * 192 _如何扩展卷积核使其尺寸为16*16
文章浏览阅读1.1k次。webpack打包,突然报错this.getOptions is not a function?是不是像这种报错?是不是偷偷下载loader了?这种问题一般就是该loader版本过高了,可以换个低版本的,可以去npm里面找找该loader的低版本,反正我都是这么解决的写这篇文章,目的是为了纪念因为这个错误纠结好久的日子,顺道希望可以帮到你,要是还解决不了,可以联系作者..._webpack this.getoptions is not a function