spring 整合hibernate
来源:互联网 发布:淘宝魔切页面 编辑:程序博客网 时间:2024/06/05 10:33
Spring整合hibernate
基本原理:就是由spring来管理hibernate的SessionFactory
方式一:零障碍整合(了解)
spring中提供的一个LocalSessionFacotry来加载Hibernate的配置文件 (需要自己定义一个hibernate.cfg.xml)
<bean id="sessionFactoy" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"><property name="configLocation" value="classpath:hibernate.cfg.xml"></property></bean>
我们必须配置spring的ContextLoaderListener
<bean id="sessionFactoy" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"><property name="configLocation" value="classpath:hibernate.cfg.xml"></property></bean>
方式二 spring管理hibernate配置
不在需要hibernate.cfg.xml文件,所有关于hibernate.cfg.xml文件中的配置都在spring的配置文件中来配置。
需要一个db.properties 文件
jdbc.driverClass=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql:///sshtestjdbc.username=rootjdbc.password=123spring 中的配置<!-- 引入properties 文件 --><context:property-placeholder location="classpath:db.properties"/><!-- 不需要hibernate.cfg.xml 的hibernate 的配置 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcurl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property></bean>
然后创建LocalSessionFacotry 加载连接池
<!-- 加载hibernate配置文件 -->
<bean id="sessionFactoy" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"> <props> <prop key="hiernate.show_sql">true</prop> <prop key="hiernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hiernate.hbm2ddl.auto">update</prop> <prop key="hiernate.format_sql">true</prop> </props> </property> </bean>public class UserDaoImpl extends HibernateDaoSupport implements IUserDao{ @Override public void add(User user) { // TODO Auto-generated method stub this.getHibernateTemplate().save(user); }}xml中声明bean 引入sessionFactoy <!-- 声明dao --> <bean id="userDao" class="com.ithiema.dao.UserDaoImpl"> <!-- 只需要注入sessionFactoyBean 就可以获取hibernate sessionFactoyBean是对hibernate 的图个封装 --> <property name="sessionFactoyBean" ref="sessionFactoyBean"></property> </bean>
spring 整合hibernate 后的dao
要找Dao 上继承HibernateDaoSupport 就可以获得jdbcTemplate
spring 中配置事物管理器
<!-- 事物管理 -->
<!-- 配置事物管理器 --> <bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <!-- 引入dataSource 这里我们用的是dataSource 的封装 sessionFactory --> <property name="sessionFactoryBean" ref="sessionFactoyBean"></property> </bean> <!-- 通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add"/> <tx:method name="update"/> <tx:method name="del"/> </tx:attributes> </tx:advice> <!-- 切面 --> <aop:config><!-- 这里只写了add()方法 如果是想匹配所有的 * *..*(..)--> <aop:pointcut expression="execution(* *.add(..))" id="mypointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="mypointcut"/> </aop:config>
Spring整合struts2框架
(1)spring 管理action.
1.写一个addUser.jsp
<formaction="${pageContext.request.contextPath}/user_add"method="post">
name<inputtype="text"name="username">
age<inputtype="text"name="age">
<inputtype="submit"value="addUser">
</form>
2.在applicationContext.xml 中配置 action
<!-- spring 中整合struts --> <bean id="userAction" class="com.ithiema.action.UserAction"> <property name="userService" ref="userService"></property> </bean>
3.action 类中要提供 目标对象的set方法
public class UserAction extends ActionSupportimplements ModelDriven<User>{
private UserService userService;
public void setUserService(UserService userService) {
this.userService =userService;
}
3.在struts 中配置
<struts>
<packagename="default"namespace="/"extends="struts-default">
<!-- class 就是spring 配置的stutsbean的id -->
<actionname="user_*"class="userAction"method="{1}">
<resultname="success">/success.jsp</result>
</action>
</package>
</struts>
必须在web.xml 中配置struts2 框架的filter
<!-- spring 整合strtus-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
spring整合struts2框架方式二(action中自动注入service)
1.Struts.xml 中的Class需要全类名
<struts>
<packagename="default"namespace="/"extends="struts-default">
<!-- class 就是spring 配置的stutsbean的id -->
<actionname="user_*"class="com.itheima.action.UserAction"method="{1}">
<resultname="success">/success.jsp</result>
</action>
</package>
</struts>
这时 就会将action 中需要的注入到service中
public class UserAction extends ActionSupportimplements ModelDriven<User>{
private UserService userService;
public void setUserService(UserService userService) {
this.userService =userService;
}
在配置文件中配置 自动注入
struts.objectFactory.spring.autoWire=name //表示根据name 自动注入
也可以在struts.xml 中修改注入的方式
<constantname="struts.objectFactory.spring.autoWire"value="type"></constant> <!-- 表示根据type 自动注入-->
总结:
如果struts中的class写的是全类名 则是根据自动注入 自动注入的话就要在 db.properties配置文件中 配置注入的方式
如果不是全类名而是 applicationContext.xml 中配置的action 的bean的 id那么就要在applicationContext.xml中配置action 的bean
记住区别 非自动注入就是在applicationContext.xml 中配置了bean 而且class非全类名
自动注入就是在 db.properties配置文件 中配置了autoWire class 为全类名
*action 中的目标对象要有set方法
阅读全文
0 0
- Spring:Spring整合Hibernate
- 【Spring】Spring+hibernate整合
- struts整合spring整合hibernate
- spring 整合hibernate整合方式
- 整合 struts spring hibernate
- struts+spring+hibernate整合
- struts,hibernate,spring整合
- struts+spring+hibernate整合
- struts+spring+hibernate整合
- Spring+hibernate+DWR整合
- webwork+spring+hibernate整合
- webwork+spring+hibernate整合
- Spring整合Hibernate
- Struts hibernate Spring 整合
- Struts hibernate Spring 整合
- Spring整合Hibernate
- 整合Spring、Struts、Hibernate
- webwork+spring+hibernate整合
- IDEA下maven工程的classpath
- EL JSTL
- RPC(Remote Procedure Call Protocol远程过程调用)
- Android Phone
- 输出金字塔:*,***,*****,*******,*********
- spring 整合hibernate
- nginx 配置详解
- recyclerview禁止水平滑动
- python模块-os,sys
- Java ZIP(二)
- 用construct2制作简单游戏
- Codeforces 876 A Trip For Meal
- C语言位操作
- IT界的大佬——库克