- 引入jar
引入lib/required中的所有的jar包,log4j-x.x.x.jar包,mysql的驱动包。
- 在项目的src目录下建立hibernate.cfg.xml文件
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 指定数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 指定数据库连接属性 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123</property>
<!-- 显示sql语句 -->
<property name="show_sql">true</property>
<!-- 自动根据xxx.hbm.xml文件生成DDL语句完成数据的update操作 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!--orm的映射文件,指定实体模型与数据库中表及字段的关联 -->
<mapping resource="com/gcflowers/hibernate/model/User.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
- 建立实体类User.java
public class User {
private int id;
private String username;
private String password;
private String nickname;
private Date born;
public User(){
}
public User(String username, String password, String nickname,
Date born) {
super();
this.username = username;
this.password = password;
this.nickname = nickname;
this.born = born;
}
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;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public Date getBorn() {
return born;
}
public void setBorn(Date born) {
this.born = born;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password="
+ password + ", nickname=" + nickname + ", born=" + born + "]";
}
}
- 在配置文件中的目录下建立User.hbm.xml文件,实现实体对象和数据库记录的映射
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.gcflowers.hibernate.model">
<class name="User" table="h_user">
<id name="id">
<generator class="native" />
</id>
<property name="username" />
<property name="password" />
<property name="nickname" />
<property name="born" type="timestamp"/>
</class>
</hibernate-mapping>
- 使用junit4建立测试类
public class TestHibernate {
@Test
public void testSave() {
Configuration cfg = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry);
Session session = null;
try {
session = sessionFactory.openSession();
session.beginTransaction();
User user = new User("zhangsan","123","zs",new java.util.Date());
session.save(user);
session.getTransaction().commit();
} catch (HibernateException e) {
e.printStackTrace();
if(session != null) session.getTransaction().rollback();
}finally{
if(session != null) session.close();
}
}
@Test
//mysql的分页查询
public void testListPage(){
Session session = HibernateUtil.getSession();
List<User> users = session.createQuery("from User")
.setFirstResult(2).setMaxResults(2).list();
for(User user : users){
System.out.println(user);
}
}
}
posted on
2013-05-08 23:43
zcjava
阅读(
257)
评论()
编辑
收藏
举报