轻量级Java_EE企业应用实战-第5章Hibernate的基本用法-001
1.
1 package org.crazyit.app.domain; 2 3 import javax.persistence.*; 4 5 /** 6 * Description: <br/> 7 * ��վ: <a href="http://www.crazyit.org">���Java����</a> <br/> 8 * Copyright (C), 2001-2016, Yeeku.H.Lee <br/> 9 * This program is protected by copyright laws. <br/> 10 * Program Name: <br/> 11 * Date: 12 * 13 * @author Yeeku.H.Lee kongyeeku@163.com 14 * @version 1.0 15 */ 16 @Entity 17 @Table(name = "news_inf") 18 public class News { 19 @Id 20 @GeneratedValue(strategy = GenerationType.IDENTITY) 21 private Integer id; 22 private String title; 23 private String content; 24 25 public void setId(Integer id) { 26 this.id = id; 27 } 28 29 public Integer getId() { 30 return this.id; 31 } 32 33 public void setTitle(String title) { 34 this.title = title; 35 } 36 37 public String getTitle() { 38 return this.title; 39 } 40 41 public void setContent(String content) { 42 this.content = content; 43 } 44 45 public String getContent() { 46 return this.content; 47 } 48 }
2.
1 package lee; 2 3 import org.hibernate.*; 4 import org.hibernate.cfg.*; 5 import org.hibernate.service.*; 6 import org.hibernate.boot.registry.*; 7 import org.crazyit.app.domain.*; 8 9 /** 10 * Description: <br/> 11 * ��վ: <a href="http://www.crazyit.org">���Java����</a> <br/> 12 * Copyright (C), 2001-2016, Yeeku.H.Lee <br/> 13 * This program is protected by copyright laws. <br/> 14 * Program Name: <br/> 15 * Date: 16 * 17 * @author Yeeku.H.Lee kongyeeku@163.com 18 * @version 1.0 19 */ 20 public class NewsManager { 21 public static void main(String[] args) throws Exception { 22 Configuration conf = new Configuration().configure(); 23 24 ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() 25 .applySettings(conf.getProperties()).build(); 26 SessionFactory sf = conf.buildSessionFactory(serviceRegistry); 27 Session sess = sf.openSession(); 28 Transaction tx = sess.beginTransaction(); 29 News n = new News(); 30 n.setTitle("Java web"); 31 n.setContent("java web content"); 32 sess.save(n); 33 tx.commit(); 34 sess.close(); 35 sf.close(); 36 } 37 }
3.
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 8 <property name="hibernate.connection.password">1234</property> 9 <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property> 10 <property name="hibernate.connection.username">root</property> 11 <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> 12 <property name="hbm2ddl.auto">update</property> 13 <property name="show_sql">true</property> 14 <mapping class="org.crazyit.app.domain.News"/> 15 </session-factory> 16 </hibernate-configuration>
4.
1 package lee; 2 3 import org.hibernate.*; 4 import org.hibernate.cfg.*; 5 import org.hibernate.service.*; 6 import org.hibernate.boot.registry.*; 7 import org.crazyit.app.domain.*; 8 9 /** 10 * Description: 11 * <br/>ÍøÕ¾: <a href="http://www.crazyit.org">·è¿ñJavaÁªÃË</a> 12 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee 13 * <br/>This program is protected by copyright laws. 14 * <br/>Program Name: 15 * <br/>Date: 16 * @author Yeeku.H.Lee kongyeeku@163.com 17 * @version 1.0 18 */ 19 public class NewsManager2 20 { 21 public static void main(String[] args) throws Exception 22 { 23 Configuration conf = new Configuration() 24 .addAnnotatedClass(org.crazyit.app.domain.News.class) 25 .setProperty("hibernate.connection.driver_class" 26 , "com.mysql.jdbc.Driver") 27 .setProperty("hibernate.connection.url" 28 , "jdbc:mysql://localhost/hibernate") 29 .setProperty("hibernate.connection.username" , "root") 30 .setProperty("hibernate.connection.password" , "1234") 31 .setProperty("hibernate.c3p0.max_size" , "20") 32 .setProperty("hibernate.c3p0.min_size" , "1") 33 .setProperty("hibernate.c3p0.timeout" , "5000") 34 .setProperty("hibernate.c3p0.max_statements" , "100") 35 .setProperty("hibernate.c3p0.idle_test_period" , "3000") 36 .setProperty("hibernate.c3p0.acquire_increment" , "2") 37 .setProperty("hibernate.c3p0.validate" , "true") 38 .setProperty("hibernate.dialect" 39 , "org.hibernate.dialect.MySQL5InnoDBDialect") 40 .setProperty("hibernate.hbm2ddl.auto" , "update"); 41 ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() 42 .applySettings(conf.getProperties()).build(); 43 SessionFactory sf = conf.buildSessionFactory(serviceRegistry); 44 Session sess = sf.openSession(); 45 Transaction tx = sess.beginTransaction(); 46 News n = new News(); 47 n.setTitle("测试"); 48 n.setContent("测试测试"); 49 sess.save(n); 50 tx.commit(); 51 sess.close(); 52 } 53 }
You can do anything you set your mind to, man!