JavaWeb_(Spring框架)Spring整合Hibernate
Dao层类要继承HibernateDaoSupport.java父类
原先使用Hibernate框架hibernate.cfg.xml配置数据库
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///spring</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connecton.password">123456</property> <!-- 数据库的方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 是否显示sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 是否格式化sql语句 -->
<property name="hibernate.format_sql">true</property>
<!-- 数据库是否帮我们创建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 隔离级别 不可重复读 默认4级别-->
<property name="hibernate.connection.isolation">4</property>
<!-- 与线程绑定 -->
<property name="hibernate.current_session_context_class">thread</property> <mapping resource="com/Gary/domain/User.hbm.xml"/> </session-factory>
</hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///spring</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connecton.password">123456</property> <!-- 数据库的方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 是否显示sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 是否格式化sql语句 -->
<property name="hibernate.format_sql">true</property>
<!-- 数据库是否帮我们创建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 隔离级别 不可重复读 默认4级别-->
<property name="hibernate.connection.isolation">4</property>
<!-- 与线程绑定 -->
<property name="hibernate.current_session_context_class">thread</property> <mapping resource="com/Gary/domain/User.hbm.xml"/> </session-factory>
</hibernate-configuration>
hibernate.cfg.xml
使用Spring框架整合Hibernate后
<!-- 配置数据库 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql:///spring"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean> <!-- 配置sessionFactory -->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref=""></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 开启注解模式 开启包的时候,会开启包下的所有子包-->
<context:component-scan base-package="com.Gary"></context:component-scan> <!-- 配置数据库 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql:///spring"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean> <!-- 配置sessionFactory -->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property> <!-- 配置orm元素据,会自动扫描该包下的 所有 配置文件 --> <property name="mappingDirectoryLocations" value="classpath:com/Gary/domain"></property> </bean> <!-- 配置Action -->
<bean name = "userAction" class="com.Gary.web.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean> <!-- 配置Service -->
<bean name="userService" class="com.Gary.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean> <!--配置Dao -->
<bean name="userDao" class="com.Gary.dao.UserDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> </beans>
applicationContext.xml
在Dao层用到Session session = HibernateUtils.getCurrentSession();获得session可以修改成
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
在Service层用到的关于事务的开启我们可以统一在applicationContext.xml中进行配置
开启事务Transaction beginTransaction = HibernateUtils.getCurrentSession().beginTransaction();
事务回滚beginTransaction.rollback();
提交事务beginTransaction.commit();
在applicationContext.xml中配置事务核心管理器
<!-- 事务的核心管理器 -->
<bean name="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<!-- 需要sessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- 配置事务的通知 -->
<tx:advice id="advice"
transaction-manager="transactionManager">
<tx:attributes>
<!-- 所有的方法都去增强 -->
<tx:method name="*" />
</tx:attributes>
</tx:advice> <!-- 织入 -->
<aop:config>
<!-- 切入点 -->
<aop:pointcut
expression="execution(* com.Gary.service.*.*(..))" id="pc" />
<!-- 配置切面 -->
<aop:advisor advice-ref="advice" pointcut-ref="pc" />
</aop:config>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 开启注解模式 开启包的时候,会开启包下的所有子包-->
<context:component-scan base-package="com.Gary"></context:component-scan> <!-- 配置数据库 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql:///spring"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean> <!-- 配置sessionFactory -->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property> <!-- 配置orm元素据,会自动扫描该包下的 所有 配置文件 --> <property name="mappingDirectoryLocations" value="classpath:com/Gary/domain"></property> </bean> <!-- 事务的核心管理器 -->
<bean name="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<!-- 需要sessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- 配置事务的通知 -->
<tx:advice id="advice"
transaction-manager="transactionManager">
<tx:attributes>
<!-- 所有的方法都去增强 -->
<tx:method name="*" />
</tx:attributes>
</tx:advice> <!-- 织入 -->
<aop:config>
<!-- 切入点 -->
<aop:pointcut
expression="execution(* com.Gary.service.*.*(..))" id="pc" />
<!-- 配置切面 -->
<aop:advisor advice-ref="advice" pointcut-ref="pc" />
</aop:config> <!-- 配置Action -->
<bean name = "userAction" class="com.Gary.web.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean> <!-- 配置Service -->
<bean name="userService" class="com.Gary.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean> <!--配置Dao -->
<bean name="userDao" class="com.Gary.dao.UserDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> </beans>
applicationContext.xml
在web.xml中扩大Session范围,使Session作用于从页面发起请求一直到页面的关闭
<!-- 扩大到Session范围,页面发起请求 到 页面的关闭 -->
<filter>
<filter-name>openSession</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Spring</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <!-- 让spring随着web项目的启动而启动 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 配置spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!-- 扩大到Session范围,页面发起请求 到 页面的关闭 -->
<filter>
<filter-name>openSession</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>
web.xml
JavaWeb_(Spring框架)在Struts+Hibernate框架中引入Spring框架 传送门
现在使用Spring框架整合Struts+Hibernate项目,运行项目
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8"> <link rel="stylesheet" href="css/head.css" />
<link rel="stylesheet" type="text/css" href="css/login.css" />
</head> <body>
<div class="dvhead">
<div class="dvlogo">
<a href="index.html">你问我答</a>
</div>
<div class="dvsearch">10秒钟注册账号,找到你的同学</div>
<div class="dvreg">
已有账号,立即 <a href="login.html">登录</a>
</div>
</div>
<section class="sec">
<form action="${pageContext.request.contextPath }/UserAction_login" method="post">
<div class="register-box">
<label for="username" class="username_label"> 用 户 名 <input
maxlength="20" name="username" type="text" placeholder="您的用户名和登录名" />
</label>
<div class="tips"></div>
</div>
<div class="register-box">
<label for="username" class="other_label"> 密 码 <input
maxlength="20" type="password" name="password"
placeholder="建议至少使用两种字符组合" />
</label>
<div class="tips"></div>
</div>
<div class="arguement">
<input type="checkbox" id="xieyi" /> 阅读并同意 <a
href="javascript:void(0)">《你问我答用户注册协议》</a> <a href="register.html">没有账号,立即注册</a>
<div class="tips" style="color: red" > <s:property value="#error"/> </div>
</div>
<div class="submit_btn">
<button type="submit" id="submit_btn">立 即 登录</button>
</div>
</form>
</section>
<script src="js/index.js" type="text/javascript" charset="utf-8"></script>
</body>
login.jsp
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Spring</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <!-- 让spring随着web项目的启动而启动 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 配置spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!-- 扩大到Session范围,页面发起请求 到 页面的关闭 -->
<filter>
<filter-name>openSession</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>
web.xml
com.Gary.dao
package com.Gary.dao; import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.NativeQuery;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository; import com.Gary.domain.User;
import com.Gary.utils.HibernateUtils; public class UserDao extends HibernateDaoSupport{ public User findUser(User user) { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
//Dao层控制事务 做法是非常不好的 //Session session = HibernateUtils.getCurrentSession();
//sql操作数据库
String sql = "select * from user where username = ? and password = ?";
NativeQuery sqlQuery = session.createSQLQuery(sql);
sqlQuery.addEntity(User.class);
sqlQuery.setParameter(1, user.getUsername());
sqlQuery.setParameter(2, user.getPassword()); User result = (User) sqlQuery.uniqueResult(); return result;
} }
UserDao.java
com.Gary.domain
package com.Gary.domain; public class User { private String id;
private String username;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} }
User.java
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.Gary.domain"> <class name="User" table="user">
<id name="id">
<generator class="uuid"></generator>
</id> <property name="username" column="username"></property>
<property name="password" column="password"></property> </class> </hibernate-mapping>
User.hbm.xml
com.Gary.service
package com.Gary.service; import javax.annotation.Resource; import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import com.Gary.dao.UserDao;
import com.Gary.domain.User;
import com.Gary.utils.HibernateUtils; public class UserService { private UserDao userDao; public boolean findUser(User user) { //Transaction beginTransaction = HibernateUtils.getCurrentSession().beginTransaction();
User temp = null;
try { temp = userDao.findUser(user);
}catch(Exception e){
//beginTransaction.rollback();
} //beginTransaction.commit(); //System.out.println("UserService"+temp); return temp==null?false:true;
} public UserDao getUserDao() {
return userDao;
} public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} }
UserService.java
com.Gary.utils
package com.Gary.utils; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; public class HibernateUtils { private static SessionFactory sessionFactory = null; static {
Configuration config = new Configuration().configure();
sessionFactory = config.buildSessionFactory();
} public static Session getSession()
{
return sessionFactory.openSession();
} public static Session getCurrentSession()
{
return sessionFactory.getCurrentSession();
} }
HibernateUtils.java
com.Gary.web
package com.Gary.web; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller; import com.Gary.domain.User;
import com.Gary.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; public class UserAction extends ActionSupport implements ModelDriven<User>{ public User user = new User(); private UserService userService; public String login() throws Exception { boolean success = userService.findUser(user); //System.out.println("UserService"+user.getUsername()+user.getPassword()); if(success)
{
return "toIndex";
}else {
ActionContext.getContext().put("error", "用户名或者密码错误!!");
return "login";
} } @Override
public User getModel() { return user;
} public UserService getUserService() {
return userService;
} public void setUserService(UserService userService) {
this.userService = userService;
} }
UserAction.java
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 开启注解模式 开启包的时候,会开启包下的所有子包-->
<context:component-scan base-package="com.Gary"></context:component-scan> <!-- 配置数据库 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql:///spring"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean> <!-- 配置sessionFactory -->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property> <!-- 配置orm元素据,会自动扫描该包下的 所有 配置文件 --> <property name="mappingDirectoryLocations" value="classpath:com/Gary/domain"></property> </bean> <!-- 事务的核心管理器 -->
<bean name="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<!-- 需要sessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- 配置事务的通知 -->
<tx:advice id="advice"
transaction-manager="transactionManager">
<tx:attributes>
<!-- 所有的方法都去增强 -->
<tx:method name="*" />
</tx:attributes>
</tx:advice> <!-- 织入 -->
<aop:config>
<!-- 切入点 -->
<aop:pointcut
expression="execution(* com.Gary.service.*.*(..))" id="pc" />
<!-- 配置切面 -->
<aop:advisor advice-ref="advice" pointcut-ref="pc" />
</aop:config> <!-- 配置Action -->
<bean name = "userAction" class="com.Gary.web.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean> <!-- 配置Service -->
<bean name="userService" class="com.Gary.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean> <!--配置Dao -->
<bean name="userDao" class="com.Gary.dao.UserDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> </beans>
applicationContext.xml
hibernate.cfg.xml【可不写】
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///spring</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connecton.password">123456</property> <!-- 数据库的方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 是否显示sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 是否格式化sql语句 -->
<property name="hibernate.format_sql">true</property>
<!-- 数据库是否帮我们创建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 隔离级别 不可重复读 默认4级别-->
<property name="hibernate.connection.isolation">4</property>
<!-- 与线程绑定 -->
<property name="hibernate.current_session_context_class">thread</property> <mapping resource="com/Gary/domain/User.hbm.xml"/> </session-factory>
</hibernate-configuration>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd"> <struts>
<constant name="struts.devMode" value="true"></constant>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <!-- 告诉struts,你不用创建Action,sping来帮你创建Action -->
<constant name="struts.objectFactory" value="spring"></constant> <package name="spring" namespace="/" extends="struts-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="UserAction_*" class="com.Gary.web.UserAction" method="{1}">
<result name="login">/login.jsp</result>
<result name="toIndex" type="redirect">/index.html</result>
</action>
</package>
</struts>
struts.xml
JavaWeb_(Spring框架)Spring整合Hibernate的更多相关文章
- Spring框架 - Spring和Spring框架组成
Spring框架 - Spring和Spring框架组成 Spring是什么?它是怎么诞生的?有哪些主要的组件和核心功能呢? 本文通过这几个问题帮助你构筑Spring和Spring Framework ...
- [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.
前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...
- 跟着刚哥学习Spring框架--Spring容器(二)
Spring容器 启动Spring容器(实例化容器) -- IOC容器读取Bean配置创建Bean实例之前,必须对它进行实例化(加载启动),这样才可以从容器中获取Bean的实例并使用. Bean是S ...
- JavaWeb_(Mybatis框架)MyBatis整合Spring框架
MyBatis + Spring整合开发 a)使用Spring容器用单例模式管理Mybatis的sqlSessionFactory:b)使用Spring管理连接池.数据源等:c)将Dao/Mapper ...
- JavaWeb_(Spring框架)在Struts+Hibernate框架中引入Spring框架
spring的功能:简单来说就是帮我们new对象,什么时候new对象好,什么时候销毁对象. 在MySQL中添加spring数据库,添加user表,并添加一条用户数据 使用struts + hibern ...
- Spring Data初步--整合Hibernate
Spring Data课程中的技术介绍 Hibernate: Hibernate 是一个开放源代码的对象关系映射框架,它对 JDBC 进行了非常轻量级的对象封装,它将 pojo 与数据库表建立映射关系 ...
- JavaWeb_(Spring框架)Spring中IoC与DI概念入门
Spring是于2003 年兴起的一个轻量级的Java 开源框架,它由Rod Johnson创建.传统J2EE应用的开发效率低,Spring作为开源的中间件,提供J2EE应用的各层的解决方案,Spri ...
- Spring学习7-Spring整合Hibernate
一.Springl为什么要整合Hibernate 二者的整合主要是把hibernate中核心的一些类型交给spring管理,这些类型主要包括sessionFactory. transactionM ...
- 【Spring】Spring框架如何集成Hibernate框架
下面个整理一下hibernate和Spring框架的结合. 首先是引入hibernate框架的包.Spring框架的包.数据库驱动包. User.java文件 package cn.shop.bean ...
随机推荐
- 怎样终止HTTP请求
使用 xhr.abort() var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://www.example.com/page.php', tr ...
- POJ 2485 Prim 找最长的边
A国没有高速公路,因此A国的交通很困难.政府意识到了这个问题并且计划建造一些高速公路,以至于可以在不离开高速公路的情况下在任意两座城镇之间行驶. A国的城镇编号为1到N, 每条高速公路连接这两个城镇, ...
- 复习最短路 spfa+dijstra堆优化
题目很简单,, 但是wa了三次,, 用<vector>之前一定要记得clear()...简单说下 spfa的问题 和bell_forman有点类似 每次取出一个点 然后更新 并把更新了的节 ...
- js之数据类型(对象类型——单体内置对象——Math)
Math是一个内置对象,它具有数学常数和函数的属性和方法.Math对象用于执行数学任务,和其它对象不同,Math只是一个静态对象并没有Math()构造函数,实际上,Math()只是一个由js设置的对象 ...
- LCD1602 液晶
它是一种专门用来显示字母.数字.符号的点阵型液晶模块 它是由若干个5x7或者5x10的点阵字符位组成 引脚 时序 操作步骤 初始化 写命令(RS=L)设置显示坐标 写数据(RS=H) 指令 清屏: 指 ...
- VIM从原理上认识^M问题
问题背景 VIM在打开文件的时候如果遇到两种换行符风格(dos与unix)共存的文件,通常会在行尾显示出烦人的^M.如果^M较少,比较容易定位到哪几行出了问题,但是如果^M较多,就很难搞.下面先给出解 ...
- 【leetcode】617. Merge Two Binary Trees
原题 Given two binary trees and imagine that when you put one of them to cover the other, some nodes o ...
- Delphi 类的特性
- python学习笔记:安装boost python库以及使用boost.python库封装
学习是一个累积的过程.在这个过程中,我们不仅要学习新的知识,还需要将以前学到的知识进行回顾总结. 前面讲述了Python使用ctypes直接调用动态库和使用Python的C语言API封装C函数, C+ ...
- IDEA常见问题和设置
1.查看方法的文档:快捷键 Ctrl-Q(Ctrl-J(mac)) 2.历史记录 3.IDEA控制台输出中文乱码问题 4.修改Idea默认的maven等全局设置 5.idea 启动时报错javax.i ...