Hibernate simple annotation (1. 1), hibernate annotation. 1
**************************************** **************************************** ***************************
Package com. ij34.web;
Import org. hibernate .*;
Import org. hibernate. boot. registry .*;
Import org. hibernate. cfg .*;
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 ()
. AddAnnotatedClass (com. ij34.dao. New. class)
. SetProperty ("hibernate. connection. driver_class", "com. mysql. jdbc. Driver") // you must add hibernate.
. SetProperty ("hibernate. connection. url", "jdbc: mysql: // localhost/hibernate") // you must add hibernate
. SetProperty ("hibernate. connection. username", "root") // you must add hibernate.
. SetProperty ("hibernate. connection. password", "123456") // you must add hibernate
. SetProperty ("hibernate. dialect", "org. hibernate. dialect. MySQL5InnoDBDialect") // hibernate must be added.
. SetProperty ("show_mysql", "true ")
. SetProperty ("hibernate. hbm2ddl. auto", "create"); // you must add hibernate
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 ("2016 hello world 123456789 ");
Session. save (n );
Tx. commit ();
Session. close ();
SF. close ();
}
}
**************************************** **************************************** *******************
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;
}
}