一. 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 之后的版本.如果數據庫原來是存在的,則不會出現上面的錯誤.
新聞熱點
疑難解答