mybatis中怎样mybatis实现批量删除select

&&&&&&& oracle 实现在Mybatis中批量插入,下面测试可以使用,在批量插入中不能使用insert 标签,只能使用select标签进行批量插入,否则会提示错误### Cause: java.sql.SQLSyntaxErrorException: ORA-00933: SQL 命令未正确结束; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: ORA-00933: SQL 命令未正确结束以下正确的批量插入配置&?xml version=&1.0& encoding=&UTF-8&?&&!DOCTYPE mapper
PUBLIC &-//mybatis.org//DTD Mapper 3.0//EN&
&http://mybatis.org/dtd/mybatis-3-mapper.dtd&&&mapper namespace=&com.attence.attence.dao.AttenceDao&& &resultMap id=&Attence& type=&Attence&&
&id column=&ATTENCEKEY&
property=&attenceKey&/&
&result column=&EPC&
property=&epc&/&
&result column=&ANT&
property=&ant&/&
&result column=&RSSI&
property=&rssi&/&
&result column=&DEVICE&
property=&device&/&
&result column=&CRC&
property=&crc&/&
&result column=&BCC&
property=&bcc&/&
&result column=&DATETIME&
property=&dateTime& /& &/resultMap&
&select id=&insertAttence& parameterType=&List&&
INSERT ALL
&foreach collection=&list& item=&attence& index=&index& separator=&&&
into Attence(
attenceKey,
SEQ_ATTENCE.NEXTVAL,
#{attence.epc},
#{attence.ant},
#{attence.rssi},
#{attence.device},
#{attence.crc},
#{attence.bcc},
#{attence.dateTime}
&/foreach&
FROM dual &/select& &/mapper&& & & 由于使用oracle的自增加序列,无法联合union all使用,会提示错误。&?xml version=&1.0& encoding=&UTF-8&?&&!DOCTYPE mapper
PUBLIC &-//mybatis.org//DTD Mapper 3.0//EN&
&http://mybatis.org/dtd/mybatis-3-mapper.dtd&&&mapper namespace=&com.attence.attence.dao.AttenceDao&& &resultMap id=&Attence& type=&Attence&&
&id column=&ATTENCEKEY&
property=&attenceKey&/&
&result column=&EPC&
property=&epc&/&
&result column=&ANT&
property=&ant&/&
&result column=&RSSI&
property=&rssi&/&
&result column=&DEVICE&
property=&device&/&
&result column=&CRC&
property=&crc&/&
&result column=&BCC&
property=&bcc&/&
&result column=&DATETIME&
property=&dateTime& /& &/resultMap&
&insert id=&insertAttence& parameterType=&java.util.List&&
insert into Attence(
attenceKey,
&foreach collection=&list& item=&attence& index=&index& separator=&union all&&
SEQ_ATTENCE.NEXTVAL,
#{attence.epc},
#{attence.ant},
#{attence.rssi},
#{attence.device},
#{attence.crc},
#{attence.bcc},
#{attence.dateTime}
&/foreach&
&/insert&&/mapper&详解MyBatis直接执行SQL查询及数据批量插入
作者:izumi
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了MyBatis直接执行SQL查询及数据批量插入的相关知识,需要的朋友一起学习吧
一、直接执行SQL查询:
1、mappers文件节选
&resultMap id="AcModelResultMap" type="com.izumi.InstanceModel"&
&result column="instanceid" property="instanceID" jdbcType="VARCHAR" /&
&result column="instancename" property="instanceName" jdbcType="VARCHAR" /&
&/resultMap&
&select id="getInstanceModel" resultType="com.izumi.InstanceModel"&
${paramSQL}
2、DAO类节选
public interface SomeDAO{
List&InstanceModel& getInstanceModel(@Param("paramSQL")String sql);
3、注意事项
3.1:传入方法的参数sql必须遵循以下规范"select XXX as instanceid, XXX as instancename ....." ,否则MyBatis无法自动将查询结果变成Java对象。
3.2: mappers文件中的#{}语法与${}语法的区别:
默认情况下, #{}语法会促使MyBatis生成PreparedStatement属性并且使用PreparedStatement的参数(=?)来设置值。如果你想直接将未更改的字符串代入到sql中,可以使用${}。
也就是说,MyBatis看到 #{}会认为你在给sql中的变量赋值,就像JDBC编程中给问号赋值一样(比如MyBatis会判断它的类型,并自动在前后加单引号)。而当MyBatis看到${}的时候会直接将之替换成变量的值而不做任何处理。
所以在使用${}的时候,不需要像#{}一样写"jdbcType=VARCHAR"之类的属性。
3.3:resultType和resultMap
按照1中的写法, & resultMap & 部分可以删除不用了,因为在接下来的&select &中没用使用定义的resultMap,而是使用了resultType。
所以我们可以看出,关于&select &返回值的定义有两种写法,一种是定义一个resultMap然后引用这个resultMap,还有一种就是直接使用resultType指定一个类的路径。
二、批量插入数据
1、经验告诉我们,使用insert into XXX values(XX)(XXX)(XXX),比使用insert into XXX values(XX),insert into XXX values(XXX),insert into XXX values(XXX)效率要高。
2、在MyBatis中的用法
2.1、mappers文件节选
&insert id="insertBatch" & insert into student ( &include refid="Base_Column_List" /& ) values &foreach collection="list" item="item" index="index" separator=","& (null,#{item.name},#{item.sex},#{item.address},#{item.telephone},#{item.tId}) &/foreach&
2.2、DAO类节选
public interface SomeDAO{
public void insertBatch(@Param("list")List&Student& students);
详解mybatis批量插入数据
首先看看批处理的mapper.xml文件
&insert id="insertbatch" parameterType="java.util.List"&
&selectKey keyProperty="fetchTime" order="BEFORE"
resultType="java.lang.String"&
SELECT CURRENT_TIMESTAMP()
&/selectKey&
insert into kangaiduoyaodian ( depart1, depart2, product_name,
generic_name, img, product_specification, unit,
approval_certificate, manufacturer, marketPrice, vipPrice,
website, fetch_time, productdesc ) values
&foreach collection="list" item="item" index="index"
separator=","&
( #{item.depart1}, #{item.depart2}, #{item.productName},
#{item.genericName}, #{item.img},
#{item.productSpecification}, #{item.unit},
#{item.approvalCertificate}, #{item.manufacturer},
#{item.marketprice}, #{item.vipprice}, #{item.website},
#{fetchTime}, #{item.productdesc} )
&/foreach&
在批处理中,我发现有几个需要注意的问题
1、主键的自动获取,在insert中添加useGeneratedKeys=”true” keyProperty=”id”这两个属性无效,并且或中断数据插入,如果id是数据库自增的话,可以什么都不写,在插入的语句中去除主键属性,还有就是利用
&selectKey keyProperty="id" order="BEFORE"
resultType="java.lang.Integer"&
SELECT LAST_INSERT_ID()
&/selectKey&
注意 :&selectKey & 标签在insert下只能存在一个;批处理的时候不适合使用&selectKey &,主键自增最好,或者指定
2,插入时间的获取如上面所示,我用的是mysql,只要是mysql函数都可以拿来使用,插入时间和主键都是mysql函数中的一个。。。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具The page is temporarily unavailable
nginx error!
The page you are looking for is temporarily unavailable.
Please try again later.
Website Administrator
Something has triggered an error on your
This is the default error page for
nginx that is distributed with
It is located
/usr/share/nginx/html/50x.html
You should customize this error page for your own
site or edit the error_page directive in
the nginx configuration file
/etc/nginx/nginx.conf.所有回答(2)
那你是用namespace.id试试
也就是假设&select id="getTeacherById"&所在的xml文件的命名空间为A,那么就写作A.getTeacherById
我也超好奇这个问题!!
&&&您需要以后才能回答,未注册用户请先。MyBatis直接执行SQL查询及批量插入数据 - izumi - 博客园
趁还没忘掉,赶快记录下来
一、直接执行SQL查询:&1、mappers文件节选&resultMap&id="AcModelResultMap"&type="com.izumi.InstanceModel"&&&&result&column="instanceid"&property="instanceID"&jdbcType="VARCHAR"&/&&&&result&column="instancename"&property="instanceName"&jdbcType="VARCHAR"&/&&/resultMap&&select&id="getInstanceModel"&resultType="com.izumi.InstanceModel"&&&${paramSQL}&&/select&&2、DAO类节选public&interface&SomeDAO{&&List&InstanceModel&&getInstanceModel(@Param("paramSQL")String&sql);}&&3、注意事项&3.1:传入方法的参数sql必须遵循以下规范"select XXX as instanceid, XXX as instancename ....." ,否则MyBatis无法自动将查询结果变成Java对象。3.2: mappers文件中的#{}语法与${}语法的区别:& & 默认情况下, #{}语法会促使MyBatis生成PreparedStatement属性并且使用PreparedStatement的参数(=?)来设置值。如果你想直接将未更改的字符串代入到sql中,可以使用${}。& & 也就是说,MyBatis看到&#{}会认为你在给sql中的变量赋值,就像JDBC编程中给问号赋值一样(比如MyBatis会判断它的类型,并自动在前后加单引号)。而当MyBatis看到${}的时候会直接将之替换成变量的值而不做任何处理。所以在使用${}的时候,不需要像#{}一样写"jdbcType=VARCHAR"之类的属性。&3.3:resultType和resultMap按照1中的写法,&&&resultMap&&&部分可以删除不用了,因为在接下来的&select
&中没用使用定义的resultMap,而是使用了resultType。所以我们可以看出,关于&select&&返回值的定义有两种写法,一种是定义一个resultMap然后引用这个resultMap,还有一种就是直接使用resultType指定一个类的路径。二、批量插入数据1、经验告诉我们,使用insert into XXX values(XX)(XXX)(XXX),比使用insert into XXX values(XX),insert into XXX values(XXX),insert into XXX values(XXX)效率要高。2、在MyBatis中的用法&2.1、mappers文件节选&&insert id="insertBatch" &
insert into student ( &include refid="Base_Column_List" /& )
&foreach collection="list" item="item" index="index" separator=","&
(null,#{item.name},#{item.sex},#{item.address},#{item.telephone},#{item.tId})
&/foreach&&/insert&&&2.2、DAO类节选&public&interface&SomeDAO{ public void insertBatch(@Param("list")List&Student& students);&&}&参考:1、《MyBatis用户指南中文版》 译者:曾令祝&2、&&&
随笔 - 203

我要回帖

更多关于 mybatis select count 的文章

 

随机推荐