{"id":218211,"date":"2021-05-22T08:57:51","date_gmt":"2021-05-22T00:57:51","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=218211"},"modified":"2021-05-05T15:58:42","modified_gmt":"2021-05-05T07:58:42","slug":"primary-key-backfill","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/primary-key-backfill.html","title":{"rendered":"MyBatis\u4e2d\u4e3b\u952e\u56de\u586b\u7684\u4e24\u79cd\u5b9e\u73b0\u65b9\u5f0f"},"content":{"rendered":"
\u5bfc\u8bfb<\/td>\n | \u4e3b\u952e\u56de\u586b\u5176\u5b9e\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u7279\u522b\u662f\u5728\u6570\u636e\u6dfb\u52a0\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u6dfb\u52a0\u5b8c\u6570\u636e\u4e4b\u540e\uff0c\u9700\u8981\u83b7\u53d6\u521a\u521a\u6dfb\u52a0\u7684\u6570\u636e id\uff0c\u65e0\u8bba\u662f Jdbc \u8fd8\u662f\u5404\u79cd\u5404\u6837\u7684\u6570\u636e\u5e93\u6846\u67b6\u90fd\u5bf9\u6b64\u63d0\u4f9b\u4e86\u76f8\u5173\u7684\u652f\u6301\uff0c\u672c\u6587\u6211\u5c31\u6765\u548c\u548c\u5927\u5bb6\u5206\u4eab\u4e0b\u6570\u636e\u5e93\u4e3b\u952e\u56de\u586b\u5728 MyBatis \u4e2d\u7684\u4e24\u79cd\u5b9e\u73b0\u601d\u8def\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \u539f\u751f\u5199\u6cd5<\/strong><\/div>\n \u6846\u67b6\u6765\u6e90\u4e8e\u6211\u4eec\u5b66\u8fc7\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u4e3b\u952e\u56de\u586b\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u5728 JDBC \u4e2d\u5c31\u88ab\u652f\u6301\u7684\u5199\u6cd5\uff0c\u6709\u7684\u5c0f\u4f19\u4f34\u53ef\u80fd\u4e0d\u77e5\u9053\u8fd9\u4e00\u70b9\uff0c\u56e0\u6b64\u8fd9\u91cc\u6211\u5148\u6765\u8bf4\u8bf4\u5728 JDBC \u4e2d\u5982\u4f55\u5b9e\u73b0\u4e3b\u952e\u56de\u586b\u3002<\/p>\n JDBC \u4e2d\u5b9e\u73b0\u4e3b\u952e\u56de\u586b\u5176\u5b9e\u975e\u5e38\u5bb9\u6613\uff0c\u4e3b\u8981\u662f\u5728\u6784\u9020 PreparedStatement \u65f6\u6307\u5b9a\u9700\u8981\u4e3b\u952e\u56de\u586b\uff0c\u7136\u540e\u5728\u63d2\u5165\u6210\u529f\u540e\uff0c\u67e5\u8be2\u521a\u521a\u63d2\u5165\u6570\u636e\u7684 id \uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n \r\npublic int insert(Person person) {\r\n Connection con = null;\r\n PreparedStatement ps = null;\r\n ResultSet rs = null;\r\n con = DBUtils.getConnection();\r\n ps = con.prepareStatement(\"INSERT INTO person(username,password,money) VALUES(?,?,?)\", PreparedStatement.RETURN_GENERATED_KEYS);\r\n ps.setObject(1, person.getUsername());\r\n ps.setObject(2, person.getPassword());\r\n ps.setObject(3, person.getMoney());\r\n int i = ps.executeUpdate();\r\n rs = ps.getGeneratedKeys();\r\n int id = -1;\r\n if (rs.next()) {\r\n id = rs.getInt(1);\r\n }\r\n return id;\r\n}\r\n<\/pre>\n |