{"id":1895,"date":"2017-01-16T11:46:33","date_gmt":"2017-01-16T03:46:33","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=1895"},"modified":"2026-08-18T11:20:38","modified_gmt":"2026-08-18T03:20:38","slug":"hibernate%e7%bc%93%e5%ad%98","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=1895","title":{"rendered":"Hibernate\u7f13\u5b58"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<p style=\"margin: 1em 0px;\">\u7f13\u5b58\u5939\u5728\u5e94\u7528\u7a0b\u5e8f\u548c\u7269\u7406\u6570\u636e\u6e90\u4e4b\u95f4\uff0c\u7528\u6765\u964d\u4f4e\u5bf9\u6570\u636e\u5e93\u7684\u8bbf\u95ee\u6b21\u6570\u3002\u7f13\u5b58\u91cc\u662f\u6570\u636e\u6e90\u7684\u4e00\u4efd\u62f7\u8d1d\uff0c\u8fd0\u884c\u65f6\u5148\u8bfb\u5199\u7f13\u5b58\uff0c\u518d\u5728\u7279\u5b9a\u65f6\u523b\u628a\u4e24\u8fb9\u540c\u6b65\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u4ecb\u8d28\u4e00\u822c\u662f\u5185\u5b58\uff0c\u6570\u636e\u91cf\u5f88\u5927\u65f6\u4e5f\u4f1a\u843d\u5230\u786c\u76d8\u3002\u5b9e\u73b0\u65f6\u8fd8\u8981\u7ba1\u5e76\u53d1\u548c\u751f\u547d\u5468\u671f\uff0c\u6240\u4ee5\u65e2\u8981\u786c\u4ef6\u4e5f\u8981\u8f6f\u4ef6\u3002<\/p>\n<p style=\"margin: 1em 0px;\">Hibernate \u8fd9\u8fb9\u5206\u4e24\u5c42\uff1a<strong style=\"color: #4870ac;\">Session \u4e00\u7ea7\u7f13\u5b58<\/strong>\u9ed8\u8ba4\u5f00\u3001\u5378\u4e0d\u6389\uff1b<strong style=\"color: #4870ac;\">SessionFactory \u4e8c\u7ea7\u7f13\u5b58<\/strong>\u662f\u53ef\u9009\u63d2\u4ef6\uff0c\u9ed8\u8ba4\u5173\u3002\u67e5\u5bf9\u8c61\u65f6\u5148\u4e00\u7ea7\u3001\u518d\u4e8c\u7ea7\u3001\u6700\u540e\u624d\u6253 SQL\u3002<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e00\u3001\u7f13\u5b58<\/h2>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">1. Hibernate \u7f13\u5b58\u673a\u5236<\/h3>\n<p style=\"margin: 1em 0px;\">Hibernate \u7f13\u5b58\u5305\u62ec Session \u7f13\u5b58\u548c SessionFactory \u7f13\u5b58\u3002SessionFactory \u7f13\u5b58\u53c8\u5206\u5185\u7f6e\u548c\u5916\u7f6e\u3002<\/p>\n<p style=\"margin: 1em 0px;\">Session \u7f13\u5b58\u662f\u5185\u7f6e\u7684\uff0c\u4e0d\u80fd\u5378\u8f7d\uff0c\u4e5f\u53eb<strong style=\"color: #4870ac;\">\u7b2c\u4e00\u7ea7\u7f13\u5b58<\/strong>\u3002SessionFactory \u5185\u7f6e\u7f13\u5b58\u548c Session \u7f13\u5b58\u5b9e\u73b0\u4e0a\u5f88\u50cf\uff1a\u524d\u8005\u662f SessionFactory \u4e00\u4e9b\u96c6\u5408\u5c5e\u6027\u91cc\u7684\u6570\u636e\uff0c\u540e\u8005\u662f Session \u96c6\u5408\u5c5e\u6027\u91cc\u7684\u6570\u636e\u3002\u5185\u7f6e\u7f13\u5b58\u653e\u6620\u5c04\u5143\u6570\u636e\u548c\u9884\u5b9a\u4e49 SQL\u2014\u2014\u5143\u6570\u636e\u662f\u6620\u5c04\u6587\u4ef6\u7684\u62f7\u8d1d\uff0cSQL \u5728\u521d\u59cb\u5316\u9636\u6bb5\u6839\u636e\u5143\u6570\u636e\u63a8\u51fa\u6765\u3002\u5185\u7f6e\u7f13\u5b58\u53ea\u8bfb\uff0c\u5e94\u7528\u6539\u4e0d\u4e86\uff0c\u56e0\u6b64\u4e0d\u5fc5\u548c\u6620\u5c04\u6587\u4ef6\u540c\u6b65\u3002<\/p>\n<p style=\"margin: 1em 0px;\">SessionFactory \u5916\u7f6e\u7f13\u5b58\u662f\u53ef\u914d\u7f6e\u63d2\u4ef6\uff0c\u9ed8\u8ba4\u4e0d\u542f\u7528\u3002\u6570\u636e\u662f\u5e93\u8868\u62f7\u8d1d\uff0c\u4ecb\u8d28\u53ef\u4ee5\u662f\u5185\u5b58\u6216\u786c\u76d8\u3002\u5916\u7f6e\u7f13\u5b58\u4e5f\u53eb<strong style=\"color: #4870ac;\">\u7b2c\u4e8c\u7ea7\u7f13\u5b58<\/strong>\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">2. \u4e24\u79cd\u7f13\u5b58<\/h3>\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e00\u7ea7\u7f13\u5b58<\/strong>\u9ed8\u8ba4\u5f00\u542f\uff0c\u5f00\u53d1\u8005\u914d\u7f6e\u4e0d\u4e86\u3001\u4e5f\u5378\u4e0d\u6389\u3002\u8303\u56f4\u662f Session\uff1a\u540c\u4e00\u4e2a Session \u91cc get\/load \u8fc7\u4e00\u6b21\uff0c\u77ed\u65f6\u95f4\u5185\u518d\u67e5\u540c\u4e00\u6761\uff0c\u76f4\u63a5\u4ece\u7f13\u5b58\u62ff\uff0c\u4e0d\u518d\u53d1 SQL\u3002Session \u5173\u6389\u5c31\u6ca1\u4e86\u3002<\/p>\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e8c\u7ea7\u7f13\u5b58<\/strong>\u662f SessionFactory \u7ea7\u522b\u3002SessionFactory \u6bd4\u8f83\u91cd\uff0c\u5185\u5b58\u91cc\u901a\u5e38\u5355\u4f8b\u3002\u67e5\u8be2\u7ed3\u679c\u53ef\u4ee5\u653e\u8fdb\u4e8c\u7ea7\u7f13\u5b58\uff0c\u5373\u4fbf\u4e0d\u662f\u540c\u4e00\u4e2a Session\uff0c\u53ea\u8981\u5de5\u5382\u8fd8\u5728\uff0c\u4f1a\u5148\u67e5\u4e8c\u7ea7\uff0c\u6ca1\u6709\u518d\u53d1 SQL\u3002<\/p>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>\u4e00\u7ea7\u5c5e\u4e8e\u4e8b\u52a1\u8303\u56f4\uff0cHibernate \u81ea\u5df1\u7ba1\uff0c\u7a0b\u5e8f\u5458\u4e0d\u7528\u5e72\u9884\u3002<\/section>\n<\/li>\n<li>\n<section>\u4e8c\u7ea7\u5c5e\u4e8e\u8fdb\u7a0b\u8303\u56f4\u548c\u96c6\u7fa4\u8303\u56f4\uff0c\u53ef\u4ee5\u914d\u7f6e\uff0c\u4e5f\u80fd\u52a8\u6001\u5378\u8f7d\/\u52a0\u8f7d\u3002<\/section>\n<\/li>\n<\/ul>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e8c\u3001Hibernate \u7f13\u5b58\u539f\u7406<\/h2>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">1. \u4e00\u7ea7\u7f13\u5b58\uff1aSession \u7684\u7f13\u5b58<\/h3>\n<p style=\"margin: 1em 0px;\">Session \u5185\u7f6e\u3001\u4e0d\u80fd\u5378\u8f7d\u3002\u751f\u547d\u5468\u671f\u901a\u5e38\u5bf9\u5e94\u4e00\u4e2a\u6570\u636e\u5e93\u4e8b\u52a1\u6216\u4e00\u4e2a\u5e94\u7528\u4e8b\u52a1\u3002\u4e00\u7ea7\u7f13\u5b58\u91cc\uff0c\u6301\u4e45\u5316\u7c7b\u7684\u6bcf\u4e2a\u5b9e\u4f8b\u90fd\u6709\u552f\u4e00 OID\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">2. \u4e8c\u7ea7\u7f13\u5b58\uff1aSessionFactory \u7684\u7f13\u5b58<\/h3>\n<p style=\"margin: 1em 0px;\">SessionFactory \u751f\u547d\u5468\u671f\u8986\u76d6\u6574\u4e2a\u5e94\u7528\uff0c\u6240\u4ee5\u4e8c\u7ea7\u7f13\u5b58\u662f\u8fdb\u7a0b\u8303\u56f4\u6216\u96c6\u7fa4\u8303\u56f4\uff0c\u53ef\u80fd\u5e76\u53d1\uff0c\u9700\u8981\u5e76\u53d1\u8bbf\u95ee\u7b56\u7565\u6765\u63d0\u4f9b\u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\u3002\u5b83\u662f\u53ef\u9009\u63d2\u4ef6\uff0c\u9ed8\u8ba4\u4e0d\u542f\u7528\u3002<\/p>\n<p style=\"margin: 1em 0px;\">Hibernate \u63d0\u4f9b <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">org.hibernate.cache.CacheProvider<\/code>\uff0c\u5145\u5f53\u7f13\u5b58\u63d2\u4ef6\u548c\u6846\u67b6\u4e4b\u95f4\u7684\u9002\u914d\u5668\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u4ec0\u4e48\u6837\u7684\u6570\u636e\u9002\u5408\u653e\u4e8c\u7ea7\u7f13\u5b58\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\u9002\u5408\uff1a\n1) \u5f88\u5c11\u88ab\u4fee\u6539\u7684\u6570\u636e\n2) \u4e0d\u662f\u5f88\u91cd\u8981\u7684\u6570\u636e\uff0c\u5141\u8bb8\u51fa\u73b0\u5076\u5c14\u5e76\u53d1\n3) \u4e0d\u4f1a\u88ab\u5e76\u53d1\u8bbf\u95ee\u7684\u6570\u636e\n4) \u5e38\u91cf\u6570\u636e\n\n\u4e0d\u9002\u5408\uff1a\n1) \u7ecf\u5e38\u88ab\u4fee\u6539\u7684\u6570\u636e\n2) \u7edd\u5bf9\u4e0d\u5141\u8bb8\u5e76\u53d1\u8bbf\u95ee\u7684\u6570\u636e\uff0c\u5982\u8d22\u52a1\u6570\u636e\n3) \u4e0e\u5176\u4ed6\u5e94\u7528\u5171\u4eab\u7684\u6570\u636e<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">3. \u5ef6\u8fdf\u52a0\u8f7d\u8981\u89e3\u51b3\u7684\u4e24\u4e2a\u95ee\u9898<\/h3>\n<p style=\"margin: 1em 0px;\">Hibernate Session \u662f <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">java.sql.Connection<\/code> \u7684\u4e00\u5c42\u5c01\u88c5\uff0c\u4e00\u4e2a Session \u5bf9\u5e94\u4e00\u4e2a Connection\u3002<\/p>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>HTTP \u8bf7\u6c42\u7ed3\u675f\u540e\u6b63\u786e\u5173\u95ed Session\uff08\u8fc7\u6ee4\u5668\u8d1f\u8d23\u5173\uff09\u3002<\/section>\n<\/li>\n<li>\n<section>\u5ef6\u8fdf\u52a0\u8f7d\u5fc5\u987b\u4fdd\u8bc1\u662f\u540c\u4e00\u4e2a Session\uff08Session \u7ed1\u5728 ThreadLocal \u4e0a\uff09\u3002<\/section>\n<\/li>\n<\/ul>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">4. \u6309 ID \u67e5\u627e\u65f6\u600e\u4e48\u7528\u7f13\u5b58<\/h3>\n<ol style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem;\">\n<li>\n<section>\u5148\u4ece Session \u4e00\u7ea7\u7f13\u5b58\u67e5<\/section>\n<\/li>\n<li>\n<section>\u6ca1\u6709\uff0c\u4e14\u914d\u7f6e\u4e86\u4e8c\u7ea7\u7f13\u5b58\uff0c\u518d\u4ece\u4e8c\u7ea7\u67e5<\/section>\n<\/li>\n<li>\n<section>\u90fd\u6ca1\u6709\uff0c\u518d\u67e5\u6570\u636e\u5e93\uff0c\u628a\u7ed3\u679c\u6309 ID \u653e\u5165\u7f13\u5b58<\/section>\n<\/li>\n<\/ol>\n<p style=\"margin: 1em 0px;\">\u5220\u9664\u3001\u66f4\u65b0\u3001\u589e\u52a0\u65f6\uff0c\u540c\u65f6\u66f4\u65b0\u7f13\u5b58\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">5. \u4e00\u7ea7\u7f13\u5b58\u4e0e\u4e8c\u7ea7\u7f13\u5b58\u5bf9\u6bd4<\/h3>\n<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u5bf9\u6bd4\u9879<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u4e00\u7ea7\u7f13\u5b58<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u4e8c\u7ea7\u7f13\u5b58<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5b58\u653e\u6570\u636e\u7684\u5f62\u5f0f<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u76f8\u4e92\u5173\u8054\u7684\u6301\u4e45\u5316\u5bf9\u8c61<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5bf9\u8c61\u7684\u6563\u88c5\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7f13\u5b58\u7684\u8303\u56f4<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u4e8b\u52a1\u8303\u56f4\uff0c\u6bcf\u4e2a\u4e8b\u52a1\u90fd\u6709\u5355\u72ec\u7684\u4e00\u7ea7\u7f13\u5b58<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u8fdb\u7a0b\u8303\u56f4\u6216\u96c6\u7fa4\u8303\u56f4\uff0c\u540c\u4e00\u8fdb\u7a0b\u6216\u96c6\u7fa4\u5185\u6240\u6709\u4e8b\u52a1\u5171\u4eab<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5e76\u53d1\u8bbf\u95ee\u7b56\u7565<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u6bcf\u4e2a\u4e8b\u52a1\u5355\u72ec\u4e00\u4efd\uff0c\u65e0\u5e76\u53d1\uff0c\u65e0\u987b\u7b56\u7565<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u591a\u4e8b\u52a1\u4f1a\u540c\u65f6\u8bbf\u95ee\u76f8\u540c\u6570\u636e\uff0c\u5fc5\u987b\u63d0\u4f9b\u5e76\u53d1\u7b56\u7565\u4ee5\u4fdd\u8bc1\u9694\u79bb\u7ea7\u522b<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u6570\u636e\u8fc7\u671f\u7b56\u7565<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5bf9\u8c61\u4e0d\u4f1a\u8fc7\u671f\uff0c\u9664\u975e\u5e94\u7528\u663e\u5f0f\u6e05\u7a7a<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5fc5\u987b\u63d0\u4f9b\u8fc7\u671f\u7b56\u7565\uff1a\u5185\u5b58\u6700\u5927\u6570\u76ee\u3001\u6700\u957f\u5b58\u6d3b\u3001\u6700\u957f\u7a7a\u95f2<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7269\u7406\u4ecb\u8d28<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5185\u5b58<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5185\u5b58\u548c\u786c\u76d8\uff1b\u5185\u5b58\u6570\u76ee\u8fbe\u5230 maxElementsInMemory \u540e\u5176\u4f59\u5199\u5165\u786c\u76d8\u7f13\u5b58<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u8f6f\u4ef6\u5b9e\u73b0<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5305\u542b\u5728 Hibernate Session \u5b9e\u73b0\u4e2d<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7b2c\u4e09\u65b9\u63d0\u4f9b\uff0cHibernate \u53ea\u7ed9\u9002\u914d\u5668<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u542f\u7528\u65b9\u5f0f<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u901a\u8fc7 Session \u505a\u4fdd\u5b58\u3001\u66f4\u65b0\u3001\u5220\u9664\u3001\u52a0\u8f7d\u3001\u67e5\u8be2\u5c31\u4f1a\u542f\u7528\uff1b\u6279\u91cf\u64cd\u4f5c\u4e0d\u60f3\u8d70\u7f13\u5b58\u53ef\u76f4\u63a5\u7528 JDBC API<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u53ef\u6309\u5355\u4e2a\u7c7b\u6216\u96c6\u5408\u914d\u7f6e\uff1b\u7ecf\u5e38\u8bfb\u3001\u5f88\u5c11\u6539\u7684\u7c7b\u624d\u503c\u5f97\u5f00\uff1b\u6ca1\u914d\u7f6e\u5c31\u4e0d\u4f1a\u8fdb\u4e8c\u7ea7\u7f13\u5b58<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7528\u6237\u7ba1\u7406\u65b9\u5f0f<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5185\u5b58\u6709\u9650\uff0c\u8981\u7528\u68c0\u7d22\u7b56\u7565\u9650\u5236\u52a0\u8f7d\u6570\u91cf\uff1bSession.evict() \u53ef\u6e05\u7a7a\u6307\u5b9a\u5bf9\u8c61\uff0c\u4f46\u4e0d\u63a8\u8350<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u9009\u54ea\u4e9b\u7c7b\u5f00\u4e8c\u7ea7\u7f13\u5b58\u5e76\u8bbe\u5e76\u53d1\u7b56\u7565\uff1b\u9009\u9002\u914d\u5668\u5e76\u8bbe\u8fc7\u671f\u7b56\u7565\u3002SessionFactory.evict() \u4e5f\u80fd\u6e05\u7a7a\uff0c\u4f46\u4e0d\u63a8\u8350<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e09\u3001\u7f13\u5b58\u673a\u5236\u5e94\u7528<\/h2>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">1. \u4e00\u7ea7\u7f13\u5b58\u7684\u7ba1\u7406<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">evict(Object obj)   \/\/ \u5c06\u6307\u5b9a\u6301\u4e45\u5316\u5bf9\u8c61\u4ece\u4e00\u7ea7\u7f13\u5b58\u6e05\u9664\uff0c\u91ca\u653e\u5185\u5b58\uff0c\u5bf9\u8c61\u4ece\u6301\u4e45\u5316\u53d8\u4e3a\u8131\u7ba1\nclear()             \/\/ \u6e05\u9664\u4e00\u7ea7\u7f13\u5b58\u4e2d\u6240\u6709\u6301\u4e45\u5316\u5bf9\u8c61\ncontains(Object obj)\/\/ \u5224\u65ad\u6307\u5b9a\u5bf9\u8c61\u662f\u5426\u5728\u4e00\u7ea7\u7f13\u5b58\u4e2d\nflush()             \/\/ \u5237\u65b0\u4e00\u7ea7\u7f13\u5b58\uff0c\u4f7f\u4e4b\u4e0e\u6570\u636e\u5e93\u540c\u6b65<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">2. \u4e00\u7ea7\u7f13\u5b58\u5e94\u7528<\/h3>\n<p style=\"margin: 1em 0px;\"><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">save()<\/code> \u4e4b\u540e\u5bf9\u8c61\u4f1a\u8fdb Session \u7f13\u5b58\uff1b<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">get()<\/code> \/ <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">load()<\/code> \u4ece\u5e93\u53d6\u51fa\u540e\u4e5f\u4f1a\u8fdb\u7f13\u5b58\u3002HQL\u3001QBC \u67e5\u51fa\u6765\u7684\u540c\u6837\u5982\u6b64\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">@Test\npublic void testQueryOneCache() {\n    Session session = HibernateUtil.getSessionFactory().getCurrentSession();\n    session.beginTransaction();\n    Query query = session.createQuery(\"FROM Student\");\n    List&lt;Student&gt; list = query.list();\n    for (Student stu : list) {\n        System.out.println(stu);\n        \/\/ if (stu.getNumber() != 1) {\n        \/\/     session.evict(stu); \/\/ \u6e05\u9664\u6307\u5b9a\u7f13\u5b58\u5bf9\u8c61\n        \/\/ }\n    }\n    Student student = (Student) session.get(Student.class, 1);\n    System.out.println(student);\n    \/\/ \u6e05\u9664 session \u7f13\u5b58\u4e24\u79cd\u65b9\u5f0f\uff1aclear()\u3001\u5173\u95ed session\n    session.clear();\n\n    System.out.println(\"--------------------\");\n    Student stu2 = (Student) session.get(Student.class, 1);\n    System.out.println(stu2);\n\n    session.getTransaction().commit();\n}<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">3. \u4e8c\u7ea7\u7f13\u5b58\u7684\u7ba1\u7406<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">sessionFactory.evict(Customer.class, new Integer(1));\n\/\/ evict(Class, Serializable) \u5c06\u67d0\u4e2a\u7c7b\u6307\u5b9a ID \u7684\u5bf9\u8c61\u4ece\u4e8c\u7ea7\u7f13\u5b58\u6e05\u9664\n\nsessionFactory.evict(Customer.class);\n\/\/ evict(Class) \u5c06\u6307\u5b9a\u7c7b\u7684\u6240\u6709\u6301\u4e45\u5316\u5bf9\u8c61\u4ece\u4e8c\u7ea7\u7f13\u5b58\u6e05\u9664\n\nsessionFactory.evictCollection(\"Customer.orders\");\n\/\/ evictCollection(String) \u5c06\u6307\u5b9a\u7c7b\u7684\u6307\u5b9a\u96c6\u5408\u4ece\u4e8c\u7ea7\u7f13\u5b58\u6e05\u9664<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">4. \u4e8c\u7ea7\u7f13\u5b58\u7684\u914d\u7f6e<\/h3>\n<p style=\"margin: 1em 0px;\">\u5e38\u7528\u63d2\u4ef6\uff1a<\/p>\n<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u63d2\u4ef6<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">Provider<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">EHCache<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">org.hibernate.cache.EhCacheProvider<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">OSCache<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">org.hibernate.cache.OSCacheProvider<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">SwarmCache<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">org.hibernate.cache.SwarmCacheProvider<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">JBossCache<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">org.hibernate.cache.TreeCacheProvider<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u56db\u3001\u4e8c\u7ea7\u7f13\u5b58\u4ee5 EHCache \u4e3a\u4f8b<\/h2>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">1. \u5bfc\u5165 ehcache \u5305<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">ehcache-core-2.4.3.jar\nhibernate-ehcache-4.3.11.Final.jar\nslf4j-api-1.6.1.jar<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">2. \u521b\u5efa ehcache.xml<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;ehcache&gt;\n  &lt;!-- \u7f13\u5b58\u5bf9\u8c61\u8d85\u8fc7\u6307\u5b9a\u6570\u91cf\u540e\uff0c\u843d\u5230\u78c1\u76d8\u7684\u5730\u5740 --&gt;\n  &lt;diskStore path=\"E:\/ehcache\"\/&gt;\n\n  &lt;!-- \u9ed8\u8ba4 cache\uff1a\u6ca1\u6709\u5bf9\u5e94\u7279\u5b9a\u533a\u57df\u65f6\u4f7f\u7528 --&gt;\n  &lt;defaultCache maxElementsInMemory=\"10000\"\n                eternal=\"false\"\n                timeToIdleSeconds=\"300\"\n                timeToLiveSeconds=\"600\"\n                overflowToDisk=\"false\"\/&gt;\n  &lt;!-- \u6307\u5b9a\u533a\u57df cache\uff1aname \u5bf9\u5e94 Hibernate \u4e2d\u7684\u533a\u57df\u540d --&gt;\n  &lt;cache name=\"com.ittx.hibernate001.model.Student\"\n                eternal=\"false\"\n                maxElementsInMemory=\"100\"\n                timeToIdleSeconds=\"1200\"\n                timeToLiveSeconds=\"1200\"\n                overflowToDisk=\"false\"&gt;\n  &lt;\/cache&gt;\n&lt;\/ehcache&gt;<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u53c2\u6570\u542b\u4e49\uff1aname \u533a\u57df\u540d\uff1bmaxElementsInMemory \u5185\u5b58\u6700\u5927\u6570\u76ee\uff1bmaxElementsOnDisk \u78c1\u76d8\u6700\u5927\u6570\u76ee\uff1beternal \u662f\u5426\u6c38\u4e0d\u8fc7\u671f\uff1boverflowToDisk \u662f\u5426\u6ea2\u51fa\u5230\u786c\u76d8\uff1btimeToIdleSeconds \u7a7a\u95f2\u591a\u5c11\u79d2\u540e\u9500\u6bc1\uff1btimeToLiveSeconds \u5b58\u6d3b\u591a\u5c11\u79d2\u540e\u9500\u6bc1\uff1bmemoryStoreEvictionPolicy \u7f13\u5b58\u7b97\u6cd5\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5e38\u89c1\u7b97\u6cd5\u4e09\u79cd\uff1a<\/p>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section><strong style=\"color: #4870ac;\">LRU<\/strong>\uff08Least Recently Used\uff09\uff1a\u65b0\u5bf9\u8c61\u66ff\u6362\u6389\u6700\u8fd1\u5f88\u5c11\u4f7f\u7528\u7684\u5bf9\u8c61\uff08\u9ed8\u8ba4\uff09<\/section>\n<\/li>\n<li>\n<section><strong style=\"color: #4870ac;\">LFU<\/strong>\uff08Least Frequently Used\uff09\uff1a\u66ff\u6362\u547d\u4e2d\u7387\u8f83\u4f4e\u7684\u5bf9\u8c61<\/section>\n<\/li>\n<li>\n<section><strong style=\"color: #4870ac;\">FIFO<\/strong>\uff08First In First Out\uff09\uff1a\u66ff\u6362\u6700\u65e9\u8fdb\u5165\u4e8c\u7ea7\u7f13\u5b58\u7684\u5bf9\u8c61<\/section>\n<\/li>\n<\/ul>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">3. hibernate.cfg.xml<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">&lt;!-- \u9ad8\u901f\u7f13\u5b58\u63d0\u4f9b\u7a0b\u5e8f --&gt;\n&lt;property name=\"hibernate.cache.region.factory_class\"&gt;org.hibernate.cache.ehcache.EhCacheRegionFactory&lt;\/property&gt;\n&lt;!-- \u8bbe\u7f6e\u4e8c\u7ea7\u7f13\u5b58\u63d2\u4ef6 EHCache \u7684 Provider \u7c7b --&gt;\n&lt;property name=\"hibernate.cache.provider_class\"&gt;org.hibernate.cache.EhCacheProvider&lt;\/property&gt;\n&lt;!-- enable second level cache and query cache --&gt;\n&lt;property name=\"hibernate.cache.use_second_level_cache\"&gt;true&lt;\/property&gt;\n&lt;!-- \u542f\u52a8\u67e5\u8be2\u7f13\u5b58\uff1afindall()\u3001list()\u3001Iterator()\u3001createCriteria()\u3001createQuery() \u7684\u7ed3\u679c\u96c6\u8981\u7f13\u5b58\u5fc5\u987b\u914d\u8fd9\u9879 --&gt;\n&lt;property name=\"hibernate.cache.use_query_cache\"&gt;true&lt;\/property&gt;\n&lt;!-- \u6307\u5b9a\u7f13\u5b58\u914d\u7f6e\u6587\u4ef6\u4f4d\u7f6e --&gt;\n&lt;property name=\"hibernate.cache.provider_configuration_file_resource_path\"&gt;ehcache.xml&lt;\/property&gt;\n&lt;!-- \u5f3a\u5236 Hibernate \u4ee5\u66f4\u4eba\u6027\u5316\u7684\u683c\u5f0f\u5c06\u6570\u636e\u5b58\u5165\u4e8c\u7ea7\u7f13\u5b58 --&gt;\n&lt;property name=\"hibernate.cache.use_structured_entries\"&gt;true&lt;\/property&gt;\n&lt;!-- Hibernate \u5c06\u6536\u96c6\u6709\u52a9\u4e8e\u6027\u80fd\u8c03\u8282\u7684\u7edf\u8ba1\u6570\u636e --&gt;\n&lt;property name=\"hibernate.generate_statistics\"&gt;true&lt;\/property&gt;<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">4. \u914d\u7f6e model.hbm.xml<\/h3>\n<p style=\"margin: 1em 0px;\">\u5728 class \u6807\u7b7e\u4e0b\u6dfb\u52a0\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">&lt;cache usage=\"read-write\"\/&gt;<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">5. \u6d4b\u8bd5\u4ee3\u7801<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">@Test\npublic void testQueryCacheTwo() {\n    Session session = HibernateUtil.getSessionFactory().getCurrentSession();\n    session.beginTransaction();\n\n    System.out.println(\"\u7b2c\u4e00\u6b21\u67e5\u8be2Student\");\n    Query q = session.createQuery(\"from Student\");\n    q.setCacheable(true);\n    q.list();\n    System.out.println(\"\u653e\u8fdb\u4e8c\u7ea7\u7f13\u5b58\");\n\n    session.getTransaction().commit();\n\n    session = HibernateUtil.getSessionFactory().getCurrentSession();\n    session.beginTransaction();\n\n    System.out.println(\"\u7b2c\u4e8c\u6b21\u67e5\u8be2Student,\u5c06\u4e0d\u4f1a\u53d1\u51fasql\");\n    q = session.createQuery(\"from Student\");\n    q.setCacheable(true);\n    q.list();\n\n    session.getTransaction().commit();\n\n    \/\/ \u914d\u7f6e\u6587\u4ef6\u6253\u5f00 generate_statistics \u540e\uff0c\u53ef\u4ee5\u5f97\u5230\u4e8c\u7ea7\u7f13\u5b58\u547d\u4e2d\u6b21\u6570\u7b49\u6570\u636e\n    Statistics s = HibernateUtil.getSessionFactory().getStatistics();\n    System.out.println(s);\n    System.out.println(\"put:\" + s.getSecondLevelCachePutCount());\n    System.out.println(\"hit:\" + s.getSecondLevelCacheHitCount());\n    System.out.println(\"miss:\" + s.getSecondLevelCacheMissCount());\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u5173\u4e8e<strong style=\"color: #4870ac;\">\u67e5\u8be2\u7f13\u5b58<\/strong>\uff1a\u4e00\u7ea7\u4e0e\u4e8c\u7ea7\u53ea\u80fd\u7f13\u5b58\u5b9e\u4f53\u5bf9\u8c61\uff1b\u67e5\u8be2\u7f13\u5b58\u53ef\u4ee5\u7f13\u5b58\u7ed3\u679c\u96c6\uff08\u542b\u666e\u901a\u5c5e\u6027\uff09\u3002\u53ea\u6709\u7ecf\u5e38\u4f7f\u7528\u540c\u6837\u53c2\u6570\u7684\u67e5\u8be2\u624d\u5212\u7b97\uff0c\u751f\u547d\u5468\u671f\u76f4\u5230\u5c5e\u6027\u88ab\u4fee\u6539\u4e3a\u6b62\u3002\u5bf9\u5b9e\u4f53\u7ed3\u679c\u96c6\uff0c\u67e5\u8be2\u7f13\u5b58\u53ea\u7f13\u5b58 id\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">&lt;!-- \u5f00\u542f\u67e5\u8be2\u7f13\u5b58\uff0c\u9ed8\u8ba4\u662f\u5173\u95ed\u7684 --&gt;\n&lt;property name=\"hibernate.cache.use_query_cache\"&gt;true&lt;\/property&gt;\n\/\/ \u7a0b\u5e8f\u4e2d\u5e94\u5f00\u542f\uff1a\nQuery query = session.createQuery(\"select s.name from Student s\");\nquery.setCacheable(true); \/\/ \u5f00\u542f\u67e5\u8be2\u7f13\u5b58<\/code><\/pre>\n<blockquote style=\"display: block; font-size: 0.9em; border-left: 3px solid #4870ac; padding: 0.5em 1em; margin: 0px; background: #f6f8fa; color: #40464f;\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1a\u6309 ID \u67e5\u5bf9\u8c61\u65f6\u5148 Session \u4e00\u7ea7\u3001\u518d SessionFactory \u4e8c\u7ea7\u3001\u6700\u540e\u624d\u6253 SQL\uff1b\u4e8c\u7ea7\u7f13\u5b58\u7528 EHCache \u65f6\u8981\u540c\u65f6\u5f00 use_second_level_cache\uff0c\u67e5\u8be2\u7f13\u5b58\u8fd8\u8981 setCacheable(true)\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u7f13\u5b58\u5939\u5728\u5e94\u7528\u7a0b\u5e8f\u548c\u7269\u7406\u6570\u636e\u6e90\u4e4b\u95f4\uff0c\u7528\u6765\u964d\u4f4e\u5bf9\u6570\u636e\u5e93\u7684\u8bbf\u95ee\u6b21\u6570\u3002\u7f13\u5b58\u91cc\u662f\u6570\u636e\u6e90\u7684\u4e00\u4efd\u62f7\u8d1d\uff0c\u8fd0\u884c\u65f6\u5148\u8bfb\u5199\u7f13\u5b58\uff0c\u518d\u5728 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[89],"tags":[189],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1895"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1895"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1895\/revisions"}],"predecessor-version":[{"id":1917,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1895\/revisions\/1917"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}