1 Downloads hibernate-3.6.0 final.zip to any directory, unzip to get hibernate directory
2 download Slf4j-1.7.13.zip to any directory and get slf4j-1.7.13 after decompression
3 Creating a student table in the test library
Msql-localhost-u root–p
Use test
CREATE TABLE student (no char, name varchar (), PrimaryKey (no));
4 Creating a Java Project named Hibernatedemo
5 Adding a Package
Add all packages in the Hibernate\jar
Add Slf4j-nop-1.7.13.jar in slf4j-1.7.13
Add a MySQL driver Mysql-connector-java-5.1.38-bin.jar
6 adding two configuration files and two classes
Hibernate.cfg.xml
Student.java
Student.hbm.xml
Test.java
(1) Add Hibernate.cfg.xml in the SRC directory under Add directory
[Java]View PlainCopy
- <! DOCTYPE hibernate-configuration Public
- "-//hibernate/hibernate Configuration DTD 3.0//en"
- "HTTP://HIBERNATE.SOURCEFORGE.NET/HIBERNATE-CONFIGURATION-3.0.DTD" >
- <session-factory>
- < whether a real SQL statement is displayed when the program executes!--
- <property name="Show_sql" >true</property>
- <!--uses the corresponding "dialect" of SQL, here is the "dialect" of Oracle11--
- <property name="dialect" >org.hibernate.dialect.mysqlinnodbdialect
- </property>
- Driver--> of <!--connected Database
- <property name="Connection.driver_class" >
- Com.mysql.jdbc.Driver
- </property>
- <!--database connection url-->
- <property name="Connection.url" >
- Jdbc:mysql://localhost:3306/test
- </property>
- <!--user name--
- <property name="Connection.username" >root</property>
- <!--password--
- <property name="Connection.password" >123456</property>
- <mapping resource="Student.hbm.xml"/>
- </session-factory>
(2) Add Student.java to the COM.ABC package in the SRC directory
[Java]View PlainCopy
- Package com.abc;
- Public class Student {
- private String NO;
- private String name;
- Public String Getno () {
- return NO;
- }
- public void Setno (String NO) {
- this.no = NO;
- }
- Public String GetName () {
- return name;
- }
- public void SetName (String name) {
- this.name = name;
- }
- }
(3) Add Student.hbm.xml in src directory
[Java]View PlainCopy
- <?xml version="1.0" encoding="Utf-8"?>
- <! DOCTYPE hibernate-mapping
- Public "-//hibernate/hibernate Mapping DTD 3.0//en"
- "HTTP://HIBERNATE.SOURCEFORGE.NET/HIBERNATE-MAPPING-3.0.DTD" >
- package="COM.ABC" >
- <class Name="Student" table="Student" >
- <id name="NO" >
- <generator class="Assigned"/>
- </id>
- <property name="name" column="name" type="java.lang.String"/>
- </class>
(4) Test.java
[Java]View PlainCopy
- Package com.abc;
- Import org.hibernate.*;
- Import org.hibernate.cfg.*;
- Public class Test {
- public static void Main (string[] args) {
- try {
- //Get a Sessionfactory object through the configuration
- Sessionfactory SF = new Configuration (). Configure (). Buildsessionfactory ();
- //Open a session
- Session session = Sf.opensession ();
- //Start a transaction
- Transaction tx = Session.begintransaction ();
- //Create a Student object
- Student stu = new Student ();
- //Save the Student object to the database using the Save () method of the session
- Stu.setno ("2016003");
- Stu.setname ("Zhang San");
- Session.save (Stu);
- //Commit a transaction
- Tx.commit ();
- //Close Session
- Session.close ();
- } catch (Exception e) {
- E.printstacktrace ();
- }
- }
- }
7 Verification
(1) Run the Test.java, the result is
Hibernate:insert into Student (name, NO) VALUES (?,?)
(2) querying data from MySQL
Hibernate connection MySQL