WEB Service Dao
hibernate处于 Dao层 是ORM框架 帮助我们操作数据库的
object Relational mapping 对象关系映射,是一种为了解决面向对象与关系型数据库存在不匹配现象的技术,简单说,orm通过描述对象和数据库之间映射的元数据,将程序中的对象自动持久化到关系型数据库中
1.导入所需的包
在src目录下创建hibernate.cfg.xml 作为配
org.hibernate.dialect.MySQL5InnoDBDialect com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/mydatabase?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&allowPublicKeyRetrieval=true root root
true
true update
创建domain包 放置对象
package domain; import java.util.HashSet; import java.util.Set; public class User { public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public SetgetRoles() { return roles; } public void setRoles(Set roles) { this.roles = roles; } private Long user_id; private String user_code; private String user_name; private String user_password; private Character user_state; private Set roles = new HashSet (); public Long getUser_id() { return user_id; } public void setUser_id(Long user_id) { this.user_id = user_id; } public String getUser_code() { return user_code; } public void setUser_code(String user_code) { this.user_code = user_code; } public String getUser_password() { return user_password; } public void setUser_password(String user_password) { this.user_password = user_password; } public Character getUser_state() { return user_state; } public void setUser_state(Character user_state) { this.user_state = user_state; } }
在相同的domain包下 放置对应的xml文件 例如User.hbm.xml
配置完成 可以开始进行测试了 创建demo类
public class demo { public static void main(String[] args){ //创建 调用构造方法指定cfg.xml Configuration configuration = new Configuration().configure(new File("src/hibernate.cfg.xml")); //读取orm元数据 主配置加载了映射配置 就不需要在加载了 //configuration.addResource(resourceName); //configuration.addClass(persistentClass); /* SessionFactory功能: 用于创建操作数据库核心对象session对象的工厂. 功能就是创建session对象 注意:1.sessionfactory 负责保存和使用所有配置信息.消耗内存资源非常大. 2.sessionFactory属于线程安全的对象设计. 结论: 保证在web项目中,只创建一个sessionFactory. */ SessionFactory sessionfactory = configuration.buildSessionFactory(); //openSession 新的session对象 /*session对象功能 类似connection对象 完成增删改查操作 session是hibernate操作数据库的核心对象*/ Session session = sessionfactory.openSession();
//开启事务 Transaction txTransaction = session.beginTransaction();
//操作数据库的代码
//-------------------------------------------------------------------------- try { /*存数据*/ User user = new User(); user.setUser_name("asdf"); session.save(user);
/*调用session.save保存对象*/
/*错误 事务会回滚*/ throw new Exception("出错"); /*调用session.save保存对象*/ }catch(Exception e) {
/*回滚*/ txTransaction.rollback(); }finally {
/*提交 结束代码--------------------------------------------------------------*/ txTransaction.commit(); session.close(); /*释放资源*/ sessionfactory.close(); } } }
会出错 因为抛出了异常
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Hibernate:
insert
into
sys_user
(user_name, user_code, user_password, user_state)
values
(?, ?, ?, ?)
Exception in thread "main" java.lang.IllegalStateException: Transaction not successfully started
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:98)
at demo.main(demo.java:57)
注释掉 在运行
插入成功
查询
//1 创建,调用空参构造 Configuration conf = new Configuration().configure(); //2 根据配置信息,创建 SessionFactory对象 SessionFactory sf = conf.buildSessionFactory(); //3 获得session Session session = sf.openSession(); //4 session获得操作事务的Transaction对象 //获得操作事务的tx对象 //Transaction tx = session.getTransaction(); //开启事务并获得操作事务的tx对象(建议使用) Transaction tx2 = session.beginTransaction(); //---------------------------------------------- User user = session.get(User.class, 1l); System.out.println(user); //---------------------------------------------- tx2.commit();//提交事务 session.close();//释放资源 sf.close();//释放资源
Hibernate:
select
user0_.user_id as user_id1_3_0_,
user0_.user_name as user_nam2_3_0_,
user0_.user_code as user_cod3_3_0_,
user0_.user_password as user_pas4_3_0_,
user0_.user_state as user_sta5_3_0_
from
sys_user user0_
where
user0_.user_id=?
domain.User@31e04b13
修改
//1 创建,调用空参构造 Configuration conf = new Configuration().configure(); //2 根据配置信息,创建 SessionFactory对象 SessionFactory sf = conf.buildSessionFactory(); //3 获得session Session session = sf.openSession(); //4 session获得操作事务的Transaction对象 //获得操作事务的tx对象 //Transaction tx = session.getTransaction(); //开启事务并获得操作事务的tx对象(建议使用) Transaction tx2 = session.beginTransaction(); //---------------------------------------------- //1 获得要修改的对象 User user = session.get(User.class, 1l); //2 修改 user.setUser_name("程序员"); //3 执行update session.update(user); //---------------------------------------------- tx2.commit();//提交事务 session.close();//释放资源 sf.close();//释放资源
删除
//1 创建,调用空参构造 Configuration conf = new Configuration().configure(); //2 根据配置信息,创建 SessionFactory对象 SessionFactory sf = conf.buildSessionFactory(); //3 获得session Session session = sf.openSession(); //4 session获得操作事务的Transaction对象 //获得操作事务的tx对象 Transaction tx = session.getTransaction(); tx.begin(); //开启事务并获得操作事务的tx对象(建议使用) Transaction tx2 = session.beginTransaction(); //---------------------------------------------- //1 获得要修改的对象 User user = session.get(User.class, 1l); //2 调用delete删除对象 session.delete(user); //---------------------------------------------- tx2.commit();//提交事务 session.close();//释放资源 sf.close();//释放资源