<!--简单SQL-->
insert into userinfo (USERID, USERNAME, AGE) values(1001,'小明',20);
<!--Mybatis写法1,有序列,主键是自增ID,主键是序列-->
<insert id="insert" parameterType="com.zznode.modules.bean.UserInfo">
<selectKey resultType="java.lang.Integer" order="BEFORE" keyProperty="userid">
SELECT userinfo_userid_seq.nextval as userid from dual
</selectKey>
insert into EPG_ALARM_INFO (USERID, USERNAME, AGE)
values (#{userid}, #{username}, #{age})
</insert>
<!--Mybatis写法2,无序列,主键是uuid,字符串-->
<insert id="insert" parameterType="com.zznode.modules.bean.UserInfo">
insert into EPG_ALARM_INFO (USERID, USERNAME, AGE, TIME)
values (#{userid}, #{username}, #{age}, sysdate)
</insert>
3. Der Minimalwert ist n
<!--简单SQL, 方法1--> INSERT ALL INTO userinfo (USERID, USERNAME, AGE) values(1001,'小明',20) INTO userinfo (USERID, USERNAME, AGE) values(1002,'小红',18) INTO userinfo (USERID, USERNAME, AGE) values(1003,'张三',23) select 3 from dual; <!--简单SQL, 方法2--> begin insert into userinfo (USERID, USERNAME, AGE) values(1001,'小明',20); insert into userinfo (USERID, USERNAME, AGE) values(1001,'小红',18); insert into userinfo (USERID, USERNAME, AGE) values(1001,'张三',23); end; <!--简单SQL, 方法3--> insert into userinfo (USERID, USERNAME, AGE) select 1001, '小明', 20 from dual union all select 1002, '小红', 18 from dual union all select 1003, '张三', 23 from dual
Service-Geschäftsimplementierung:
<!--Mybatis写法1,无序列--> <insert id="insertBatch" parameterType="java.util.List"> INSERT ALL <foreach collection="list" index="index" item="item"> INTO userinfo (USERID, USERNAME, AGE) VALUES (#{item.userid}, #{item.username}, #{item.age}) </foreach> select list.size from dual </insert> <!--Mybatis写法2,无序列--> <insert id="insertBatch"> insert into EPG_ALARM_INFO (USERID, USERNAME, AGE) <foreach collection="list" item="item" index="index" separator="union all"> <!-- <foreach collection="list" item="item" index="index" separator="union all" open="(" close=")"> --> <!-- (select #{item.userid}, #{item.username}, #{item.age} from dual) --> <!-- 上面带括号,下面不带括号,都可以,少量数据不带括号效率高 --> select #{item.userid}, #{item.username}, #{item.age} from dual </foreach> </insert> <!--Mybatis写法3,有序列--> <insert id="insertBatch"> insert into EPG_ALARM_INFO (USERID, USERNAME, AGE) SELECT userinfo_userid_seq.nextval, m.* FROM ( <foreach collection="list" item="item" index="index" separator="union all"> select #{item.username}, #{item.age} from dual </foreach> ) m </insert>
Mapper. Mapper.xml-Datei schreiben
<!-- create sequence 序列名 increment by 1 --每次增加几个,我这里是每次增加1 start with 1 --从1开始计数 nomaxvalue --不设置最大值 nocycle --一直累加,不循环 nocache; --不建缓冲区 在插入语句中调用:序列名.nextval 生成自增主键。 --> <!--创建序列--> create sequence SEQ_USERINFO minvalue 1 maxvalue 9999999999 start with 1 increment by 1 nocache; <!--删除序列--> drop sequence SEQ_USERINFO
Das obige ist der detaillierte Inhalt vonSo verwenden Sie Mybatis, um Oracle-Batch-Einfüge- und Paging-Abfragen basierend auf Java zu implementieren. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!