hibernate.cfg.xml its a main xml that can call all the 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>
<!--配置数据库网络连接的url-->
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate</property>
<!--配置数据库网络连接的用户名,默认一般为root-->
<property name="hibernate.connection.username">root</property>
<!--配置数据库网络连接的密码-->
<property name="hibernate.connection.password">1281985</property>
<!--配置数据库网络连接池的大小-->
<property name="hibernate.connection.pool.size">20</property>
<!--后台是否显示sql,开发时为true,运行时为false-->
<property name="hibernate.show_sql">true</property>
<!-- 设置JDBC的隔离级别-->
<property name="hibernate.connection.isolation">2</property>
<property name="hibernate.format_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>
<!--编码方式,最好是gbk,用gb2312有的字符不全-->
<property name="connection.characterEncoding">gb2312</property>
<!--数据库方言,每个数据库都有方言,hibernate已经为大多数数据库指明了方言-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="hibernate/ch01/UserInfo.hbm.xml" />
</session-factory>
</hibernate-configuration>
UserInfo.hbm.xml about the persistance configuration
<?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.ch01.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>
UserInfo 持久类
package hibernate.ch01;
public class UserInfo {
//对象id
private Integer id;
//用户名
private String userName;
//密码
private String password;
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
}
a test
package hibernate.ch01;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateTest {
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("开始插入数据到数据库……");
// //保存数据到数据库
session.save(u);
//从持久化类UserInfo中读取数据
// String hql="from UserInfo userInfo where userInfo.userName like ?";
// Query query=session.createQuery(hql);
// query.setParameter(0,"fujingzhou1");
// List list=query.list();
// Iterator it=list.iterator();
// while(it.hasNext()){
// UserInfo userInfo=(UserInfo)it.next();
// System.out.println(userInfo.getUserName());
// }
//结束事务
tx.commit();
tx = null;
System.out.println("hi,恭喜你,第一个程序运行成功!");
} catch (HibernateException e) {
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
}
}