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();
}
}

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

  1. Spring + Mybatis 企业应用实战 第3章 Sping MVC的常用注解

    注解(annotation) @Controller @Controller是扩展的@Component的,可以说基本一样,就是作为一种标志. @RequestMapping value:     指 ...

  2. Spring + Mybatis 企业应用实战 第1章 Java EE应用

    Java EE应用的分层模型: Domain Object 领域对象层.就是一些pojo. DAO(data access object) 数据访问对象 Service 业务逻辑层 Controlle ...

  3. 轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)

    轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)(国家级奖项获奖作品升级版,四版累计印刷27次发行量超10万册的轻量级Jav ...

  4. 轻量级Java EE企业应用实战:Struts2+Spring5+Hibernate5/JPA2

    轻量级Java EE企业应用实战(第5版)——Struts 2+Spring 5+Hibernate 5/JPA 2整合开发是<轻量级Java EE企业应用实战>的第5版,这一版保持了前几 ...

  5. 【JavaEE企业应用实战学习记录】optiontransferselect实现两个列表选择框

    <%@ page contentType="text/html; charset=GBK" language="java"%> <%@tagl ...

  6. 轻量级JAVA+EE企业应用实战(第4版)pdf电子书和源码的免费下载链接

    轻量级JAVA+EE企业应用实战(第4版)pdf电子书和源码的免费下载链接: pdf链接:https://pan.baidu.com/s/1dYIWtsv2haL4v7vx3w-8WQ 无提取密码源码 ...

  7. Spring3.x企业开发应用实战读书笔记 —— 第三章IoC容器概述

    声明:    本篇博客绝大多数内容为<Spring3.x企业开发应用实战>一书原内容,所有版权归原书作者所有!,仅供学习参考,勿作他用! 3.2 相关Java基础知识 Java语言允许通过 ...

  8. 两个月吃透阿里P9推荐260页SpringBoot2企业应用实战pdf入职定P6+

    前言 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置 ...

  9. 图书-技术-SpringBoot:《Spring Boot2 + Thymeleaf 企业应用实战》

    ylbtech-图书-技术-SpringBoot:<Spring Boot2 + Thymeleaf 企业应用实战> <Spring Boot 2+Thymeleaf企业应用实战&g ...

随机推荐

  1. Mac下安装Twig模版引擎的方法

    本人收集了几种方法: 安装前: twig需要依赖PHP 5.2.7 才能运行. 1 通过composer安装(2种) 方法一: 首先确保你已经安装了composer,如果没有安装,可以参考https: ...

  2. [Bootstrap]全局样式(四)

    按钮 1.基本类.btn {display/padding/margin-bottom/font-size/border-radius/border} 作用于< a:role:button &g ...

  3. 使用git的正确姿势

    1.Windows上安装git:从https://git-for-windows.github.io下载msysgit. 2.linux(Debian或Ubuntu)安装git:sudo apt-ge ...

  4. Android Animation ---TranslateAnimation

    if(stopBtn.getVisibility()==View.VISIBLE){ Animation animation_stop = new TranslateAnimation( Animat ...

  5. Eclipse经验总结

    1.在Project Explorer中显示src.resource目录:通过Project->properties->java Build Path->Source设置 2.解决J ...

  6. syntaxhighlighter语法高亮

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  7. 使用GruntJS构建Web程序

    Gruntjs是JavaScript项目的构建工具,也是基于node的一个命令行工具.很多开源JS项目都是使用它搭建.如jQuery.Qunit.CanJS等.它有以下作用 合并JS文件 压缩JS文件 ...

  8. JavaScript加强之自定义callback示例

    callback回调函数在本文以自定义的方式出现.   html:  <select id="select">  <option value="111& ...

  9. 2015年1月最新中国行政区划县及以上代码mysql数据库

    中华人民共和国国家统计局>> 行政区划代码>>mysql数据格式 截图如下 行政区划mysql数据库文件下载:nation.zip 转载:http://www.sdhack.c ...

  10. phonegap + 推送 + 页面跳转 (ios)

    os如果没有页面跳转的需求的话就使用极光推送,如果有页面跳转如果用了极光推送就只有用oc去写,但是我不会oc,所以智能放弃极光选用ios自己的apns来实现 新建项目跟安卓创建项目差不多,新建完成后就 ...