
jun - 云代码空间
—— 相信 ,梦
package cn.itcast.lucene.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* hibernate 操作工具类
*
* @author liao
*
*/
public class HibernateUtils {
private static Configuration configuration;
private static SessionFactory sessionFactory;
static {
configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
}
public static Session openSession() {
return sessionFactory.openSession();
}
//检测是否能连接数据库
public static void main(String[] args) {
}
}
hibernate.cfg.xml 配置 <?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> <!-- JDBC基本连接参数 --> <session-factory> <!-- 理解为连接池 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hibernate3day1</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">abc</property> <!-- 配置方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 常见其它配置 --> <property name="hibernate.show_sql">true</property> <!-- 控制台上打印SQL --> <property name="hibernate.format_sql">true</property> <!-- 控制台输出时,对SQL语句格式化 --> <!-- 测试环境 create/ create-drop 正式环境 update validate --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 自动建表 --> <property name="hibernate.connection.autocommit">true</property> <!-- 在核心配置文件中 引用 mapping 映射文件 --> </session-factory> </hibernate-configuration>