日期:2014-05-20 浏览次数:20978 次
<session-factory> <!--配置数据库连接 --> <property name="connection.url"> jdbc:oracle:thin:@localhost:1521:orcl </property> <property name="connection.username">scott</property> <property name="connection.password">tigger</property> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- 是否打印Sql语句 --> <property name="hibernate.show_sql">true</property> <mapping resource="com/szy/hibernate/user.hbm.xml" /> </session-factory>
<hibernate-mapping package="com.szy.hibernate"> <class name="User" table="t_user"> <id column="ID" name="id" length="10" type="java.lang.String"> <generator class="assigned"/> </id> <property column="USERNAME" name="username" type="java.lang.String" length="20" not-null="true" unique="true"/> <property column="PASSWORD" name="password" type="java.lang.String" length="20"/> <property column="AGE" generated="never" lazy="false" name="age" type="java.lang.Integer"/> </class> </hibernate-mapping>
Configuration config=new Configuration().configure(); SchemaExport export=new SchemaExport(config); export.create(true, true);
User user=new User(); user.setId("001"); user.setUsername("coolszy"); user.setPassword("test"); user.setAge(21); Session session=HibernateUtil.getSession(); Transaction ts=session.beginTransaction(); session.save(user); ts.commit();