Hibernate 基本配置 (脚手架)
一 . hibernate 配置需要 4 步
1. 配置 hibernate.cfg.xml 文件,里面配置 基本的数据库连接, 还有一些其他的 比如: dialect, format_sql, show_sql, hbm2ddl.auto 等等.
2. 创建实体类.
3. 创建实体类的映射文件,如: 若实体类是 News, 那么实体类映射文件就是: News.hbm.xml.
4. 进行测试.
1. 配置 hibernate.cfg.xml 文件
<hibernate-configuration>
<session-factory>
<!-- 配置 hibernate0 的基本信息 -->
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///test</property>
<!-- 配置 hibernate0 其他信息 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 映射到 News.hbm.xml -->
<mapping resource="org/blanck/entities/News.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2. 创建实体类:
public class News {
private Integer id;
private String title;
private Date newsTimes;
} // 省略 get/set 方法 // 省略有参/无参构造器
3. 配置实体类的映射文件,(使用 Eclipse hibernatetools-Update-4.1.1.Final 插件,可以生成配置文件)
<hibernate-mapping>
<class name="org.blanck.entities.News" table="T_NEWS">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<!-- 数据库的生成策略 -->
<generator class="native" />
</id>
<property name="title" type="java.lang.String">
<column name="TITLE" />
</property>
<property name="newsTimes" type="java.util.Date">
<column name="NEWSTIMES" />
</property>
</class>
</hibernate-mapping>
4. 下面是测试类:
public class HibernateTest {
// hibernate0 3 个接口,Session 工厂, session, 事务
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
/**
* 测试之前,初始化方法
*/
@Before
public void init(){
System.out.println("初始化方法...");
// 1.初始化 Configuration 对象,创建构建 SessionFactory 需要的对象
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
// 2.使用 ServiceRegistry 构建 SessionFactory 对象
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
// 用 sessionFactory 打开一个 Session, 开启事务
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
/**
* 测试完成,销毁对象
*/
@After
public void destory(){
System.out.println("销毁方法...");
// 提交事务,关闭 Session,关闭 SessionFactory
transaction.commit();
session.close();
sessionFactory.close();
}
/**
* 测试方法
*/
@Test
public void test() {
System.out.println("测试方法");
// 创建一个 News 对象,进行测试
News news = new News("helloKitty", new Date());
session.save(news);
}
}
5. 错误, 如果出现: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=InnoDB' at line 6
请把 :
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
修改成:
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
这主要是因为数据库版本的问题,使用
MySQL5InnoDBDialect 可以应用到 mysql 5.1 之后的版本. 如果数据库原来存在的,则不会出现上面的错误.