This article mainly describes how to use the configuration file to relate an object to the database table, in fact, with the configuration file is the same as in the way the annotation is the same.
Idea: 1. Write a domain object (such as Person.java)
2. Write the configuration file for this domain object: Person.hbm.xml. This configuration file mainly specifies the properties and column names of the Damain object. and the table name of domain. Primary key generation policy.
3. Map to Person.hbm.xml in Hibernate_cfg.xml:
<mapping resource= "Com/qls/configurationfile/person.hbm.xml"/>
The code for the domain object person is as follows:
1 PackageCom.qls.domain;2 3 Importjava.util.Date;4 5 /**6 * Created by Quinlinson on 2017/5/21.7 */8 Public classPerson {9 PrivateInteger ID;Ten PrivateString name; One PrivateDate entercampusdate; A - PublicInteger getId () { - returnID; the } - - Public voidsetId (Integer id) { - This. ID =ID; + } - + PublicString GetName () { A returnname; at } - - Public voidsetName (String name) { - This. Name =name; - } - in PublicDate getentercampusdate () { - returnentercampusdate; to } + - Public voidsetentercampusdate (Date entercampusdate) { the This. entercampusdate =entercampusdate; * } $}
The code for the Person.hbm.xml file is as follows:
1<?xml version= "1.0"?>2<! DOCTYPE hibernate-Mapping Public3"-//hibernate/hibernate Mapping DTD 3.0//en"4"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >5 6 Package= "Com.qls.domain" >7<className= "person" table= "person" >8<id name= "id" column= "person_id" >9<generatorclass= "Native"/>Ten</id> One<property name= "Name"/> A<property name= "entercampusdate" type= "timestamp"/> -</class> -The code for the Hibernate.cfg.xml file is as follows:
1<?xml version= ' 1.0 ' encoding= ' utf-8 '?>2<!--3~ Hibernate, relational persistence foridiomatic Java4~5~ License:gnu Lesser general public License (LGPL), version 2.1or later.6~ See the Lgpl.txt file in the root directory or //www.gnu.org/licenses/lgpl-2.1.html>.7-8<! DOCTYPE hibernate-Configuration Public9"-//hibernate/hibernate Configuration DTD 3.0//en"Ten"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" > One A - -<session-factory> the -<!--Database connection Settings-- -<property name= "Connection.driver_class" >oracle.jdbc.OracleDriver</property> -<property name= "Connection.url" >jdbc:oracle:thin: @localhost:1521:orcl</property> +<property name= "Connection.username" >scott</property> -<property name= "Connection.password" >a123456</property> + A<!--JDBC Connection pool (use the built-in)-- at<property name= "Connection.pool_size" >10</property> - -<!--SQL dialect-- -<property name= "dialect" >org.hibernate.dialect.Oracle10gDialect</property> -<!--Echo all executed SQL to stdout -<property name= "Show_sql" >true</property> in -<!--Drop and re-create the database schema on startup-- to<!--<property name= "Hbm2ddl.auto" >create</property>--> + -<!--Names The annotated entityclass- the<mappingclass= "Com.qls.test.Ouyangfeng"/> *<mappingclass= "Com.qls.domain.DiaoChan"/>
<!--location of Person.hbm.xml file-- $ <mapping resource= "Com/qls/configurationfile/person.hbm.xml"/>Panax Notoginseng</session-factory> - theThe code for the test class is as follows:
1 Packagecom.qls.test;2 3 ImportCom.qls.domain.Person;4 Importorg.hibernate.Session;5 Importorg.hibernate.SessionFactory;6 Importorg.hibernate.Transaction;7 Importorg.hibernate.cfg.Configuration;8 9 Importjava.text.ParseException;Ten ImportJava.text.SimpleDateFormat; One A /** - * Created by ${Quinlinson} on 2017/5/21. - */ the Public classTest3 { - Public Static voidMain (string[] args)throwsexception{
Get the session factory -Sessionfactory sessionfactory =NewConfiguration (). Configure (). Buildsessionfactory (); -Session session =sessionfactory.opensession (); +Transaction tx = Session.begintransaction ();//Open the transaction. -Person person =NewPerson (); +Person.setname ("Wang Zhaojun");
Give a specific time-type format. APerson.setentercampusdate (NewSimpleDateFormat ("Yyyy_mm_dd"). Parse ("0093_07_01"))); at Session.save (person); -Tx.commit ();//commits the transaction. - //Close the reply: - session.close (); - } -}
Hibernate implements ORM in the form of configuration files