
<?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数据库的基本参数 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_demo01?useSSL=false</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<!-- 配置Hibernate的方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 下面两个是可选的配置哟! -->
<!-- 打印sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化sql语句 -->
<property name="hibernate.format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!-- 告诉Hibernate的核心配置文件加载哪个映射文件 -->
<!-- <mapping resource="com/testHibernate/Customer.hbm.xml"/> -->
</session-factory>
</hibernate-configuration>
private SessionFactory sessionFactory;
private Transaction transaction;
private Session session;
@Before
public void init() {
Configuration configure = new Configuration().configure();
ServiceRegistry serviceRegistry=
new StandardServiceRegistryBuilder().applySettings(configure.getProperties()).build();
sessionFactory=configure.buildSessionFactory(serviceRegistry);
session=sessionFactory.openSession();
transaction=session.beginTransaction();
}
@After
public void tory() {
transaction.commit();
session.close();
sessionFactory.close();
}











这篇博客介绍了如何配置Hibernate以连接到MySQL数据库。配置包括设置驱动类、URL、用户名、密码,选择MySQL的方言,并可选地开启SQL的打印和格式化。在测试代码中,展示了初始化SessionFactory和Session的过程。
2006

被折叠的 条评论
为什么被折叠?



