hibernate基本的配置与验证
导入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基本的配置与验证的更多相关文章
- hibernate.hbm2ddl.auto配置详解
hibernate.cfg.xml 中hibernate.hbm2ddl.auto配置节点如下:<properties><property name="hibernate. ...
- Mingyang.net:hibernate.hbm2ddl.auto配置详解【转】
原文地址:http://www.cnblogs.com/feilong3540717/archive/2011/12/19/2293038.html hibernate.cfg.xml 中hibern ...
- Java工具类——通过配置XML验证Map
Java工具类--通过配置XML验证Map 背景 在JavaWeb项目中,接收前端过来的参数时通常是使用我们的实体类进行接收的.但是呢,我们不能去决定已经搭建好的框架是怎么样的,在我接触的框架中有一种 ...
- SpringMVC+Apache Shiro+JPA(hibernate)整合配置
序: 关于标题: 说是教学,实在愧不敢当,但苦与本人文笔有限,实在找不到更合理,谦逊的词语表达,只能先这样定义了. 其实最真实的想法,只是希望这个关键词能让更多的人浏览到这篇文章,也算是对于自己写文章 ...
- Hibernate中用注解配置一对多双向关联和多对一单向关联
Hibernate中用注解配置一对多双向关联和多对一单向关联 Hibernate提供了Hibernate Annotations扩展包,使用注解完成映射.在Hibernate3.3之前,需单独下载注解 ...
- hibernate.cfg.xml配置
hibernate.hbm2ddl.auto 配置: create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这 ...
- JAVA spring hibernate 多数据源配置记录
数据源配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...
- 【Mongodb】3.X 配置身份验证
配置身份验证详解: 开启认证: 启动MongoDB./mongodb --syslog --fork --port 20000 --auth 1.如果不添加参数:auth,表明用默认的root的权限 ...
- 攻城狮在路上(壹) Hibernate(十五)--- Hibernate的高级配置
一.配置数据库连接池: 1.使用默认的数据库连接池: Hibernate提供了默认了数据库连接池,它的实现类为DriverManegerConnectionProvider,如果在Hibernate的 ...
随机推荐
- swift 有道 翻译文档(2 条件语句 循环语句)
控制流使用if和switch来创建条件语句,使用for-in.while和repeat-while来创建循环.条件或循环变量的括号是可选的.身体周围需要支撑. let individualScores ...
- Windows安装zookeeper 单机版
首先需要安装JdK,从Oracle的Java网站下载,安装很简单,就不再详述. 1.下载zookeeper, https://mirrors.tuna.tsinghua.edu.cn/apache/z ...
- [python]目录及文件操作
Python OS模块和shutil模块 获取路径 # 获取当前路径 pwd = os.getcwd() # 获取上级路径 a_pwd = os.path.abspath(os.path.dirnam ...
- legend2---项目总结(legend2的意义)
legend2---项目总结(legend2的意义) 一.总结 一句话总结:总体来说还是化腐朽为神奇的,之前投了很多精力在学习上面,学的内容非常多,但是都记不住,尤其是英语,感悟也是没办法继续深悟,这 ...
- [English] Time complexity wise this solution is the best among all
Time complexity wise this solution is the best among all, we can do all operations in O(1) time. 时间复 ...
- Windows系统Nessus离线(Offline) 版的安装
Nessus离线(offline)版可以在局域网内进行系统漏洞扫描,下面简单介绍其windows系统版本的安装过程. 1. 登陆Tenable网站: https://www.tenable.com/ ...
- python3.7导入gevent模块报错的解决方案
最近更新了python解释器3.7 结果安装gevent,在导入gevent之后就报错了,错误信息如下 RuntimeWarning: greenlet.greenlet size changed, ...
- 在命令行启动vscode
1.windows使用 code . 命令打开vscode 1.打开vscode安装位置,进入bin文件夹,复制路径 eg:E:\Microsoft VS Code\bin:2.回到桌面,右键我的电脑 ...
- 使用logstash同步mysql数据到elasticsearch
下载 logstash tar -zxvf https://artifacts.elastic.co/downloads/logstash/logstash-6.3.2.tar.gz .tar.gz ...
- 笔记《JavaScript 权威指南》(第6版) 分条知识点概要3—表达式和运算符
[表达式和运算符]原始表达式,初始化表达式(对象和数组的),函数定义表达式,属性访问表达式,调用表达式,对象创建表达式,运算符概述,算术表达式,关系表达式,逻辑表达式,赋值表达式,表达式计算,其他运算 ...