Java 字符串转换为日期,hibernate配置文件<property name="hbm2ddl.auto">的含义

本文介绍了如何使用Java将字符串转换为日期的方法,并详细解析了Hibernate配置文件中hbm2ddl.auto属性的不同设置选项及其作用,帮助开发者更好地理解和配置Hibernate。

1、字符串转换为日期函数

 

 

public Date strToDate(String dateString,String formatter)
	{
		Date d = null;
		
		if(null == dateString || null == formatter ||
			dateString.trim().length() <=0 || formatter.trim().length() <=0	)
		{
			return new Date();
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat(formatter);
		
		try
		{
			d = sdf.parse(dateString);
		}catch(ParseException e)
		{
			d = new Date();
			e.printStackTrace();
		}
		return  d;
	}

 

 

2、hibernate配置文件<property name="hbm2ddl.auto">的含义

      hibernate在创建时是否重建数据库的schema,数据库的schema实际上就是数据库里面的对象,包括表、视图...

      写道

Automatically validates or exports 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.

 

 

   validate:当sessionFactory创建时,自动验证或者schema定义导入数据库。

   create:每次启动都drop掉原来的schema,创建新的。

   create-drop:当sessionFactory明确关闭时,drop掉schema。

   update(常用):如果没有schema就创建,有就更新。

<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- MySQL 8.x 配置(使用新版驱动时) --> Connection conn = DriverManager.getConnection(url, user, pwd); <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/message_db?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true</property> <property name="connection.username">root</property> <property name="connection.password">82615486</property> <!-- 连接池设置 --> <property name="hibernate.c3p0.min_size">5</property> <property name="hibernate.c3p0.max_size">20</property> <property name="hibernate.c3p0.timeout">1800</property> <property name="hibernate.c3p0.max_statements">50</property> <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <property name="show_sql">true</property> <!-- 强制使用TLSv1.2协议 --> <property name="hibernate.connection.sslProtocol">TLSv1.2</property> <property name="hibernate.connection.sslEnabled">true</property> <mapping class="model.Message"/> <mapping class="model.User"/> </session-factory> </hibernate-configuration>tly registered JDBC driver list: Exception in thread "main" java.lang.ClassCastException: com.mysql.cj.jdbc.Driver cannot be cast to java.sql.Driver at servlet.TestConnection.main(TestConnection.java:24)
06-22
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="sapInfDataSource" class="com.bs.apps.core.datasource.wrapper.DataSourceWrapper"> <property name="dataSource"> <bean class="com.bs.apps.core.datasource.wrapper.CustomDataSource" destroy-method="close"> <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property> <property name="url"><value>jdbc:oracle:thin:@10.216.109.93:1521:misdevdb</value></property> <property name="username"><value>mesinterface</value></property> <property name="password"><value>mesinterface12c</value></property> <property name="validationQuery"><value>select 1 from DUAL</value></property> </bean> </property> </bean> <bean id="sapInfSessionFactory" class="com.bs.apps.core.persistence.impl.DefaultSessionFactoryBean"> <property name="dataSource" ref="sapInfDataSource"/> <property name="mappingResources"> <list> <value>com/sap/apps/sapInterface/domain/SAPInfConfig.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <!-- 测试的时候HSQL的dialect --> <prop key="hibernate.dialect">com.bs.apps.core.dialect.CustOracle10gExtendedDialect</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.query.substitutions">true 1, false 0</prop> <!-- “当前session”的范围和上下文 (scopeand context)的定义,session要绑定的上下文设置为由spring环境管理 --> <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop> </props> </property> <!-- DB中table名字的前缀配置 --> <!-- <property name="tablePrefix"> <value>sap_</value> </property> --> </bean> <bean id="sapInfConfigService" class="com.sap.apps.sapInterface.service.impl.SAPInfConfigServiceImpl"> <property name="sessionFactory" ref="sapInfSessionFactory"/> </bean> </beans>
最新发布
08-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值