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 ...
随机推荐
- EF语句拦截器-匹配当前的Controller,Action,User
示例代码,ps:一切都能实现,关键是你尝试的方向,别把简单问题复杂化导致进入死胡同出不来. using Mobile360.Core.Interfaces; using Mobile360.Core. ...
- 解决video标签在微信中强制全屏、微信全屏播放(Android和IOS)
在video标签中加上代码: x5-playsinline="true" webkit-playsinline="true" playsinline=" ...
- js拷贝指定内容到剪切板
function copyTextToClipboard(text) { var textArea = document.createElement("textarea"); te ...
- c# 上传excel数据总结(一)线程的使用
1: 因为程序涉及到上传,开始暂停,继续,删除, thread 在老版本用使用th.Abort(); th.Resume(); 停止 恢复 th.Suspend(); 挂起 猛的一看挺合适啊..但微 ...
- Oracle_PL/SQL(8) 动态sql
动态sql0.pl/sql块的限制 不能执行ddl操作(create.drop.alter): 不能执行部分dcl操作(grant.revoke). 1.语法动态sql:在执行时才能确定要执行的sql ...
- Oracle to_char函数的使用方法
Oracle to_char函数的功能是将数值型或者日期型转化为字符型,下面就为您详细介绍Oracle to_char函数的使用,希望对您能有所帮助. Postgres 格式化函数提供一套有效的工具用 ...
- 01. pt-align
01. pt-align pt-align xxx.txt =========================================== pt-align对齐输出格式 name city a ...
- Web 文件上传方面的安全问题
一. 文件上传漏洞与WebShell的关系 文件上传漏洞是指网络攻击者上传了一个可执行的文件到服务器并执行.这里上传的文件可以是木马,病毒,恶意脚本或者WebShell等.这种攻击方式是最为直接和有效 ...
- Spring 注解驱动(一)基本使用规则
Spring 注解驱动(一)基本使用规则 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 一.基本使用 @Configur ...
- [规则原则定理]规则原则定理章2ACID原则
ACID,指数据库事务正确执行的四个基本要素的缩写.包含:原子性(Atomicity).一致性(Consistency).隔离性(Isolation).持久性(Durability).一个支持事务(T ...