微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

hibernate的入门

1. 什么是hibernate  

   ORM框架/持久层框架 jdbc的一个框架  

   ORM(Object Relational Mapping):对象关系映射。
   对象与关系型数据间之间的映射管理框架
   通过管理对象来改变数据库中的数据
   通过管理对象来操作数据库

  优势:跨数据库的无缝移植 

 

2.如何使用hibernate

  2.1 添加hibernate相关依赖 

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 3   <modelVersion>4.0.0</modelVersion>
 4   <groupId>com.yuan</groupId>
 5   <artifactId>T226_hibernate</artifactId>
 6   <packaging>war</packaging>
 7   <version>0.0.1-SNAPSHOT</version>
 8   <name>T226_hibernate Maven Webapp</name>
 9   <url>http://maven.apache.org</url>
10   <properties>
11         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12         <maven.compiler.source>1.8</maven.compiler.source>
13         <maven.compiler.target>1.8</maven.compiler.target>
14         <junit.version>4.12</junit.version>
15         <servlet.version>4.0.0</servlet.version>
16         <hibernate.version>5.3.0.Final</hibernate.version>
17         <MysqL.driver.version>5.1.46</MysqL.driver.version>
18     </properties>
19   <dependencies>
20     
21         <dependency>
22             <groupId>junit</groupId>
23             <artifactId>junit</artifactId>
24             <version>${junit.version}</version>
25             <scope>test</scope>
26         </dependency>
27 
28         <dependency>
29             <groupId>javax.servlet</groupId>
30             <artifactId>javax.servlet-api</artifactId>
31             <version>${servlet.version}</version>
32             <scope>provided</scope>
33         </dependency>
34 
35         <dependency>
36             <groupId>org.hibernate</groupId>
37             <artifactId>hibernate-core</artifactId>
38             <version>${hibernate.version}</version>
39         </dependency>
40 
41         <dependency>
42             <groupId>MysqL</groupId>
43             <artifactId>mysql-connector-java</artifactId>
44             <version>${MysqL.driver.version}</version>
45         </dependency>
46   </dependencies>
47   <build>
48     <finalName>T226_hibernate</finalName>
49     <plugins>
50             <plugin>
51                 <groupId>org.apache.maven.plugins</groupId>
52                 <artifactId>maven-compiler-plugin</artifactId>
53                 <version>3.7.0</version>
54                 <configuration>
55                     <source>${maven.compiler.source}</source>
56                     <target>${maven.compiler.target}</target>
57                     <encoding>${project.build.sourceEncoding}</encoding>
58                 </configuration>
59             </plugin>
60         </plugins>
61   </build>
62 </project>

 

  2.2 在resource目录下添加hibernate.cfg.xml(核心配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5     <hibernate-configuration>
 6       <session-factory>
 7         <!-- 1. 数据库相关 -->
 8         <property name="connection.username">root</property>
 9         <property name="connection.password">***</property>
10         <property name="connection.url">jdbc:MysqL://localhost:3306/xm_sc?useUnicode=true&amp;characterEncoding=UTF-8
11         </property>
12         <property name="connection.driver_class">com.MysqL.jdbc.Driver</property>
13         <property name="dialect">org.hibernate.dialect.MysqLDialect</property>
14 
15         <!-- 配置本地事务(No CurrentSessionContext configured!) -->
16         <property name="hibernate.current_session_context_class">thread</property>
17 
18         <!-- 2. 调试相关 -->
19         <property name="show_sql">true</property>
20         <property name="format_sql">true</property>
21 
22         <!-- 3. 添加实体映射文件 -->
23         <mapping resource="com/yuan/one/entity/User.hbm.xml" />
24       </session-factory>
25     </hibernate-configuration>

 

  2.2.1 添加DTD支持

  添加到hibernate.cfg.xml

1 <!DOCTYPE hibernate-configuration PUBLIC
2     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
3     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

  添加到User.hbm.xml

 

1 <!DOCTYPE hibernate-mapping PUBLIC 
2     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

 

  2.2.2 添加Hibernate的配置


  2.2.2.1 数据库相关(connection.username|connection.password|connection.url|connection.driver_class|dialect)
  2.2.2.2 调试相关(show_sql|format_sql)
  2.3 在开发阶段再创建实体类和实体映射文件(*.hbm.xml)
      实体必须实现Serializable接口

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。