搭配Hibernate环境有如下步骤:
1新建一个动态网页项目后,导入相关包。相关包在相应的required文件夹下,同时加载数据库jdbc文件
2在src目录下,新建hibernate.cfg.xml ,该名字不建议修改,简化版本的如下:
<?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 name="SessionFactory">
<property name="hibernate.show_sql">true</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=MyDataBase</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">123456</property
<propertyname="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.max_size">20</property>
<!-- 最小连接数 -->
<property name="hibernate.c3p0.min_size">5</property>
<!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
<property name="hibernate.c3p0.timeout">120</property>
<!-- 最大的PreparedStatement的数量 -->
<property name="hibernate.c3p0.max_statements">100</property>
<!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->
<property name="hibernate.c3p0.idle_test_period">120</property>
<!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
<property name="hibernate.c3p0.acquire_increment">2</property>
<!-- 每次都验证连接是否可用 -->
<property name="hibernate.c3p0.validate">true</property>
<mapping resource="com/model/User.hbm.xml"></mapping>
3编写与数据库对应的类,比如在MyDataBase数据库下有个_login的表,分别有id ,name ,password。这时做如下设计:
public class User {
private int id;
private String username ;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
}
4建立隐射文件User.hbm.xml,要求与类在同一个包下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-9-5 19:57:46 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.model.User" table="_login">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
//native 表示自行判断,如果用increment则表示递增
</id>
<property name="username" type="java.lang.String">
<column name="name" />
</property>
<property name="password" type="java.lang.String">
<column name="password" />
</property>
</class>
</hibernate-mapping>
最后一个测试程序:
Configuration config =new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tc = null;
try {
//开启事物
tc = session.beginTransaction();
User user = (User)session.get(User.class, new Integer(3));
System.out.println("用户名为:"+user.getUsername()+";用户密码为:"+user.getPassword());
//提交事物
tc.commit();
} catch (Exception e) {
// 不是空值则回滚
if (tc!=null) {
tc.rollback();
}
e.printStackTrace();
}finally{
session.close();
}
}
}