Cet article décrit la version annotation de Hello World Implementation Method of Hibernate. Partagez-le pour votre référence, comme suit:
Packages qui doivent être introduits: Hibernate-Commons-Annotations-4.0.4.Final.jar
Comme j'utilise: Hibernate-Release-4.3.5.Final, il est déjà disponible dans le répertoire requis.
haricot:
import javax.persistence.column; import javax.persistence.entity; import javax.persistence.id; import javax.persistence.table; @ entité @ table (name = "enseignant") enseignant de classe publique {private int id; nom de chaîne privé; titre de chaîne privée; @Id public int getID () {return id; } public void setid (int id) {this.id = id; } @Column (name = "name") public String getName () {return name; } public void setName (string name) {this.name = name; } @Column (name = "title") public String getTitle () {return title; } public void SettItle (String Title) {this.title = title; }}Fichier Hibernate.cfg.xml correspondant:
<? xml version = '1.0' Encoding = 'UTF-8'?> <! Doctype Hibernate-Configuration Public "- // Hibernate / Hibernate Configuration DTD 3.0 // En" "http://www.hibernate.org/dtd/hibernate> <Session-Factory> <! - Paramètres de connexion de la base de données -> <propriété name = "connection.driver_class"> com.mysql.jdbc.driver </ propriété> <propriété name = "connection.url"> jdbc: mysql: // localhost / hibernate </ propriété> <propriété name = "Connexion.Username"> </preweth <! - JDBC Connection Pool (Utilisez le intégré) -> <! - <propriété name = "Connection.pool_size"> 1 </ Property> -> <! - SQL Dialect -> <propriété name = "Dialect"> org.hibernate.dialect.mysqldialect </parem name = "current_session_context_class"> thread </ propriété> <! - Désactivez le cache de deuxième niveau -> <propriété name = "cache.provider_class"> org.hibernate.cache.internal.nocacheprovider </ propriété> <! - Echo tous exécutés sql à stdout -> <propriété nom = "show_sq"> true <! - Drop et gouthom Recréez le schéma de base de données sur le démarrage -> <! - <propriété name = "hbm2ddl.auto"> update </ propriété> -> <mapping ressource = "com / hibernate / modèle / student.hbm.xml" /> <mapping /> </ session-factory> </ hibernate-configuration>
Classe de test:
import org.hibernate.ession; import org.hibernate.essionfactory; import org.hibernate.cfg.annotationConfiguration; import org.hibernate.cfg.configuration; import com.huxing.hibernate.model.Student; Import com.huxing.Hibernate.Model.Teacher; main (String [] args) {Student a = new Student (); a.setid (123); a.setage (32); A.SetName ("Hello Hibernate!"); Thé enseignant = nouveau professeur (); thé.setid (4); the.setname ("mysql"); thé.settitle ("high"); Configuration cfg = new AnnotationConfiguration (); SessionFactory CF = CFG.Configure (). BuildSessionFactory (); Session Session = cf.OpenSession (); session.begintransaction (); Session.Save (thé); session.getTransaction (). commit (); session.close (); cf.close (); }}Remarque: le code omet le chemin d'accès à package.
Autres aspects:
1. Des annotations peuvent être ajoutées aux attributs ou aux méthodes.
2. La différence entre le mappage et la configuration XML du XML annoté! L'un est une ressource et l'autre est la classe.
J'espère que cet article sera utile à la programmation du cadre Hibernate de tous.