Hibernate Hibernate Environment Configuration
First, hibernate environment to build the steps
1. Add the jar for Hibernate && SQL Server
Antlr-2.7.7.jar
Dom4j-1.6.1.jar
Hibernate-commons-annotations-4.0.5.final.jar
Hibernate-core-4.3.11.final.jar
Hibernate-jpa-2.1-api-1.0.0.final.jar
Jandex-1.1.0.final.jar
Javassist-3.18.1-ga.jar
Jboss-logging-3.1.3.ga.jar
Jboss-logging-annotations-1.2.0.beta1.jar
Jboss-transaction-api_1.2_spec-1.0.0.final.jar
Sqljdbc4.jar
2. Create JavaBean class: User.java
Package cn.com.zfc.hibernate.entities;
/**
*
* @title User
* @describe Hibernate's POJO
* @author Zhang Fuchang
* @date April 7, 2017 10:47:27
*/
public class User {
Private Integer ID;
Private String UserName;
Private String Userdesc;
Public User () {
Super ();
}
Public User (String userName, String userdesc) {
Super ();
This.username = UserName;
This.userdesc = Userdesc;
}
Public Integer getId () {
return ID;
}
public void SetId (Integer id) {
This.id = ID;
}
Public String GetUserName () {
return userName;
}
public void Setusername (String userName) {
This.username = UserName;
}
Public String Getuserdesc () {
return USERDESC;
}
public void Setuserdesc (String userdesc) {
This.userdesc = Userdesc;
}
@Override
Public String toString () {
Return "User [id=" + ID + ", username=" + UserName + ", userdesc=" + Userdesc + "]";
}
}
3. Mapping JavaBean attributes to database tables using the Xxx.hbm.xml mapping file: User.hbm.xml
<?xml version= "1.0"?
<! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en"
"http://hibernate.sourceforge.net/ HIBERNATE-MAPPING-3.0.DTD,
<!--Generated 2017-3-19 13:53:52 by Hibernate Tools 3.5.0.Final-->
<!- -Generally write the package, simplify the whole class name-->
<class name= " User "table=",
<!--map PRIMARY key, ID: attribute, column: field-->
<id name= "id" type= "java.lang.Integer" >
<column name= "ID"/>
<!--primary key generation policy-->
<generator class= "native"/>
</i D>
<!--map Normal field, property: Attribute, column: field-->
<property name= "UserName" type= "java.lang.String" > <column name= "user_name"/>
</property>
<property name= "Userdesc" type= "java.lang.String" >
<column name= "User_desc"/>
</property>
</class>
4. Configure hibernate data sources and related properties in the Hibernate.cfg.xml hibernate configuration file: hibernate.cfg.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >
<session-factory>
<!--Hibernate Basic configuration--
<!--data source information configuration-->
<!--database user name-->
<property name= "Hibernate.connection.username" >sa& Lt;/property>
<!--database password-->
<property name= "Hibernate.connection.password" >123456</prope Rty>
<!--database drive PO-->
<property name= "Hibernate.connection.driver_class" >com.microsoft.sqlser Ver.jdbc.sqlserverdriver</property>
<!--do not specify a database name, you can specify the database name--> in the mapping file;
<property name= "Hibern Ate.connection.url ">JDBC:SQLSERVER://LOCALHOST:1433;DATABASENAME=HIBERNATE-01</PROPERTY>
<!--Specifies that SQL statements are output in the console for easy debugging--
<property name= "Show_sql" >true</property>
<!--formatting SQL statements--
<property name= "Format_sql" >true</property>
<!--Specify a database generation strategy--
<property name= "Hbm2ddl.auto" >update</property>
<!--configuration Database dialect---
<property name= "dialect" >org.hibernate.dialect.SQLServerDialect</property>
<!--registering Hibernate mapping Files--
<mapping resource= "Cn/com/zfc/hibernate/entities/user.hbm.xml"/>
</session-factory>
5. Using the Hibernate ORM Framework to complete the operation of the JavaBean object
① Creating a Configuration object
Configuration configuration = new configuration (). Configure ();
② Creating Sessionfactory Factory class
Serviceregistry serviceregistry = new Standardserviceregistrybuilder (). Applysettings (Configuration.getproperties () ). build ();
Sessionfactory sessionfactory = configuration.buildsessionfactory (serviceregistry);
③ Creating a Session Object
Session session = Sessionfactory.opensession ();
④ Open Things Transaction (queries do not require transactions)
Transaction Transaction = Session.begintransaction ();
⑤ Execution Business ******
CRUD Operations
⑥ Submitting Things
Transaction.commit ();
⑦ Closing a session
Session.close ();
⑧ Close Sessionfactory
Sessionfactory.close ();
6. Database: hibernate-01
Data Sheet: Users
Second, JUNIT unit test first knowledge
1. Basic concepts of Unit testing
2. Using JUnit to build hibernate test environments
[Email protected] @Before @After
Test of 4.Hibernate Environment: Testhibernate.java
Package cn.com.zfc.hibernate.test;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import Org.hibernate.boot.registry.StandardServiceRegistryBuilder;
Import org.hibernate.cfg.Configuration;
Import Org.hibernate.service.ServiceRegistry;
Import Org.junit.After;
Import Org.junit.Before;
Import Org.junit.Test;
Import Cn.com.zfc.hibernate.entities.User;
/**
*
* @title Testhibernate
* @describe to test Hibernate environment
* @author Zhang Fuchang
* @date April 7, 2017 10:47:05
*/
public class Testhibernate {
Private configuration configuration = null;
Private sessionfactory sessionfactory = null;
Private session session = NULL;
Private Transaction Transaction;
@Before
public void init () {
Configuration = new configuration (). Configure ();
Serviceregistry serviceregistry = new Standardserviceregistrybuilder (). Applysettings (Configuration.getproperties () ). build ();
Sessionfactory = Configuration.buildsessionfactory (serviceregistry);
Session = Sessionfactory.opensession ();
Transaction = Session.begintransaction ();
}
@After
public void Destory () {
Transaction.commit ();
Session.close ();
Sessionfactory.close ();
}
Add to
@Test
public void Testsave () {
User user = New User ("UU-1", "Love Life, Love Programming");
Session.save (user);
}
Load now
@Test
public void Testget () {
User user = (user) Session.get (user.class, 1);
System.out.println ("User:" + user);
}
Lazy loading: When the data is actually used at the beginning of the load
@Test
public void TestLoad () {
User user = (user) session.load (user.class, 1);
System.out.println ("User:" + user);
}
Delete
@Test
public void Testdelete () {
User user = (user) session.load (User.class, 4);
Session.delete (user);
}
Modify
@Test
public void Testupdate () {
User user = (user) session.load (user.class, 1);
User.setuserdesc ("I am a modified thing");
Session.update (user);
}
@Test
public void DataSource () {
}
}
Hibernate Hibernate Environment Configuration