Spring中配置数据源

Spring中配置数据源

在Sping的配置文件中,关于dataSource的配置,就我个人知道的方法大致可以有三种:  

1. 一般的配置方法,直接在配置中指定其值。具体的例子我们参照Mysql的配置如下:

 <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

  <property name="driverClassName">

   <value>com.mysql.jdbc.Driver</value>

  </property>

  <property name="url">

   <value>jdbc:mysql://Service_Path:3306/dataBase</value> <!-- 其中参数:Service_path表示所连接

数据库的IP,dataBase:表示所连接的数据库名 -->

  </property>

  <property name="username" value="root"></property>

  <property name="password" value="root"></property>

 </bean>

  2. 通过读取文件信息资源,其原理与方法一相同。示例:

 <bean id="preferences"

class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">

  <property name="location">

   <value>/WEB-INF/files.properties</value> <!-- 指定文件路径 -->

  </property>

 </bean>

 <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

  <property name="driverClassName">

   <value>${driverClassName}</value>   <!-- 这里的值要通过${}进行转义,其driverClassName参数要

在上面的文件中指定 -->

  </property>

  <property name="url">

   <value>${url}</value>

  </property>

  <property name="username" value="${userName}"></property>

  <property name="password" value="${userPwd}"></property>

 </bean>

  3。 通过数据连接池。在此我们只需指定jndiName的值为服务器中配置的数据连接池的JNDI名称即

可。

 <bean id="datasource" class="org.springframework.jndi.JndiObjectFactoryBean">

  <property name="jndiName" value="jdbc/mysql"></property>

 </bean>

在上述方法配置成功之后,我们可以通过JdbcTemplate绑定dataSource

 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">

  <property name="dataSource">

   <ref bean="datasource" />

  </property>

 </bean>

两种将DbResource-->sessionFactory--->beanDAO--->beanService方法:

一、hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql://localhost:3306/online_order</property>
		<property name="connection.username">root</property>
		<property name="connection.password">123456</property>
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="connection.autocommit">true</property>
		<property name="show_sql">true</property>
		<property name="format_sql">true</property>
		
    <mapping class="com.org.order.bean.Admin"></mapping>
    <mapping class="com.org.order.bean.Dish"></mapping>
    <mapping class="com.org.order.bean.Log"></mapping>
    <mapping class="com.org.order.bean.Menu"></mapping>
    <mapping class="com.org.order.bean.Reply"></mapping>
    <mapping class="com.org.order.bean.Sort"></mapping>
    <mapping class="com.org.order.bean.Theme"></mapping>
    <mapping class="com.org.order.bean.User"></mapping>
  </session-factory>
</hibernate-configuration>

applicationContext.xml

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
	</bean>
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
    
    
    <bean id="adminDao" class="com.org.order.dao.hibernateTemplate.AdminDaoHibernateTemplate">
		<property name="hibernateTemplate" ref="hibernateTemplate"/>
	</bean>
 <bean id="adminService" class="com.org.order.service.impl.AdminServiceImpl">
  <property name="adminDao" ref="adminDao"></property>
 </bean>
    <!--beanService注入  这句话使得在action里面取beanService时,不用写set和get方法,只要在其上一行写@-->
    <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor">
    </bean>

二、applicationContext.xml配置文件会加载的applicationContext-resources.xml

<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>oracle.jdbc.driver.OracleDriver</value>
		</property>
		<property name="url">
		<!-- 124.207.35.148 -->
			<value>jdbc:oracle:thin:@124.207.35.148:1521:orcl</value>
		</property>
		<property name="username">
			<value>novodemo</value>
		</property>
		<property name="password">
			<value>novodemo_2012</value>
		</property>
	</bean>	

	<!-- =====================SessionFactory Definition==============================-->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref local="dataSource" />
		</property>
		<property name="lobHandler" ref="lobHandler" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.Oracle9Dialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>

		<property name="mappingDirectoryLocations">
			<list>				
				<value>
					classpath*:com/lenovo/platform/admin/entity
				</value>																		
			</list>
		</property>

 applicationContext.xml配置文件会加载的applicationContext-dao.xml

<bean id="baseDAOHibernate"
		class="com.lenovo.platform.core.dao.impl.BaseDAOHibernateImpl">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

 applicationContext.xml配置文件会加载的applicationContext-service.xml

<bean id="baseService"
		class="com.lenovo.platform.core.service.impl.BaseServiceImpl">
		<property name="baseDAOHibernate">
			<ref bean="baseDAOHibernate" />
		</property>
		<property name="baseDAOIbatis">
			<ref bean="baseDAOIbatis" />
		</property>		
	</bean>

 applicationContext-action.xml

<bean name="/download"
		class="com.lenovo.platform.common.web.action.DownloadAction">
		<property name="employeeService" ref="employeeService" />
	</bean>

相关推荐