环境搭建

    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(环境搭建)的更多相关文章

  1. 最新版ssh hibernate spring struts2环境搭建

    最新版ssh hibernate spring struts2环境搭建 最新版spring Framework下载地址:spring4.0.0RELEASE环境搭建 http://repo.sprin ...

  2. Hibernate 系列 02 - Hibernate介绍及其环境搭建

    引导目录: Hibernate 系列教程 目录 昨晚喝多了,下午刚清醒,继续搞Hibernate.走起. 觉得还行的话,记得点赞哈,给我这个渣渣点学习的动力.有错误的话也请指出,省的我在错误上走了不归 ...

  3. Hibernate 介绍及其 环境搭建

    介绍 数据持久化概念 数据持久化是将内存中的数据模型转换为存储模型,以及将存储模型转换为内存中的数据模型的统称.例如:文件的存储.数据的读取等都是数据持久化操作.数据模型可以是任何数据结构或对象模型, ...

  4. Hibernate(一)——采用Hibernate框架开发环境搭建

    Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员充分使用对象编程思维来操作数据库.HIbernate的移植性很好,它可以应用于任何JDB ...

  5. hibernate介绍及环境搭建

    1.前言 hibernate与mybatis的位置一样,都是属于DAO层的框架,代替我们原来的JDBC操作数据库,属于ORM(object relationg mapping. 对象关系映射)框架.O ...

  6. Hibernate学习笔记--环境搭建及运行

    1.hibernate开发包下载 http://sourceforge.net/projects/hibernate/files/ 如果不能访问请用代理http://dongtaiwang.com/l ...

  7. Hibernate之环境搭建

    开始之前,我想先理清一个概念,即ORM是什么? ORM介绍 全称:Object/Relation Mapping,即对象/关系映射. ORM也可以理解为一种规范,具体的ORM框架可作为应用程序和数据库 ...

  8. Hibernate学习之——Hibernate环境搭建

    之前在写关于安卓闹钟的教程,写了一半就没后一半了,其实自己也没做好,在校外实习,校内毕业实习又有任务,只能先放放了,等毕业实习结束之后,在继续安卓闹钟开发之旅,相信这个时间不会很久的.现在毕业实习用到 ...

  9. Hibernate之环境搭建及demo

    ORM概念 ORM即Object/Relation Mapping, 对象/关系数据库映射.ORM是一种规范,完成面向对象编程语言到关系数据库之间的映射.J2EE中的JPA就是一种ORM规范. ORM ...

随机推荐

  1. Memcached使用与纠错(附代码和相关dll)

    今天没事研究一下,谁想到遇到了几个dll找不到,网上也不好找到,索性功夫不负有心人.贴出代码和相关的dll Memcached代码:(网上都是的,很多人都保存了这个代码) using Memcache ...

  2. txt写入时报错出现:正由另一进程使用,原来是多此一举的操作

    //if (!File.Exists(newfilepath + "\\" + name + num + ".txt")) //{ // File.Create ...

  3. find和find_if,value_type

    find算法:返回 [first,end)中第一个值等于value元素的位置 线性复杂度:最多比较次数:元素的总个数 find函数的最后一个参数,必须是string,float,char,double ...

  4. 在eclipse中import java web项目时遇到的一些问题并将该项目通过tomcat发布

    1.首先是import一个新的项目,会将已有的项目import到working space中,注意,你现在的项目路径就在working space了,而不是已有的项目路径! 2.点击eclipse上面 ...

  5. BZOJ1912或洛谷3629 [APIO2010]巡逻

    一道树的直径 BZOJ原题链接 洛谷原题链接 显然在原图上路线的总长为\(2(n-1)\). 添加第一条边时,显然会形成一个环,而这条环上的所有边全部只需要走一遍.所以为了使添加的边的贡献最大化,我们 ...

  6. hihoCoder1159 扑克牌

    一道记忆化搜索 原题链接 和着色方案很像,这里就不详细阐述,可以去我博客里的着色方案里看. 但要注意本题不一样的是同种面值的牌花色不同,所以在转移时还需要乘上同种面值的牌的个数. #include&l ...

  7. word2vec_训练模型

    from gensim.models import Word2Vecfrom gensim.models.word2vec import LineSentence # 原始的训练语料转化成一个sent ...

  8. Linux修改/etc/profile配置错误command is not found自救方法

    export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

  9. maven打包证(支付)书问题

    要将证书文件后缀添加到resource文件夹内范围,添加打包文件后缀(include) 添加maven配置: <resources> <resource> <direct ...

  10. Java第2章笔记

    1.什么是变量:在程序运行过程中它的值是允许改变的量 2.java中常用的数据类型分为四类八种  第一类:整型   int(整数类型)     byte(字节类型)    short(短整形)     ...