One, the first example
1. Import Hibernate's necessary development Jar packages and database-driven Jar packages
Ø antlr-2.7.7.jar - Hql parsing tool
Ø dom4j-1.6.1.jar - XML configuration file reading tool
Ø hibernate-commons-annotations-4.0.2.Final.jar - Hibernate annotation package
Ø hibernate-core-4.2.4.Final.jar - Hibernate core package
Ø hibernate-jpa-2.0-api-1.0.1.Final.jar - complete dependency package of javaBean mapping table
Ø javassist-3.15.0-GA.ja - code generation package that extends Java classes and implementations at runtime
Ø jboss-logging-3.1.0.GA.jar - logging tool
Ø jboss-transaction-api_1.1_spec-1.0.1.Final.jar - transaction tool
Ø mysql-connector-java-5.1.7-bin.jar -mysql driver
2. Build and hibernate.cfg.xml configuration file
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- The path and name of the class used to specify the database connection driver --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- Used to specify the url address to connect to the database --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property> <!-- Username to connect to the database --> <property name="hibernate.connection.username">root</property> <!-- Password to connect to the database--> <property name="hibernate.connection.password">root</property> <!-- The dialect of the SQL statement used when connecting to the database --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Whether the HIbernate framework will automatically create the database form or modify the form according to the mapping file when it is working--> <property name="hibernate.hbm2ddl.auto">update</property> <!-- In order to facilitate our observation of HIBERNATE work, then we also need to manually add the following two configurations --> <!-- Automatically output SQL statements--> <property name="show_sql">true</property> <!-- formatted output sql --> <property name="format_sql">true</property> <!-- Register the mapping file with hibernate--> <mapping resource="com/hibernate/domain/User.hbm.xml"/> </session-factory> </hibernate-configuration>
3, create a database table
CREATE TABLE user_infor ( id int(11) unsigned NOT NULL AUTO_INCREMENT, name varchar(50) NOT NULL, code varchar(20) NOT NULL, age int(3) DEFAULT NULL, PRIMARY KEY (id) )
4. Create the entity object of the database table
public class UserInfor { private Integer id; private String name; private String code; private Integer age; public UserInfor() { } public UserInfor(String name, String code,Integer age) { super(); this.name = name; this.code = code; this.age=age; } }
5. Create a mapping file between entity classes and database tables
<?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"> <hibernate-mapping> <class name="com.hibernate.demo.pojo.UserInfor" table="user_infor" catalog="hibernate"> <id name="id" type="integer" column="id"> <generator class="native" /> </id> <property name="name" type="string" column="name" /> <property name="code" type="string" column="code" /> <property name="age" type="integer" column="age" /> </class> </hibernate-mapping>6. Register the mapping file of the fifth step in the hibernate.cfg.xml file
<hibernate-configuration> <session-factory> 。。。。 <!-- Register the mapping file with hibernate--> <mapping resource="com/hibernate/demo/pojo/UserInfor.hbm.xml"/> </session-factory> </hibernate-configuration>
7. Create a test class
public class TestUser { @Test public void testAdd(){ //1. Load configuration hibernate.cfg.xml Configuration cfg = (new Configuration()).configure(); //2. Create a factory class SessionFactory SessionFactory factory = cfg.buildSessionFactory(); //3. Create a session object for operation Session session = factory.openSession(); //4. Create an entity class UserInfor user = new UserInfor("zhangsan","0011",12); //5. Start transaction Transaction tx = session.beginTransaction(); //6. Save data session.save(user); //7. Commit the transaction. tx.commit(); //8. Close the connection session.close(); } }