Hibernate

来源:互联网 发布:日本军工 知乎 编辑:程序博客网 时间:2024/05/16 15:55

1.Hibernate是什么?

    Hibernate是持久型框架(将数据存储在硬盘中),ORM(Object Relation Map,对象关系映射)框架。

2为什么要使用Hibernate?

    Hibernate可以永久的保存数据,代码更加简洁,开发效率也高。jdbc虽开发效率慢,但性能高。

3.ORM框架的分类:

Hibernate:成熟稳定优秀(优化了)的框架。对数据库版本的支持。开发效率高。运行速度慢(重量级)

myBatis:运行速度快,开发效率慢。是一个半成品。需要自己写sql语句。(轻量级:频繁改动数据。)

4.和数据库有关的操作可使用Hibernate

       步骤:1.下载Hibernate插件:离线下载|在线下载

                   离线下载:Help-->>InstallSoftware

                  

                            


                       在线下载:Help-->>Eclipse Marketplace(在网络下运行,才不会报错)


                       2.导入Hibernate的依赖(在http://hibernate.org下找寻)

                         对于Maven:    

<dependency>   <groupId>org.hibernate</groupId>   <artifactId>hibernate-core</artifactId>   <version>5.2.10.Final</version></dependency>
                      对于Gradle

org.hibernate:hibernate-core:5.2.10.Final
                     3.创建实体类(建议:与数据库的字段的命名一致)

                     4.在entity包下创建映射文件



             5.配置xml

 

 

   配置hibernate.cfg.xml

 

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory>     <!-- 配置数据库连接信息 -->     <property name="connection.username">root</property>     <property name="connection.password">password</property>     <property name="connection.url">jdbc:mysql://localhost:3306/student</property>     <property name="connection.driver_class">com.mysql.jdbc.Driver</property>               <!-- 映射文件 -->     <mapping resource="com/zking/entity/Stu.hbm.xml"/>    </session-factory></hibernate-configuration>
同时也不要忘记配置pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>student</groupId>  <artifactId>student</artifactId>  <packaging>war</packaging>  <version>0.0.1-SNAPSHOT</version>  <name>student Maven Webapp</name>  <url>http://maven.apache.org</url>    <properties>        <argLine>-Dfile.encoding=UTF-8</argLine>  </properties>    <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.12</version>      <scope>test</scope>    </dependency>    <!-- 解决jsp报错 -->    <dependency>    <groupId>javax.servlet</groupId>    <artifactId>javax.servlet-api</artifactId>    <version>4.0.0-b07</version>    <scope>provided</scope></dependency>    <!-- 加入Hibernate依赖 --><dependency>   <groupId>org.hibernate</groupId>   <artifactId>hibernate-core</artifactId>   <version>5.2.10.Final</version></dependency><!-- mysql -->    <dependency>    <groupId>mysql</groupId>    <artifactId>mysql-connector-java</artifactId>    <version>5.1.37</version></dependency><!-- struts2 --><dependency>    <groupId>org.apache.struts</groupId>    <artifactId>struts2-core</artifactId>    <version>2.5.12</version></dependency>  </dependencies>  <build>    <finalName>student</finalName>  </build></project>

使用:

package com.zking.test;import java.util.List;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.After;import org.junit.Before;import org.junit.Test;import com.zking.entity.Student;public class TestCRUD {private SessionFactory sessionFactory;private Session session;private Transaction transaction;@Beforepublic void before(){//1.读取hibernate配置文件Configuration configuration=new Configuration().configure();sessionFactory = configuration.buildSessionFactory();session = sessionFactory.openSession();transaction = session.beginTransaction();}@Afterpublic void after(){//提交事务transaction.commit();session.close();sessionFactory.close();}@Testpublic void testAdd(){Student student=new Student("哥哥", 18);session.save(student);}@Testpublic void testGet(){//查询单个//Student student=session.get(Student.class, 1);//System.out.println("查询单个:"+student.getSid()+" "+student.getSname());//查询所有List<Student> students=session.createCriteria(Student.class).list();for (Student student : students) {System.out.println(student.getSname());}}}


          


                      

































           



xi

原创粉丝点击