{"id":10008151,"date":"2023-08-23T18:04:07","date_gmt":"2023-08-23T10:04:07","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008151"},"modified":"2023-08-23T18:04:07","modified_gmt":"2023-08-23T10:04:07","slug":"13-%e7%a7%92%e6%8f%92%e5%85%a5-30-%e4%b8%87%e6%9d%a1%e6%95%b0%e6%8d%ae%ef%bc%8c%e6%88%91%e6%83%8a%e5%91%86%e4%ba%86%ef%bc%81","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008151","title":{"rendered":"13 \u79d2\u63d2\u5165 30 \u4e07\u6761\u6570\u636e\uff0c\u6211\u60ca\u5446\u4e86\uff01"},"content":{"rendered":"<p>\u672c\u6587\u4e3b\u8981\u8bb2\u8ff0\u901a\u8fc7MyBatis\u3001JDBC\u7b49\u505a\u5927\u6570\u636e\u91cf\u6570\u636e\u63d2\u5165\u7684\u6848\u4f8b\u548c\u7ed3\u679c\u3002<\/p>\n<h2>30\u4e07\u6761\u6570\u636e\u63d2\u5165\u63d2\u5165\u6570\u636e\u5e93\u9a8c\u8bc1<\/h2>\n<ul>\n<li>\u5b9e\u4f53\u7c7b\u3001mapper\u548c\u914d\u7f6e\u6587\u4ef6\u5b9a\u4e49<\/li>\n<li>\n<ul>\n<li>User\u5b9e\u4f53<\/li>\n<li>mapper\u63a5\u53e3<\/li>\n<li>mapper.xml\u6587\u4ef6<\/li>\n<li>jdbc.properties<\/li>\n<li>sqlMapConfig.xml<\/li>\n<\/ul>\n<\/li>\n<li>\u4e0d\u5206\u6279\u6b21\u76f4\u63a5\u68ad\u54c8<\/li>\n<li>\u5faa\u73af\u9010\u6761\u63d2\u5165<\/li>\n<li>MyBatis\u5b9e\u73b0\u63d2\u516530\u4e07\u6761\u6570\u636e<\/li>\n<li>JDBC\u5b9e\u73b0\u63d2\u516530\u4e07\u6761\u6570\u636e<\/li>\n<li>\u603b\u7ed3<\/li>\n<\/ul>\n<p>\u9a8c\u8bc1\u7684\u6570\u636e\u5e93\u8868\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<pre><code>CREATE TABLE `t_user` (\n  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '\u7528\u6237id',\n  `username` varchar(64) DEFAULT NULL COMMENT '\u7528\u6237\u540d\u79f0',\n  `age` int(4) DEFAULT NULL COMMENT '\u5e74\u9f84',\n  PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='\u7528\u6237\u4fe1\u606f\u8868';<\/code><\/pre>\n<p>\u8bdd\u4e0d\u591a\u8bf4\uff0c\u5f00\u6574\uff01<\/p>\n<p>\u63a8\u8350\u4e00\u4e2a\u5f00\u6e90\u514d\u8d39\u7684 Spring Boot \u5b9e\u6218\u9879\u76ee\uff1a<\/p>\n<blockquote><p><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=V8lI0GBkmo%2FRS8l9ixfvZQ%3D%3D.jZv4AK0Ps34iD85tQ%2FfkmEjJVgxvvoTpkjccuHeTPNZTYC9TaDwmYSeAM2htx5lkkglyQVnz8L4U3eeatIZgLw%3D%3D\" rel=\"noopener noreferrer\">https:\/\/github.com\/javastacks\/spring-boot-best-practice<\/a><\/p><\/blockquote>\n<h3>\u5b9e\u4f53\u7c7b\u3001mapper\u548c\u914d\u7f6e\u6587\u4ef6\u5b9a\u4e49<\/h3>\n<p>User\u5b9e\u4f53<\/p>\n<pre><code>\/**\n * &lt;p&gt;\u7528\u6237\u5b9e\u4f53&lt;\/p&gt;\n *\n * @Author zjq\n *\/\n@Data\npublic class User {\n\n    private int id;\n    private String username;\n    private int age;\n\n}<\/code><\/pre>\n<p>mapper\u63a5\u53e3<\/p>\n<pre><code>public interface UserMapper {\n\n    \/**\n     * \u6279\u91cf\u63d2\u5165\u7528\u6237\n     * @param userList\n     *\/\n    void batchInsertUser(@Param(&quot;list&quot;) List&lt;User&gt; userList);\n\n}<\/code><\/pre>\n<p>mapper.xml\u6587\u4ef6<\/p>\n<pre><code>&lt;!-- \u6279\u91cf\u63d2\u5165\u7528\u6237\u4fe1\u606f --&gt;\n&lt;insert id=&quot;batchInsertUser&quot; parameterType=&quot;java.util.List&quot;&gt;\n    insert into t_user(username,age) values\n    &lt;foreach collection=&quot;list&quot; item=&quot;item&quot; index=&quot;index&quot; separator=&quot;,&quot;&gt;\n        (\n        #{item.username},\n        #{item.age}\n        )\n    &lt;\/foreach&gt;\n&lt;\/insert&gt;<\/code><\/pre>\n<p>jdbc.properties<\/p>\n<pre><code>jdbc.driver=com.mysql.jdbc.Driver\njdbc.url=jdbc:mysql:\/\/localhost:3306\/test\njdbc.username=root\njdbc.password=root<\/code><\/pre>\n<p>sqlMapConfig.xml<\/p>\n<pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;\n&lt;!DOCTYPE configuration PUBLIC &quot;-\/\/mybatis.org\/\/DTD Config 3.0\/\/EN&quot; &quot;http:\/\/mybatis.org\/dtd\/mybatis-3-config.dtd&quot;&gt;\n&lt;configuration&gt;\n\n    &lt;!--\u901a\u8fc7properties\u6807\u7b7e\u52a0\u8f7d\u5916\u90e8properties\u6587\u4ef6--&gt;\n    &lt;properties resource=&quot;jdbc.properties&quot;&gt;&lt;\/properties&gt;\n\n    &lt;!--\u81ea\u5b9a\u4e49\u522b\u540d--&gt;\n    &lt;typeAliases&gt;\n        &lt;typeAlias type=&quot;com.zjq.domain.User&quot; alias=&quot;user&quot;&gt;&lt;\/typeAlias&gt;\n    &lt;\/typeAliases&gt;\n\n    &lt;!--\u6570\u636e\u6e90\u73af\u5883--&gt;\n    &lt;environments default=&quot;developement&quot;&gt;\n        &lt;environment id=&quot;developement&quot;&gt;\n            &lt;transactionManager type=&quot;JDBC&quot;&gt;&lt;\/transactionManager&gt;\n            &lt;dataSource type=&quot;POOLED&quot;&gt;\n                &lt;property name=&quot;driver&quot; value=&quot;${jdbc.driver}&quot;\/&gt;\n                &lt;property name=&quot;url&quot; value=&quot;${jdbc.url}&quot;\/&gt;\n                &lt;property name=&quot;username&quot; value=&quot;${jdbc.username}&quot;\/&gt;\n                &lt;property name=&quot;password&quot; value=&quot;${jdbc.password}&quot;\/&gt;\n            &lt;\/dataSource&gt;\n        &lt;\/environment&gt;\n    &lt;\/environments&gt;\n\n    &lt;!--\u52a0\u8f7d\u6620\u5c04\u6587\u4ef6--&gt;\n    &lt;mappers&gt;\n        &lt;mapper resource=&quot;com\/zjq\/mapper\/UserMapper.xml&quot;&gt;&lt;\/mapper&gt;\n    &lt;\/mappers&gt;\n\n&lt;\/configuration&gt;<\/code><\/pre>\n<h3>\u4e0d\u5206\u6279\u6b21\u76f4\u63a5\u68ad\u54c8<\/h3>\n<p>MyBatis\u76f4\u63a5\u4e00\u6b21\u6027\u6279\u91cf\u63d2\u516530\u4e07\u6761\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>@Test\npublic void testBatchInsertUser() throws IOException {\n    InputStream resourceAsStream =\n            Resources.getResourceAsStream(&quot;sqlMapConfig.xml&quot;);\n    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);\n    SqlSession session = sqlSessionFactory.openSession();\n    System.out.println(&quot;===== \u5f00\u59cb\u63d2\u5165\u6570\u636e =====&quot;);\n    long startTime = System.currentTimeMillis();\n    try {\n        List&lt;User&gt; userList = new ArrayList&lt;&gt;();\n        for (int i = 1; i &lt;= 300000; i++) {\n            User user = new User();\n            user.setId(i);\n            user.setUsername(&quot;\u5171\u996e\u4e00\u676f\u65e0 &quot; + i);\n            user.setAge((int) (Math.random() * 100));\n            userList.add(user);\n        }\n        session.insert(&quot;batchInsertUser&quot;, userList); \/\/ \u6700\u540e\u63d2\u5165\u5269\u4f59\u7684\u6570\u636e\n        session.commit();\n\n        long spendTime = System.currentTimeMillis()-startTime;\n        System.out.println(&quot;\u6210\u529f\u63d2\u5165 30 \u4e07\u6761\u6570\u636e,\u8017\u65f6\uff1a&quot;+spendTime+&quot;\u6beb\u79d2&quot;);\n    } finally {\n        session.close();\n    }\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\u63a7\u5236\u53f0\u8f93\u51fa\uff1a<\/p>\n<blockquote><p>Cause: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (27759038 &gt;yun 4194304). You can change this value on the server by setting the max_allowed_packet\u2019 variable.<\/p><\/blockquote>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"723\" height=\"278\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9nvm\" alt=\"\" title=\"\"><\/p>\n<p>\u8d85\u51fa\u6700\u5927\u6570\u636e\u5305\u9650\u5236\u4e86\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574<code>max_allowed_packet<\/code>\u9650\u5236\u6765\u63d0\u9ad8\u53ef\u4ee5\u4f20\u8f93\u7684\u5185\u5bb9\uff0c\u4e0d\u8fc7\u7531\u4e8e30\u4e07\u6761\u6570\u636e\u8d85\u51fa\u592a\u591a\uff0c\u8fd9\u4e2a\u4e0d\u53ef\u53d6\uff0c\u68ad\u54c8\u770b\u6765\u662f\u4e0d\u884c\u4e86 \ud83d\ude05\ud83d\ude05\ud83d\ude05<\/p>\n<p>\u65e2\u7136\u68ad\u54c8\u4e0d\u884c\u90a3\u6211\u4eec\u5c31\u4e00\u6761\u4e00\u6761\u5faa\u73af\u7740\u63d2\u5165\u884c\u4e0d\u884c\u5462\uff01<\/p>\n<h3>\u5faa\u73af\u9010\u6761\u63d2\u5165<\/h3>\n<p>mapper\u63a5\u53e3\u548cmapper\u6587\u4ef6\u4e2d\u65b0\u589e\u5355\u4e2a\u7528\u6237\u65b0\u589e\u7684\u5185\u5bb9\u5982\u4e0b:<\/p>\n<pre><code>\/**\n * \u65b0\u589e\u5355\u4e2a\u7528\u6237\n * @param user\n *\/\nvoid insertUser(User user);\n&lt;!-- \u65b0\u589e\u7528\u6237\u4fe1\u606f --&gt;\n&lt;insert id=&quot;insertUser&quot; parameterType=&quot;user&quot;&gt;\n    insert into t_user(username,age) values\n        (\n        #{username},\n        #{age}\n        )\n&lt;\/insert&gt;<\/code><\/pre>\n<p>\u8c03\u6574\u6267\u884c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>@Test\npublic void testCirculateInsertUser() throws IOException {\n    InputStream resourceAsStream =\n            Resources.getResourceAsStream(&quot;sqlMapConfig.xml&quot;);\n    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);\n    SqlSession session = sqlSessionFactory.openSession();\n    System.out.println(&quot;===== \u5f00\u59cb\u63d2\u5165\u6570\u636e =====&quot;);\n    long startTime = System.currentTimeMillis();\n    try {\n        for (int i = 1; i &lt;= 300000; i++) {\n            User user = new User();\n            user.setId(i);\n            user.setUsername(&quot;\u5171\u996e\u4e00\u676f\u65e0 &quot; + i);\n            user.setAge((int) (Math.random() * 100));\n            \/\/ \u4e00\u6761\u4e00\u6761\u65b0\u589e\n            session.insert(&quot;insertUser&quot;, user);\n            session.commit();\n        }\n\n        long spendTime = System.currentTimeMillis()-startTime;\n        System.out.println(&quot;\u6210\u529f\u63d2\u5165 30 \u4e07\u6761\u6570\u636e,\u8017\u65f6\uff1a&quot;+spendTime+&quot;\u6beb\u79d2&quot;);\n    } finally {\n        session.close();\n    }\n}<\/code><\/pre>\n<p>\u6267\u884c\u540e\u53ef\u4ee5\u53d1\u73b0\u78c1\u76d8IO\u5360\u6bd4\u98d9\u5347\uff0c\u4e00\u76f4\u5904\u4e8e\u9ad8\u4f4d\u3002<\/p>\n<p><img decoding=\"async\" width=\"269\" height=\"307\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9nvn\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u7b49\u554a\u7b49\u7b49\u554a\u7b49\uff0c\u597d\u4e45\u8fd8\u6ca1\u6267\u884c\u5b8c<\/p>\n<p><img decoding=\"async\" width=\"640\" height=\"328\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9nvo\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5148\u4e0d\u7ba1\u4ed6\u4e86\u592a\u6162\u4e86\u5148\u641e\u5176\u4ed6\u7684\uff0c\u7b49\u4f1a\u518d\u6765\u770b\u770b\u7ed3\u679c\u5427\u3002<\/p>\n<p>two thousand year later \u2026<\/p>\n<p>\u63a7\u5236\u53f0\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"250\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9nvq\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u603b\u5171\u6267\u884c\u4e8614909367\u6beb\u79d2\uff0c\u6362\u7b97\u51fa\u6765\u662f4\u5c0f\u65f6\u516b\u5206\u949f\u3002\u592a\u6162\u4e86\u3002\u3002<\/p>\n<p><img decoding=\"async\" width=\"632\" height=\"591\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9nvs\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u8fd8\u662f\u4f18\u5316\u4e0b\u4e4b\u524d\u7684\u6279\u5904\u7406\u65b9\u6848\u5427<\/p>\n<h3>MyBatis\u5b9e\u73b0\u63d2\u516530\u4e07\u6761\u6570\u636e<\/h3>\n<p>\u5148\u6e05\u7406\u8868\u6570\u636e\uff0c\u7136\u540e\u4f18\u5316\u6279\u5904\u7406\u6267\u884c\u63d2\u5165\uff1a<\/p>\n<pre><code>-- \u6e05\u7a7a\u7528\u6237\u8868\nTRUNCATE table  t_user;<\/code><\/pre>\n<p>\u4ee5\u4e0b\u662f\u901a\u8fc7 MyBatis \u5b9e\u73b0 30 \u4e07\u6761\u6570\u636e\u63d2\u5165\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<pre><code>\/**\n * \u5206\u6279\u6b21\u6279\u91cf\u63d2\u5165\n * @throws IOException\n *\/\n@Test\npublic void testBatchInsertUser() throws IOException {\n    InputStream resourceAsStream =\n            Resources.getResourceAsStream(&quot;sqlMapConfig.xml&quot;);\n    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);\n    SqlSession session = sqlSessionFactory.openSession();\n    System.out.println(&quot;===== \u5f00\u59cb\u63d2\u5165\u6570\u636e =====&quot;);\n    long startTime = System.currentTimeMillis();\n    int waitTime = 10;\n    try {\n        List&lt;User&gt; userList = new ArrayList&lt;&gt;();\n        for (int i = 1; i &lt;= 300000; i++) {\n            User user = new User();\n            user.setId(i);\n            user.setUsername(&quot;\u5171\u996e\u4e00\u676f\u65e0 &quot; + i);\n            user.setAge((int) (Math.random() * 100));\n            userList.add(user);\n            if (i % 1000 == 0) {\n                session.insert(&quot;batchInsertUser&quot;, userList);\n                \/\/ \u6bcf 1000 \u6761\u6570\u636e\u63d0\u4ea4\u4e00\u6b21\u4e8b\u52a1\n                session.commit();\n                userList.clear();\n\n                \/\/ \u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\n                Thread.sleep(waitTime * 1000);\n            }\n        }\n        \/\/ \u6700\u540e\u63d2\u5165\u5269\u4f59\u7684\u6570\u636e\n        if(!CollectionUtils.isEmpty(userList)) {\n            session.insert(&quot;batchInsertUser&quot;, userList);\n            session.commit();\n        }\n\n        long spendTime = System.currentTimeMillis()-startTime;\n        System.out.println(&quot;\u6210\u529f\u63d2\u5165 30 \u4e07\u6761\u6570\u636e,\u8017\u65f6\uff1a&quot;+spendTime+&quot;\u6beb\u79d2&quot;);\n    } catch (Exception e) {\n        e.printStackTrace();\n    } finally {\n        session.close();\n    }\n}<\/code><\/pre>\n<p>\u4f7f\u7528\u4e86 MyBatis \u7684\u6279\u5904\u7406\u64cd\u4f5c\uff0c\u5c06\u6bcf 1000 \u6761\u6570\u636e\u653e\u5728\u4e00\u4e2a\u6279\u6b21\u4e2d\u63d2\u5165\uff0c\u80fd\u591f\u8f83\u4e3a\u6709\u6548\u5730\u63d0\u9ad8\u63d2\u5165\u901f\u5ea6\u3002\u540c\u65f6\u8bf7\u6ce8\u610f\u5728\u5faa\u73af\u63d2\u5165\u65f6\u8981\u5e26\u6709\u5408\u9002\u7684\u7b49\u5f85\u65f6\u95f4\u548c\u6279\u5904\u7406\u5927\u5c0f\uff0c\u4ee5\u9632\u6b62\u51fa\u73b0\u5185\u5b58\u5360\u7528\u8fc7\u9ad8\u7b49\u95ee\u9898\u3002\u6b64\u5916\uff0c\u8fd8\u9700\u8981\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bbe\u7f6e\u5408\u7406\u7684\u8fde\u63a5\u6c60\u548c\u6570\u636e\u5e93\u7684\u53c2\u6570\uff0c\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\u3002<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"476\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9nvt\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6bcf\u63d2\u51651000\u884c\u6570\u636e\u5c31\u8fdb\u884c\u4e00\u6b21\u6279\u5904\u7406\u63d0\u4ea4\uff0c\u5e76\u7b49\u5f8510\u79d2\u949f\u3002\u8fd9\u6709\u52a9\u4e8e\u63a7\u5236\u5185\u5b58\u5360\u7528\uff0c\u5e76\u786e\u4fdd\u63d2\u5165\u64cd\u4f5c\u5e73\u7a33\u8fdb\u884c\u3002<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"187\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9nvv\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4e94\u5341\u5206\u949f\u6267\u884c\u5b8c\u6bd5\uff0c\u65f6\u95f4\u4e3b\u8981\u7528\u5728\u4e86\u7b49\u5f85\u4e0a\u3002<\/p>\n<p>\u5982\u679c\u4f4e\u8c37\u65f6\u671f\u6267\u884c\uff0cCPU\u548c\u78c1\u76d8\u6027\u80fd\u53c8\u8db3\u591f\u7684\u60c5\u51b5\u4e0b\uff0c\u76f4\u63a5\u6279\u5904\u7406\u4e0d\u7b49\u5f85\u6267\u884c\uff1a<\/p>\n<pre><code>\/**\n * \u5206\u6279\u6b21\u6279\u91cf\u63d2\u5165\n * @throws IOException\n *\/\n@Test\npublic void testBatchInsertUser() throws IOException {\n    InputStream resourceAsStream =\n            Resources.getResourceAsStream(&quot;sqlMapConfig.xml&quot;);\n    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);\n    SqlSession session = sqlSessionFactory.openSession();\n    System.out.println(&quot;===== \u5f00\u59cb\u63d2\u5165\u6570\u636e =====&quot;);\n    long startTime = System.currentTimeMillis();\n    int waitTime = 10;\n    try {\n        List&lt;User&gt; userList = new ArrayList&lt;&gt;();\n        for (int i = 1; i &lt;= 300000; i++) {\n            User user = new User();\n            user.setId(i);\n            user.setUsername(&quot;\u5171\u996e\u4e00\u676f\u65e0 &quot; + i);\n            user.setAge((int) (Math.random() * 100));\n            userList.add(user);\n            if (i % 1000 == 0) {\n                session.insert(&quot;batchInsertUser&quot;, userList);\n                \/\/ \u6bcf 1000 \u6761\u6570\u636e\u63d0\u4ea4\u4e00\u6b21\u4e8b\u52a1\n                session.commit();\n                userList.clear();\n            }\n        }\n        \/\/ \u6700\u540e\u63d2\u5165\u5269\u4f59\u7684\u6570\u636e\n        if(!CollectionUtils.isEmpty(userList)) {\n            session.insert(&quot;batchInsertUser&quot;, userList);\n            session.commit();\n        }\n\n        long spendTime = System.currentTimeMillis()-startTime;\n        System.out.println(&quot;\u6210\u529f\u63d2\u5165 30 \u4e07\u6761\u6570\u636e,\u8017\u65f6\uff1a&quot;+spendTime+&quot;\u6beb\u79d2&quot;);\n    } catch (Exception e) {\n        e.printStackTrace();\n    } finally {\n        session.close();\n    }\n}<\/code><\/pre>\n<p>\u521924\u79d2\u53ef\u4ee5\u5b8c\u6210\u6570\u636e\u63d2\u5165\u64cd\u4f5c\uff1a<\/p>\n<p><img decoding=\"async\" width=\"673\" height=\"399\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9nvw\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"346\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9nvx\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u77ed\u65f6CPU\u548c\u78c1\u76d8\u5360\u7528\u4f1a\u98d9\u9ad8\u3002<\/p>\n<p>\u628a\u6279\u5904\u7406\u7684\u91cf\u518d\u8c03\u5927\u4e00\u4e9b\u8c03\u52305000\uff0c\u5728\u6267\u884c\uff1a<\/p>\n<p><img decoding=\"async\" width=\"418\" height=\"346\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9nvy\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>13\u79d2\u63d2\u5165\u6210\u529f30\u4e07\u6761\uff0c\u76f4\u63a5\u829c\u6e56\u8d77\u98de\ud83d\udeeb\ud83d\udeeb\ud83d\udeeb<\/p>\n<h3>JDBC\u5b9e\u73b0\u63d2\u516530\u4e07\u6761\u6570\u636e<\/h3>\n<p>JDBC\u5faa\u73af\u63d2\u5165\u7684\u8bdd\u8ddf\u4e0a\u9762\u7684mybatis\u9010\u6761\u63d2\u5165\u7c7b\u4f3c\uff0c\u4e0d\u518d\u8d58\u8ff0\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f Java \u4f7f\u7528 JDBC \u6279\u5904\u7406\u5b9e\u73b0 30 \u4e07\u6761\u6570\u636e\u63d2\u5165\u7684\u793a\u4f8b\u4ee3\u7801\u3002\u8bf7\u6ce8\u610f\uff0c\u8be5\u4ee3\u7801\u4ec5\u63d0\u4f9b\u601d\u8def\uff0c\u5177\u4f53\u5b9e\u73b0\u9700\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<pre><code>\/**\n * JDBC\u5206\u6279\u6b21\u6279\u91cf\u63d2\u5165\n * @throws IOException\n *\/\n@Test\npublic void testJDBCBatchInsertUser() throws IOException {\n    Connection connection = null;\n    PreparedStatement preparedStatement = null;\n\n    String databaseURL = &quot;jdbc:mysql:\/\/localhost:3306\/test&quot;;\n    String user = &quot;root&quot;;\n    String password = &quot;root&quot;;\n\n    try {\n        connection = DriverManager.getConnection(databaseURL, user, password);\n        \/\/ \u5173\u95ed\u81ea\u52a8\u63d0\u4ea4\u4e8b\u52a1\uff0c\u6539\u4e3a\u624b\u52a8\u63d0\u4ea4\n        connection.setAutoCommit(false);\n        System.out.println(&quot;===== \u5f00\u59cb\u63d2\u5165\u6570\u636e =====&quot;);\n        long startTime = System.currentTimeMillis();\n        String sqlInsert = &quot;INSERT INTO t_user ( username, age) VALUES ( ?, ?)&quot;;\n        preparedStatement = connection.prepareStatement(sqlInsert);\n\n        Random random = new Random();\n        for (int i = 1; i &lt;= 300000; i++) {\n            preparedStatement.setString(1, &quot;\u5171\u996e\u4e00\u676f\u65e0 &quot; + i);\n            preparedStatement.setInt(2, random.nextInt(100));\n            \/\/ \u6dfb\u52a0\u5230\u6279\u5904\u7406\u4e2d\n            preparedStatement.addBatch();\n\n            if (i % 1000 == 0) {\n                \/\/ \u6bcf1000\u6761\u6570\u636e\u63d0\u4ea4\u4e00\u6b21\n                preparedStatement.executeBatch();\n                connection.commit();\n                System.out.println(&quot;\u6210\u529f\u63d2\u5165\u7b2c &quot;+ i+&quot; \u6761\u6570\u636e&quot;);\n            }\n\n        }\n        \/\/ \u5904\u7406\u5269\u4f59\u7684\u6570\u636e\n        preparedStatement.executeBatch();\n        connection.commit();\n        long spendTime = System.currentTimeMillis()-startTime;\n        System.out.println(&quot;\u6210\u529f\u63d2\u5165 30 \u4e07\u6761\u6570\u636e,\u8017\u65f6\uff1a&quot;+spendTime+&quot;\u6beb\u79d2&quot;);\n    } catch (SQLException e) {\n        System.out.println(&quot;Error: &quot; + e.getMessage());\n    } finally {\n        if (preparedStatement != null) {\n            try {\n                preparedStatement.close();\n            } catch (SQLException e) {\n                e.printStackTrace();\n            }\n        }\n\n        if (connection != null) {\n            try {\n                connection.close();\n            } catch (SQLException e) {\n                e.printStackTrace();\n            }\n        }\n    }\n}<\/code><\/pre>\n<p><img decoding=\"async\" width=\"640\" height=\"352\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9nvz\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><img decoding=\"async\" width=\"406\" height=\"339\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9nvA\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4e0a\u8ff0\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7 JDBC \u8fde\u63a5 MySQL \u6570\u636e\u5e93\uff0c\u5e76\u6267\u884c\u6279\u5904\u7406\u64cd\u4f5c\u63d2\u5165\u6570\u636e\u3002\u5177\u4f53\u5b9e\u73b0\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/li>\n<li>\u521b\u5efa Statement \u5bf9\u8c61\u3002<\/li>\n<li>\u5b9a\u4e49 SQL \u8bed\u53e5\uff0c\u4f7f\u7528 <code>PreparedStatement<\/code> \u5bf9\u8c61\u9884\u7f16\u8bd1 SQL \u8bed\u53e5\u5e76\u8bbe\u7f6e\u53c2\u6570\u3002<\/li>\n<li>\u6267\u884c\u6279\u5904\u7406\u64cd\u4f5c\u3002<\/li>\n<li>\u5904\u7406\u5269\u4f59\u7684\u6570\u636e\u3002<\/li>\n<li>\u5173\u95ed Statement \u548c Connection \u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p>\u4f7f\u7528<code>setAutoCommit(false) <\/code>\u6765\u7981\u6b62\u81ea\u52a8\u63d0\u4ea4\u4e8b\u52a1\uff0c\u7136\u540e\u5728\u6bcf\u6b21\u6279\u91cf\u63d2\u5165\u4e4b\u540e\u624b\u52a8\u63d0\u4ea4\u4e8b\u52a1\u3002\u6bcf\u6b21\u63d2\u5165\u6570\u636e\u65f6\u90fd\u65b0\u5efa\u4e00\u4e2a <code>PreparedStatement<\/code> \u5bf9\u8c61\u4ee5\u907f\u514d\u72b6\u6001\u4e0d\u4e00\u81f4\u95ee\u9898\u3002\u5728\u63d2\u5165\u6570\u636e\u7684\u5faa\u73af\u4e2d\uff0c\u6bcf 10000 \u6761\u6570\u636e\u5c31\u6267\u884c\u4e00\u6b21 <code>executeBatch()<\/code> \u63d2\u5165\u6570\u636e\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u4f18\u5316\u8fde\u63a5\u6c60\u548c\u6570\u636e\u5e93\u7684\u76f8\u5173\u914d\u7f6e\uff0c\u4ee5\u9632\u6b62\u8fde\u63a5\u8d85\u65f6\u7b49\u95ee\u9898\u3002<\/p>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u5b9e\u73b0\u9ad8\u6548\u7684\u5927\u91cf\u6570\u636e\u63d2\u5165\u9700\u8981\u7ed3\u5408\u4ee5\u4e0b\u4f18\u5316\u7b56\u7565\uff08\u5efa\u8bae\u7efc\u5408\u4f7f\u7528\uff09\uff1a<\/p>\n<p><strong>1.\u6279\u5904\u7406\uff1a<\/strong> \u6279\u91cf\u63d0\u4ea4SQL\u8bed\u53e5\u53ef\u4ee5\u964d\u4f4e\u7f51\u7edc\u4f20\u8f93\u548c\u5904\u7406\u5f00\u9500\uff0c\u51cf\u5c11\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u6b21\u6570\u3002\u5728Java\u4e2d\u53ef\u4ee5\u4f7f\u7528Statement\u6216\u8005<code>PreparedStatement<\/code>\u7684<code>addBatch()<\/code>\u65b9\u6cd5\u6765\u6dfb\u52a0\u591a\u4e2aSQL\u8bed\u53e5\uff0c\u7136\u540e\u4e00\u6b21\u6027\u6267\u884c<code>executeBatch()<\/code>\u65b9\u6cd5\u63d0\u4ea4\u6279\u5904\u7406\u7684SQL\u8bed\u53e5\u3002<\/p>\n<ul>\n<li>\u5728\u5faa\u73af\u63d2\u5165\u65f6\u5e26\u6709\u9002\u5f53\u7684\u7b49\u5f85\u65f6\u95f4\u548c\u6279\u5904\u7406\u5927\u5c0f\uff0c\u4ece\u800c\u907f\u514d\u5185\u5b58\u5360\u7528\u8fc7\u9ad8\u7b49\u95ee\u9898\uff1a<\/li>\n<li>\n<ul>\n<li>\u8bbe\u7f6e\u9002\u5f53\u7684\u6279\u5904\u7406\u5927\u5c0f\uff1a\u6279\u5904\u7406\u5927\u5c0f\u6307\u5728\u4e00\u6b21\u63d2\u5165\u64cd\u4f5c\u4e2d\u63d2\u5165\u591a\u5c11\u884c\u6570\u636e\u3002\u5982\u679c\u6279\u5904\u7406\u5927\u5c0f\u592a\u5c0f\uff0c\u63d2\u5165\u64cd\u4f5c\u7684\u9891\u7387\u5c06\u5f88\u9ad8\uff0c\u800c\u5982\u679c\u6279\u5904\u7406\u5927\u5c0f\u592a\u5927\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u5360\u7528\u8fc7\u9ad8\u3002\u901a\u5e38\uff0c\u5efa\u8bae\u5c06\u6279\u5904\u7406\u5927\u5c0f\u8bbe\u7f6e\u4e3a1000-5000\u884c\uff0c\u8fd9\u5c06\u51cf\u5c11\u63d2\u5165\u64cd\u4f5c\u7684\u9891\u7387\u5e76\u964d\u4f4e\u5185\u5b58\u5360\u7528\u3002<\/li>\n<li>\u91c7\u7528\u9002\u5f53\u7684\u7b49\u5f85\u65f6\u95f4\uff1a\u7b49\u5f85\u65f6\u95f4\u6307\u5728\u6279\u5904\u7406\u64cd\u4f5c\u4e4b\u95f4\u7b49\u5f85\u7684\u65f6\u95f4\u91cf\u3002\u7b49\u5f85\u65f6\u95f4\u8fc7\u77ed\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u5360\u7528\u8fc7\u9ad8\uff0c\u800c\u7b49\u5f85\u65f6\u95f4\u8fc7\u957f\u5219\u53ef\u80fd\u4f1a\u5ef6\u8fdf\u63d2\u5165\u64cd\u4f5c\u7684\u901f\u5ea6\u3002\u901a\u5e38\uff0c\u5efa\u8bae\u5c06\u7b49\u5f85\u65f6\u95f4\u8bbe\u7f6e\u4e3a\u51e0\u79d2\u949f\u5230\u51e0\u5341\u79d2\u949f\u4e4b\u95f4\uff0c\u8fd9\u5c06\u4f7f\u64cd\u4f5c\u53d8\u5f97\u5e73\u6ed1\u4e14\u907f\u514d\u51fa\u73b0\u5185\u5b58\u5360\u7528\u8fc7\u9ad8\u7b49\u95ee\u9898\u3002<\/li>\n<li>\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u4e00\u4e9b\u5185\u5b58\u4f18\u5316\u7684\u6280\u5de7\uff0c\u4f8b\u5982\u4f7f\u7528\u5185\u5b58\u6570\u636e\u5e93\u6216\u4f7f\u7528\u6e38\u6807\u65b9\u5f0f\u63d2\u5165\u6570\u636e\uff0c\u4ee5\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u603b\u7684\u6765\u8bf4\uff0c\u9009\u62e9\u9002\u5f53\u7684\u6279\u5904\u7406\u5927\u5c0f\u548c\u7b49\u5f85\u65f6\u95f4\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5e73\u7a33\u5730\u8fdb\u884c\u63d2\u5165\u64cd\u4f5c\uff0c\u907f\u514d\u51fa\u73b0\u5185\u5b58\u5360\u7528\u8fc7\u9ad8\u7b49\u95ee\u9898\u3002<\/li>\n<\/ul>\n<p><strong>2.\u7d22\u5f15:<\/strong> \u5728\u5927\u91cf\u6570\u636e\u63d2\u5165\u524d\u6682\u65f6\u53bb\u6389\u7d22\u5f15\uff0c\u6700\u540e\u518d\u6253\u4e0a\uff0c\u8fd9\u6837\u53ef\u4ee5\u5927\u5927\u51cf\u5c11\u5199\u5165\u65f6\u5019\u7684\u66f4\u65b0\u7d22\u5f15\u7684\u65f6\u95f4\u3002<\/p>\n<p><strong>3.\u6570\u636e\u5e93\u8fde\u63a5\u6c60\uff1a<\/strong> \u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u53ef\u4ee5\u51cf\u5c11\u6570\u636e\u5e93\u8fde\u63a5\u5efa\u7acb\u548c\u5173\u95ed\u7684\u5f00\u9500\uff0c\u63d0\u9ad8\u6027\u80fd\u3002\u5728\u6ca1\u6709\u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u7684\u60c5\u51b5\uff0c\u8bb0\u5f97\u5728finally\u4e2d\u5173\u95ed\u76f8\u5173\u8fde\u63a5\u3002<\/p>\n<p>\u6570\u636e\u5e93\u53c2\u6570\u8c03\u6574\uff1a\u589e\u52a0MySQL\u6570\u636e\u5e93\u7f13\u51b2\u533a\u5927\u5c0f\u3001\u914d\u7f6e\u9ad8\u6027\u80fd\u7684\u78c1\u76d8\u548cI\/O\u7b49\u3002<\/p>\n<blockquote><p>\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3aCSDN\u535a\u4e3b\u300c\u5171\u996e\u4e00\u676f\u65e0\u300d\u7684\u539f\u521b\u6587\u7ae0\uff0c\u9075\u5faaCC 4.0 BY-SA\u7248\u6743\u534f\u8bae\uff0c\u8f6c\u8f7d\u8bf7\u9644\u4e0a\u539f\u6587\u51fa\u5904\u94fe\u63a5\u53ca\u672c\u58f0\u660e\u3002\u539f\u6587\u94fe\u63a5\uff1a<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=%2FazAWC5HQlvZiCsxn3wN1w%3D%3D.xF2%2FlT3eHAB0F64UOrrqJA07hTRI%2BFzR7L7k%2F9CNWzFkEYbpKoPR7Kh6CiQhX7LPtbNMh6yjp6ziNgXZkYJ1kw%3D%3D\" rel=\"noopener noreferrer\">https:\/\/blog.csdn.net\/qq_35427589\/article\/details\/129665307<\/a><\/p><\/blockquote>\n<p><strong>\u8fd1\u671f\u70ed\u6587\u63a8\u8350\uff1a<\/strong><\/p>\n<p>1.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=8NGe9UT4N1WEvGzFR9W3qQ%3D%3D.%2BHXxtrC4eJYMh9qxigHnhJ6PTqkOMySKin%2FBoe8y7D0%3D\" rel=\"noopener noreferrer\">1,000+ \u9053 Java\u9762\u8bd5\u9898\u53ca\u7b54\u6848\u6574\u7406(2022\u6700\u65b0\u7248)<\/a><\/p>\n<p>2.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=FBEHmduTZP5EMgbtjh1a8A%3D%3D.KgXCvVckmeLnZIic9xZfDq7LL5AjzoptvavKu4Dkh1LLoqpc6KJRhIIuao1cbG3Sujklfei20nCRfxrloBY2eg%3D%3D\" rel=\"noopener noreferrer\">\u52b2\u7206\uff01Java \u534f\u7a0b\u8981\u6765\u4e86\u3002\u3002\u3002<\/a><\/p>\n<p>3.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=FJ9vFp7LDESjvPoQgD3gPQ%3D%3D.i8GMcHg%2B9mPq4at1aJibrw7u9p2cKKBWJuCkJ4jFaFF%2F7Tw0B2w8yKoFKyH1klGp\" rel=\"noopener noreferrer\">Spring Boot 2.x \u6559\u7a0b\uff0c\u592a\u5168\u4e86\uff01<\/a><\/p>\n<p>4.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=yBNn6M3Ano5RpsLJg8FXQA%3D%3D.Lc0HdpqO0Suny7WE32f9gb%2F5oKUnkGk5NnsTULyZpKIS1OwlbWKOwq0T0V1QCQGtCHlCfO33iltMTJjJqyTcfQ%3D%3D\" rel=\"noopener noreferrer\">\u522b\u518d\u5199\u6ee1\u5c4f\u7684\u7206\u7206\u7206\u70b8\u7c7b\u4e86\uff0c\u8bd5\u8bd5\u88c5\u9970\u5668\u6a21\u5f0f\uff0c\u8fd9\u624d\u662f\u4f18\u96c5\u7684\u65b9\u5f0f\uff01\uff01<\/a><\/p>\n<p>5.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=FBbALP%2FR1kvJHkys8j5IDA%3D%3D.79bqJHcbj5f7M%2B64%2BZ3cRedQIW%2BiGjcGYB%2FhPW%2FCnTVN2gf47n7PQLh%2BpTJHAqNxFM%2BKZqTeCcETByZxZiMJT4FO08UCH33hFczVRmZAQ5M%3D\" rel=\"noopener noreferrer\">\u300aJava\u5f00\u53d1\u624b\u518c\uff08\u5d69\u5c71\u7248\uff09\u300b\u6700\u65b0\u53d1\u5e03\uff0c\u901f\u901f\u4e0b\u8f7d\uff01<\/a><\/p>\n<p>\u89c9\u5f97\u4e0d\u9519\uff0c\u522b\u5fd8\u4e86\u968f\u624b\u70b9\u8d5e+\u8f6c\u53d1\u54e6\uff01<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044141981\">https:\/\/segmentfault.com\/a\/1190000044141981<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4e3b\u8981\u8bb2\u8ff0\u901a\u8fc7MyBatis\u3001JDBC\u7b49\u505a\u5927\u6570\u636e\u91cf\u6570\u636e\u63d2\u5165\u7684\u6848\u4f8b\u548c\u7ed3\u679c\u3002 30\u4e07\u6761\u6570\u636e\u63d2\u5165\u63d2\u5165\u6570\u636e\u5e93\u9a8c\u8bc1 \u5b9e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008151","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008151"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008151\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}