导入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. Go-延时函数defer

    关于延时调用函数(Deferred Function Calls)      延时调用函数基本语法如下: defer func_name(param-list) {} 当一个函数前有关键字 defer ...

  2. sublime 部分插件

    https://www.cnblogs.com/qingkong/p/5039527.html

  3. Kinect外包团队— 2016中国VR开发者论坛第一期

    由VR界网和暴风魔镜联合举办的2016中国VR开发者论坛第一期已于3月2日下午5点在吉林动画学院圆满落幕,本次论坛云集了VR相关领域的精英,邀请了VR社交<极乐王国>.暴风魔镜.南京睿悦. ...

  4. (一)为什么要UML

    1 建模的意义 模型是对于现实的简化,建模是为了更好的理解系统 模型帮助我们按照实际情况或需求对系统可视化 模型允许我们详细说明系统的构造,行为 模型给出一个构造系统的模板 模型对我们做出的决策进行文 ...

  5. Mybatis日志

    [DEBUG] 2018-05-20 09:56:36,404(19404) --> [main] org.slf4j.impl.JCLLoggerAdapter.trace(JCLLogger ...

  6. arcgis raster clip and mask difference 栅格 提取 clip 和 mask 方法的区别

    建议使用 数据管理工具下的此工具进行操作: 在选择参数时,注意做如下选择,可以保证栅格不会轻微错开: 也就是勾选第一个,而不勾选第二个 看看效果,使用mask得到的裁剪结果,注意有错开现象: 使用cl ...

  7. linux软件管理之python包管理

    Python包管理 ====================================================================================python ...

  8. python标准库总的random函数用法

    Python标准库中的random函数,可以生成随机浮点数.整数.字符串,甚至帮助你随机选择列表序列中的一个元素,打乱一组数据等.random中的一些重要函数的用法:1 ).random() 返回0& ...

  9. 学习node.js的一些笔记

    最近看了几眼node.js,以前曾听说它用途很大. 在菜鸟教程上,已看了过半的内容:http://www.runoob.com/nodejs/nodejs-web-module.html,如今看到了这 ...

  10. 一个空格引起的错误。 python

    'render_field' tag requires a form field followed by a list of attributes and values in the form att ...