Hibernate – 手动配置环境

一、建立Web项目

二、添加Hibernate的库文件到WEB-INF/lib文件夹下

image

三、添加数据库的驱动到WEB-INF/lib文件夹下

四、添加Hibernate配置文件到项目的src下面

image

代码:

<?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">

<!-- Generated by MyEclipse Hibernate Tools. -->

<hibernate-configuration>

<session-factory>

<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:myoracle</property>

<property name="hibernate.connection.username">scott</property>

<property name="hibernate.connection.password">tiger</property>

<property name="show_sql">true</property>

<property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>

<mapping resource="com/www/hibernate/pojo/User.hbm.xml" />

</session-factory>

</hibernate-configuration>

五、创建SessionFactory获取的工具类。

代码:

public class HibernateUtile {

private static SessionFactory factory;

static{

Configuration cfg = new Configuration().configure();

factory = cfg.buildSessionFactory();

}

public static SessionFactory getSessionFactory(){

return factory;

}

}

六、编写一个简单的实体类

代码:

public class User {

private int id;

private String userName;

private String passWord;

public User() {

super();

// TODO Auto-generated constructor stub

}

public User(int id, String userName, String passWord) {

super();

this.id = id;

this.userName = userName;

this.passWord = 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;

}

@Override

public String toString() {

return "User [id=" + id + ", passWord=" + passWord + ", userName="

+ userName + "]";

}

}

七、编写一个简单的映射文件。

代码:

<?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>

<class name="com.www.hibernate.pojo.User">

<id name="id">

<generator class="native"></generator>

</id>

<property name="passWord"></property>

<property name="userName"></property>

</class>

</hibernate-mapping>

八、实现一个表的增删改查

代码:

public class Test {

public static void main(String[] args) {

create();

}

public static void create(){

Configuration cfg = new Configuration().configure();

SchemaExport export = new SchemaExport(cfg);

export.create(true, true);

}

}

public static void delUser(){

Session session = HibernateUtils.getSessionFactory().openSession();

Transaction tran = session.beginTransaction();

User user = new User();

user.setId(1);

session.delete(user);

tran.commit();

session.close();

}

public static void updateUpdate(){

Session session = HibernateUtils.getSessionFactory().openSession();

Transaction tran = session.beginTransaction();

User user = new User();

user.setId(2);

user.setPassWord("321");

user.setUserName("qqq");

session.saveOrUpdate(user);

tran.commit();

session.close();

}

public static void updateSaveOrUpdate(){

User user = new User();

user.setId(1);

user.setPassWord("321");

user.setUserName("qqq");

Session session = HibernateUtils.getSessionFactory().openSession();

Transaction tran = session.beginTransaction();

session.saveOrUpdate(user);

tran.commit();

session.close();

}

public static void queryAll(){

Session session = HibernateUtils.getSessionFactory().openSession();

Transaction tran = session.beginTransaction();

Query query = session.createQuery("from User");

List<User> list = query.list();

for(User user:list){

System.out.println(user);

}

tran.commit();

session.close();

}

public static void queryId(){

Session session = HibernateUtils.getSessionFactory().openSession();

Transaction tran = session.beginTransaction();

User user = (User)session.get(User.class, new Integer(1));

System.out.println(user);

tran.commit();

session.close();

}

public static void addUser(){

User user = new User();

user.setPassWord("321");

user.setUserName("qqq");

Session session = HibernateUtils.getSessionFactory().openSession();

Transaction tran = session.beginTransaction();

session.save(user);

tran.commit();

session.close();

}

你可能感兴趣的:(Hibernate)