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来处理

猜你喜欢

转载自beck5859509.iteye.com/blog/2183819