开始之前先介绍一下Hibernate开发的步骤:
①.创建hIbernate的配置文件
②. 创建持久化类
③. 创建对象-关系映射文件
④. 通过Hibernate API 编写访问数据库的代码
2. 项目结构:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~分割线~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.创建hIbernate的配置文件 :hibernate.cfg.xml
2.创建持久化类:User.java
package edu.cloud.hibernate.model;
public class User {
private Long id;
private String username;
private String password;
public Long getId() {
return id;
}
public void setId(Long 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 toString() {
String s = "id:" + id + " username:" + username + " password:" + password;
return s;
}
}
3.创建对象-关系映射文件 : User.hbm.xml
4. . 通过Hibernate API 编写访问数据库的代码 : Test.java
工具类:HibernateUtils
package edu.cloud.hibernate.ut;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
private static Configuration configuration;
private static SessionFactory sessionFactory;
static{
//① 创建Configuration 对象:对应hibernate 的基本配置信息,和对象关系映射信息
configuration = new Configuration().configure();
/**
* 在hibernate 4.0 以前 创建session工厂 就是下面一条信息就可以了,4.0 版本以后的稍稍麻烦一点
* sessionFactory = configuration.buildSessionFactory();
* 4.0之后:
* //② 创建一个ServiceRegistry 对象: 这个对象是hibernate4.0版本以后新添加的对象
// 其作用:hibernate 的任何配置和服务都要在该对象中注册后才能有效
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
//③ 创建sessionFactory
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
*/
//这个Demo使用的是Hibernate3.0
sessionFactory = configuration.buildSessionFactory();
}
/**
* 创建一个Session 对象
* @return
*/
public static Session openSession(){
return sessionFactory.openSession();
}
public static void main(String[] args) {
openSession();
}
}
测试类 Test.java:
package edu.cloud.hibernate.model;
import java.sql.SQLException;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import edu.cloud.hibernate.ut.HibernateUtils;
public class Test {
//创建一个新的日志记录器:
private static Logger logger = Logger.getLogger(Test.class);
public static void main(String[] args) throws HibernateException, SQLException {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
System.out.println(tx);
// Insert new object
/*for(int i = 1; i < 3; i++) {
User user = new User();
user.setUsername("admin" + i);
user.setPassword("1234");
session.save(user);
}
//插入
User user = new User();
//user.setId(3L);
user.setUsername("qq");
user.setPassword("123");
session.saveOrUpdate(user);//注意使用的细粒度,identifier 设置的时候unsaved-value默认值设置为空,然后使用session.saveOrUpdate()方法存储的话,identifier要为null才会存储
//查询
Query query = session.createQuery("from User user where user.id = ?");
query.setLong(0, 3L);
User user = (User) query.uniqueResult();
System.out.println(user);//注意,要在实体类中重写toString()方法,增加程序的可读性
*/
/* //删除
User user = (User) session.get(User.class, new Long(5));
session.delete(user);*/
//多条件查询
Query query = session.createQuery("from User where username like :name");
query.setString("name", "%2%");
List<User> list = query.list();
for(User u : list) {
logger.info(u);//日志,找个时间看一下
}
tx.commit();
}
}