hibernate如何連接mysql數據庫?

教大家如何把hibernate連接mysql數據庫,只要大家跟著我下面的步驟走,就絕對沒有問題的。代碼都是可以直接用到或者復制的。

hibernate如何連接mysql數據庫?

1.首先,我們把hibernate最基本的數據庫連接,使用mysql。 見一個java工程,見一個包名為book, 在book的包下加一個java類Book.java,其代碼如下:

package?book;?public?class?Book?{?private?Integer?id;?private?String?name;?private?String?writer;?  public?Integer?get?hibernate

最基本的數據庫連接,使用mysql。 ? ?見一個java工程,見一個包名為“book”

hibernate如何連接mysql數據庫?

2.然后在在book的包下加一個java類Book.java,其代碼如下:?

package?book;????  public?class?Book?  {????  private?Integer?id;????  private?String?name;????  private?String?writer;????  public?Integer?getId()?  {????  return?id;????  }????  public?void?setId(Integer?id)?  {????  this.id?=?id;????  }???  ?public?String?getName()?  ?{????  ?return?name;????  ?}????  ?public?void?setName(String?name)?  ?{????  ?this.name?=?name;????  ?}????  ?public?String?getWriter()?  ?{????  ?return?writer;????  ?}????  ?public?void?setWriter(String?writer)?  ?{????  ?this.writer?=?writer;????  ?}????  ?}

hibernate如何連接mysql數據庫?

3.然后在book包下建一個book.hbm.xml,其代碼如下:

<?xml  version="1.0"?>????  nbsp;hibernate-mapping?PUBLIC????"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"????"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;????  <hibernate-mapping>????  <class>????  <id>????  <generator></generator>????  </id>????  <property></property>????  <property></property>????  </class>????  </hibernate-mapping>

hibernate如何連接mysql數據庫?

4.這個事與數據庫里面的字段名形成映射關系,自己在mysql建立book表時與之對應,id是自增長的,??? 然后在工程的根目錄下建一個hibernate.cfg.xml.其代碼如下:

?<?xml  version=&#39;1.0&#39; encoding=&#39;UTF-8&#39;?>????  ?nbsp;hibernate-configuration?PUBLIC????"-//Hibernate/Hibernate?Configuration?DTD?3.0//EN"????  ?"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;????  ?<hibernate-configuration>????  ?<session-factory>????  ?<property>com.mysql.jdbc.Driver</property>????  ?<property>jdbc:mysql://localhost/mydb</property>????  ?<property>root</property>????  ?<property>root</property>????  ?<property>org.hibernate.dialect.MySQLDialect</property>????  ?<property>true</property>????  ?<!-- <property name="hbm2ddl.auto">${hibernate.hbm2ddl.auto}</property>-->????  ?<!-- <property name="current_session_context_class">thread</property>-->????  ?<mapping></mapping>????  ?</session-factory>????  ?</hibernate-configuration>

hibernate如何連接mysql數據庫?

5.這是連接mysql數據庫的,用戶名和密碼改為你mysql數據庫的?

<property>true</property>這是在后臺打印sql語句????  <mapping></mapping>這是找到映射文件。

hibernate如何連接mysql數據庫?

6.然后些個測試類:代碼如下:

package?test;????  import?org.hibernate.Session;????  import?org.hibernate.SessionFactory;????  import?org.hibernate.Transaction;????  import?org.hibernate.cfg.Configuration;????  import?book.Book;????  public?class?MainTest?{????  /**????  *?@param?args????  */????  public?static?void?main(String[]?args)?{????  try?{????  Configuration?cfg=new?Configuration()。configure();????  SessionFactory?sf=cfg.buildSessionFactory();????  Session?session?=?sf.openSession();????  Transaction?ts=session.beginTransaction();????  Book?b=new?Book();???  ?b.setName("hibernate");???  b.setWriter("div");????  session.save(b);???  ?//?Book?b=(Book)?session.get(Book.class,1);????  ?//?if(b!=null){????  ?//?b.setName("xujun");???  ??//?System.out.println("書名為:"+b.getName());????  ??//?System.out.println("作者為:"+b.getWriter());????  ??//?session.delete(b);????//?}????ts.commit();????  ??session.close();????sf.close();???  ???}?  ??catch?(Exception?e)?{???  ???e.printStackTrace();????  ???}????  ???}????  ???}

hibernate如何連接mysql數據庫?

7.mysql表的字段如下:

hibernate如何連接mysql數據庫?

8.把數據庫建好后就可以測試。對了,關鍵的還沒有說,還得把antlr.jar,cglib.jar,asm.jar,asm-attrs.jar,commons-colletions.jar,commons-logging.jar,ehcache.jar,??? jta.jar,dom4.jar,log4.jar,hibernate3.jar引入到lib目錄下

hibernate如何連接mysql數據庫?

? 版權聲明
THE END
喜歡就支持一下吧
點贊10 分享