构建简单Hibernate环境
前面准备:JDK ,Tomcat,MySQL|SqlServer|Oracle,Eclipse,Hibernate
中期,
1建一个表
数据库名:hibernate 表名:login 字段 id(主键), name ,password
2建一项目:Hibernate
3在src下建立包hibernate.ch1
4在hibernate.ch1下建立类文件
UserInfo.java
package hibernate.ch1;
public class UserInfo {
private Integer id;
private String userName;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
5在hibernate.ch1下编写UserInfo.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">
<hibernate-mapping>
<class name="hibernate.ch1.UserInfo" table="login">
<id name="id" type="integer">
<column name="id"/>
<generator class="identity"/>
</id>
<property name="userName" type="string">
<column name="name" length="100"/>
</property>
<property name="password" type="string">
<column name="password" length="100"/>
</property>
</class>
</hibernate-mapping>
6将hibernate3.jar、mysql-connector-java-3.1.16-bin.jar以及hibernate3\lib下所有jar文件复制到src/lib文件夹下,
并加载(右键点击项目名字,选择属性...)
7在src下编写hibernate.cfg.xml文件
<?xml version="1.0" encoding='gb2312'?>
<!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.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">026516</property>
<property name="hibernate.connection.pool.size">20</property>
<property name="hibernate.show_sql">true</property>
<property name="jdbc.fetch_size">50</property>
<property name="jdbc.batch_size">25</property>
<property name="jdbc.use_scrollable_resultset">false</property>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">gb2312</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect </property>
<mapping resource="hibernate/ch1/UserInfo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
8编写测试类
package hibernate.ch1;
import hibernate.ch1.UserInfo;
import org.hibernate.cfg.Configuration;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class HibernateTest {
/**
* @param args
*/
public static void main(String[] args) {
SessionFactory sessions=new Configuration().configure().buildSessionFactory();
Session session=sessions.openSession();
Transaction tx=null;
try{
tx=session.beginTransaction();
UserInfo u=new UserInfo();
u.setUserName("Fujingzhou");
u.setPassword("123");
System.out.println("start to insert data to db...");
session.save(u);
UserInfo ul=(UserInfo) session.load(UserInfo.class, new Integer(1));
System.out.println("the user load from db is"+ul.getUserName());
tx.commit();
tx=null;
System.out.println("Congratulation to you ,Your first program run success!");
}
catch(HibernateException e)
{
e.printStackTrace();
if(tx!=null)
{
tx.rollback();
}
}
finally{
session.close();
}
}
}