(using the XML configuration method in hibernate to connect to the database, later in updating other ways of connection)
Hibernate is the framework of Java background database persistence layer, is also the most enterprise database framework, mainly based on ORM-Object Relationship Mapping, Translated into Chinese called "Object Relational Mapping", that is, the non-object-oriented language of SQL into the hibernation object-oriented writing, this article will be built in Eclipse under the Hibernate framework
The hibernate version I'm using is Hibernate3.3.2, andthe Hibernate website is available for download www.hibernate.org
The following starts the Hibernate database setup:
1. Create a new Java Project project in Eclipse with the name: Hibernatetest. Right-click the project name, click Property, select Java Build Path, and add the jar package that needs to be imported.
2. Under the SRC folder, create a new hibernate.cfg.xml configuration file (connection database parameter information)
The hibernate.cfg.xml file code is as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<session-factory>
<property name= "Hibernate.connection.driver_class" >
Com.mysql.jdbc.Driver
</property>
<property name= "Hibernate.connection.url" >
Jdbc:mysql://localhost:3306/test
</property>
<!--database connection settings--
<property name= "Hibernate.connection.username" >root</property>
<property name= "Hibernate.connection.password" >123456</property>
<!--show_sql Generate SQL statement output to log for tune-
<property name= "Hibernate.show_sql" >true</property>
<!--SQL dialect dialect---
<property name= "Hibernate.dialect" >
Org.hibernate.dialect.MySQLDialect
</property>
<!--Specifies that the session is tracked and defined by the currently executing thread
<property name= "Hibernate.current_session_contecxt_class" >
Thread
</property>
<!--Add a mapping file for an entity class--
<mapping resource= "XXXXX.hbm.xml"/>
</session-factory>
3. In the MySQL database there is a login table, field ID (int), name (string), password (string), new Login.java under the model package,
The Login.java file code is as follows:
public class Login {
private int id;
private String name;
private String password;
public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
}
4. Under the model package, create a new login.hbm.xml for mapping database fields and tables
The login.hbm.xml file code is as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<class name= "Login" table= "Login" >
<id name= "id" type= "java.lang.Integer" ></id>
<property name= "name" type= "java.lang.String"/>
<property name= "Password" type= "java.lang.String"/>
</class>
5. Under the model package, create a new Testlogin.java as the test file
The Testlogin.java code is as follows:
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.cfg.Configuration;
public class Test {
public static void Main (String a[])
{
Login L = new login ();
L.setid (1);
L.setname ("456");
L.setpassword ("456");
Configuration cfg = new configuration ();
Sessionfactory SF = Cfg.configure (). Buildsessionfactory ();
Session session = Sf.opensession ();
Session.begintransaction ();
Session.save (l);
Session.gettransaction (). commit ();
Session.close ();
Sf.close ();
System.out.print ("success!");
}
}
6. Run the program and run successfully.
Hibernate connect MySQL database practice in eclipse