hibernate环境搭建
在经历重重困难,尝试各种方法,终于搭建好了hibernate的开发环境
1、hibernate版本:
不要尝试高版本的hibernate,会出现某些系统依赖库找不到,我用的版本是:hibernate-release-4.2.21.Final,
2、jdk版本:官方要求1.6以上,我用的是jdk1.7.0_79
3、需要加载的jar包
lib/required里面全部
lib\optional\c3p0里面全部
jdbc的jar包(我用的是sql server2005)
4、bean对象:
就是普通的bean文件,带有set和get方法
5、主程序:
News n = new News();
n.setId(5);
n.setMac("1230");
n.setSn("renliansong");
@SuppressWarnings("deprecation")
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(n);
session.getTransaction().commit();
6、配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.password">******</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=suifeng</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="org.crazyit.app.domain.News"/>
</session-factory>
</hibernate-configuration>
(注意红色字体部分根据实际修改,连接信息也需要根据数据库进行修改)