导入jar包与mysql驱动包

javaBean

src/com/crazyit/app/domain/News.java

package com.crazyit.app.domain;

import javax.persistence.*;

@Entity
@Table(name="news_info")
public class News {
private Integer id;
private String title;
private String content; @Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer getId() {
return id;
}
public void setId(Integer 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;
} }

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 指定连接数据库所用的驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 指定连接数据库的URL,其中hibernatedemo是本应用连接的数据库名 -->
<property name="connection.url">jdbc:mysql://localhost:3306/hibernatedemo</property>
<!-- 指定连接数据库的用户名 -->
<property name="connection.username">lidian</property>
<!-- 指定连接数据库的密码 -->
<property name="connection.password">lidian1234</property>
<!-- 指定连接池里最大连接数 -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 指定连接池里最小连接数 -->
<property name="hibernate.c3p0.min_size">1</property>
<!-- 指定连接池里连接的超时时长 -->
<property name="hibernate.c3p0.timeout">5000</property>
<!-- 指定连接池里最大缓存多少个Statement对象 -->
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.acquire_ increment">2</property>
<property name="hibernate.c3p0.validate">true</property> <!-- 指定数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 根据需要自动创建数据库表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 显示hibernate持久化所生成的sql -->
<property name="show_sql">true</property>
<!-- 将SQL脚本进行格式化再输出 -->
<property name="hibernate.format_sql">true</property> <!-- 罗列所有持久化的类名 -->
<mapping class="com.crazyit.app.domain.News"/>
</session-factory>
</hibernate-configuration>

验证结果

src/lee/NewsManager.java

package lee;

import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry; import com.crazyit.app.domain.News; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;; public class NewsManager {
public static void main(String[] args) throws Exception{
//实例化Configuration
//不带参数的configure()默认加载hibernate.cfg.xml
//如果传入abc.xml作为参数,则不再加载hibernate.cfg.xml,改为加载abc.xml
Configuration conf = new Configuration().configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(conf.getProperties()).build();
//以Configuration的实例创建SessionFactory
SessionFactory sf = conf.buildSessionFactory(serviceRegistry);
//创建Session
Session se = sf.openSession();
//开始事物
Transaction tx = se.beginTransaction();
//创建消息对象
News news = new News();
news.setTitle("疯狂java");
news.setContent("疯狂java,"+"网址https://www.baidu.com/"); //保存消息
se.save(news);
//提交事务
tx.commit();
//关闭session
se.close();
sf.close();
}
}

hibernate基本的配置与验证的更多相关文章

  1. hibernate.hbm2ddl.auto配置详解

    hibernate.cfg.xml 中hibernate.hbm2ddl.auto配置节点如下:<properties><property name="hibernate. ...

  2. Mingyang.net:hibernate.hbm2ddl.auto配置详解【转】

    原文地址:http://www.cnblogs.com/feilong3540717/archive/2011/12/19/2293038.html hibernate.cfg.xml 中hibern ...

  3. Java工具类——通过配置XML验证Map

    Java工具类--通过配置XML验证Map 背景 在JavaWeb项目中,接收前端过来的参数时通常是使用我们的实体类进行接收的.但是呢,我们不能去决定已经搭建好的框架是怎么样的,在我接触的框架中有一种 ...

  4. SpringMVC+Apache Shiro+JPA(hibernate)整合配置

    序: 关于标题: 说是教学,实在愧不敢当,但苦与本人文笔有限,实在找不到更合理,谦逊的词语表达,只能先这样定义了. 其实最真实的想法,只是希望这个关键词能让更多的人浏览到这篇文章,也算是对于自己写文章 ...

  5. Hibernate中用注解配置一对多双向关联和多对一单向关联

    Hibernate中用注解配置一对多双向关联和多对一单向关联 Hibernate提供了Hibernate Annotations扩展包,使用注解完成映射.在Hibernate3.3之前,需单独下载注解 ...

  6. hibernate.cfg.xml配置

    hibernate.hbm2ddl.auto 配置: create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这 ...

  7. JAVA spring hibernate 多数据源配置记录

    数据源配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...

  8. 【Mongodb】3.X 配置身份验证

    配置身份验证详解: 开启认证: 启动MongoDB./mongodb --syslog --fork --port 20000  --auth 1.如果不添加参数:auth,表明用默认的root的权限 ...

  9. 攻城狮在路上(壹) Hibernate(十五)--- Hibernate的高级配置

    一.配置数据库连接池: 1.使用默认的数据库连接池: Hibernate提供了默认了数据库连接池,它的实现类为DriverManegerConnectionProvider,如果在Hibernate的 ...

随机推荐

  1. R-画图

    1.par(mar=c(8,5.2,8,5.2),new=TRUE,cex=1.5,mfrow=c(2,2))   (参考:http://blog.sina.com.cn/s/blog_6caea8b ...

  2. git命令-切换分支

    git一般有很多分支,我们clone到本地的时候一般都是master分支,那么如何切换到其他分支呢? 1. 查看远程分支 $ git branch -a 我在mxnet根目录下运行以上命令: ~/mx ...

  3. Java中的各种bean对应的意义(VO,PO,BO,QO, DAO,POJO,DTO)

    VO(value object) 值对象 通常用于业务层之间的数据传递,用 new 关键字创建,由 GC 回收的,和 PO 一样也是仅仅包含数据而已.但应是抽象出的业务对象 , 可以和表对应 , 也可 ...

  4. oledb

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data; ...

  5. Ocelot概述

    Ocelot是一个基于netcoreapp2.0构建,.NET Core框架下的开源Api网关项目,用于开发基于.NET微服务架构或面向服务架构系统的统一入口.

  6. 怎么样启用红米手机5的ROOT权限

    红米手机5能如何拥有了root超级权限?各位清楚,android机器有root超级权限,一旦手机拥有了root相关权限,就能够实现更强的功能,举个栗子各位公司的营销部门的妹纸,使用较多营销工具都需要在 ...

  7. Matlab-6:解非线性方程组newton迭代法

    函数文件: function x=newton_Iterative_method(f,n,Initial) x0=Initial; tol=1e-11; x1=x0-Jacobian(f,n,x0)\ ...

  8. 使用 requests 模块

    官网:http://docs.python-requests.org/en/master/ 请求方式 requests.get() requests.post() requests.put() req ...

  9. zzw原创_非root安装fastDFS

    zzw原创_非root安装fastDFS fastDFS 想要非root安装,没找到资料,分析了一下安装脚本,原来作者是留了安装路径的,但没有放出来. 1.解包 [bdc@svr001 setup]$ ...

  10. 相似的RGB颜色——算法面试刷题3(for google),考察二分

    在本题中,每个大写字母代表从“0”到“f”的一些十六进制数字. 红绿蓝三元色#AABBCC可以简写为#ABC. 例如,#15c是颜色#1155cc的简写. 现在,假设两种颜色#ABCDEF和#UVWX ...