Hibernate single-table mapping one of the Learning Notes--hibernalnate development environment configuration

Source: Internet
Author: User

1. What is ORM?

Object/relationship Mapping: Object/Relationship Mapping

2, write the SQL statement is bad:

(1) different SQL syntax used in different databases (PL/t/sql)

(2) The same functionality has different implementations in different databases (paging SQL)

(3) Excessive reliance on SQL statements is detrimental to the porting and expansion of programs

3. Hibernate

(1) ORM Framework Technology

(2) A very lightweight object encapsulation of JDBC

4. Other ORM Framework Technologies

(1) Mybatis (formerly known as Ibatis)

(2) Toplink (now Oracle as Toplink)

(3) EJB: Java EE specification itself

5, the required tools:

(1) Hibernate core pack;

(2) Hibernate Eclipse plugin;

6. To create Hibernate project steps:

(1) Import core package and database driver:

  

(2) Create the configuration file;

  

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://www.hi Bernate.org/dtd/hibernate-configuration-3.0.dtd "><hibernate-configuration>    <session-factory>        < Propertyname= "Connection.username">Root</ Property>        < Propertyname= "Connection.password">Root</ Property>        < Propertyname= "Connection.driver_class">Com.mysql.jdbc.Driver</ Property>        < Propertyname= "Connection.url">Jdbc:mysql:///hibernate?useunicode=true&amp;Characterencoding=utf-8</ Property>        < Propertyname= "dialect">Org.hibernate.dialect.MySQLDialect</ Property>        < Propertyname= "Show_sql">True</ Property>        < Propertyname= "Format_sql">True</ Property>        < Propertyname= "Hbm2ddl.auto">Create</ Property>        <MappingResource= "Student.hbm.xml"/>    </session-factory></hibernate-configuration>

(3) Create a persistence class;

Package hibernate001;//Student Import Java.util.date;public class student{//design principles for persistence classes//1, public classes//2, providing a common default construction method with no parameters//3, Property Private//4, attribute setter/getter encapsulation private int sid;//number private string name;//name private string gender;//sex private Date birthday;/ /date of birth private String address;//address public Student () {}public Student (int sid, string name, string gender, Date birthday, Strin G address) {This.sid = Sid;this.name = Name;this.gender = Gender;this.birthday = Birthday;this.address = Address;} public int GetSID () {return SID;} public void Setsid (int sid) {this.sid = SID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String Getgender () {return gender;} public void Setgender (String gender) {This.gender = gender;} Public Date Getbirthday () {return birthday;} public void Setbirthday (Date birthday) {this.birthday = birthday;} Public String getaddress () {return address;} public void setaddress (String address) {this.address = address;} @Overridepublic String toString () {return ' StUdent [sid= "+ Sid +", name= "+ name +", gender= "+ Gender +", birthday= "+ Birthday +", address= "+ address +"] ";}} 

  

(4) Create object--relational mapping file;

<?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-4-14 17:17:44 by Hibernate Tools 3.5.0.Final -<hibernate-mapping>    <classname= "hibernate001." Student "Table= "STUDENT">        <IDname= "Sid"type= "int">            <columnname= "SID" />            <Generatorclass= "Assigned" />        </ID>        < Propertyname= "Name"type= "Java.lang.String">            <columnname= "NAME" />        </ Property>        < Propertyname= "Gender"type= "Java.lang.String">            <columnname= "GENDER" />        </ Property>        < Propertyname= "Birthday"type= "Java.util.Date">            <columnname= "BIRTHDAY" />        </ Property>        < Propertyname= "Address"type= "Java.lang.String">            <columnname= "ADDRESS" />        </ Property>    </class></hibernate-mapping>

(5) Write access to database code through the Hibernate API:

I'm using JUNTIL4, the test class.

Package Hibernate001;import Org.hibernate.transaction;import Java.util.date;import org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;import Org.hibernate.service.ServiceRegistry ; Import Org.hibernate.service.serviceregistrybuilder;import Org.junit.after;import Org.junit.before;import org.junit.test;//test class public class Studenttest {private sessionfactory sessionfactory;private Session session;private Transaction Transaction; @Beforepublic void init () {//Wear piece Config object configuration = new configuration (). Configure ();// Create a Service registration object Serviceregistry Serviceregistry = new Serviceregistrybuilder (). Applysettings (Configuration.getproperties () ). Buildserviceregistry ();//Create Session Object Sessionfactory = Configuration.buildsessionfactory (serviceregistry);// Open session = Sessionfactory.opensession ();//Open transaction transaction = Session.begintransaction ();} @Afterpublic void Destory () {transaction.commit ();//COMMIT Transaction session.close ();//Close session sessionfactory.close ();//close Session factory}@ Testpublic VOID Testsavestudent () {Student S1 = new Student (1, "Zhang Sanfeng", "male", New Date (), "Wudangshan"), Session.save (S1);}} 

  

Hibernate single-table mapping one of the Learning Notes--hibernalnate development environment configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.