myeclipse2013+spring mvc 3.1+hibernate 4.1.4集成

最近正在学习springmvc和hibernate的集成框架,经过一个多星期的查询资料终于实现了hibernate4 和hibernate3有一些区别,hibernate4抛弃了hibernate3的HibernateTemplate模板类。直接使用session进行对数据库的操作。springmvc和hibernate的集成在一些配置方面也发生了改变,在这里主要给大家展示springmvc和hibernate的是这样配置的

1首先进行配置需要引进的JAR包
这些JAR包你可以在官网上直接下,也可以用myeclipse2013自带的jar包,本人用的是mysql数据库,如果数据库不同的话,下面的配置文件中需要修改,连接数据库jar包最好放在lib下
2.

首先先建个web—project工程名就叫chapter-2-hibernate,第一步springjar包的引入,右击工程选择properties—>libraries选择右边的addLibrary-—>myeclipselibrary—>选择spring3.1jlibrary点击finish。hibernatejar包的引入同上注入

1>首先进入springmvc的配置.先配置web.xml

web.xml如下配置:

<?xml version="1.0"encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<!--对应的工程名-->

 <display-name> chapter-2-hibernate /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容器的监听器,它将引用1出的上下文获得spring配置文件地址 -->

  <listener>

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

  </listener>

  <context-param>

    <param-name>webAppRootKey</param-name>

    <param-value>chapter-2-hibernate</param-value>

  </context-param>

  <!--1从类路径下加载spring配置文件,classp关键字特指类路径下加载  -->

  <context-param>

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

    <param-value>classpath:applicationContext.xml</param-value>

  </context-param>

 

  <!-- spring mvc的主控servelt -->

  <servlet>

        <servlet-name>b505</servlet-name>

        <servlet-class>

            org.springframework.web.servlet.DispatcherServlet

        </servlet-class>

        <load-on-startup>3</load-on-startup>

    </servlet>

 

    <servlet-mapping>

        <servlet-name>b505

</servlet-name>

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

  </servlet-mapping>

<!-- 日志的配置-->

<context-param> 

        <param-name>log4jConfigLocation</param-name> 

        <param-value>/WEB-INF/classes/log4j.properties</param-value> 

    </context-param> 

      

    <context-param> 

        <param-name>log4jRefreshInterval</param-name> 

        <param-value>60000</param-value> 

    </context-param> 

    <listener> 

        <listener-class> 

           org.springframework.web.util.Log4jConfigListener  

        </listener-class> 

<!--opensession的配置-->

<filter>
<filter-name>osivFilter</filter-name> 
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> 
<init-param> 
<param-name>sessionFactoryBeanName</param-name> 
<param-value>sessionFactory</param-value> 
</init-param> 
<init-param> 
<param-name>singleSession</param-name> 
<param-value>true</param-value> 
</init-param> 
<init-param> 
<param-name>flushMode</param-name> 
<param-value>AUTO</param-value> 
</init-param> 
</filter> 
<filter-mapping> 
<filter-name>osivFilter</filter-name> 
<url-pattern>*.do</url-pattern> 
</filter-mapping> 

</listener>  

在src目录下建一个aplicationContext.xml配置如下与web.xml中的applicationContent,xml相对应appl

<?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"
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-3.1.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<!-- 扫描类包,将标注Spring注解的类自动转换为Bean,同时完成Bean的注入 -->
<context:component-scan base-package="com.b505.dao"/>
<context:component-scan base-package="com.b505.service"/>
<context:property-placeholder location="classpath:jdbc.properties" />  
<!-- decp数据源配置方式 --> 
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"/> 

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
>
<property name="dataSource" ref="dataSource"></property>    
<property name="hibernateProperties">    
<props>    
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>    
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>  
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
</props>
</property>
<property name="mappingResources">
<list>    

 <!--对应的两个实体映射表--> 


<value>com/b505/domain/User.hbm.xml</value>
<value>com/b505/domain/LoginLog.hbm.xml</value>
</list>
</property>
</bean>


</beans>

</beans>在WEB—INF下建一个b505—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"

    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-3.1.xsd

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

      http://www.springframework.org/schema/context/spring-context-3.1.xsd

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

      http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

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

      http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

<!-- 扫描包,应用spring的注解 -->

<context:component-scan base-package="com.b505.web"/>

  

<!-- 配置解析器,将ModelANDVIew及字符串解析为具体的页面 -->

<!-- ViewResolver --> 

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"

        p:viewClass="org.springframework.web.servlet.view.JstlView"

        p:prefix="/WEB-INF/jsp/"

        p:suffix=".jsp" />

</beans>

WEB-INF下新建一个classes文件夹,在其中建一个log4j.propertiesweb.xml中日志的配置相对应内容如下

log4j.rootLogger=INFO, A1

log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender

log4j.appender.A1.File=log_

log4j.appender.A1.DatePattern=yyyy-MM-dd'.log'

log4j.appender.A1.layout=org.apache.log4j.PatternLayout

log4j.appender.A1.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n

 

src下建一个jdbc.properties文件与aaplicationContext.xml中的数据库资源配置相对应

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/sampledb

jdbc.username=root

jdbc.password=123456

接下来在实体所在的包下建两个文件,我把两个实体UerLoginLog建立在了com.b505.domain包下所以两个映射文件也建在此包下其配置如下

LoginLog.hbm.xml如下

<?xml version="1.0"encoding="utf-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/HibernateMapping DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<!--

   Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping package="com.b505.domain">

 

   <class name="LoginLog"  table="t_login_log">

        <id name="loginLogid"column="login_log_id">

           <generator class="native"/>

       </id>

     <property name="userId" column="user_id"/>

    <property name="ip" column="ip"/>

   <property name="loginDate" column="login_datetime"/>

  </class>

</hibernate-mapping>

User.hbm.xml如下

<?xml version="1.0"encoding="utf-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/HibernateMapping DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<!--

   Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping package="com.b505.domain">

 

   <class name="User"  table="t_user">

        <id name="userId"column="user_id">

           <generator class="native"/>

        </id>

    <property name="userName" column="user_name"/>

    <property name="credits" column="credits"/>

    <property name="passWord" column="password"/>

    <property name="lastVisit" column="last_visit"/>

    <property name="lastIp" column="last_ip"/> 

   </class>

</hibernate-mapping>

在mysql数据库下建一个数据库sampledb再在数据库下建两个数据库表t_usert_login_log与上面的两个映射文件相对应

到此整个框架的配置文件配置完成,其中有些配置文件可以利用myeclipse直接生成springhibernate的配置文件,这里采用的都是手动配置的

 这个实例的源代码大家可以下载http://download.csdn.net/detail/zql781744363/6619405









  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值