1. 在工作中很多时候需要执行一个SQL脚本文件到数据库中作为初始化数据;spring提供了一个工具类ScriptUtils,具体用法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@SpringBootTest class ExecuteSqlScriptApplicationTests { @Autowired private DataSource dataSource; @Test void contextLoads() throws SQLException, IOException { Resource classPathResource = new ClassPathResource("init/student.sql"); ScriptUtils.executeSqlScript(dataSource.getConnection(), classPathResource); } } |
2. 但是有时候我们的SQL脚本文件很大,甚至是几百mb,这样容易造成内存溢出的情况,因此我写了一个工具类,对SQL脚本进行拆解,然后批量执行。 这样每批量执行后,就清空缓存中的SQL,因此解决内存溢出问题。如下: 具体还没有用大数据量的脚本测试,等周一到公司再测试一下吧,哈哈哈。。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
@SpringBootTest class ExecuteSqlScriptApplicationTests { @Autowired private DataSource dataSource; @Test void contextLoads() throws SQLException, IOException { Resource classPathResource = new ClassPathResource("init/student.sql"); ScriptUtils.executeSqlScript(dataSource.getConnection(), classPathResource); // 分批处理SQL脚本 batchExecuteSql(classPathResource, 5); } /** * SQL脚本分解执行 * @param resource SQL脚本资源 * @param batchNumber 每多少条SQL执行一次 * @throws SQLException * @throws IOException */ public void batchExecuteSql(Resource resource, int batchNumber) throws SQLException, IOException { Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement(); BufferedReader bufferedReader = null; try { //获取字符缓冲流 bufferedReader = new BufferedReader(new InputStreamReader(resource.getInputStream())); int l; int i = 0; StringBuilder sql = new StringBuilder(); while ((l = bufferedReader.read()) != -1) { char read = (char) l; sql.append(read); if (read == ';') { // 一个完整的SQL语句 i ++; statement.addBatch(sql.toString()); if (i % batchNumber == 0) { System.out.println("每" + batchNumber + "条语句批量执行开始......"); statement.executeBatch(); statement.clearBatch(); System.out.println("每" + batchNumber + "条语句批量执行结束......"); } //清除StringBuilder中的SQL语句 sql.delete(0, sql.length()); } } if (i % batchNumber != 0) { System.out.println("执行最后不足" + batchNumber + "条语句开始!!!"); statement.executeBatch(); statement.clearBatch(); System.out.println("执行最后不足" + batchNumber + "条语句结束!!!"); } } finally { if (bufferedReader != null) { bufferedReader.close(); } if (connection != null) { connection.close(); } if (statement != null) { statement.close(); } } } } |
from:https://www.cnblogs.com/fangyan1994/p/14123592.html
View Details服务器:centos7 apollo:1.7 开发环境:springboot2.3 1.整合
1 2 3 4 5 6 7 8 9 10 |
<dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>1.7.0</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.10</version> </dependency> |
1 2 3 4 |
app.id=dev apollo.meta=http://192.167.2.123:8080 #eureka地址 apollo.bootstrap.enable=true apollo.bootstrap.namespaces=application |
1 2 3 4 |
@SpringBootApplication @EnableApolloConfig public class ApolloServerApplication { } |
2.启动时异常
1 2 3 4 5 6 7 8 |
Load config failed, will retry in 1 SECONDS. appId: dev, cluster: default, namespaces: application 2021-02-01 19:02:30.617 WARN 42252 --- [ main] c.c.f.a.i.AbstractConfigRepository : Sync config failed, will retry. Repository class com.ctrip.framework.apollo.internals.RemoteConfigRepository, reason: Load Apollo Config failed - appId: dev, cluster: default, namespace: application, url: http://192.168.1.26:8080/configs/dev/default/application?ip=192.168.1.2 [Cause: Could not complete get operation [Cause: connect timed out]] 2021-02-01 19:02:31.623 WARN 42252 --- [ main] c.c.f.a.i.RemoteConfigRepository : Load config failed, will retry in 1 SECONDS. appId: dev, cluster: default, namespaces: application 2021-02-01 19:02:33.624 WARN 42252 --- [ main] c.c.f.a.i.LocalFileConfigRepository : Sync config from upstream repository class com.ctrip.framework.apollo.internals.RemoteConfigRepository failed, reason: Load Apollo Config failed - appId: dev, cluster: default, namespace: application, url: http://192.168.1.26:8080/configs/dev/default/application?ip=192.168.1.2 [Cause: Could not complete get operation [Cause: connect timed out]] 2021-02-01 19:02:33.628 WARN 42252 --- [ngPollService-1] c.c.f.a.i.RemoteConfigLongPollService : Long polling failed, will retry in 1 seconds. appId: dev, cluster: default, namespaces: application, long polling url: http://192.168.1.26:8080/notifications/v2?cluster=default&appId=dev&ip=192.168.1.2¬ifications=%5B%7B%22namespaceName%22%3A%22application%22%2C%22notificationId%22%3A-1%7D%5D, reason: Could not complete get operation [Cause: connect timed out] 2021-02-01 19:02:34.625 WARN 42252 --- [ main] c.c.f.a.i.RemoteConfigRepository : Load config failed, will retry in 1 SECONDS. appId: dev, cluster: default, namespaces: application 2021-02-01 19:02:35.629 WARN 42252 --- [ngPollService-1] c.c.f.a.i.RemoteConfigLongPollService : Long polling failed, will retry in 2 seconds. appId: dev, cluster: default, namespaces: application, long polling url: http://192.168.1.26:8080/notifications/v2?cluster=default&appId=dev&ip=192.168.1.2¬ifications=%5B%7B%22namespaceName%22%3A%22application%22%2C%22notificationId%22%3A-1%7D%5D, reason: Could not complete get operation [Cause: connect timed out] 2021-02-01 19:02:36.625 WARN 42252 --- [ main] c.c.f.a.i.LocalFileConfigRepository : Sync config from upstream repository class com.ctrip.framework.apollo.internals.RemoteConfigRepository failed, reason: Load Apollo Config failed - appId: dev, cluster: default, namespace: application, url: http://192.168.1.26:8080/configs/dev/default/application?ip=192.168.1.2 [Cause: Could not complete get operation [Cause: connect timed out]] |
3.分析原因 http://192.168.1.26:8080 这个地址不是我eureka的地址,所以才找的时候会找不到 4.解决办法 启动的环境变量中加入: -Dapollo.configService=http://192.167.2.123:8080 将地址改为eureka的地址就好 ———————————————— 版权声明:本文为CSDN博主「weixin_38777065」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/weixin_38777065/article/details/113527235
View Details在Mybatis中,有一个强大的功能元素resultMap。当我们希望将JDBC ResultSets中的数据,转化为合理的Java对象时,你就能感受到它的非凡之处。正如其官方所述的那样:
resultMap元素是 MyBatis 中最重要最强大的元素。它可以让你从 90% 的 JDBC ResultSets 数据提取代码中解放出来,并在一些情形下允许你进行一些 JDBC 不支持的操作。实际上,在为一些比如连接的复杂语句编写映射代码的时候,一份 resultMap 能够代替实现同等功能的长达数千行的代码。ResultMap 的设计思想是,对于简单的语句根本不需要配置显式的结果映射,而对于复杂一点的语句只需要描述它们的关系就行了。
“30 * * * * ?” 每半分钟触发任务
“30 10 * * * ?” 每小时的10分30秒触发任务
“30 10 1 * * ?” 每天1点10分30秒触发任务
“30 10 1 20 * ?” 每月20号1点10分30秒触发任务
“30 10 1 20 10 ? *” 每年10月20号1点10分30秒触发任务
“30 10 1 20 10 ? 2011” 2011年10月20号1点10分30秒触发任务
“30 10 1 ? 10 * 2011” 2011年10月每天1点10分30秒触发任务
“30 10 1 ? 10 SUN 2011” 2011年10月每周日1点10分30秒触发任务
“15,30,45 * * * * ?” 每15秒,30秒,45秒时触发任务
“15-45 * * * * ?” 15到45秒内,每秒都触发任务
“15/5 * * * * ?” 每分钟的每15秒开始触发,每隔5秒触发一次
“15-30/5 * * * * ?” 每分钟的15秒到30秒之间开始触发,每隔5秒触发一次
“0 0/3 * * * ?” 每小时的第0分0秒开始,每三分钟触发一次
“0 15 10 ? * MON-FRI” 星期一到星期五的10点15分0秒触发任务
“0 15 10 L * ?” 每个月最后一天的10点15分0秒触发任务
“0 15 10 LW * ?” 每个月最后一个工作日的10点15分0秒触发任务
“0 15 10 ? * 5L” 每个月最后一个星期四的10点15分0秒触发任务
“0 15 10 ? * 5#3” 每个月第三周的星期四的10点15分0秒触发任务
MyBatis利用For循环批量插入
MyBatis的手动批量提交
MyBatis以集合方式批量新增
MyBatis-Plus提供的SaveBatch方法
MyBatis-Plus提供的InsertBatchSomeColumn方法
P2P 通信最大的障碍就是 NAT(网络地址转换),NAT 使得局域网内的设备也可以与公网进行通讯,但是不同 NAT 下的设备之间通讯将会变得很困难。UDP 打洞就是用来使得设备间绕过 NAT 进行通讯的一种技术。
View Details1、WebSocket是一种在单个TCP连接上进行全双工通信的协议。
2、WebSocket通信协议于2011年被IETF定为标准RFC 6455,并由RFC7936补充规范,WebSocket API也被W3C定为标准。
3、WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据。
4、在WebSocket API中,浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久性的连接,并进行双向数据传输。
* UDP服务器 * <p> UDP说明: * <p>UDP是无连接的通信协议,本质上不分服务端和客户端,两个段都可以对方当作服务器。 * <p> Java版本的可使用DatagramSocket接口实现,关键函数说明: * <p> .bind(): 表示监听,监听本机端口(任意定义),增加本地地址表明指明本地网卡; * <p> .send(): 表示发送数据,每个发送的数据包都要指明发送目的服务器地址和端口号; * <p> .receive(): 表示接收数据,每个接收数据包都要指明接收源服务器地址和端口号; * <> 笔记本开热点,就是新建独立网卡,组成局域网,局域网内设备可以相互通信,接收发送数据,发送方需要指明地址。 * <p>使用方法: * <p>1.笔记嗯电脑开启局域网: * <p>本机开启WiFi热点,在cmd中ipconfig, 查看本机热点IP,如下: * <p>2. 查看本机局域网IP: * <p>无线局域网适配器 本地连接* 10: * <p> * <p> 连接特定的 DNS 后缀 . . . . . . . : * <p> 本地链接 IPv6 地址. . . . . . . . : fe80::e4xx:93xx:47xx:xxx%4 * <p> IPv4 地址 . . . . . . . . . . . . : […]
View Details