瀏覽代碼

修改验证 mysql 批处理功能

zxy 2 年之前
父節點
當前提交
e07c03a307

+ 5 - 1
pom.xml

@@ -53,7 +53,6 @@
53
             <groupId>org.springframework</groupId>
53
             <groupId>org.springframework</groupId>
54
             <artifactId>spring-test</artifactId>
54
             <artifactId>spring-test</artifactId>
55
             <version>5.2.2.RELEASE</version>
55
             <version>5.2.2.RELEASE</version>
56
-
57
             <!--            <version></version>-->
56
             <!--            <version></version>-->
58
         </dependency>
57
         </dependency>
59
         <dependency>
58
         <dependency>
@@ -66,6 +65,11 @@
66
             <artifactId>fastjson</artifactId>
65
             <artifactId>fastjson</artifactId>
67
             <version>1.2.72</version>
66
             <version>1.2.72</version>
68
         </dependency>
67
         </dependency>
68
+        <dependency>
69
+            <groupId>org.projectlombok</groupId>
70
+            <artifactId>lombok</artifactId>
71
+            <version>1.18.20</version>
72
+        </dependency>
69
     </dependencies>
73
     </dependencies>
70
 
74
 
71
 </project>
75
 </project>

+ 4 - 0
springMvc/src/main/webapp/WEB-INF/web.xml

@@ -25,6 +25,9 @@
25
          xmlns="http://java.sun.com/xml/ns/j2ee"
25
          xmlns="http://java.sun.com/xml/ns/j2ee"
26
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
26
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
27
          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
27
          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
28
+  <welcome-file-list>
29
+    <welcome-file>index.jsp</welcome-file>
30
+  </welcome-file-list>
28
   <listener>
31
   <listener>
29
 <!--    <listener-class>com.listener.ContextLoadListener</listener-class>-->
32
 <!--    <listener-class>com.listener.ContextLoadListener</listener-class>-->
30
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
33
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
@@ -59,5 +62,6 @@
59
     <servlet-name>User</servlet-name>
62
     <servlet-name>User</servlet-name>
60
     <url-pattern>/user</url-pattern>
63
     <url-pattern>/user</url-pattern>
61
   </servlet-mapping>
64
   </servlet-mapping>
65
+
62
 </web-app>
66
 </web-app>
63
         
67
         

+ 16 - 0
springMvc/src/main/webapp/index.jsp

@@ -0,0 +1,16 @@
1
+<%--
2
+  Created by IntelliJ IDEA.
3
+  User: semsx
4
+  Date: 2022/5/12
5
+  Time: 上午9:47
6
+  To change this template use File | Settings | File Templates.
7
+--%>
8
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
9
+<html>
10
+<head>
11
+    <title>Title</title>
12
+</head>
13
+<body>
14
+<h1>hello world</h1>
15
+</body>
16
+</html>

+ 6 - 2
spring_annotation/src/main/java/AnnotationMain.java

@@ -1,8 +1,10 @@
1
+import com.Config.JdbcUtil;
1
 import com.Config.SpringConfiguration;
2
 import com.Config.SpringConfiguration;
2
 import com.alibaba.druid.pool.DruidDataSource;
3
 import com.alibaba.druid.pool.DruidDataSource;
3
 import com.alibaba.druid.pool.DruidPooledConnection;
4
 import com.alibaba.druid.pool.DruidPooledConnection;
4
 import com.service.JwtService;
5
 import com.service.JwtService;
5
 import com.service.StudentService;
6
 import com.service.StudentService;
7
+import com.service.UserService;
6
 import org.springframework.context.ApplicationContext;
8
 import org.springframework.context.ApplicationContext;
7
 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
9
 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
8
 
10
 
@@ -17,7 +19,9 @@ public class AnnotationMain {
17
         JwtService bean1 = applicationContext.getBean(JwtService.class);
19
         JwtService bean1 = applicationContext.getBean(JwtService.class);
18
         String s = bean1.generateToken("123");
20
         String s = bean1.generateToken("123");
19
         bean1.parseToken(s);
21
         bean1.parseToken(s);
20
-        DruidDataSource dataSource = (DruidDataSource) applicationContext.getBean("dataSource");
21
-        DruidPooledConnection connection = dataSource.getConnection();
22
+        UserService userService = applicationContext.getBean(UserService.class);
23
+        userService.addUserBatch();
24
+        System.out.println(JdbcUtil.URL);
25
+//        JdbcUtil bean2 = applicationContext.getBean(JdbcUtil.class);
22
     }
26
     }
23
 }
27
 }

+ 0 - 3
spring_annotation/src/main/java/Main.java

@@ -1,10 +1,7 @@
1
-import com.Config.JdbcData;
2
 import com.alibaba.druid.pool.DruidDataSource;
1
 import com.alibaba.druid.pool.DruidDataSource;
3
 import com.alibaba.druid.pool.DruidPooledConnection;
2
 import com.alibaba.druid.pool.DruidPooledConnection;
4
 import com.controller.UserController;
3
 import com.controller.UserController;
5
-import com.service.MqService;
6
 import com.service.UserService;
4
 import com.service.UserService;
7
-import com.service.impl.MqServiceImpl;
8
 import com.service.impl.UserServiceImpl;
5
 import com.service.impl.UserServiceImpl;
9
 import org.springframework.beans.factory.BeanFactory;
6
 import org.springframework.beans.factory.BeanFactory;
10
 import org.springframework.context.ApplicationContext;
7
 import org.springframework.context.ApplicationContext;

+ 0 - 10
spring_annotation/src/main/java/com/Config/JdbcData.java

@@ -1,10 +0,0 @@
1
-package com.Config;
2
-
3
-import org.springframework.beans.factory.annotation.Value;
4
-import org.springframework.stereotype.Component;
5
-
6
-@Component
7
-public class JdbcData {
8
-    @Value("${jdbc.url}")
9
-    private String url;
10
-}

+ 20 - 0
spring_annotation/src/main/java/com/Config/JdbcUtil.java

@@ -0,0 +1,20 @@
1
+package com.Config;
2
+
3
+import lombok.Getter;
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+import org.springframework.beans.factory.annotation.Value;
6
+import org.springframework.stereotype.Component;
7
+
8
+import javax.sql.DataSource;
9
+
10
+@Component
11
+public class JdbcUtil {
12
+    @Getter
13
+    public static String URL;
14
+    public static String driver;
15
+    @Value("${jdbc.url}")
16
+    public void setUrl(String url) {
17
+        URL = url;
18
+    }
19
+
20
+}

+ 1 - 0
spring_annotation/src/main/java/com/Config/SpringConfiguration.java

@@ -41,4 +41,5 @@ public class SpringConfiguration {
41
         return druidDataSource;
41
         return druidDataSource;
42
     }
42
     }
43
 
43
 
44
+
44
 }
45
 }

+ 2 - 2
spring_annotation/src/main/java/com/controller/UserController.java

@@ -1,6 +1,6 @@
1
 package com.controller;
1
 package com.controller;
2
 
2
 
3
-import com.Config.JdbcData;
3
+import com.Config.JdbcUtil;
4
 import org.springframework.beans.factory.annotation.Autowired;
4
 import org.springframework.beans.factory.annotation.Autowired;
5
 import org.springframework.beans.factory.annotation.Value;
5
 import org.springframework.beans.factory.annotation.Value;
6
 import org.springframework.stereotype.Controller;
6
 import org.springframework.stereotype.Controller;
@@ -8,7 +8,7 @@ import org.springframework.stereotype.Controller;
8
 @Controller
8
 @Controller
9
 public class UserController {
9
 public class UserController {
10
     @Autowired
10
     @Autowired
11
-    private JdbcData jdbcData;
11
+    private JdbcUtil jdbcData;
12
     @Value("zxy")
12
     @Value("zxy")
13
     private String name;
13
     private String name;
14
     @Value("1971")
14
     @Value("1971")

+ 1 - 0
spring_annotation/src/main/java/com/service/UserService.java

@@ -1,4 +1,5 @@
1
 package com.service;
1
 package com.service;
2
 
2
 
3
 public interface UserService {
3
 public interface UserService {
4
+    void addUserBatch();
4
 }
5
 }

+ 37 - 2
spring_annotation/src/main/java/com/service/impl/UserServiceImpl.java

@@ -1,10 +1,45 @@
1
 package com.service.impl;
1
 package com.service.impl;
2
 
2
 
3
 import com.service.UserService;
3
 import com.service.UserService;
4
+import org.springframework.beans.factory.annotation.Autowired;
4
 import org.springframework.stereotype.Service;
5
 import org.springframework.stereotype.Service;
5
 
6
 
7
+import javax.sql.DataSource;
8
+import java.sql.Connection;
9
+import java.sql.PreparedStatement;
10
+import java.sql.SQLException;
11
+
6
 @Service("UserServiceImpl")
12
 @Service("UserServiceImpl")
7
 public class UserServiceImpl implements UserService {
13
 public class UserServiceImpl implements UserService {
8
-
9
-
14
+    @Autowired
15
+    DataSource dataSource;
16
+    @Override
17
+    /**
18
+     * 使用批处理add
19
+     */
20
+    public void addUserBatch() {
21
+        try {
22
+            Connection connection = dataSource.getConnection();
23
+            String sql = "insert into wb_tbl_a (`company_id`) values(?)";
24
+            PreparedStatement preparedStatement = connection.prepareStatement(sql);
25
+            System.out.println("开始执行");
26
+            long start = System.currentTimeMillis();
27
+            for (int i = 0; i < 100000; i++){
28
+                preparedStatement.setInt(1, i);
29
+                //将sql 语句加入到批处理包中 -> 看源码
30
+                preparedStatement.addBatch();
31
+                //当有1000条记录时,在批量执行
32
+                if ((i + 1) % 1000 == 0){
33
+                    preparedStatement.executeBatch();
34
+                    //清空一把
35
+                    preparedStatement.clearBatch();
36
+                }
37
+            }
38
+            long end = System.currentTimeMillis();
39
+            System.out.println("批处理的sql耗时:" + (end - start));
40
+            connection.close();
41
+        } catch (SQLException e) {
42
+            e.printStackTrace();
43
+        }
44
+    }
10
 }
45
 }

+ 3 - 3
spring_annotation/src/main/resources/jdbc.properties

@@ -1,4 +1,4 @@
1
 jdbc.driver=com.mysql.cj.jdbc.Driver
1
 jdbc.driver=com.mysql.cj.jdbc.Driver
2
-jdbc.url=jdbc:mysql://47.110.156.18:3306/sczn_eshop_test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
3
-jdbc.username=sczn
4
-jdbc.password=sczn159357
2
+jdbc.url=jdbc:mysql://192.168.50.132:3306/test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
3
+jdbc.username=root
4
+jdbc.password=123456

+ 2 - 2
spring_annotation/src/test/java/CommonTest1.java

@@ -1,10 +1,10 @@
1
-import com.Config.JdbcData;
1
+import com.Config.JdbcUtil;
2
 import org.junit.Test;
2
 import org.junit.Test;
3
 import org.springframework.beans.factory.annotation.Autowired;
3
 import org.springframework.beans.factory.annotation.Autowired;
4
 
4
 
5
 public class CommonTest1 {
5
 public class CommonTest1 {
6
     @Autowired
6
     @Autowired
7
-    JdbcData jdbcData;
7
+    JdbcUtil jdbcData;
8
     @Test
8
     @Test
9
     public void testValue() {
9
     public void testValue() {
10
         System.out.println(jdbcData);
10
         System.out.println(jdbcData);