hibernate之helloword(环境搭建)
环境搭建
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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///web_shop</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123</property> <!-- 设置连接提供者 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- c3p0连接池的配置 -->
<property name="hibernate.c3p0.max_size">20</property> <!-- 最大连接池 -->
<property name="hibernate.c3p0.min_size">5</property> <!-- 最小连接数 -->
<property name="hibernate.c3p0.timeout">120</property> <!-- 超时 -->
<property name="hibernate.c3p0.idle_test_period">3000</property> <!-- 空闲连接 --> <!-- 可以将向数据库发送的sql显示出来 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化sql -->
<property name="hibernate.format_sql">true</property> <!-- hibernate的方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 自动创建表 -->
<!-- <property name="hibernate.hbm2ddl.auto">update</property> --> <!-- 用于设置事务提交方式 -->
<property name="hibernate.connection.autocommit">false</property> <!-- 配置hibernate的映射文件所在位置 -->
<mapping resource="com/baidu/domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
javabean
public class User {
private String uid;
private String username;
private String password;
private String name;
private String email;
private String telephone;
private Date birthday;
private String sex;
private Integer state;
private String code;
public User() {
super();
}
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
public User(String uid, String username, String password, String name,
String email, String telephone, Date birthday, String sex,
Integer state, String code) {
super();
this.uid = uid;
this.username = username;
this.password = password;
this.name = name;
this.email = email;
this.telephone = telephone;
this.birthday = birthday;
this.sex = sex;
this.state = state;
this.code = code;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getState() {
return state;
}
public void setState(Integer state) {
this.state = state;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public String toString() {
return "User [uid=" + uid + ", username=" + username + ", password="
+ password + ", name=" + name + ", email=" + email
+ ", telephone=" + telephone + ", birthday=" + birthday
+ ", sex=" + sex + ", state=" + state + ", code=" + code + "]";
}
}
xxx.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--name表示类的全路径名 table表名 catalog表示数据库名 -->
<class name="com.baidu.domain.User" table="user" catalog="web_shop">
<!-- 表示主键 -->
<id name="uid" column="uid">
<!-- <generator class="native"></generator> -->
</id>
<property name="username" column="username" length="20"></property>
<property name="password" column="password" length="20"></property>
<property name="name" column="name" length="20"></property>
<property name="email" column="email" length="20"></property>
<property name="telephone" column="telephone" length="20"></property>
<property name="birthday" column="birthday" ></property>
<property name="sex" column="sex" length="10"></property>
<property name="state" column="state" length="11"></property>
<property name="code" column="code" length="64"></property>
</class>
</hibernate-mapping>
导包:

数据库连接池包

数据库连接

hibernate连接池依赖c3p0包


创建工具类
public class HibernateUtils {
private static SessionFactory sessionFactory=null;
static{
//获取config 加载配置文件
Configuration configure = new Configuration().configure();
sessionFactory = configure.buildSessionFactory();
}
//获取session
public static Session getSession(){
Session session = sessionFactory.openSession();
return session;
}
}
测试类:
@Test //删除方法
public void test1(){
// 创建一个User对象
//保证每个用户去出来的session都不相同
User user = new User();
user.setUid("1234");
Session session = HibernateUtils.getSession(); // 相当于得到一个Connection。
// 开启事务
Transaction transaction = session.beginTransaction();
// 操作
//删除时必须根据主键进行删除
session.delete(user);
// 事务提交
transaction.commit();
//不需要关闭Configuration 因为Configuration不是轻量级的。这样一个项目就只产生一个Configuration
//configuration相当于连接池
session.close();
}
hibernate之helloword(环境搭建)的更多相关文章
- 最新版ssh hibernate spring struts2环境搭建
最新版ssh hibernate spring struts2环境搭建 最新版spring Framework下载地址:spring4.0.0RELEASE环境搭建 http://repo.sprin ...
- Hibernate 系列 02 - Hibernate介绍及其环境搭建
引导目录: Hibernate 系列教程 目录 昨晚喝多了,下午刚清醒,继续搞Hibernate.走起. 觉得还行的话,记得点赞哈,给我这个渣渣点学习的动力.有错误的话也请指出,省的我在错误上走了不归 ...
- Hibernate 介绍及其 环境搭建
介绍 数据持久化概念 数据持久化是将内存中的数据模型转换为存储模型,以及将存储模型转换为内存中的数据模型的统称.例如:文件的存储.数据的读取等都是数据持久化操作.数据模型可以是任何数据结构或对象模型, ...
- Hibernate(一)——采用Hibernate框架开发环境搭建
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员充分使用对象编程思维来操作数据库.HIbernate的移植性很好,它可以应用于任何JDB ...
- hibernate介绍及环境搭建
1.前言 hibernate与mybatis的位置一样,都是属于DAO层的框架,代替我们原来的JDBC操作数据库,属于ORM(object relationg mapping. 对象关系映射)框架.O ...
- Hibernate学习笔记--环境搭建及运行
1.hibernate开发包下载 http://sourceforge.net/projects/hibernate/files/ 如果不能访问请用代理http://dongtaiwang.com/l ...
- Hibernate之环境搭建
开始之前,我想先理清一个概念,即ORM是什么? ORM介绍 全称:Object/Relation Mapping,即对象/关系映射. ORM也可以理解为一种规范,具体的ORM框架可作为应用程序和数据库 ...
- Hibernate学习之——Hibernate环境搭建
之前在写关于安卓闹钟的教程,写了一半就没后一半了,其实自己也没做好,在校外实习,校内毕业实习又有任务,只能先放放了,等毕业实习结束之后,在继续安卓闹钟开发之旅,相信这个时间不会很久的.现在毕业实习用到 ...
- Hibernate之环境搭建及demo
ORM概念 ORM即Object/Relation Mapping, 对象/关系数据库映射.ORM是一种规范,完成面向对象编程语言到关系数据库之间的映射.J2EE中的JPA就是一种ORM规范. ORM ...
随机推荐
- 关于thymeleaf的if多条件判断
<ul class="nav nav-second-level"> <li th:each="cmenu : ${menu.children}" ...
- 一位大牛整理的Python资源
Python基本安装: * http://www.python.org/ 官方标准Python开发包和支持环境,同时也是Python的官方网站: * http://www.activestate ...
- linux 输出重定向
输出重定向 标准输入 文件描述符:0 设备:键盘 设备文件名:/dev/stdin 标准输出 文件描述符:1 设备:显示器 设备文件名:/dev/sdtout 标准输出重定向 命令 >> ...
- js对象(BOM部分/DOM部分)
JS总体包括ECMAScript,DOM,BOM三个部分,但是能够和浏览器进行交互的只有DOM和BOM,那么到底什么是DOM和BOM呢 概念 BOM(Browser Object Model)是指浏览 ...
- android xml解析中的null问题
当我们从服务器或者xml文件加载xml进行解析的时候,往往报告 nullpointer 错误.这是原始代码: String short_name = doc.getElementsByTagName( ...
- PHP TP 生成二维码
vendor('phpqrcode.phpqrcode'); $value = "http://www.baidu.com";//二维码内容 $errorCorrectionLev ...
- centos安装tomcat7.0.70
抄自:https://www.cnblogs.com/www1707/p/6592504.html apache-tomcat-7.0.70jdk-7u67-linux-x64 下载tomcathtt ...
- Redis的基操
redis:通常BOLEAN操作类型,操作成功返回1,操作失败返回0 通常如果往设置的key插入值,但是这个key不存在,redis则会创建 向redis里的某个key插入多个值时,值和值之间用空格隔 ...
- iOS中四种实例变量的范围类型@private@protected@public@package
文档上记录是这样的 The Scope of Instance Variables Toenforce the ability of an object to hide its data, the c ...
- 浅谈System.gc()
今天巩固给大家讲讲System.gc().Java的内存管理着实给各位编程者带来很大的方便,使我们不再需要为内存分配烦太多神.那么讲到垃圾回收机制,就不得不讲讲System.gc(). 先简单 ...