一,myeclipse部署hhibernate开发环境
1,右击项目,添加hibernate
2,选择版本,next
3,next,自动创建HibernateSessionFactory类
如果出现以下错误:
解决方法,更改java版本1.5或更高
4,next,添加数据库信息
5,next,直到完成。
package com.bean;
public class User {
private Integer uid;
private String name;
private int age;
public User() {
super();
}
public User(Integer uid, String name, int age) {
super();
this.uid = uid;
this.name = name;
this.age = age;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "User [uid=" + uid + ", name=" + name + ", age=" + age + "]";
}
}
//待继续学习标志Hibernate 保证,仅在特定会话范围内,持久化标识(数据库的行)和 Java 标识是等价的。因此,一旦我们混合了从不同会话中获取的实例,如果希望 Set 有明确的语义,就必须实现equals() 和 hashCode() 。
@Override
public int hashCode() {
return this.uid;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj instanceof User){
User user = (User)obj;
if(user.getUid() == this.getUid())
return true;
}
return false;
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.bean">
<!--
package :默认的包前缀,如果没有指定包名,则默认使用此包名
-->
<!--
当表名和类名相同时,可以省略table属性,默认以类名作为表名
-->
<class name="User">
<id name="uid" column="Uid">
<!-- 指定ID的增长策略 -->
<generator class="native"></generator>
</id>
<property name="name" column="Name"></property>
<property name="age" column="Age"></property>
</class>
</hibernate-mapping>
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- hibernate中session对象的管理模式 -->
<property name="current_session_context_class">thread</property>
<!-- hibernate中sql语句的设置(是否在控制台输出SQL语句) -->
<property name="show_sql">true</property>
<!-- database自动创建 -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/bean/User.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
<!-- 配置连接池
优点:
1、提交了访问效率(不用再访问时创建连接)
2、使连接可以重用
缺点:
在服务器中需要时刻维护一个连接池和一定的连接数,故如果网站(应用)的访问量不是太大时,不建议使用
-->
<!-- <property name="hibernate.connection.pool_size">5</property> -->
<!-- c3p0连接池
博客:http://blog.youkuaiyun.com/mercenarylin/article/details/21605223
-->
<!-- 连接池中的最小连接数 -->
<property name="hibernate.c3p0.min_size">3</property>
<!-- 连接池中的最大连接数 -->
<property name="hibernate.c3p0.max_size">10</property>
<!-- 最长等待时间,以毫秒为单位 -->
<property name="hibernate.c3p0.timeout">3000</property>
<!-- 应用最多创建的statement对象 -->
<property name="hibernate.c3p0.max_statements">50</property>
二,可编程的配置方式
configuration.addResource("com/bean/Vip.hbm.xml");
//通过编程的方式,添加映射文件
configuration.setProperty("show_sql", "true");
//通过编程的方式,添加配置