spring集成hibernate

一、准备工作和整体架构

    首先导入spring相关的jar包,然后在web中配置spring的配置文件,在spring的配置文件中配置hibernate,而hibernate配置数据源要用到的变量则是在properties文件中配置,下面就来一一说明

二、web配置

先贴出web的全部代码

<?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/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ExpressDoor2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <servlet>  
     <servlet-name>spring</servlet-name>  
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>classpath:config/spring-servlet.xml</param-value>  
        </init-param>  
      <load-on-startup>1</load-on-startup>  
  	</servlet>
 	 <servlet-mapping>  
      <servlet-name>spring</servlet-name>  
      <url-pattern>/*</url-pattern>  
  	</servlet-mapping>
  	<context-param>  
      <param-name>contextConfigLocation</param-name>  
      <param-value>classpath:config/applicationContext.xml</param-value>  
  </context-param>
</web-app>

 在web.xml中,配置了spring的servlet因为使用的是springmvc所以,还包含了springmvc的配置文件spring-servlet.xml,这里不多说,需要了解的可以看我其他的文章,这里只看application.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:context="http://www.springframework.org/schema/context"
          xmlns:jms="http://www.springframework.org/schema/jms"
          xmlns:amq="http://activemq.apache.org/schema/core"
        xsi:schemaLocation="  
        	  http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd
              http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
              http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
              http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
    <context:annotation-config/>
    <bean class = "org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
	<import resource="hibernate.xml"/>
	
  </beans>

 在这里,配置了注解并引入了一个hibernate.xml配置文件,hibernate配置文件的全部内容如下:

<?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:jms="http://www.springframework.org/schema/jms"
          xmlns:amq="http://activemq.apache.org/schema/core"
        xsi:schemaLocation="  
        	  http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd
              http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
              http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
              http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
        <!-- spring加载properties文件 -->
        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations">  
           <list>  
              <!-- 这里支持多种寻址方式:classpath和file -->  
              <value>classpath:config/properties/db.properties</value>  
            </list>  
        </property>  
   		 </bean>
  		<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${db.driverClassName}">
		</property>
		<property name="url" value="${db.url}">
		</property>
		<property name="username" value="${db.username}"></property>
		<property name="password" value="${db.password}"></property>
		</bean>
    	
		<!-- 配置数据库 -->
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>config/hibernate/getExpress.hbm.xml</value>
				<value>config/hibernate/UserInfo.hbm.xml</value>
			</list>
		</property>
	</bean>
	<bean id="expressService" class="service.ExpressService">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
  </beans>

 这里,首先定义了一个加载properties文件的bean,在locations中填写properties文件的路径,properties文件的内容如下:

db.driverClassName=com.mysql.jdbc.Driver

db.url=jdbc:mysql://localhost:3306/express_door

db.username=root

db.password=root

看的出来,properties文件就是包含了一些数据库的驱动以及数据库名字等,这些单独写出来便于理解与管理,接着hibernate.xml往下走,通过propertyConfigurer这个bean加载properties文件后我们就可以引用他的内容了,在定义datasource数据源的时候可以看到,我们可以通过${  }来引用properties文件中的内容,配置数据源后就需要配置hibernate的sessionfactory这是hibernate的核心配置,在这里面需要将前面定义的数据源注入进去,dialect是配置方言(不确定),mappingResources则是配置hibernate的一些映射文件,最后我们可以新建一个类继承HibernateDaoSupport类,在配置文件中将sessionfactory注入到这个类中,我们就可以通过getHibernateTemplate()方法来进行数据库操作了

public class ExpressService extends HibernateDaoSupport{
	/**
	 * 添加一条取快递信息
	 * @param bean
	 */
	public void saveExpressGet(getExpressBean bean){
		if(bean != null ){
			getHibernateTemplate().save(bean);
		}
	}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值