Java-hibernate's Hello World

Source: Internet
Author: User

Hibernate is the ORM framework for a lightweight encapsulation of JDBC, which acts as a persistence layer for the project.

To use hibernate, you need to continue configuring it first,

Primer pack: Download hibernate and add the jar package

Simultaneously introducing the MySQL jar package

<1> First, manually create a test table, where the database chooses MySQL

CREATE TABLE ' employee ' (' id ' int ') NOT null auto_increment, ' name ' varchar (a) not null, ' email ' varchar (#) NOT NULL, ' Hi Redate ' date not null,primary KEY (' id ')) engine=innodb auto_increment=12 DEFAULT charset=latin1;

Then go to manual data entry

<2> Create our domain object

Build a package named Com.sun.domain, under the package build a Java file named: Employee.java

It contains all the fields in the table.

Package Com.sun.domain;public class Employee {private static final long Serialversionuid = 1l;private Integer id;private S Tring name;private String email;private java.util.Date hiredate;public Integer getId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String Getemail () {return email;} public void Setemail (String email) {this.email = email;} Public Java.util.Date Gethiredate () {return hiredate;} public void Sethiredate (Java.util.Date hiredate) {this.hiredate = HireDate;}}

  <3> Create a mapping relationship for objects and tables under Com.sun.domain Employee.hbm.xml

<property></property> tag under name is the property name of the domain object,<column/> is the field name of the table corresponding to the property name
<?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 ">

  <4> Manually configure our Hibernate.cfg.xml file, which is used to configure the type of database connected, driver, Object-relational mapping file

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">                < connection to the!--database--><property name= "Connection.driver_class" >com.mysql.jdbc.Driver</property>< Property Name= "Connection.url" >jdbc:mysql://127.0.0.1:3306/test</property>
                <!--database username and password--
                
<property name= "Connection.password" >123456</property>
<property name= "Show_sql" >true</property><property name= "dialect" > Org.hibernate.dialect.mysqlinnodbdialect</property><mapping resource= "Com/sun/domain/Employee.hbm.xml" /></session-factory>

  <5> Write a manual test file, because hibernate is a database persistence framework, so it's not possible to use the web, SE can also be used, which is tested with SE

Create a package com.sun.seriver, create a file below Student.java

Package Com.sun.seriver;import Java.util.date;import Org.hibernate.session;import org.hibernate.SessionFactory; Import Org.hibernate.transaction;import Org.hibernate.cfg.configuration;import Com.sun.domain.employee;public  Class Student {public static void main (string[] args) {//TODO auto-generated method Stubstudent.delempoyee (); Delete//student.upempoyee ();//update//student.selectempoyee ();//Query//student.addempoyee ();//Add}private static void Delempoyee () {Configuration cfg=new configuration (); Sessionfactory SF = cfg.configure ("Hibernate.cfg.xml"). Buildsessionfactory (); Session session = Sf.opensession (); Transaction ts=session.begintransaction (); Employee emp= (Employee) session.load (Employee.class, 2); Session.delete (EMP); Ts.commit ();} private static void Upempoyee () {Configuration cfg=new configuration (); Sessionfactory SF = cfg.configure ("Hibernate.cfg.xml"). Buildsessionfactory (); Session session = Sf.opensession (); Transaction ts= session.begintransaction (); Employee emp= (Employee) session.load (EmPloyee.class, 1); Emp.setname ("Usagi"); Ts.commit ();} SQL SELECT private static void Selectempoyee () {Configuration cfg=new configuration (); Sessionfactory SF = cfg.configure ("Hibernate.cfg.xml"). Buildsessionfactory (); Session session = Sf.opensession (); Employee emp= (Employee) session.load (Employee.class, 2); System.out.println (Emp.getid () + "" +emp.getname () + "" +emp.getemail ()); Session.close (); INSERT SELECT private static void Addempoyee () {Employee s = new Employee (); S.setname ("Sunzhiyan"); S.setemail ("[email& Nbsp;protected] "); S.sethiredate (new Date ()); SYSTEM.OUT.PRINTLN ("Test is OK"); Configuration cfg=new configuration (); Sessionfactory SF = cfg.configure ("Hibernate.cfg.xml"). Buildsessionfactory (); Session session = Sf.opensession (); Session.begintransaction (); Session.save (s); Session.gettransaction (). commit (); Session.close (); Sf.close ();}}

In this way, basic hibernate basic configuration and testing is done

Java-hibernate's Hello World

Related Article

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.