Home  >  Article  >  Database  >  How does hibernate connect to mysql database?

How does hibernate connect to mysql database?

黄舟
黄舟Original
2017-08-08 15:10:502410browse

Teach you how to connect hibernate to mysql database. As long as you follow my steps below, there will be absolutely no problem. The code can be used directly or copied.

How does hibernate connect to mysql database?

#1. First, we connect hibernate to the most basic database using mysql. See a java project and a package named book. Add a java class Book.java under the book package. The code is as follows:

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

The most basic database connection , using mysql. See a java project and a package named "book"

How does hibernate connect to mysql database?

2. Then add a java class Book.java under the book package , the code is as follows:

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;    
 }    
 }

How does hibernate connect to mysql database?

3. Then create a book.hbm.xml under the book package, its code As follows:

<?xml version="1.0"?>    
<!DOCTYPE hibernate-mapping PUBLIC    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">    
<hibernate-mapping package="book"    default-lazy="false">    
<class name="Book">    
<id name="id">    
<generator class="increment"/>    
</id>    
<property name="name" ></property>    
<property name="writer" ></property>    
</class>    
</hibernate-mapping>

How does hibernate connect to mysql database?

4. This matter forms a mapping relationship with the field names in the database. Create the book table in mysql yourself. Correspondingly, the id is self-increasing. Then create a hibernate.cfg.xml in the root directory of the project. The code is as follows:

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

How does hibernate connect to mysql database?

5. This is to connect to the mysql database. Change the username and password to your mysql database

<property name="show_sql">true</property>这是在后台打印sql语句    
<mapping resource="book/Book.hbm.xml" />这是找到映射文件。

How does hibernate connect to mysql database?

6. Then some test classes: the code is as follows:

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();    
   }    
   }    
   }

How does hibernate connect to mysql database?

7. The fields of the mysql table are as follows :

How does hibernate connect to mysql database?

8. After the database is built, you can test it. By the way, the key thing hasn’t been mentioned yet, we have to add 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 are introduced into the lib directory

How does hibernate connect to mysql database?

#

The above is the detailed content of How does hibernate connect to mysql database?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn