hibernate配置文件hibernate.cfg.xml
- <!--
- ~ Hibernate, Relational Persistence for Idiomatic Java
- ~
- ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
- ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
- -->
- <!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>
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/student</property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password"></property>
- <property name="hbm2ddl.auto">update</property>
- <property name="myeclipse.connection.profile">com.mysql.jdbc.Driver</property>
- <property name="show_sql">true</property>
- <mapping resource="com/student/Item.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
然后我们试着插如一条数据
- package com.student;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- public class TestDemo {
- public static void main(String[] args){
- Configuration cfg = ;
- SessionFactory sf = ;
- Session s = ;
- Transaction ts= ;
- Student student = new Student();
- student.setUsername("zhangmo");
- student.setPassword("123456789");
- student.setAge(20);
- try {
- cfg = new Configuration().configure();
- sf = cfg.buildSessionFactory();
- s = sf.openSession();
- ts = s.beginTransaction();
- s.save(student);
- ts.commit();
- } catch (HibernateException e) {
- // TODO Auto-generated catch block
- if(ts != )
- {
- ts.rollback();
- }
- e.printStackTrace();
- } finally{
- if(s != ){
- s.close();
- }
- }
- }
- }
运行结果: