Spring 中sessionFactory的几种配置方式

来源:互联网 发布:怎么查手机ip地址端口 编辑:程序博客网 时间:2024/05/01 04:33

1. 以各种数据源的方式来配置sessionFactory

<?xml version='1.0' encoding='UTF-8'?><beans xmlns="http://www.springframework.org/beans"xmlns:xsi="http://www.w3.org/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/beanshttp://www.springframework.org/beans/spring-beans-3.0.xsd"><!-- c3p0 数据源 的配置  --><bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destory-method="close"><property name="driverClass" value="com.mysql.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/purchase"/><property name="user" value="purchase"/><property name="password" value="purchaseLiu"/><!--连接池中保留的最大连接数,Default: 15 --><property name="maxPoolSize" value="40"/><!--连接池中保留的最小连接数--><property name="minPoolSize" value="1"/><!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间,Default: 3 --><property name="initialPoolSize" value="1"/><!--最大空闲时间,60秒内未使用则连接被丢弃,若为0则永不丢弃,Default: 0 --><property name="maxIdleTime" value="20"/><!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数,Default: 3 --><property name="acquireIncrement" value="5"/><!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->        <property name="maxStatements" value="0"/><!--每60秒检查所有连接池中的空闲连接。Default: 0 -->        <property name="idleConnectionTestPeriod" value="60"/>   <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->        <property name="acquireRetryAttempts" value="30"/><!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。Default: false-->        <property name="breakAfterAcquireFailure" value="true"/><!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable等方法来提升连接测试的性能。Default: false -->        <property name="testConnectionOnCheckout" value="false"></bean><!-- 使用apache 下的数据源配置 --><bean id="myDataSource" class="org.apache.commons.BasicDataSource" destory-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/purchase"/><property name="username" value="purchase"/><property name="password" value="purchaseLiu"/></bean><!-- jndi 数据源 配置 --><bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"><property name="jndiName" value="java:comp/env/jdbc/myds"/></bean><!-- 以 jee标签来配置 --><jee:jndi-lookup id="myDataSource" jndi-name="java:comp/env/jdbc/myds"/><!-- 配置时请选择其中的一种进行配置即可,这里由于说明需要,把它们全都列举出来了 --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="myDataSource"/></bean></beans>


2. 使用 hibernate 进行配置

    2.1  不使用hibernate.cfg.xml来进行配置

<?xml version='1.0' encoding='UTF-8'?><beans xmlns="http://www.springframework.org/beans"xmlns:xsi="http://www.w3.org/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/beanshttp://www.springframework.org/beans/spring-beans-3.0.xsd"><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="mappingResource"><list><value>tarena/entity/user.hbm.xml</value><!-- 继续添加相应的映射表 --></list></property><property name="hibernateProperties"><value>connection.url=jdbc:mysql://localhost:3306/purchaseconnection.driver_class=com.mysql.jdbc.Driverconnection.username=purchaseconnection.password=purchaseLiuhibernate.dialect=org.hibernate.MySQLInnoDBDialecthibernate.hbm2ddl.auto=update<!--继续添加关于hibernate的相关配置--></value></property></bean></beans>

    2.2 使用 hibernate.cfg.xml来进行配置

<?xml version='1.0' encoding='UTF-8'?><beans xmlns="http://www.springframework.org/beans"xmlns:xsi="http://www.w3.org/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/beanshttp://www.springframework.org/beans/spring-beans-3.0.xsd"><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="configLocation" value="classpath:hibernate.cfg.xml"/><!--不清楚下面这句话是什么意思,但看到过别人这样写,故且先粘在这--><!--<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />--></bean><!-- 或者如下的 sessionFactory 配置 --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="configLocations"><value>classpath:hibernate.cfg.xml</value></property></bean></beans>



3. 使用 hibernate与数据源配置 sessionFactory

<?xml version='1.0' encoding='UTF-8'?><beans xmlns="http://www.springframework.org/beans"xmlns:xsi="http://www.w3.org/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/beanshttp://www.springframework.org/beans/spring-beans-3.0.xsd"><!-- c3p0 数据源 的配置  --><bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destory-method="close"><property name="driverClass" value="com.mysql.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/purchase"/><property name="user" value="purchase"/><property name="password" value="purchaseLiu"/><property name="maxPoolSize" value="40"/><property name="minPoolSize" value="1"/><property name="initialPoolSize" value="1"/><property name="maxIdleTime" value="20"/></bean><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="c3p0DataSource"/><property name="mappingResource"><list><value>tarena/entity/user.hbm.xml</value><!-- 继续添加相应的映射表 --></list></property><property name="hibernateProperties"><value>hibernate.dialect=org.hibernate.MySQLInnoDBDialecthibernate.hbm2ddl.auto=update<!--继续添加关于hibernate的相关配置--></value></property></bean></beans>

暂时本人接触到的就是这些了。希望更多的人分享各种  sessionFactory 的配置方式,若有错,希望路过者能够给予指正,在下不胜感激