下面就是 insert,update 和 delete 语句的示例: <insert id="insertAuthor" parameterType="domain.blog.Author"> insert into Author (id,username,password,email,bio) values (#{id},#{username},#{password},#{email},#{bio}) </insert> 如前所述,插入语句…
@Insert("<script>INSERT INTO scp_activity_gift (activity_id,type,gift_id,status,limit_num,num) " + "VALUES " +" <foreach collection='items' item='item' separator=',' >" +"(#{item.activityId},#{item.type},#{it…
今天在写项目的时候,遇到一个需求,就是在像数据库插入数据的时候,要保留插入数据的主键,以便后续进行级联时,可以将该主键作为另张表的外键. 但是在正常情况下我们使用插入语句返回的是int型,含义是影响该表数据的条数.但是这时候我们想要的得到的却是主键,这时候就可以对mybatis文件进行配置 如图: 属性详解: parameterType ,入参的全限定类名或类型别名 useGeneratedKeys ,取值范围true|false(默认值), 设置是否使用JDBC的getGenereatedKe…
今天整合mybatis时候用到返回主键问题,批量插入总是返回不了主键还报错. 记录入下: pom版本: <mybatis.version>3.2.6</mybatis.version> mybatis.xml: <insert id="addUserBatch" useGeneratedKeys="true" keyProperty="id" parameterType="java.util.List&qu…
参考:mybatis添加记录时返回主键id 场景 有些时候我们在添加记录成功后希望能直接获取到该记录的主键id值,而不需要再执行一次查询操作.在使用mybatis作为ORM组件时,可以很方便地达到这个目的.鉴于mybatis目前已经支持xml配置和注解2种方式,所以分别给予详细介绍. 数据表设计: drop table if exists `test`; create table `test` ( `id` ) NOT NULL AUTO_INCREMENT COMMENT 'ID', // 主…
我们都知道Mybatis在插入单条数据的时候有两种方式返回自增主键: 1.对于支持生成自增主键的数据库:增加 useGenerateKeys和keyProperty ,<insert>标签属性. 2.不支持生成自增主键的数据库:使用<selectKey>. 但是怎么对批量插入数据返回自增主键的解决方式网上看到的还是比较少,至少百度的结果比较少. Mybatis官网资料提供如下: First, if your database supports auto-generated key…
IN查询 @Select({"<script> " + " select * "+ " from business_threat bt \n" + " join abnormal_event_type aet on bt.event_type_id = aet.id " + " where 1=1 " + " <if test = ' ids != null'> "…
映射配置文件  好947  <!-- type:映射实体类的数据类型 id:resultMap的唯一标识 -->  <resultMap type="person" id="BaseResultMap">   <!-- column:库表的字段名 property:实体类里的属性名 -->   <id column="person_id" property="personId" /&g…
insert 返回主键值 useGeneratedKeys=“true” parameterType=“USer” keyProperty=“id”, <insert id="insert" useGeneratedKeys="true" parameterType=“Car”  keyProperty="car.id"> INSERT INTO car(customer_id,car_no,car_brand_model,insur…
在使用MyBatis做持久层时,insert语句默认是不返回记录的主键值,而是返回插入的记录条数:如果业务层需要得到记录的主键时,可以通过Mapper.XML配置的方式来完成这个功能. 在 INSERT 标签 添加 useGeneratedKeys="true" keyProperty="id" 即可: <insert id="insertFeedback" useGeneratedKeys="true" keyProp…