hibernate开发环境搭建
MyEclipse6.5+hibernate3.6+mysql5.5
1:新建一个java project
2:导入hibernate包
hibernate3.jar
required下的6个包
antlr-2.7.6.jar
commons-collections.jar
dom4j-1.6.1.jar
javassist-3.12.0.jar
jta-1.1.jar
slf4j-api-1.6.1.jar
jpa包
hibernate-jpa-2.0-api-1.0.0.jar
c3p0包
c3p0-0.9.1.jar
mysql驱动
3:新建hibernate.properties
内容参考hibernate自带的properties文件及
http://java.chinaitlab.com/Hibernate/850082.html
#hibernate常量
hibernate.query.substitutions yes 'Y', no 'N'
#hibernate数据库连接信息
hibernate.connection.username=root
hibernate.connection.password=zhchx0827
hibernate.connection.url=jdbc:mysql:///test
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQLDialect
#c3p0连接池的配置
hibernate.c3p0.max_size=20
hibernate.c3p0.min_size=2
<!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
hibernate.c3p0.timeout=5000
<!-- 最大的PreparedStatement的数量 -->
hibernate.c3p0.max_statements=100
<!-- 每隔120秒检查连接池里的空闲连接,单位是秒-->
hibernate.c3p0.idle_test_period=3000
<!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
hibernate.c3p0.acquire_increment=2
<!-- 每次都验证连接是否可用 -->
hibernate.c3p0.validate=true
hibernate.show_sql=true
hibernate.format_sql=true
#这两个选项非常非常非常重要!!!将严重影响Hibernate的CRUD性能!
#Fetch Size 是设定JDBC的Statement读取数据的时候每次从数据库中取出的记录条数。
#例如一次查询1万条记录,对于Oracle的JDBC驱动来说,是不会1次性把1万条取出来的,而只会取出Fetch Size条数,当纪录集遍历完了这些记录以后,再去数据库取Fetch Size条数据。
#因此大大节省了无谓的内存消耗。当然Fetch Size设的越大,读数据库的次数越少,速度越快;Fetch Size越小,读数据库的次数越多,速度越慢。
#这有点像平时我们写程序写硬盘文件一样,设立一个Buffer,每次写入Buffer,等Buffer满了以后,一次写入硬盘,道理相同。
#Oracle数据库的JDBC驱动默认的Fetch Size=10,是一个非常保守的设定,根据我的测试,当Fetch Size=50的时候,性能会提升1倍之多。
hibernate.jdbc.fetch_size=50
hibernate.jdbc.batch_size=25
4:4:新建entity和hbm文件,hbm文件参考hibernate包中自带的文件
5:Main方法
public class Main {
public static void main(String[] args) {
/*Configuration cfg = new Configuration()
.addFile("Student.hbm.xml");*/
Configuration cfg = new Configuration()
.addClass(Student.class);
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Student student = new Student();
student.setName("zhang san");
student.setBirthday(new Date());
session.save(student);
tx.commit();
session.close();
}
}