Advantages and disadvantages of 1.JDBC connections
Advantages of JDBC
Direct underlying operation, provides a very simple and convenient way to access the database, cross-platform relatively strong. The flexibility is strong, can write very complex SQL statement.
Disadvantages of JDBC
1). Because Java is object-oriented, JDBC does not make the data object-oriented programming, so that the programmer's thinking is still stuck on the SQL statement.
2). The operation is cumbersome, many of the code needs to be repeated many times.
3). If a batch operation is encountered, frequent interaction with the database can lead to a decrease in efficiency.
JDBC is a relatively low-level thing, the flexibility to write SQL statements
1), registration drive
2), Get the connection
3), produce a statement
4), to operate
Return Data resultset
1), new List Object
2), put resultset data into the list process
A = new A ();
A.setxxx (rs.getstring ("xxx"));
The code is cumbersome, and pure JDBC is not cached.
2. Hibernate Introduction
L model mismatch (impedance mismatch)
Java object-oriented language, object model, the main concepts are: inheritance, association, polymorphism, etc., the database is a relational model, the main concepts are: Table, primary key, foreign key and so on.
• Solutions
1 convert manually using JDBC.
The 2 is addressed using the ORM (object Relation mapping Objects relational mapping) framework.
The relationship between an object and the object in the object model cannot correspond to the relationships between the database tables in the relational model.
The inheritance relationships of objects in the object model cannot be represented directly in the relational model.
The equivalence of objects in the object model (equals) cannot be directly implemented in the relational model.
navigation access between objects that are associated in the object model cannot be directly implemented in the relational model.
3 Hibernate
L Hibernate is an open source ORM Framework.
L ORM Full Name object Relation Mapping. It is a mapping technique used to complete the object model to the relational model. Is the technique of persisting object data in an application to a table in a relational database.
L Use the ORM (Object Relation Mapping) framework to resolve. The main ORM Framework is the JBoss company hibernate, Oracle's TopLink, Apache organization's OJB, and Sun's jdo.
L simply say: ORM can use object-oriented thinking to open the application based on the relational database, its main work is to save the object data to the table in the relational database, and to read the data in the relational database table into the object.
4 Installation Configuration
Http://www.hibernate.org, the 3.3 version is used here for example. Unzip to get the required class library file. Add the packages that are required for Hibernate runtime under download directories/hibernate3.jar and/lib to classpath:
The jar packages and jar packages that need to be used correspond to the following meanings:
L configuration Files Hibernate.cfg.xml and Hibernate.properties,xml and properties two kinds, the function of these two files, provide a can, the recommended XML format, download directory etc is a sample configuration file, The latter is listed in more detail, mainly with a variety of database connection templates.
L can specify in the configuration file :
Database URL, user name, password, JDBC driver class, dialect, and so on.
Hibernate will look for this configuration file in Classpath at startup.
L Mapping Files (Hbm.xml, object model and relational model mappings). The full hibernate example is available under the/eg directory.
L A small example step :
1). Create a new Java or Web project and add the appropriate jar package and JDBC driver.
2). Create a persisted class (Java Bean)
3). Prepare the database table
4). Create a configuration file Hibernate.cfg.xml
5). Create a mapping file Xxx.hbm.xml (in the same package as the Bean Class)
6). Create a test file (test this project)
5 Detail Analysis
Hibernate.connection.url indicates the database address to link to
Hibernate.connection.driver_class the drive class that represents the database to be linked
Hibernate.connection.username the user name of the database to connect to
Hibernate.connection.password the password of the database to connect to
Hibernate.dialect indicates the type of database to use
Org.hibernate.dialect.MySQL5Dialect MySQL Database
Org.hibernate.dialect.Oracle9Dialect Oracle Database
Org.hibernate.dialect.SQLServerDialect SQL Server Database
Hibernate.hbm2ddl.auto
Validate: Validating CREATE TABLE structure when Hibernate is loaded
Update: Automatically updates the database structure when hibernate is loaded, if the table does not exist and is created if it does not exist.
Create: Creates a table structure every time hibernate is loaded
Create-drop: Created when loading hibernate, deleting on exit
6. Basic concepts and curd
Development process:
1, by the domain object--mapping->db. (Official recommendation)
2. Starting with DB, use tools to generate mapping and domain object. (More use)
3. Start with the mapping file.
Domain Object Restriction
1, the default construction method (required).
2. There is no meaningful identifier ID (primary key) (optional)
3, non-final, lazy loading has an impact (optional)
7. Case description
Domain Java Object (User)
public class User {
private int id;
private String name;
Private Date BirthDay;
Getter Setter ...
}
L Object Relational Mapping file: the object-oriented entity class objects are mapped to entities in the database (table records), and the association between entity classes is mapped to the relationships among the multiple tables in the database. In this way, the operations on these entity objects in Hibernate are converted directly to the records of the database tables.
User.hbm.xml
<?xml version= "1.0"?>
<class name= "user" table= "user" >
<id name= "id" >
<generator class= "native"/>
</id>
<property name= "Name"/>
<property name= "Birthday"/>
</class>
Hibernate.cfg.xml
<! Doctypehibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<session-factoryname="foo">
<propertyname="Connection.driver_class">com.mysql.jdbc.Driver</property>
<propertyname="Connection.url">jdbc:mysql:///hibernate</property>
<propertyname="Connection.username">root</property>
<propertyname="Connection.password">root</property>
<propertyname="dialect">org.hibernate.dialect.HSQLDialect</property>
<propertyname="Show_sql">true</property>
<propertyname="Hbm2ddl.auto">update</property>
<mappingresource="Com/hbsi/domain/user.hbm.xml"/>
</session-factory>
Test class Demotest.java
Package com.hbsi.test;
Import Java.text.DateFormat;
Import java.text.ParseException;
Import Java.util.Date;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.cfg.Configuration;
Import Com.hbsi.domain.User;
public class Usertest {
/**
* @param args
* @throws ParseException
*/
public static void Main (string[] args) throws ParseException {
Assigning a value to the User class field
User user = new user ();
User.setname ("AA");
User.setbirthday (New Date ());
User.setbirthday (Dateformat.getdateinstance (). Parse ("1992-01-01"));
Get the configuration file for Hibernate.cfg.xml
Configuration cfg = new configuration (). Configure ();
Get session from session factory, equivalent to connection in JDBC
Sessionfactory factory = Cfg.buildsessionfactory ();
Session session = Factory.opensession ();
Session.begintransaction ();
Session.save (user);
Session.gettransaction (). commit ();
Session.close ();
}
}
Overview Hibernate Getting Started installation configuration