hibernate+spring的整合

来源:互联网 发布:手机淘宝怎么贷款 编辑:程序博客网 时间:2024/05/24 23:15

一,新建一个web工程,添加hibernate的支持(要选择copy方式来引入jar包,不要用映射方式的,而且要注意不要创建hibernatesessionFactory的类

二,随即添加spring的支持(要选择copy方式来引入jar包,不要用映射方式的,在选择的时候选择五个jar包,前面三个,和webmis

三,偷梁换柱,把asm2.2.3的这个包删掉,build path -->config build path -- > remove asm2.2.3 ,删掉这里面的之后,还要删除lib包下面的jar

四,把log4j属性文件放置src目录下面

五,先对数据库的表反转生成pojo类和DAO的方法,然后把dao层的方法移到dao层,然后建立service的包和action的包

六,配置hibernate.cfg.xml中的一些配置项(要使用连接池要手工的导入c3p0的包

<property name="show_sql">true</property>

<property name="connection.driver_class">org.hibernate.connection.C3P0ConnectionProvider</property>

<property name="cache.provider_class">org.hibernate.cache.OSCacheProvider</property>

<property name="hbm2ddl.auto">update</property>

七,写一个测试的方法得到一张表的所有内容,到页面显示

action中的内容:action是要交个springMVC-servlet.xml里面来注册的

@Controller

@RequestMapping("/article.do")

public class ArticleAction {

@Resource private PersonService personService ;

@RequestMapping(params="method=getAllArticles")

public void getAllArticles(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{

List<Articles> articlesList = personService.getArticlesList();

request.setAttribute("list", articlesList);

request.getRequestDispatcher("/myjsp.jsp").forward(request, response);

}

}

七,把applicationContext.xml中的头子换掉,然后在里面配置事务管理

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="configLocation"

value="classpath:hibernate.cfg.xml">

</property>

</bean>

<!-- 事务管理器 -->

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory"/>

</bean>

<!-- 注解方式的事务 -->

<tx:annotation-driven transaction-manager="transactionManager"/>

<!-- xml的事务管理 -->

<!-- <aop:config>

<aop:pointcut id="mypt" expression="execution(* hwt.service.PersonService.*(..))"/>

<aop:advisor advice-ref="mytx" pointcut-ref="mypt"/>

</aop:config>

<tx:advice id="mytx" transaction-manager="transactionManager">

<tx:attributes>

<tx:method name="get*" propagation="NOT_SUPPORTED"/>

<tx:method name="*"/>

</tx:attributes>

</tx:advice> -->

<!-- 用扫描的方式来注册bean -->

<context:component-scan base-package="hwt.service"/>

<!-- 注册dao -->

<bean id="ArticlesDAO" class="hwt.DAO.ArticlesDAO">

<property name="sessionFactory">

<ref bean="sessionFactory" />

</property>

</bean>

八,配置完了之后不要急着去页面测试,先到控制台输出一下结果

public static void main(String[] args) {

ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");

PersonService personService = (PersonService) act.getBean("personService");

List<Articles> articleList = personService.getArticlesList();

for (Articles articles : articleList) {

System.out.println(articles.getAtitle());

}

}

九,配置web.xml

<!--context配置-->

<context-param>

  <param-name>contextConfigLocation</param-name>

  <param-value>classpath:app*.xml</param-value>

 </context-param>

 <!-- 过滤器 -->

 <filter>

  <filter-name>chineseFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

  <init-param>

   <param-name>encoding</param-name>

   <param-value>utf-8</param-value>

  </init-param>

 </filter>

 <filter-mapping>

  <filter-name>chineseFilter</filter-name>

  <url-pattern>/*</url-pattern>

 </filter-mapping>

<!--监听器-->

 <listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

 </listener>

<!--springDispatcherServlet-->

 <servlet>

  <servlet-name>SpringMVC</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

  <load-on-startup>0</load-on-startup>

 </servlet>

 <servlet-mapping>

  <servlet-name>SpringMVC</servlet-name>

  <url-pattern>*.do</url-pattern>

 </servlet-mapping>

要在WEB-INF下面配置SpringMVC-servlet.xml

<?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:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans 

    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

    http://www.springframework.org/schema/context 

    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    

    <!-- 扫描 -->

    <context:component-scan base-package="hwt.action"></context:component-scan>

</beans>

ps:如果在service层需要用到SessionFactory的话

可以依赖注入到serviceBean

如:

@resource private SessionFactory factory;

//要用factory.getCurrentSession();来得到session,这样得到的session是有spring来管理的

public void test(Articles articles){

//如果在service层需要用到session的话

Session session = factory.getCurrentSession();

session.persist(articles);

}