标签:password sql auto pre 格式化 one automatic 关于 nat
hibernate与spring整合
<?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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- Spring整合Hibernate --> <!-- 引入Hibernate配置信息===== --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 引入Hibernate配置文件 --> <property name="configLocation" value="classpath:hibernate.cfg.xml"/> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 开启注解事务,在业务层使用注解@Transactional --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 通过context标签引入jdbc.properties,在Hibernate核心配置文件里,免去 --> <!-- 配置C3P0连接池,在Hibernate核心配置文件里面,免去 --> <!-- 配置Action --> <bean id="customerAction" class="com.ssh.web.action.CustomerAction" scope="prototype"> <property name="customerService" ref="customerService"/> </bean> <!-- 注入CustomerService --> <bean id="customerService" class="com.ssh.service.impl.CustomerServiceImpl"> <property name="customerDao" ref="customerDao"/> </bean> <!-- 注入Dao CustomerDaoImpl继承HibernateDaoSupport,在Dao注入SessionFactory,创建HibernateTemplate模版 --> <bean id="customerDao" class="com.ssh.dao.impl.CustomerDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> </beans>
<?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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--通过context标签引入jdbc.properties --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置C3P0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- Spring整合Hibernate --> <!-- 方式二:不带Hibernate核心文件配置===== --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 注入连接池 --> <property name="dataSource" ref="dataSource" /> <!-- 配置Hibernate相关属性 --> <property name="hibernateProperties"> <props> <!-- 配置方言,打印语句,格式化sql,自动建表 --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- 设置映射 --> <property name="mappingResources"> <list> <value>com/ssh/domain/Customer.hbm.xml</value> </list> </property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 开启注解事务,在业务层使用注解@Transactional --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 配置Action --> <bean id="customerAction" class="com.ssh.web.action.CustomerAction" scope="prototype"> <property name="customerService" ref="customerService"/> </bean> <!-- 注入CustomerService --> <bean id="customerService" class="com.ssh.service.impl.CustomerServiceImpl"> <property name="customerDao" ref="customerDao"/> </bean> <!-- 注入Dao CustomerDaoImpl继承HibernateDaoSupport,在Dao注入SessionFactory,创建HibernateTemplate模版 --> <bean id="customerDao" class="com.ssh.dao.impl.CustomerDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> </beans>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
结果在测试时,老是发现数据库表数据丢失。这个参数以前没怎么用,查了一圈其它的东东,最后才定位到这个上面。赶紧查了一下Hibernate的参数配置,解释如下:
hibernate.hbm2ddl.auto Automatically validate or export schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly. eg. validate | update | create | create-drop
其实这个参数的作用主要用于:自动创建|更新|验证数据库表结构。如果不是此方面的需求建议set value="none".
其它几个参数的意思,我解释一下:
validate 加载hibernate时,验证创建数据库表结构
create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。
create-drop 加载hibernate时创建,退出是删除表结构
update 加载hibernate自动更新数据库结构
以上4个属性对同一配置文件下所用有的映射表都起作用
标签:password sql auto pre 格式化 one automatic 关于 nat
原文地址:https://www.cnblogs.com/codeleader/p/12460601.html