我们在做数据插入和数据更新的时候,业务产生的日志数据有好几万百万,那么正常的插入语句已性能弱,mybatis提供了实现大数据插入数据表的方法,下面我们就来实现一个例子。

1.引入mybatis的依赖jar

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>

2.配置mybatis

mybatis:
typeAliasesPackage: com.xdd.entity
mapperLocations: classpath:mapper/*.xml,classpath*:com/cloud/dataplatformbronto/dao/*Mapper.xml

3.创建 BrontoDataDao

import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface BrontoDataDao {
int insertBatchUserChannelInfo(List<BrontoChannelActiveBean> list);
int batchUpdate(Map<?,?> map);
}

4.创建BrontoDataMapper.xml

<?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.sengled.cloud.dataplatformbronto.dao.BrontoDataDao" >
<insert id="insertBatchUserChannelInfo" parameterType="java.util.List" useGeneratedKeys="false">
INSERT INTO dp_bronto_channel_active
(customer_id,channel_name,active_status,channel_type,area_info)
values
<foreach collection="list" item="item" index="index" separator=",">
(
#{item.customerId},
#{item.channelName},
#{item.activeStatus},
#{item.channelType},
#{item.areaInfo}
)
</foreach>
</insert> <!-- 批量更新第一种方法,通过接收传进来的参数list进行循环着组装sql -->
<update id="batchUpdate" parameterType="java.util.Map">
<!-- 接收list参数,循环着组装sql语句,注意for循环的写法
separator=";" 代表着每次循环完,在sql后面放一个分号
item="cus" 循环List的每条的结果集
collection="list" list 即为 map传过来的参数key -->
<foreach collection="list" separator=";" item="cus">
update t_customer set
c_name = #{cus.name},
c_age = #{cus.age},
c_sex = #{cus.sex},
c_ceroNo = #{cus.ceroNo},
c_ceroType = #{cus.ceroType}
where id = #{cus.id}
</foreach>
<!--<foreach collection="attendingUsrList" item="model" separator=";">-->
<!--UPDATE parties SET attending_user_count = #{model.attending_count}-->
<!--WHERE fb_party_id = #{model.eid}-->
<!--</foreach>-->
</update> </mapper>

5.编写业务方法

@Service("brontoDataService")
@Transactional
public class BrontoDataServiceImp implements IBrontoDataService{ private static final int BATCH_SIZE = 5000; private Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired
private BrontoDataDao brontoDataDao;
@Autowired
SqlSessionFactory sqlSessionFactory;
/**
* 用户渠道信息大批量数据插入
*@param: [brontoDeviceModelList]
**/
@Transactional
public void saveUserChannelInfo(List<BrontoChannelActiveBean> brontoDeviceModelList) {
int groupNo = brontoDeviceModelList.size() / BATCH_SIZE;
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
BrontoDataDao mapper = sqlSession.getMapper(BrontoDataDao.class);
if (brontoDeviceModelList.size() <= BATCH_SIZE) {
mapper.insertBatchUserChannelInfo(brontoDeviceModelList);
} else {
List<BrontoChannelActiveBean> subList = null;
for (int i = 0; i < groupNo; i++) {
subList = brontoDeviceModelList.subList(0, BATCH_SIZE);
mapper.insertBatchUserChannelInfo(subList);
brontoDeviceModelList.subList(0, BATCH_SIZE).clear();
}
if (brontoDeviceModelList.size() > 0) {
mapper.insertBatchUserChannelInfo(brontoDeviceModelList);
}
}
sqlSession.flushStatements();
}
}

需要测试功能再写一个测试类或测试接口来测试!

附录XML CDATA(Mybatis mapper and XML)

Tip:must be followed by either attribute specifications, ">" or "/>".

所有 XML 文档中的文本均会被解析器解析。

只有 CDATA 区段(CDATA section)中的文本会被解析器忽略。

PCDATA

PCDATA 指的是被解析的字符数据(Parsed Character Data)。

XML 解析器通常会解析 XML 文档中所有的文本。

当某个 XML 元素被解析时,其标签之间的文本也会被解析:

<message>此文本也会被解析</message>

解析器之所以这么做是因为 XML 元素可包含其他元素,就像这个例子中,其中的 <name> 元素包含着另外的两个元素(first 和 last):

<name><first>Bill</first><last>Gates</last></name>

而解析器会把它分解为像这样的子元素:

<name>
<first>Bill</first>
<last>Gates</last>
</name>

转义字符

非法的 XML 字符必须被替换为实体引用(entity reference)。

假如您在 XML 文档中放置了一个类似 "<" 字符,那么这个文档会产生一个错误,这是因为解析器会把它解释为新元素的开始。因此你不能这样写:

<message>if salary < 1000 then</message>

为了避免此类错误,需要把字符 "<" 替换为实体引用,就像这样:

<message>if salary &lt; 1000 then</message>

在 XML 中有 5 个预定义的实体引用:

&lt; < 小于
&gt; > 大于
&amp; & 和号
&apos; ' 省略号
&quot; " 引号

注释:严格地讲,在 XML 中仅有字符 "<"和"&" 是非法的。省略号、引号和大于号是合法的,但是把它们替换为实体引用是个好的习惯。

CDATA

术语 CDATA 指的是不应由 XML 解析器进行解析的文本数据(Unparsed Character Data)。

在 XML 元素中,"<" 和 "&" 是非法的。

"<" 会产生错误,因为解析器会把该字符解释为新元素的开始。

"&" 也会产生错误,因为解析器会把该字符解释为字符实体的开始。

某些文本,比如 JavaScript 代码,包含大量 "<" 或 "&" 字符。为了避免错误,可以将脚本代码定义为 CDATA。

CDATA 部分中的所有内容都会被解析器忽略。

CDATA 部分由 "<![CDATA[" 开始,由 "]]>" 结束:

<script>
<![CDATA[
function matchwo(a,b)
{
if (a < b && a < 0) then
{
return 1;
}
else
{
return 0;
}
}
]]>
</script>

在上面的例子中,解析器会忽略 CDATA 部分中的所有内容。

关于 CDATA 部分的注释:

CDATA 部分不能包含字符串 "]]>"。也不允许嵌套的 CDATA 部分。

标记 CDATA 部分结尾的 "]]>" 不能包含空格或折行。

附录部分摘自:http://www.w3school.com.cn/xml/xml_cdata.asp

XML CDATA(Mybatis mapper and XML)的更多相关文章

  1. MyBatis Mapper.xml文件中 $和#的区别

    MyBatis Mapper.xml文件中 $和#的区别   网上有很多,总之,简略的写一下,作为备忘.例子中假设参数名为 paramName,类型为 VARCHAR . 1.优先使用#{paramN ...

  2. mybatis mapper.xml 配置文件问题(有的错误xml是不报的) 导致服务无法启动 。

    转载自 开源编程 一舟mybatsi xml编译报错,tomcat启动一直循环,导致内存溢出,启动失败 mapper.xml怎么知道有没有编译错误,哪个位置有错误 这应该是mybatis的一个bug, ...

  3. mybatis mapper.xml 写关联查询 运用 resultmap 结果集中 用 association 关联其他表 并且 用 association 的 select 查询值 报错 java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for mybatis.map

    用mybaits 写一个关联查询 查询商品表关联商品规格表,并查询规格表中的数量.价格等,为了sql重用性,利用 association 节点 查询 结果并赋值报错 商品表的mapper文件为Gooo ...

  4. Mybatis Mapper.java和Mapper.xml能否分离问题

    Mybatis Mapper.java和Mapper.xml是能分离的. 从图上不难看出,不管是/java还是/resources,他们最终编译后的存放路径均是/target/classes 因此将x ...

  5. Java DB 访问之 mybatis mapper xml 配置方式

    1 项目说明 项目采用 maven 组织 ,jdbc 唯一的依赖就是 mysql-connector-java pom 依赖如下: mysql 数据连接 : mysql-connector-java ...

  6. mybatis mapper xml文件的导入方式和查询方式

    mybatis mapper xml文件的导入方式和查询方式 ssm框架 Mybatis  mapper与SQLSession的关系 每个基于MyBatis的应用都是以一个SqlSessionFact ...

  7. MyBatis——Mapper XML 文件

    Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会 ...

  8. mybatis Mapper.xml和Mapper.java

    mybatis Mapper.xml和Mapper.java 通过Mapper.xml和Mapper.java来实现mybatis.环境和入门的一样的.关键:Mapper.xml + Mapper.j ...

  9. mybatis mapper xml文件配置resultmap时,id行和result行有什么区别?

    mybatis mapper xml文件配置resultmap时,id行和result行有什么区别? <resultMap id = "CashInvoiceMap" typ ...

随机推荐

  1. Unity 之 Redux 模式(第一篇)—— 人物移动

    作者:软件猫 日期:2016年12月6日 转载请注明出处:http://www.cnblogs.com/softcat/p/6135195.html 在朋友的怂恿下,终于开始学 Unity 了,于是有 ...

  2. C#接口的使用【转】

    我们定义一个接口public interface IBark{   void Bark();}再定义一个类,继承于IBark,并且必需实现其中的Bark()方法public class Dog:IBa ...

  3. ARM64调试环境

    自从上一次ZCTF做了一道ARM64的逆向题目后,我决定记录下利用qemu搭建ARM64的环境的过程,以后肯定会遇到更多ARM平台下的Reverse和PWN. 一 安装QEMU 我要模拟的是64位的A ...

  4. RadioButtonList选择事件onclick

    <asp:RadioButtonList ID="rbtnFInvoiceType" runat="server" onclick="check ...

  5. JS实现简单倒计时

    /*倒计时*/ lcf.downTime = function (endTime,obj,callback){ /*基本判断*/ if(!endTime || typeof endTime !== & ...

  6. C# 根据年月获得此月第一天和最后一天,并计算工作日

    string str = "2015年3月"; ); ); , secondIndex - firstIndex - ); , ); DateTime dt = DateTime. ...

  7. SQL Server 2008 R2如何开启数据库的远程连接

    SQL Server 2008 R2如何开启数据库的远程连接 SQL Server 2005以上版本默认是不允许远程连接的,如果想要在本地用SSMS连接远程服务器上的SQL Server 2008,远 ...

  8. HDU 1017 - A Mathematical Curiosity

    题目简单,格式酸爽.. #include <iostream> using namespace std; int ans,n,m,p; int main() { cin>>p; ...

  9. 【衡阳八中noip模拟题】国色天香

    庭前芍药妖无格,池上芙蕖净少情.唯有牡丹真国色,花开时节动京城.——唐·刘禹锡<赏牡丹>芍药花再红终究妖艳无格.终不及牡丹,国色天香.——乌拉那拉氏宜修华妃总是想要用自己的气焰打压皇后,正 ...

  10. Git教程--Git安装和版本库的创建

    Git的诞生 很多人都知道,Linus在1991年创建了开源的Linux,从此,Linux系统不断发展,已经成为最大的服务器系统软件了. Linus虽然创建了Linux,但Linux的壮大是靠全世界热 ...