mybatis/Mysql常用命令

1.项目中很多批量插入使用for去逐个调用数据库,此方法会严重造成数据库连接瓶颈。采用mybatis的批插操作
<select id="batchSave" parameterType="java.util.List">
INSERT INTO TABLE_NAME(ID,NAME) VALUES
<foreach collection="list" item="itm" separator=",">
(#{itm.id},#{itm.name})
</foreach>
</select>
加入了foreach语句进行迭代,list是惯用写法,如果集合是array或者map可以替换成对应的。
此种写法与oralce的不尽相同,oracle方式可以采用PreparedStatement.executeBatch()的方式。
备注:首句必须是select关键字,与普通的insert语句区分开来。


2.插入时获得增长值。
<insert id="insertSelective" useGeneratedKeys="true" keyProperty="fileId" parameterType="fileAlias" >
insert into t_file_info ...
</insert>
配置了useGeneratedKeys关键字,以及映射对象的属性字段(keyProperty),插入完成后可以使用对象的类似getid方法获取插入sequence.
比较早期的用法如下。
<insert id="insert" parameterType="map">
insert into table1 (name) values (#{name})
<selectKey resultType="java.lang.Integer" keyProperty="id">
CALL IDENTITY()
</selectKey>
</insert>
需要写SelectKey语句

3.常看表各字段注释
show create table test;
或者
show full columns from test;

4.mybatis中对于String的参数,需要在Dao层添加注解,比如下面的
getAllItem(@Param("brand")String brand, @Param("model")String model)
如果不添加注解,则需要使用下标0或者1来使用,或者使用param1、param2来处理
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值