轻量级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();
}
}
轻量级Java_EE企业应用实战-第5章Hibernate的基本用法-001的更多相关文章
- Spring + Mybatis 企业应用实战 第3章 Sping MVC的常用注解
注解(annotation) @Controller @Controller是扩展的@Component的,可以说基本一样,就是作为一种标志. @RequestMapping value: 指 ...
- Spring + Mybatis 企业应用实战 第1章 Java EE应用
Java EE应用的分层模型: Domain Object 领域对象层.就是一些pojo. DAO(data access object) 数据访问对象 Service 业务逻辑层 Controlle ...
- 轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)
轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)(国家级奖项获奖作品升级版,四版累计印刷27次发行量超10万册的轻量级Jav ...
- 轻量级Java EE企业应用实战:Struts2+Spring5+Hibernate5/JPA2
轻量级Java EE企业应用实战(第5版)——Struts 2+Spring 5+Hibernate 5/JPA 2整合开发是<轻量级Java EE企业应用实战>的第5版,这一版保持了前几 ...
- 【JavaEE企业应用实战学习记录】optiontransferselect实现两个列表选择框
<%@ page contentType="text/html; charset=GBK" language="java"%> <%@tagl ...
- 轻量级JAVA+EE企业应用实战(第4版)pdf电子书和源码的免费下载链接
轻量级JAVA+EE企业应用实战(第4版)pdf电子书和源码的免费下载链接: pdf链接:https://pan.baidu.com/s/1dYIWtsv2haL4v7vx3w-8WQ 无提取密码源码 ...
- Spring3.x企业开发应用实战读书笔记 —— 第三章IoC容器概述
声明: 本篇博客绝大多数内容为<Spring3.x企业开发应用实战>一书原内容,所有版权归原书作者所有!,仅供学习参考,勿作他用! 3.2 相关Java基础知识 Java语言允许通过 ...
- 两个月吃透阿里P9推荐260页SpringBoot2企业应用实战pdf入职定P6+
前言 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置 ...
- 图书-技术-SpringBoot:《Spring Boot2 + Thymeleaf 企业应用实战》
ylbtech-图书-技术-SpringBoot:<Spring Boot2 + Thymeleaf 企业应用实战> <Spring Boot 2+Thymeleaf企业应用实战&g ...
随机推荐
- Amoeba For MySQL入门:实现数据库水平切分
当系统数据量发展到一定程度后,往往需要进行数据库的垂直切分和水平切分,以实现负载均衡和性能提升,而数据切分后随之会带来多数据源整合等等问题.如果仅仅从应用程序的角度去解决这类问题,无疑会加重应用程度的 ...
- VS 2013的初配置
首先,安装vs2013,安装过程比较简单,也已有教程,在此不赘述.只想说一下,vs2013需要占C盘比较大的空间:所有功能都安装,且装在C盘的话,约需要9G左右,即使安装在其他盘,也需要占C盘6G左右 ...
- ArcSDE for oracle10g安装后post的时候出现错误
The Post Installation Setup can not locate required Oracle files in your path.Check your Oracle inst ...
- grep比较两个文本相同不同行
grep -Fxf file1 file2 两个文本中的相同行 grep -Fvxf <(grep -Fxf file1 file2) file1 file2 两个文本中的不同行 grep - ...
- Open Live Writer增加代码插件
作为一名程序员,免不了和代码打交道,自然在写博客的时候,也会用到代码的展示,Open Live Writer确实是一个不错的工具,不用再去登录博客的后台,就可以在本地进行文章的编写,但是致 ...
- Mysql创建函数时报错
先去查询 show variables like '%func%' ; 这个语句,如果该语句最后输出的值是OFF 那么就用下面的语句去修改就可以:set global log_bin_trust_f ...
- [Linux]学习笔记(2)
本节主要学习: whoami who am i who w users tty 6个命令的用法. (1)whoami whoami用于查询当前是以哪个用户登录Linux系统: [root@linuxf ...
- 批量修改数据sql
--insert into P_ZPROMOTION_DOC_ITEMS (AKTNR,MATNR,MINGROSS,MCRANK,MCUPRICE,MAXBAKTNR,MAXBPAMONT,MAXB ...
- unity3d 延迟处理方法
Invoke("方法名", 多少秒后执行); InvokeRepeating("方法名", 多少秒后执行,开始执行后隔多长时间再次执行一次); CancelIn ...
- C# window service的创建
其实我也是第一次在博客园写博客,看到那些高手说自己要多动手写博客,于是乎自己也尝试尝试. 废话不多说.这几天在研究window service,通过查找各种大神写的博客,我终于成功的自己写出来了. 下 ...