不使用hibernate.cfg.xml
package org.xiazdong;import java.util.Properties;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.classic.Session;public class UserTest {public static void main(String[] args) {Properties p = new Properties();p.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");p.put("hibernate.connection.url", "jdbc:mysql:///hibernate");p.put("hibernate.connection.username", "root");p.put("hibernate.connection.password", "12345");p.put("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect");p.put("hibernate.hbm2ddl.auto","update");Configuration conf = new Configuration().setProperties(p).addClass(User.class);SessionFactory sf = conf.buildSessionFactory();Session session = sf.openSession();Transaction tx = session.beginTransaction();User u = new User();u.setName("xiazdong-1");u.setAge(20);session.save(u);tx.commit();session.close();sf.close();}}
?