1.hibernate学习笔记一之基本配置



可以不用sql
hql:from News(类名)
sql:select * from news_detail


save update delete get/load
saveOrUpdate()


jdbc
mybatis:半自动-->sql


所有的框架:N多配置


Hibernate:持久化框架
持久化:瞬时状态——>持久状态 这种过程
ORM:对象关系映射
(对象和表对应)
类名 属性
表名 列名
...


hibernate的目录结构:
  --documentation:学习文档(API)
hibernate-release-5.2.6.Final\documentation\quickstart\html_single\index.html
  --lib:开发所需的jar包
  --project:示例项目


步骤:
1.下载
  --官网:http://hibernate.org/orm/
  hibernate-release-5.2.6.Final
2.导入hibernate所需的相关jar
  --hibernate-release-5.2.6.Final\lib\required
  --mysql-connector-java-5.1.39-bin.jar
  在项目下创建lib目录将jar复制到该目录下
  右键lib目录选择Add as Library点击OK即可
3.编写Hibernate的配置文件(hibernate.cfg.xml)
  在src目录下创建hibernate.cfg.xml文件
 
   
       
       
        com.mysql.jdbc.Driver
       
        jdbc:mysql://localhost:3306/new_houserent
       
        root
       
        123456


       
        org.hibernate.dialect.MySQL5Dialect


       
        update


       
        true
       
        true
   

 

 --可参考hibernate-release-5.2.6.Final\project\etc目录下的
   hibernate.properties配置文件
4.创建持久化类和映射文件
 
   
   
       
           
           
       

       
       
   

 

5.引入配置文件
  在hibernate.cfg.xml文件中引入映射文件
  在session-factory节点下配置:
 
6.测试(junit)
@Test
    public void testBase(){
        SessionFactory sessionFactory = null;
        Session session = null;
        // A SessionFactory is set up once for an application!
        //注册标准服务
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                .configure().build();// configures settings from hibernate.cfg.xml
        try {
            //创建SessionFactory
            sessionFactory = new MetadataSources(registry)
                    .buildMetadata().buildSessionFactory();
            //通过sessionFactory创建session
            session = sessionFactory.openSession();
            //开始事务
            session.beginTransaction();
            //创建User对象
            User user = new User("老李","123","15900561234","li","N");
            //保存用户
            session.save(user);
            //提交事务
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            //回滚事务
            session.getTransaction().rollback();
            // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
            // so destroy it manually.
            StandardServiceRegistryBuilder.destroy( registry );
        }
    }


相关异常:
java.lang.UnsupportedClassVersionError: org/hibernate/service/ServiceRegistry : Unsupported major.minor version 52.0
--Hibernate5之后要使用JDK1.8+


Unknown database 'new_houserent'
--要预先创建数据库(new_houserent)


Access denied for user ''@'localhost'
--hibernate.cfg.xml中配置的username有误


Unable to make JDBC Connection [jdbc:mysql//localhost:3306/new_houserent]
--hibernate.cfg.xml中配置的url有误


Unknown entity: com.ibeifeng.hibernate.model.User
--hibernate.cfg.xml中没有配置mapping映射


===========JUnit简单使用=============
1.导jar包
  --junit-4.12.jar
  --hamcrest-core-1.3.jar
2.使用
  在相应的方法上添加@Test即可
  运行:选中相应的方法名,右击run即可
  
get和load区别:
1.get没有缓存,load有缓存
  即时加载(get)
  延迟加载(load):通过load方差查找的对象,先从缓存中查找,因为缓存中只有id,
2.get在没有查询到数据库时返回null
  load会抛异常

  --org.hibernate.ObjectNotFoundException: No row with the given identifier exists: 

-------------------------------------------以上摘自老师笔记------------------------------------------------

代码:https://pan.baidu.com/s/1eTUQ0CU


hibernate使用案例-基本教程

http://blog.csdn.net/xingfei_work/article/details/75290263

你可能感兴趣的:(hibernate)