hibernate经典配置与测试
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>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql:///crud
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
root
</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.pool_size">
1
</property>
<property name="hibernate.show_sql">
true
</property>
<property name="current_session_context_class">
thread
</property>
<mapping resource="com/kaisheng/pojo/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
User.java
package com.kaisheng.pojo;
public class User {
private int id;
private String userName;
private String userPwd;
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 getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
}
User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.kaisheng.pojo">
<class name="User" table="t_user">
<id name="id" column="id">
</id>
<property name="userName" column="username"></property>
<property name="userPwd" column="userpwd"></property>
</class>
</hibernate-mapping>
HibernateUtil.java
package com.kaisheng.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* 单态模式(只产生一个类的实例)
* @author Administrator
*
*/
public class HibernateUtil {
private static final SessionFactory factory=buildSessionFactory();
private static SessionFactory buildSessionFactory() {
return new Configuration().configure().buildSessionFactory();
}
public static SessionFactory getSessionFactory(){
return factory;
}
}
Test.java
package com.kaisheng.test;
import java.util.List;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import com.kaisheng.pojo.User;
import com.kaisheng.util.HibernateUtil;
public class Test {
@SuppressWarnings("unchecked")
public static void main(String[] args){
User user=new User();
user.setUserName("阿杰");
user.setUserPwd("123");
SessionFactory factory=HibernateUtil.getSessionFactory();
Session session=factory.getCurrentSession();
Transaction tran=session.beginTransaction();
//session.save(user);添加
//User userGet=(User) session.load(User.class, 2);
//System.out.println(userGet.getUserName());
List<User> userList=session.createQuery("from User").list();
for(User u:userList){
System.out.println(u.getUserName());
}
tran.commit();
}
}
by archie
posted on 2010-08-17 21:59 archie2010 阅读(663) 评论(0) 编辑 收藏 举报