spring、hibernate的整合

来源:互联网 发布:追韩国女生知乎 编辑:程序博客网 时间:2024/05/25 12:21

Spring+hibernate框架整合步骤详解  

第一步:新建web 项目:

 

第二步:右键项目myeclipse->addXXXX,按顺序添加hibernatespring开发包

注:先添加hibernate

 

next


这个部分的话,玩过hibernate的小伙伴应该知道如何弄出来的,不会的可以百度一下,这个还是比较简单的。


到这里hibernate开发包就添加成功了。

接下来是添加spring开发包

这里要注意看我选了哪六个。

(ps:这里的话jar library installation最好还是选择第一个)

这里的话jar library installation最好还是选择第一个

spring添加成功,接下来就是hibernate逆向工程操作了。首先切换视图到myeclipse的数据库界面

这里需要注意一下,daotype那里要选择spring Dao

到这里,我们就差不多可以开始正式配置项目了

这里是applicationContext.xml  这里最核心的部分是头部和我写了注释的地方,中间的那些bean是自动生成的,到时候你们可以直接复制我的头部和尾部。

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/task  http://www.springframework.org/schema/task/spring-task-3.0.xsd"><!-- 配置service 扫描service --><context:component-scan base-package="com.service"></context:component-scan><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="user" value="root"></property><property name="password" value="admin"></property><property name="jdbcUrl" value="jdbc:mysql://127.0.0.1/enterprising"></property><property name="driverClass" value="com.mysql.jdbc.Driver"></property></bean><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="configLocation" value="classpath:hibernate.cfg.xml"></property></bean><bean id="DepDAO" class="com.dao.DepDAO"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean><!-- 事物管理器 --><bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean><!-- 事物属性 --><tx:advice id="mytx" transaction-manager="transactionManager"><tx:attributes><tx:method name="*" /></tx:attributes></tx:advice><!-- 织入 --><aop:config><aop:advisor advice-ref="mytx" pointcut="execution(* com.service.*.*(..))" /><aop:advisor advice-ref="mytx"pointcut="execution(* com.back.service.*.*(..))" /></aop:config></beans>
接下来是最重要的一步,也就是web.xml的配置

web.xml里面的重要部分代码,这个是需要配置的,但通过代码实现效果也是一样的。

<context-param>  <param-name>contextConfigLocation</param-name>  <param-value>classpath:app*.xml</param-value> </context-param> <filter>  <filter-name>openSession</filter-name>  <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet>  <servlet-name>springMVC</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping>  <servlet-name>springMVC</servlet-name>  <url-pattern>*.do</url-pattern> </servlet-mapping>
新建springMVC-servlet.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/task  http://www.springframework.org/schema/task/spring-task-3.0.xsd"><!-- 扫描action --><context:component-scan base-package="com.action"></context:component-scan><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="configLocation" value="classpath:hibernate.cfg.xml"></property></bean><bean id="DepDAO" class="com.dao.DepDAO"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean></beans>

注意事项:

1.会出现asm包的冲突,解决方法:删除包:cglib-2.2.jar

2.可能会少了个c3p0的包,这个去网上找c3p0-0.9.1.jar

action的写法,一定要注意注解部分,也就是加了@符号的部分:例如

package com.action;import javax.servlet.http.HttpServletRequest;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import com.pojo.Dep;import com.service.DepService;@Controller@RequestMapping("/dep.do")public class DepAction {@Autowiredprivate DepService depService;@Autowiredprivate HttpServletRequest request;@RequestMapping(params= "p=findall")public String findall(){int id = 1;System.out.println("来了action");Dep dep = depService.find(id);System.out.println(dep);request.setAttribute("dep", dep);return "/show.jsp";}}
service也是如此,例如:

package com.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.dao.DepDAO;import com.pojo.Dep;@Servicepublic class DepService {@Autowiredprivate DepDAO depDAO;public Dep find(int id){System.out.println("来了service");Dep dep = depDAO.findById(id);return dep;}}
在jsp里面写

<jsp:forward page="dep.do?p=findall"></jsp:forward> 就能正常运行项目了

效果如下:


 

1 0
原创粉丝点击