轻量级Java_EE企业应用实战-第5章Hibernate的基本用法-001

时间:2023-03-09 16:17:44
轻量级Java_EE企业应用实战-第5章Hibernate的基本用法-001

1.

 package org.crazyit.app.domain;

 import javax.persistence.*;

 /**
* Description: <br/>
* ��վ: <a href="http://www.crazyit.org">���Java����</a> <br/>
* Copyright (C), 2001-2016, Yeeku.H.Lee <br/>
* This program is protected by copyright laws. <br/>
* Program Name: <br/>
* Date:
*
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
@Entity
@Table(name = "news_inf")
public class News {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String title;
private String content; public void setId(Integer id) {
this.id = id;
} public Integer getId() {
return this.id;
} public void setTitle(String title) {
this.title = title;
} public String getTitle() {
return this.title;
} public void setContent(String content) {
this.content = content;
} public String getContent() {
return this.content;
}
}

2.

 package lee;

 import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.service.*;
import org.hibernate.boot.registry.*;
import org.crazyit.app.domain.*; /**
* Description: <br/>
* ��վ: <a href="http://www.crazyit.org">���Java����</a> <br/>
* Copyright (C), 2001-2016, Yeeku.H.Lee <br/>
* This program is protected by copyright laws. <br/>
* Program Name: <br/>
* Date:
*
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class NewsManager {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration().configure(); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(conf.getProperties()).build();
SessionFactory sf = conf.buildSessionFactory(serviceRegistry);
Session sess = sf.openSession();
Transaction tx = sess.beginTransaction();
News n = new News();
n.setTitle("Java web");
n.setContent("java web content");
sess.save(n);
tx.commit();
sess.close();
sf.close();
}
}

3.

 <?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">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">1234</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping class="org.crazyit.app.domain.News"/>
</session-factory>
</hibernate-configuration>

4.

 package lee;

 import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.service.*;
import org.hibernate.boot.registry.*;
import org.crazyit.app.domain.*; /**
* Description:
* <br/>ÍøÕ¾: <a href="http://www.crazyit.org">·è¿ñJavaÁªÃË</a>
* <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class NewsManager2
{
public static void main(String[] args) throws Exception
{
Configuration conf = new Configuration()
.addAnnotatedClass(org.crazyit.app.domain.News.class)
.setProperty("hibernate.connection.driver_class"
, "com.mysql.jdbc.Driver")
.setProperty("hibernate.connection.url"
, "jdbc:mysql://localhost/hibernate")
.setProperty("hibernate.connection.username" , "root")
.setProperty("hibernate.connection.password" , "1234")
.setProperty("hibernate.c3p0.max_size" , "20")
.setProperty("hibernate.c3p0.min_size" , "1")
.setProperty("hibernate.c3p0.timeout" , "5000")
.setProperty("hibernate.c3p0.max_statements" , "100")
.setProperty("hibernate.c3p0.idle_test_period" , "3000")
.setProperty("hibernate.c3p0.acquire_increment" , "2")
.setProperty("hibernate.c3p0.validate" , "true")
.setProperty("hibernate.dialect"
, "org.hibernate.dialect.MySQL5InnoDBDialect")
.setProperty("hibernate.hbm2ddl.auto" , "update");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(conf.getProperties()).build();
SessionFactory sf = conf.buildSessionFactory(serviceRegistry);
Session sess = sf.openSession();
Transaction tx = sess.beginTransaction();
News n = new News();
n.setTitle("测试");
n.setContent("测试测试");
sess.save(n);
tx.commit();
sess.close();
}
}