{"id":701,"date":"2020-06-14T23:11:01","date_gmt":"2020-06-14T15:11:01","guid":{"rendered":"https:\/\/blog.humh.cn\/?p=701"},"modified":"2020-06-14T23:11:53","modified_gmt":"2020-06-14T15:11:53","slug":"%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95%e4%b9%8b%e5%b5%8c%e5%85%a5%e5%bc%8fmysql","status":"publish","type":"post","link":"https:\/\/blog.humh.cn\/?p=701","title":{"rendered":"\u5355\u5143\u6d4b\u8bd5\u4e4b\u5d4c\u5165\u5f0fmysql"},"content":{"rendered":"\n<h4>\u4e00\u3001\u9700\u6c42\u5206\u6790<\/h4>\n\n\n\n<p>\u5728\u5355\u5143\u6d4b\u8bd5UnitTest\u4e2d\u7ecf\u5e38\u6709\u8fd9\u6837\u7684\u9700\u6c42\uff0c\u9700\u8981\u6d4b\u8bd5\u6570\u636e\u4e0e\u5f00\u53d1\u6570\u636e\u3001\u751f\u4ea7\u6570\u636e\u7b49\u533a\u5206\u5f00\uff0c\u540c\u65f6\u907f\u514d\u4e0d\u540c\u6d4b\u8bd5\u7528\u4f8b\u4e4b\u95f4\u7684\u6570\u636e\u6c61\u67d3\u4ee5\u53ca\u540c\u4e00\u6d4b\u8bd5\u7528\u4f8b\u6bcf\u6b21\u8fd0\u884c\u7684\u6570\u636e\u4e0d\u4e00\u81f4\u60c5\u51b5\u53d1\u751f\u3002 <\/p>\n\n\n\n<h4>\u4e8c\u3001\u89e3\u51b3\u65b9\u6848<\/h4>\n\n\n\n<p>\u5185\u5b58\u6570\u636e\u5e93h2 \u53ef\u4ee5\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u6c42\uff0c\u4f46\u56e0\u4e3a\u9879\u76ee\u4e2d\u91c7\u7528\u7684mysql\uff0cmysql\u548ch2 \u5728\u67d0\u4e9b\u65b9\u9762\u8bed\u6cd5\u4e0a\u4f1a\u6709\u4e9b\u533a\u522b\u3002\u6240\u4ee5\u8fd9\u91cc\u76f4\u63a5\u91c7\u7528\u4e86\u5d4c\u5165\u5f0fmysql\u7684\u65b9\u6848\u3002<\/p>\n\n\n\n<p>\u5d4c\u5165\u5f0fmysql\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\u53ef\u4ee5\u89e3\u51b3\u4e0a\u8ff0\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u5728\u6bcf\u6b21\u6d4b\u8bd5\u7c7b\u5f00\u59cb\u6267\u884c\u524d\uff0c\u542f\u52a8\u5d4c\u5165\u5f0fmysql\uff0c\u5728\u8be5\u6d4b\u8bd5\u7c7b\u8dd1\u5b8c\u540e\u5173\u95ed\u3002\u4e0b\u4e00\u6b21\u6d4b\u8bd5\u7c7b\u6267\u884c\u65f6\uff0c\u518d\u91cd\u65b0\u5f00\u542f\u3002\u8fd9\u6837\u4e0d\u9700\u8981\u4f9d\u8d56\u4efb\u4f55\u5916\u7f6e\u8d44\u6e90\u5c31\u53ef\u4ee5\u89e3\u51b3\u6570\u636e\u6c61\u67d3\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u9879\u76ee\u4e2d\u4f7f\u7528\u7684\u4e00\u6b3e\u5d4c\u5165\u5f0fmysql\uff1a<a href=\"https:\/\/github.com\/wix\/wix-embedded-mysql\">https:\/\/github.com\/wix\/wix-embedded-mysql<\/a><\/p>\n\n\n\n<p>\u6838\u5fc3\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import com.wix.mysql.EmbeddedMysql;\nimport com.wix.mysql.ScriptResolver;\nimport com.wix.mysql.config.Charset;\nimport com.wix.mysql.config.MysqldConfig;\nimport com.wix.mysql.distribution.Version;\n\nimport java.util.concurrent.TimeUnit;\n\n\/**\n * @Description: \u5d4c\u5165\u5f0fmysql\u670d\u52a1\uff0c\u53ef\u7528\u4e8e\u5355\u5143\u6d4b\u8bd5\uff0c\u53ea\u5b58\u5728\u8fd0\u884c\u4e8e\u8dd1\u6d4b\u8bd5\u7528\u4f8b\u671f\u95f4\n * @Author: huminghao\n * @Date: 2019-12-12\n *\/\npublic class EmbeddedMysqlConfig {\n\n    \/**\n     * EmbeddedMysql\u5b9e\u4f8b\n     *\/\n    private EmbeddedMysql mysqlServer;\n\n    public EmbeddedMysql getMysqlServer() {\n        return mysqlServer;\n    }\n\n    \/**\n     * @Description: \u542f\u52a8Embedded Mysql\n     * @Param: sqlFilePath \u521d\u59cb\u5316sql\u811a\u672c\u8def\u5f84\n     * @Return: void\n     * @Author: huminghao\n     * @Date: 2019\/12\/27\n     *\/\n    public void startLocalMysql() {\n        \/\/ mysql\u57fa\u7840\u914d\u7f6e\n        MysqldConfig config = MysqldConfig.aMysqldConfig(Version.v5_7_18)\n                \/\/ \u6570\u636e\u5e93\u5b57\u7b26\u96c6\n                .withCharset(Charset.UTF8MB4)\n                \/\/ \u7aef\u53e3\u53f73373\n                .withPort(3373)\n\/\/                .withUser(\"admin\", \"testdb\") \/\/use root with no password\n\/\/                .withTimeZone(\"Europe\/Vilnius\")\n                \/\/ \u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\n                .withTimeout(2, TimeUnit.MINUTES)\n                .withServerVariable(\"max_connect_errors\", 666)\n                .build();\n        \/\/ \u542f\u52a8\u4e00\u4e2aEmbeddedMysql\n        EmbeddedMysql mysql = EmbeddedMysql.anEmbeddedMysql(config)\n                \/\/ \u6307\u5b9a\u5e93\u540dsuson\uff0c\u521d\u59cb\u5316\u811a\u672c\u4e3aresources\u4e0b\u7684\u201cinit.sql\u201d\n\/\/                .addSchema(\"suson\", ScriptResolver.classPathScript(\"sql\/init.sql\"))\n                .start();\n\n        mysqlServer = mysql;\n        System.out.println(\"Mysql server started!\");\n\n        \/\/ jvm\u9500\u6bc1\u65f6\uff0c\u5173\u95edmysqlServer\uff0c\u91ca\u653e\u8d44\u6e90\n        Runtime.getRuntime().addShutdownHook(\n                new Thread() {\n                    @Override\n                    public void run() {\n                        mysqlServer.stop();\n                        System.out.println(\"Mysql server closed in hook!\");\n                    }\n                }\n        );\n    }\n\n    \/**\n     * @Description: \u505c\u6b62Embedded Mysql\n     * @Return: void\n     * @Author: huminghao\n     * @Date: 2019\/12\/12\n     *\/\n    public void stopMysql() {\n        if (mysqlServer != null) {\n            mysqlServer.stop();\n            System.out.println(\"Mysql server closed by stop!\");\n        }\n    }\n\n}<\/pre>\n\n\n\n<p>\u53ef\u914d\u7f6e\u542f\u52a8\u7aef\u53e3\uff0c\u7f16\u7801\uff0c\u8d26\u6237\uff0cschema sql\u7b49\u7b49\u3002<\/p>\n\n\n\n<p>\u7136\u540e\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\u7c7b\u52a0\u8f7d\u5f00\u59cb\u5904\uff08junit&nbsp;<strong>@BeforeClass<\/strong>\uff09\u8c03\u7528startLocalMysql() \u5373\u53ef\uff0c\u5728\u6267\u884c\u5b8c\u540e\uff08junit&nbsp;<strong>@AfterClass<\/strong>\uff09\u8c03\u7528stopMysql() \u5373\u53ef\u3002\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@ActiveProfiles(\"test\")\n@SpringBootTest(classes = {InteractionTestApplication.class})\n@RunWith(SpringRunner.class)\npublic class CourseServiceTest {\n\n    private static EmbeddedMysqlConfig embeddedMysqlConfig;\n\n    \/**\n     * @Description: \u5728\u6d4b\u8bd5\u7c7b\u7c7b\u52a0\u8f7d\u65f6\uff0c\u542f\u52a8\u5d4c\u5165\u5f0fmysql\n     * @Return: void\n     * @Author: huminghao\n     * @Date: 2019\/12\/12\n     *\/\n    @BeforeClass\n    public static void setUp() {\n        embeddedMysqlConfig = new EmbeddedMysqlConfig();\n        embeddedMysqlConfig.startLocalMysql();\n    }\n\n    \/**\n     * @Description: \u5728\u6d4b\u8bd5\u7c7b\u6240\u6709\u7528\u4f8b\u8dd1\u540e\u5173\u95ed\u5d4c\u5165\u5f0fmysql\n     * @Return: void\n     * @Author: huminghao\n     * @Date: 2019\/12\/12\n     *\/\n    @AfterClass\n    public static void tearDown() {\n        embeddedMysqlConfig.stopMysql();\n    }\n}<\/pre>\n\n\n\n<p>\u5355\u5143\u6d4b\u8bd5\u6240\u9700\u8981\u7684\u8868\u7ed3\u6784\u6570\u636e\uff0c\u53ef\u4ee5\u653e\u5230\u4e00\u4e2asql\u811a\u672c\u4e2d\uff0csql\u811a\u672c\u653e\u5230resources\u76ee\u5f55\u4e0b\u5373\u53ef\uff08maven\u9879\u76ee\uff0c\u5982\u679c\u662f\u666e\u901a\u9879\u76ee\uff0c\u4fdd\u8bc1\u5728classpath\u4e0b\uff09 <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-20.png\"><img width=\"316\" height=\"186\" class=\"wp-image-702 lazyload\" title=\"\u5355\u5143\u6d4b\u8bd5\u4e4b\u5d4c\u5165\u5f0fmysql\" src=\"data:image\/gif;base64,R0lGODlhAgABAIAAALGxsQAAACH5BAAAAAAALAAAAAACAAEAAAICBAoAOw==\" data-src=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-20.png\" alt=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-20.png\" data-srcset=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-20.png 421w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-20-300x177.png 300w\" sizes=\"(max-width: 316px) 100vw, 316px\"><\/a><\/figure><\/div>\n\n\n\n<p>\u7136\u540e&nbsp;<strong>ScriptResolver.classPathScript(&#8220;sql\/init.sql&#8221;)<\/strong>&nbsp;\u4e2d\u53ef\u4ee5\u6307\u5b9a\u4f60\u7684\u811a\u672c\u8def\u5f84\uff0c\u76f8\u5bf9classpath\u7684\u76f8\u5bf9\u8def\u5f84\u3002\u5f53\u7136\u8fd8\u53ef\u4ee5\u6307\u5b9a\u5e93\u3002\u8fd9\u6837\u6211\u4eec\u7684\u6570\u636e\u5e93\u5c31\u7b97\u521b\u5efa\u5b8c\u6210\uff0c\u76f4\u63a5\u6d4b\u8bd5\u6b63\u5e38\u4f7f\u7528\u5c31\u53ef\u4ee5\u3002<\/p>\n\n\n\n<h4>\u4e09\u3001\u6280\u672f\u5206\u6790\u53ca\u8e29\u5751\u70b9 <\/h4>\n\n\n\n<p>wix-embedded-mysql\u7684\u5927\u81f4\u903b\u8f91\uff1a<\/p>\n\n\n\n<p>\u5f53\u4f9d\u8d56\u4e86wix-embedded-mysql\u7684\u5305\u540e\uff0c\u5728\u521d\u6b21\u542f\u52a8\u4f7f\u7528\u65f6\uff0c\u4f1a\u5148\u53bb\u4e0b\u8f7d\u7279\u5b9a\u7684\u5b89\u88c5\u5305\uff0c\u7136\u540e\u8fdb\u884c\u672c\u5730MySQL\u7684\u201c\u5b89\u88c5\u201d\uff0c\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5c31\u4f1a\u6b63\u5e38\u542f\u52a8\uff0c\u5f53\u4e0b\u6b21\u518d\u542f\u52a8\u540e\uff0c\u5c31\u65e0\u9700\u4e0b\u8f7d\u4e86\u3002\uff08embedded-mysql\u53ef\u4ee5\u7406\u89e3\u4e3a\uff0c\u5e2e\u6211\u4eec\u7701\u53bb\u624b\u52a8\u914d\u7f6e\u5b89\u88c5mysql\u7684\u8fc7\u7a0b\uff0c\u81ea\u52a8\u5316\u5904\u7406\uff09<\/p>\n\n\n\n<p>\u5b89\u88c5\u5305\u53ef\u4ee5\u5728\u4f60\u7684\u7cfb\u7edf\u7528\u6237\u76ee\u5f55\u4e0b\u627e\u5230\uff08windows\uff1aC:UsersAdministrator.embedmysql\u3002\u6211\u5f53\u524d\u662fAdmin\u7528\u6237\uff09\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-21.png\"><img width=\"424\" height=\"96\" class=\"wp-image-703 lazyload\" title=\"\u5355\u5143\u6d4b\u8bd5\u4e4b\u5d4c\u5165\u5f0fmysql\" src=\"data:image\/gif;base64,R0lGODlhAgABAIAAALGxsQAAACH5BAAAAAAALAAAAAACAAEAAAICBAoAOw==\" data-src=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-21.png\" alt=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-21.png\" data-srcset=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-21.png 848w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-21-300x68.png 300w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-21-768x173.png 768w\" sizes=\"(max-width: 424px) 100vw, 424px\"><\/a><\/figure><\/div>\n\n\n\n<p>\u5982\u679c\u4f60\u662fmaven\u5de5\u7a0b\uff0c\u5f53mysql\u542f\u52a8\u540e\uff0c\u4f60\u4f1a\u5728target\u76ee\u5f55\u4e0b\u627e\u5230\u5173\u4e8emysql\u7684\u8d44\u6e90\u6587\u4ef6\u3002\u5f53\u7ed3\u675f\u540e\uff0c\u6587\u4ef6\u88ab\u79fb\u9664\u3002 <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-22.png\"><img width=\"497\" height=\"121\" class=\"wp-image-704 lazyload\" title=\"\u5355\u5143\u6d4b\u8bd5\u4e4b\u5d4c\u5165\u5f0fmysql\" src=\"data:image\/gif;base64,R0lGODlhAgABAIAAALGxsQAAACH5BAAAAAAALAAAAAACAAEAAAICBAoAOw==\" data-src=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-22.png\" alt=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-22.png\" data-srcset=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-22.png 993w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-22-300x73.png 300w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-22-768x186.png 768w\" sizes=\"(max-width: 497px) 100vw, 497px\"><\/a><\/figure><\/div>\n\n\n\n<p>embedded-mysql\u4f1a\u5728\u5355\u5143\u6d4b\u8bd5jvm\u8fdb\u7a0b\u5916\u91cd\u65b0\u65b0\u542f\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u4e3amysql\u8d44\u6e90\u670d\u52a1\u3002\u5f53\u5355\u5143\u6d4b\u8bd5\u7ed3\u675f\u65f6\uff0c\u5173\u95edmysql\uff0c\u5173\u95ed\u8fdb\u7a0b\uff0c\u7136\u540e\u5173\u95ed\u5355\u5143\u6d4b\u8bd5\u7684jvm\u8fdb\u7a0b\u3002<\/p>\n\n\n\n<p>\uff08<strong>\u8e29\u5751\u70b9\uff1a\u5982\u679c\u4f60\u662f\u5728ide\u4e2d\u76f4\u63a5run\u7684test\u7c7b\uff0c\u7ec8\u6b62run\uff0c\u5c31\u662f\u7ec8\u6b62\u4e86jvm\u8fdb\u7a0b\uff0c\u90a3\u4e48\u6309\u7167\u4ee3\u7801\u4e2d\u7684\u5199\u6cd5\uff0cmysql\u8fdb\u7a0b\u4e5f\u4f1a\u5173\u95ed\uff1b\u5982\u679c\u662f\u5728maven test\u65f6\u5019\uff0ctest\u8dd1\u7684\u8fc7\u7a0b\u4e2d\uff0cmysql\u542f\u52a8\u4e86\uff0ctest\u6ca1\u6709\u7ed3\u675f\u4e4b\u524d\uff0cctrl+c \u7ed3\u675fmaven test\u5e76\u6ca1\u6709\u7ed3\u675f\u6240\u6709\u8fdb\u7a0b\uff0c\u4ec5\u4ec5\u662f\u7ed3\u675f\u4e86maven\u7684\u8fdb\u7a0b\uff0c\u800cmysql\u7684\u8fdb\u7a0b\u4ecd\u5728\u8fd0\u884c<\/strong>\uff09<\/p>\n\n\n\n<p><strong>\u5982\u679c\u6307\u5b9a\u4e86schema\u52a0\u8f7d\u7684sql\u811a\u672c\uff0c\u90a3\u4e48\u521d\u6b21\u4f1a\u521b\u5efa\u8868\uff0c\u5728\u4e0b\u6b21\u518d\u6b21\u8fd0\u884c\u65f6\uff0c\u4e0d\u4f1a\u518d\u53bb\u91cd\u65b0\u521b\u5efa\u8868\uff0c\u4e0d\u4f1a\u91cd\u65b0\u52a0\u8f7dschema\uff08\u4f30\u8ba1\u9664\u4e86\u7cfb\u7edf\u7528\u6237\u76ee\u4e0b\u6709mysql\u5b89\u88c5\u5305\u5916\u548ctarget\u4e0b\u7684\u8d44\u6e90\uff0c\u8fd8\u6709\u5728\u522b\u7684\u5730\u65b9\u5b58\u5728\u5386\u53f2\u6570\u636e\uff0c\u6240\u4ee5\u4e0b\u6b21\u542f\u52a8\u8fd8\u4f1a\u52a0\u8f7d\u4ee5\u5f80\u6570\u636e\uff09\u3002\u4f46\u8fd9\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\uff0c\u5c31\u7b97\u6211\u4eec\u6bcf\u6b21\u6d4b\u8bd5\u7528\u4f8b\u8dd1\u5b8c\u540e\uff0c\u56de\u6eda\u4e86\u6570\u636e\uff0c\u4f46\u8868\u76f8\u5173\u7684\u5386\u53f2\u64cd\u4f5c\u8fd8\u5728\uff0c\u5982\u81ea\u589e\u4e3b\u952e\u5e8f\u5217\u503c\u4e0d\u4f1a\u56de\u6eda\u3002\u6211\u4eec\u5e0c\u671b\u6d4b\u8bd5\u7528\u4f8b\u6bcf\u6b21\u8dd1\uff0c\u90fd\u662f\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u73af\u5883\u3002<\/strong><\/p>\n\n\n\n<p>\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n\n\n\n<p>1\u3001\u53bb\u9664\u4e0a\u9762config\u4ee3\u7801\u4e2d \u52a0\u8f7dschema\u7684\u90e8\u5206\uff0c\u6362\u6210spring jdbc\u5e2e\u6211\u4eec\u8fdb\u884c\u521d\u59cb\u5316\u6570\u636e\u5e93\u3002\u811a\u672c\u6587\u4ef6\u4e0d\u53d8\uff0c\u53ea\u9700\u8981\u5c06config\u7684\u914d\u7f6e\uff0c\u6362\u6210spring\u914d\u7f6e\u3002\u5982\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-23.png\"><img width=\"447\" height=\"195\" class=\"wp-image-705 lazyload\" title=\"\u5355\u5143\u6d4b\u8bd5\u4e4b\u5d4c\u5165\u5f0fmysql\" src=\"data:image\/gif;base64,R0lGODlhAgABAIAAALGxsQAAACH5BAAAAAAALAAAAAACAAEAAAICBAoAOw==\" data-src=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-23.png\" alt=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-23.png\" data-srcset=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-23.png 596w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-23-300x131.png 300w\" sizes=\"(max-width: 447px) 100vw, 447px\"><\/a><\/figure><\/div>\n\n\n\n<p>spring.datasource\u4e0b\u6709\u4e24\u4e2a\u5c5e\u6027&nbsp;schme\u3001data\uff0c\u5176\u4e2dschema\u4e3a\u8868\u521d\u59cb\u5316\u8bed\u53e5\uff0cdata\u4e3a\u6570\u636e\u521d\u59cb\u5316\uff0c\u9ed8\u8ba4\u52a0\u8f7dschema.sql\u4e0edata.sql\u3002\u811a\u672c\u4f4d\u7f6e\u53ef\u4ee5\u901a\u8fc7<strong>spring.datasource.schema<\/strong>&nbsp; \u4e0e<strong>spring.datasource.data&nbsp;<\/strong>\u6765\u6539\u53d8\u3002<\/p>\n\n\n\n<p><strong>spring.datasource.initialization-mode<\/strong>&nbsp; \u521d\u59cb\u5316\u6a21\u5f0f\uff08springboot2.0\uff09\uff0c\u5176\u4e2d\u6709\u4e09\u4e2a\u503c\uff0calways\u4e3a\u59cb\u7ec8\u6267\u884c\u521d\u59cb\u5316\uff0cembedded\u53ea\u521d\u59cb\u5316\u5185\u5b58\u6570\u636e\u5e93\uff08\u9ed8\u8ba4\u503c\uff09,\u5982h2\u7b49\uff0cnever\u4e3a\u4e0d\u6267\u884c\u521d\u59cb\u5316\u3002<\/p>\n\n\n\n<p>spring jdbc\u4f1a\u5728init datasource\u65f6\uff0c\u4f1a\u53bb\u6267\u884c\u521d\u59cb\u5316\u6570\u636e\u5e93\u7684\u8fc7\u7a0b\u3002\u914d\u7f6e\u4e86always\uff0c\u6bcf\u6b21\u90fd\u4f1a\u6267\u884c\u521d\u59cb\u5316\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u8fbe\u5230\u6bcf\u6b21\u6d4b\u8bd5\u7528\u4f8b\u6267\u884c\uff0c\u73af\u5883\u90fd\u662f\u4e00\u6837\u7684\uff0c\u65b0\u7684\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-24.png\"><img width=\"1194\" height=\"294\" class=\"wp-image-706 lazyload\" title=\"\u5355\u5143\u6d4b\u8bd5\u4e4b\u5d4c\u5165\u5f0fmysql\" src=\"data:image\/gif;base64,R0lGODlhAgABAIAAALGxsQAAACH5BAAAAAAALAAAAAACAAEAAAICBAoAOw==\" data-src=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-24.png\" alt=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-24.png\" data-srcset=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-24.png 1194w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-24-300x74.png 300w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-24-1024x252.png 1024w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-24-768x189.png 768w\" sizes=\"(max-width: 1194px) 100vw, 1194px\"><\/a><\/figure><\/div>\n\n\n\n<p>\u6574\u4e2a\u8fc7\u7a0b\u89c2\u5bdf\uff0c\u53d1\u73b0\u6570\u636e\u5e93\u5ba2\u6237\u7aef\u5de5\u5177\u4e2d\u770b\u4e0d\u5230 \u521d\u59cb\u5316\u521b\u5efa\u7684\u8868\uff0c\u8ddfconfig \u4e2d&nbsp;<strong>ScriptResolver.classPathScript(&#8220;sql\/init.sql&#8221;)&nbsp;<\/strong>\u7684\u65b9\u5f0f\u4e0d\u4e00\u6837\uff0c\u5ba2\u6237\u7aef\u5de5\u5177\u53ef\u4ee5\u770b\u5230\u521b\u5efa\u7684\u8868\u3002\u611f\u89c9\u6574\u4e2a\u914d\u7f6e\u6210always\u540e\uff0cspring jdbc\u7684\u64cd\u4f5c\u50cf\u662f\u63a7\u5236\u5728\u4e00\u4e2a\u5927\u201c\u4e8b\u52a1\u201d\u4e2d\uff0c\u6d4b\u8bd5\u7c7b\u7a0b\u5e8f\u8dd1\u5b8c\u540e\uff0c\u53c8\u56de\u6eda\u4e86\uff0c\u4e0b\u6b21\u518d\u91cd\u65b0\u521d\u59cb\u5316\u521b\u5efa\u8868\u3002<\/p>\n\n\n\n<p>\u5bf9spring-jdbc \u7684\u5904\u7406\u8fc7\u7a0b\u611f\u5174\u8da3\u7684\uff0c\u53ef\u4ee5\u53c2\u8003\u8fd9\u91cc\uff0c\u6709\u6e90\u7801\u5206\u6790\uff1a<a href=\"https:\/\/blog.csdn.net\/yinbucheng\/article\/details\/80164395\">https:\/\/blog.csdn.net\/yinbucheng\/article\/details\/80164395<\/a><\/p>\n\n\n\n<p>2\u3001\u4e0a\u9762\u7b2c\u4e00\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5bf9\u4e8e\u91c7\u7528<strong>jdbc\u3001mybatis\u3001mybatis-plus<\/strong>\u7684\u9879\u76ee\u3002\u5982\u679c\u662f\u4f7f\u7528<strong>hibernate<\/strong>\u6216\u8005<strong>jpa<\/strong>\u7684\u9879\u76ee\uff0c\u76f4\u63a5\u5982\u4e0b\u914d\u7f6e\u5373\u53ef\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">spring:\n  jpa:\n    #\u542f\u52a8\u65f6\u662f\u5426\u521d\u59cb\u5316\u6570\u636e\u5e93-hibernate\n    generate-ddl: true\n    hibernate:\n      ddl-auto: create<\/pre>\n\n\n\n<p>generate-ddl: \u4e3atrue\u65f6\uff0c\u6267\u884cschema\u521b\u5efa\uff0c\u4f1a\u68c0\u6d4bclasspath\u4e0b\u7684import.sql\u6587\u4ef6\uff0c\u5f53\u7136spring.jpa.hibernate.ddl-auto: \u5fc5\u987b\u4e3acreate\/update\/create-drop\uff0cnone\u548cvalidate\u662f\u4e0d\u884c\u7684\u3002<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u4f60\u5982\u679c\u91c7\u7528\u7684\u662fjpa\u7684\u8bdd\uff0c\u76f4\u63a5\u5f15\u5165jpa\u6ce8\u89e3\u7f16\u7801\u4f60\u7684entity\u6570\u636e\u5e93\u5b9e\u4f53\u7c7b\u5373\u53ef\u3002\u4e0d\u9700\u8981sql\u811a\u672c\uff0cjpa\u4f1a\u81ea\u52a8\u5e2e\u4f60\u521b\u5efa\u8868\u3002<\/p>\n\n\n\n<p><strong>&nbsp;\u5728\u4e0a\u8ff0\u4e24\u79cd\u65b9\u5f0f\u4efb\u610f\u4e00\u79cd\u5b8c\u6210\u540e\uff0c\u90fd\u9700\u8981\u5728\u6807\u6ce8\u8be5\u6ce8\u89e3\uff1a@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)<\/strong><\/p>\n\n\n\n<p>\u8868\u660e\u5728\u6bcf\u4e2a\u6d4b\u8bd5\u65b9\u6cd5\u8dd1\u5b8c\u540e\uff0c\u91cd\u65b0\u52a0\u8f7dspring\u4e0a\u4e0b\u6587\uff0c\u56e0\u4e3a\u6211\u4eec\u7684\u6570\u636e\u5e93\u521d\u59cb\u5316\u662f\u7531spring\u5b8c\u6210\u7684\uff0c\u6240\u4ee5\u53ef\u4ee5\u4fdd\u8bc1\u6bcf\u4e2a\u65b9\u6cd5\u90fd\u662f\u72ec\u7acb\u4e92\u4e0d\u5e72\u6270\u7684\u6570\u636e\u5e93\u73af\u5883\u3002<\/p>\n\n\n\n<p>\uff08\u4e0a\u9762\u8fd9\u4e2amodel\u4e3aclass\u7ea7\u522b\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u4e3amethod\u7ea7\u522b\uff0c\u53ea\u4e0d\u8fc7\u90a3\u6837\u9700\u8981\u6bcf\u4e2a\u65b9\u6cd5\u4e0a\u8fdb\u884c\u6807\u8bb0\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u9700\u6c42\u5206\u6790 \u5728\u5355\u5143\u6d4b\u8bd5UnitTest\u4e2d\u7ecf\u5e38\u6709\u8fd9\u6837\u7684\u9700\u6c42\uff0c\u9700\u8981\u6d4b\u8bd5\u6570\u636e\u4e0e\u5f00\u53d1\u6570\u636e\u3001\u751f\u4ea7\u6570\u636e\u7b49\u533a\u5206\u5f00\uff0c\u540c\u65f6\u907f\u514d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[51],"tags":[35,94,93],"_links":{"self":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts\/701"}],"collection":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=701"}],"version-history":[{"count":3,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts\/701\/revisions"}],"predecessor-version":[{"id":709,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts\/701\/revisions\/709"}],"wp:attachment":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}