srping配合hibernate实现POJO与数据库表映射的零配置
第一步:配置数据源
<!-- 配置数据源1 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialSize" value="${pool.initialSize}" />
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="${pool.maxActive}" />
<!-- 连接池最大空闲
<property name="maxIdle" value="${pool.maxIdle}" />
-->
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${pool.minIdle}" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${pool.maxWait}" />
<!-- <property name="poolPreparedStatements" value="true" /> <property
name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->
<property name="validationQuery" value="${validationQuery.sqlserver}" />
<property name="testOnBorrow" value="${pool.testOnBorrow}"/>
<property name="testOnReturn" value="${pool.testOnReturn}"/>
<property name="testWhileIdle" value="${pool.testWhileIdle}"/>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${pool.timeBetweenEvictionRunsMillis}" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${pool.minEvictableIdleTimeMillis}" />
<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="${pool.removeAbandoned}" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="${pool.removeAbandonedTimeout}" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="${pool.logAbandoned}" />
<!-- 开启Druid的监控统计功能 -->
<property name="filters" value="${pool.filters}" />
<!--<property name="filters" value="${pool.filters}" />
<property name="proxyFilters">
<list>
<ref bean="log-filter"/>
<ref bean="wall-filter"/>
<ref bean="stat-filter"/>
</list>
</property> -->
<!--<property name="filters" value="mergeStat" /> -->
<!-- Oracle连接是获取字段注释 -->
<property name="connectProperties">
<props>
<prop key="remarksReporting">true</prop>
</props>
</property>
</bean>
第二步:配置sessionFactroy
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="entityInterceptor" ref="entityInterceptor" />
<property name="namingStrategy" ref="databaseNamingStrategy" />
<property name="hibernateProperties">
<value>
hibernate.dialect=${hibernate.dialect}
hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto}
hibernate.show_sql=${hibernate.show_sql}
hibernate.format_sql=${hibernate.format_sql}
hibernate.cache.use_second_level_cache=${hibernate.cache.use_second_level_cache}
hibernate.cache.provider_class=${hibernate.cache.provider_class}
hibernate.cache.use_query_cache=${hibernate.cache.use_query_cache}
hibernate.jdbc.fetch_size=${hibernate.jdbc.fetch_size}
hibernate.jdbc.batch_size=${hibernate.jdbc.batch_size}
</value>
</property>
<property name="packagesToScan" value="com.ssbank.entity"/>
</bean>
<bean id="databaseNamingStrategy" class="com.ssbank.common.DatabaseNamingStrategy">
<property name="tablePrefix" value="${namingStrategy.tablePrefix}" />
<property name="isAddUnderscores" value="${namingStrategy.isAddUnderscores}" />
<property name="maxLength" value="${namingStrategy.maxLength}" />
</bean>
注意: hibernate.hbm2ddl.auto=update时,项目第一次启动,会扫描entity下的所有实体类,配合hibernate提供的命名策略DatabaseNamingStrategy,在数据库生成相应的表。
<property name="packagesToScan" value="com.ssbank.entity"/> 配置扫描哪个包