{"id":1931,"date":"2017-02-17T19:08:05","date_gmt":"2017-02-17T11:08:05","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=1931"},"modified":"2026-08-18T11:21:06","modified_gmt":"2026-08-18T03:21:06","slug":"springmvcspringhibernate%e9%9b%86%e6%88%90%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=1931","title":{"rendered":"SpringMVC+Spring+Hibernate\u96c6\u6210\u95ee\u9898"},"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;\">\u7528 <strong style=\"color: #4870ac;\">SpringMVC + Spring + Hibernate<\/strong> \u642d SSH \u65f6\uff0c\u6700\u5e38\u89c1\u7684\u4e00\u7c7b\u5751\u662f\uff1a<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;\">HibernateTemplate<\/code> \u4e00\u6267\u884c <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> \/ <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;\">update<\/code> \u5c31\u629b <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;\">InvalidDataAccessApiUsageException<\/code>\uff0c\u63d0\u793a\u5199\u64cd\u4f5c\u5728\u53ea\u8bfb\u6a21\u5f0f\uff08<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;\">FlushMode.MANUAL<\/code>\uff09\u4e0b\u4e0d\u88ab\u5141\u8bb8\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u6839\u56e0\u901a\u5e38\u4e0d\u662f Hibernate \u672c\u8eab\u574f\u4e86\uff0c\u800c\u662f <strong style=\"color: #4870ac;\">OpenSessionInViewFilter<\/strong> \u628a Session \u7684 flush \u6a21\u5f0f\u9489\u6b7b\u4e86\uff0c\u800c\u4e1a\u52a1\u65b9\u6cd5\u53c8\u6ca1\u88ab\u58f0\u660e\u5f0f\u4e8b\u52a1\u4fdd\u62a4\u3002\u4e0b\u9762\u6309\u300c\u73b0\u8c61 \u2192 \u539f\u56e0 \u2192 \u4e09\u79cd\u6539\u6cd5 \u2192 \u4e8b\u52a1\u4e0e\u626b\u63cf\u300d\u628a\u8fd9\u6761\u94fe\u8def\u8d70\u5b8c\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\u62a5\u9519\u73b0\u8c61<\/h2>\n<p style=\"margin: 1em 0px;\">\u9879\u76ee\u542f\u52a8\u540e\uff0c\u53ea\u8981\u8d70\u5230\u5199\u5e93\u64cd\u4f5c\uff0c\u5806\u6808\u4f1a\u843d\u5230 <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;\">HibernateTemplate.checkWriteOperationAllowed<\/code>\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;\">org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT\/AUTO or remove 'readOnly' marker from transaction definition.\n\tat org.springframework.orm.hibernate4.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1128)\n\tat org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:621)\n\tat org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:618)<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u4e2d\u6587\u610f\u601d\u5c31\u662f\uff1a\u5f53\u524d Session \u5904\u4e8e\u53ea\u8bfb \/ <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;\">FlushMode.MANUAL<\/code>\u3002\u8981\u4e48\u628a Session \u6539\u6210 <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;\">FlushMode.COMMIT<\/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;\">AUTO<\/code>\uff0c\u8981\u4e48\u53bb\u6389\u4e8b\u52a1\u5b9a\u4e49\u4e0a\u7684 <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;\">readOnly<\/code> \u6807\u8bb0\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;\">\u4e8c\u3001\u539f\u56e0\uff1aOpenSessionInView \u628a Session \u7ed1\u6210\u53ea\u8bfb<\/h2>\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;\">OpenSessionInViewFilter<\/code> \u5728 <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;\">getSession<\/code> \u65f6\uff0c\u4f1a\u628a\u62ff\u5230\u7684 Session \u7684 flush mode \u8bbe\u4e3a <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;\">FlushMode.NEVER<\/code>\uff08\u65b0\u7248\u672c\u5bf9\u5e94 <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;\">MANUAL<\/code>\uff09\uff0c\u518d\u628a <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;\">sessionFactory<\/code> \u7ed1\u5230 <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;\">TransactionSynchronizationManager<\/code>\u3002\u8fd9\u6837\u6574\u4e2a request \u5171\u7528\u540c\u4e00\u4e2a Session\uff0c\u8bf7\u6c42\u7ed3\u675f\u540e\u518d\u89e3\u7ed1\uff0c\u5e76\u6309\u662f\u5426\u5df2\u4e0e transaction \u7ed1\u5b9a\u6765\u51b3\u5b9a\u8981\u4e0d\u8981 <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;\">closeSession<\/code>\u3002<\/p>\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;\">HibernateTemplate<\/code> \u53ea\u6709\u53d1\u73b0\u5f53\u524d Session \u4e0a\u5b58\u5728<strong style=\"color: #4870ac;\">\u975e readOnly \u7684\u4e8b\u52a1<\/strong> \u65f6\uff0c\u624d\u4f1a\u628a flush \u5207\u5230 <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;\">FlushMode.AUTO<\/code>\uff0c\u4ece\u800c\u5141\u8bb8 insert \/ update \/ delete\u3002\u6ca1\u6709\u4e8b\u52a1\u3001\u4e5f\u6ca1\u6709\u4eba\u4e3a\u6539 flush mode\uff0c\u6574\u4e2a <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;\">doFilter<\/code> \u8fc7\u7a0b\u5c31\u4e00\u76f4\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;\">Flush.NEVER<\/code>\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u6240\u4ee5\uff1a<strong style=\"color: #4870ac;\">\u53d7\u58f0\u660e\u5f0f\u4e8b\u52a1\u4fdd\u62a4\u7684\u65b9\u6cd5\u6709\u5199\u6743\u9650<\/strong>\uff0c\u6ca1\u88ab\u4fdd\u62a4\u7684\u65b9\u6cd5\u6ca1\u6709\u3002SSH \u96c6\u6210\u65f6\u6700\u5bb9\u6613\u8e29\u7684\uff0c\u662f Controller \u548c Service \u88ab\u540c\u4e00\u5957 <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;\">component-scan<\/code> \u626b\u8fdb\u53bb\uff0c\u4e8b\u52a1\u5207\u9762\u7ed1\u9519\u5c42\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;\">\u4e09\u3001\u4e09\u79cd\u89e3\u51b3\u529e\u6cd5<\/h2>\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;\">\u529e\u6cd5<\/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;\">\u505a\u4ec0\u4e48<\/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;\">\u9002\u7528<\/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;\">\u65b9\u6cd5\u4e00\uff1a\u62c6\u5f00\u626b\u63cf<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">spring-servlet.xml \u53ea\u626b @Controller\uff1bapplicationContext.xml \u6392\u9664 @Controller<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u63a8\u8350\uff0c\u804c\u8d23\u6e05\u6670<\/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;\">\u65b9\u6cd5\u4e8c\uff1aexecute \u56de\u8c03<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u4e0d\u7528 HibernateTemplate.save\uff0c\u6539\u6210 session.save<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u4e34\u65f6\u7ed5\u8fc7 flush \u68c0\u67e5<\/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;\">\u65b9\u6cd5\u4e09\uff1a\u5408\u5e76\u914d\u7f6e<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u628a spring-servlet.xml \u548c applicationContext.xml \u5408\u6210\u4e00\u4efd<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5c0f\u9879\u76ee\u80fd\u7528\uff0c\u4e0d\u63a8\u8350\u957f\u671f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">\u65b9\u6cd5\u4e00\uff1a\u7236\u5b50\u5bb9\u5668\u5404\u626b\u5404\u7684<\/h3>\n<p style=\"margin: 1em 0px;\">SpringMVC \u7684 <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;\">DispatcherServlet<\/code> \u548c\u6839\u5bb9\u5668 <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;\">ContextLoaderListener<\/code> \u662f\u4e24\u5957\u5bb9\u5668\u3002\u7236\u5bb9\u5668\u7ba1 Service \/ Dao \/ \u4e8b\u52a1\uff0c\u5b50\u5bb9\u5668\u7ba1 Controller\u3002\u626b\u63cf\u8303\u56f4\u91cd\u53e0\u65f6\uff0cController \u4f1a\u5728\u5b50\u5bb9\u5668\u91cc\u518d new \u4e00\u4efd\uff0c\u4e8b\u52a1\u5207\u9762\u7ed1\u4e0d\u4e0a\u3002<\/p>\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;\">spring-servlet.xml<\/code> \u53ea\u626b\u63a7\u5236\u5668\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;context:component-scan base-package=\"com.ittx.spring002.controller\" &gt;\n    &lt;!-- \u53ea\u626b\u63cf base-package \u6307\u5b9a\u5305\u4e0b @Controller \u6ce8\u89e3 --&gt;\n    &lt;context:include-filter type=\"annotation\" expression=\"org.springframework.stereotype.Controller\" \/&gt;\n&lt;\/context:component-scan&gt;<\/code><\/pre>\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;\">applicationContext.xml<\/code> \u626b\u4e1a\u52a1\u5c42\uff0c\u6392\u9664\u63a7\u5236\u5668\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;context:component-scan base-package=\"com.ittx.spring002\"&gt;\n    &lt;!-- \u9664\u4e86 base-package \u5305\u4e0b @Controller \u6ce8\u89e3\u5168\u90fd\u626b\u63cf --&gt;\n    &lt;context:exclude-filter type=\"annotation\" expression=\"org.springframework.stereotype.Controller\" \/&gt;\n&lt;\/context:component-scan&gt;<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">\u65b9\u6cd5\u4e8c\uff1aHibernateCallback \u81ea\u5df1\u62ff Session<\/h3>\n<p style=\"margin: 1em 0px;\">\u539f\u6765\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;\">getHibernateTemplate().save(user)<\/code>\uff0c\u6539\u6210 execute \u56de\u8c03\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;\">getHibernateTemplate().execute(new HibernateCallback&lt;User&gt;() {\n    @Override\n    public User doInHibernate(Session session) throws HibernateException {\n        session.save(user);\n        return null;\n    }\n});<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">\u65b9\u6cd5\u4e09\uff1a\u5408\u5e76\u4e24\u4e2a xml<\/h3>\n<p style=\"margin: 1em 0px;\">\u628a <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;\">spring-servlet.xml<\/code> \u548c <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;\">applicationContext.xml<\/code> \u5408\u6210\u4e00\u4efd\uff0c\u7236\u5b50\u5bb9\u5668\u95ee\u9898\u4f1a\u6d88\u5931\uff0c\u4f46\u914d\u7f6e\u804c\u8d23\u4f1a\u7cca\u5728\u4e00\u8d77\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;\">\u56db\u3001\u4e8b\u52a1\u600e\u4e48\u914d<\/h2>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">1. \u4ee3\u7801\u5f00\u4e8b\u52a1<\/h3>\n<p style=\"margin: 1em 0px;\">\u539f\u6587\u5199\u6cd5\u91cc\u7684 <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;\">beginTrasaction<\/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;\">getTrasaction<\/code> \u662f\u62fc\u5199\u9519\u8bef\uff0c\u6b63\u786e API \u662f\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;\">session.beginTransaction();\nsession.save(user);\nsession.getTransaction().commit();<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">2. \u58f0\u660e\u5f0f\u4e8b\u52a1\uff08tx:advice\uff09<\/h3>\n<p style=\"margin: 1em 0px;\">Hibernate4 \u4e0b <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;\">getCurrentSession()<\/code> \u5fc5\u987b\u5904\u5728\u4e8b\u52a1\u4e2d\u3002\u67e5\u8be2\u65b9\u6cd5\u5373\u4fbf\u53ea\u8bfb\uff0c\u4e5f\u8981\u7ed9 <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;\">REQUIRED<\/code>\uff0c\u540c\u65f6\u53ef\u6807 <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;\">read-only<\/code>\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;tx:annotation-driven transaction-manager=\"txManager\" \/&gt;\n\n&lt;!-- \u58f0\u660e\u5f0f\u5bb9\u5668\u4e8b\u52a1\u7ba1\u7406 --&gt;\n&lt;tx:advice id=\"txAdvice\" transaction-manager=\"txManager\"&gt;\n    &lt;tx:attributes&gt;\n        &lt;tx:method name=\"save*\" propagation=\"REQUIRED\" \/&gt;\n        &lt;tx:method name=\"add*\" propagation=\"REQUIRED\" \/&gt;\n        &lt;!-- hibernate4 \u5fc5\u987b\u5f00\u542f\u4e8b\u52a1\uff0c\u5426\u5219 getCurrentSession() \u83b7\u53d6\u4e0d\u5230 --&gt;\n        &lt;tx:method name=\"get*\" propagation=\"REQUIRED\" read-only=\"true\" \/&gt;\n    &lt;\/tx:attributes&gt;\n&lt;\/tx:advice&gt;\n&lt;aop:config expose-proxy=\"true\"&gt;\n    &lt;!-- \u53ea\u5bf9\u4e1a\u52a1\u903b\u8f91\u5c42\u5b9e\u65bd\u4e8b\u52a1 --&gt;\n    &lt;aop:pointcut id=\"txPointcut\"\n        expression=\"execution(* com.ittx.spring002.server..*.*(..))\" \/&gt;\n    &lt;aop:advisor advice-ref=\"txAdvice\" pointcut-ref=\"txPointcut\" \/&gt;\n&lt;\/aop:config&gt;<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">3. \u6ce8\u89e3\u4e8b\u52a1<\/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;bean id=\"txManager\"\n    class=\"org.springframework.orm.hibernate4.HibernateTransactionManager\"&gt;\n    &lt;property name=\"sessionFactory\" ref=\"sessionFactory\" \/&gt;\n&lt;\/bean&gt;\n\n&lt;!-- \u7528\u6ce8\u89e3\u6765\u5b9e\u73b0\u4e8b\u52a1\u7ba1\u7406 --&gt;\n&lt;tx:annotation-driven transaction-manager=\"txManager\" \/&gt;<\/code><\/pre>\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;\">\u4e94\u3001component-scan \u600e\u4e48\u7528<\/h2>\n<p style=\"margin: 1em 0px;\">xml \u91cc\u914d\u4e86 <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;\">&lt;context:component-scan base-package=\"com.ittx.spring002\" \/&gt;<\/code> \u4e4b\u540e\uff0cSpring \u4f1a\u626b\u8be5\u5305\u53ca\u5b50\u5305\uff0c\u9047\u5230 <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;\">@Component<\/code>\u3001<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;\">@Controller<\/code>\u3001<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;\">@Service<\/code> \u7b49\u5c31\u6ce8\u518c\u6210 bean\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u914d\u4e86 <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;\">component-scan<\/code> \u5c31\u4e0d\u5fc5\u518d\u5199 <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;\">&lt;context:annotation-config\/&gt;<\/code>\uff0c\u524d\u8005\u5df2\u7ecf\u5305\u542b\u540e\u8005\u3002\u5b83\u8fd8\u6709\u4e24\u4e2a\u5b50\u6807\u7b7e\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;context:include-filter \/&gt;\n&lt;context:exclude-filter \/&gt;<\/code><\/pre>\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;\">use-default-filters<\/code> \u9ed8\u8ba4\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;\">true<\/code>\uff0c\u4f1a\u626b\u63cf\u6307\u5b9a\u5305\u4e0b\u5168\u90e8\u5e26 <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;\">@Component<\/code> \u53ca\u5176\u6d3e\u751f\u6ce8\u89e3\uff08<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;\">@Service<\/code>\u3001<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;\">@Repository<\/code> \u7b49\uff09\u7684\u7c7b\u3002\u5982\u679c\u53ea\u60f3\u6309 include-filter \u7cbe\u786e\u626b\uff0c\u628a\u5b83\u6539\u6210 <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;\">false<\/code>\u3002<\/p>\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\uff1aSSH \u5199\u5e93\u62a5 FlushMode.MANUAL\uff0c\u4f18\u5148\u68c0\u67e5 OpenSessionInView \u53e0\u52a0\u4e8b\u52a1\u5207\u9762\u662f\u5426\u7ed1\u5230 Service\uff1b\u7236\u5b50\u5bb9\u5668\u626b\u63cf\u91cd\u53e0\u65f6\uff0c\u7528 include\/exclude \u628a Controller \u548c\u4e1a\u52a1\u5c42\u62c6\u5f00\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u7528 SpringMVC + Spring + Hibernate \u642d SSH \u65f6\uff0c\u6700\u5e38\u89c1\u7684\u4e00\u7c7b\u5751\u662f\uff1aHiber [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[59],"tags":[189,184,185,90],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1931"}],"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=1931"}],"version-history":[{"count":2,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1931\/revisions"}],"predecessor-version":[{"id":1933,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1931\/revisions\/1933"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}