Connect to MySQL database using hibernate
1: After the project is set up, add the necessary jar packages to the Lib package, and the MySQL database driver jar Package:
The jar package can be found in Hibernate's download package (HIBERNATE3.3.2.GA), where the required jar packages are:
All jar packages under the hibernate3.jar,lib/required directory;
The jar package required to connect to the database: Mysql-connector-java-5.1.7-bin.jar;
:
After the introduction of the 2:jar package, the entity classes and mapping files are written:
The entity class is a Xx.java file; the mapping file is a xx.hbm.xml file and the file name needs to be the same;
The contents of the. java file are omitted here,
. hbm.xml File Test code:
<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<class name= "user" table= "user" >
<id name= "userid" column= "userid" >
<generator class= "uuid"/>
</id>
<property name= "UserName" type= "java.lang.String" column= "UserName"
Length= "/>"
</class>
3: Add Hibernate.cfg.xml and Hibernate configuration files
The file can be found in the Hibernate download package:
Profile Template: Hibernate3.3.2.ga\project\tutorials\web\src\main\resources\hibernate.cfg.xml
The contents of the document can be adjusted;
Here is the configuration file code:
<?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>
<!--Database Connection settings-->
<property Name= "Connection.driver_class" >COM.MYSQL.JDBC.DRIVER</PROPERTY>
<property name= "Connection.url" >JDBC:MYSQL://LOCALHOST/YIM</PROPERTY>
<property name= "Connection.username" >ROOT</PROPERTY>
<property name= "Connection.password" >MYSQL</PROPERTY>
<!--JDBC Connection pool (use the built-in)--
<property name= "Connection.pool_size" >2</property>
<!--SQL dialect--
<property name= "dialect" >org.hibernate.dialect.MySQLDialect</property>
<!--Enable Hibernate ' s current session context--
<property name= "Current_session_context_class" >org.hibernate.context.managedsessioncontext</property >
<!--Disable the Second-level cache--and
<property name= "Cache.provider_class" >org.hibernate.cache.NoCacheProvider</property>
<!--Echo all executed SQL to stdout
<property name= "Show_sql" >true</property>
<!--Drop and re-create the database schema on startup--
<property name= "Hbm2ddl.auto" >create</property>
<!--Map File declaration--
<mapping resource= "Com/yim/entity/user.hbm.xml"/>
</session-factory>
4: The above steps are all configured. You can test the database connection:
Test Class Code:
Package Com.yim.dao;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import org.hibernate.cfg.Configuration;
Import Com.yim.entity.user;
/**
* Test MySQL database connection
*
* @author Administrator
*
*/
public class Logindao {
Private session session = NULL;
Private Transaction tran = null;
Public Logindao () {
Configuration Configure = new configuration (). Configure ();
Sessionfactory factory = Configure.buildsessionfactory ();
This.session = Factory.opensession ();
}
public void Save (user user) {
try {
Tran = This.session.beginTransaction ();
This.session.save (user);
Tran.commit ();
SYSTEM.OUT.PRINTLN ("Information Preservation");
} catch (Exception e) {
Todo:handle exception
} finally {
This.session.close ();
}
}
public static void Main (string[] args) {
User user = new user ();
User.setusername ("User name");
New Logindao (). Save (user);
}
}
Run as-->java application to execute the test class;
When the console output executes the SQL statement:
Hibernate:insert into User (USERNAME, USERID) VALUES (?,?)
Information preservation
Indicates a successful database connection!
Hibernate simple connection MySQL database configuration