Hibernate simple annotation (1), hibernate Annotation
* **************************** Hibernate. cfg. xml ************************************
<? Xml version = '1. 0' encoding = 'utf-8'?>
<! DOCTYPE hibernate-configuration PUBLIC
"-// Hibernate/Hibernate Configuration DTD 3.0 // EN"
Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd>
<Hibernate-configuration>
<Session-factory>
<! -- Database connection settings -->
<Property name = "connection. driver_class"> com. mysql. jdbc. Driver </property>
<Property name = "connection. url"> jdbc: mysql: // localhost: 3306/hibernate </property>
<Property name = "connection. username"> root </property>
<Property name = "connection. password"> 123456 </property>
<! -- SQL dialect -->
<Property name = "dialect"> org. hibernate. dialect. MySQL5InnoDBDialect </property>
<! -- Echo all executed SQL to stdout -->
<Property name = "show_ SQL"> true </property>
<! -- Drop and re-create the database schema on startup -->
<Property name = "hbm2ddl. auto"> create </property>
<! -- Names the annotated entity class -->
<Mapping class = "com. ij34.dao. New"/>
</Session-factory>
</Hibernate-configuration>
**************************************** **************************************** ********
Package com. ij34.dao;
Import javax. persistence .*;
@ Entity
@ Table (name = "test01 ")
Public class New {
@ Id
@ GeneratedValue (strategy = GenerationType. IDENTITY)
Private int id;
Private String title;
Private String content;
Public int getId (){
Return id;
}
Public void setId (int id ){
This. id = id;
}
Public String getTitle (){
Return title;
}
Public void setTitle (String title ){
This. title = title;
}
Public String getContent (){
Return content;
}
Public void setContent (String content ){
This. content = content;
}
}
**************************************** *****
Package com. ij34.web;
Import org. hibernate. Session;
Import org. hibernate. SessionFactory;
Import org. hibernate. Transaction;
Import org. hibernate. boot. registry. StandardServiceRegistryBuilder;
Import org. hibernate. cfg. Configuration;
Import org. hibernate. service .*;
Import com. ij34.dao. New;
Public class test01 {
Public static void main (String [] args) throws Exception {
// Instantiate Configuration
Configuration conf = new Configuration (). configure ();
ServiceRegistry SR = new StandardServiceRegistryBuilder (). applySettings (conf. getProperties (). build ();
// Create a SessionFactory instance with a Configuration instance
SessionFactory SF = conf. buildSessionFactory (SR );
// Create session
Session session = SF. openSession ();
// Start transaction
Transaction tx = session. beginTransaction ();
New n = new New ();
N. setTitle ("hello ");
N. setContent ("hello world ");
Session. save (n );
Tx. commit ();
Session. close ();
SF. close ();
}
}