你的位置:首页 > Java教程

[Java教程]Hibernate基础案例1

使用到的是MySQL数据库

           

 

1.在项目中先引入jar包,并添加引用

 <dependencies> <dependency>  <groupId>junit</groupId>  <artifactId>junit</artifactId>  <version>4.3</version>  <scope>test</scope> </dependency> <dependency>  <groupId>antlr</groupId>  <artifactId>antlr</artifactId>  <version>2.7.7</version> </dependency> <dependency>  <groupId>commons-collections</groupId>  <artifactId>commons-collections</artifactId>  <version>3.2.1</version> </dependency> <dependency>  <groupId>commons-logging</groupId>  <artifactId>commons-logging</artifactId>  <version>1.1.1</version> </dependency> <dependency>  <groupId>dom4j</groupId>  <artifactId>dom4j</artifactId>  <version>1.6.1</version> </dependency> <dependency>  <groupId>org.hibernate</groupId>  <artifactId>hibernate-core</artifactId>  <version>5.2.10.Final</version> </dependency> <dependency>  <groupId>org.hibernate.javax.persistence</groupId>  <artifactId>hibernate-jpa-2.0-api</artifactId>  <version>1.0.1.Final</version> </dependency> <dependency>  <groupId>javassist</groupId>  <artifactId>javassist</artifactId>  <version>3.12.1.GA</version> </dependency> <dependency>  <groupId>javax.transaction</groupId>  <artifactId>jta</artifactId>  <version>1.1</version> </dependency> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency>  <groupId>log4j</groupId>  <artifactId>log4j</artifactId>  <version>1.2.17</version> </dependency> <dependency>  <groupId>org.slf4j</groupId>  <artifactId>slf4j-api</artifactId>  <version>1.6.1</version> </dependency> <dependency>  <groupId>org.slf4j</groupId>  <artifactId>slf4j-log4j12</artifactId>  <version>1.7.5</version>  <scope>test</scope> </dependency> <!--mysql--> <dependency>  <groupId>org.wisdom-framework</groupId>  <artifactId>mysql-connector-java</artifactId>  <version>5.1.34_1</version> </dependency> </dependencies>
View Code

在引用jar包的时候先不要引用javaee-api的jar,会报错

2.写学生的实体类

 //学号 private Integer sid; //姓名 private String name; //年龄 private Integer age;
View Code

3.Hibernate.cfg.

 1 <?'1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3   "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4   ""> 5 <hibernate-configuration> 6  <session-factory> 7   <!--数据库MSQ驱动--> 8   <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 9   <!--数据库URL-->10   <property name="connection.url">jdbc:mysql://localhost/hibernates?11    characterEncoding=gb2312</property>12   <!--数据库用户名-->13   <property name="connection.username">root</property>14    <!--数据库密码-->15   <property name="connection.password">root</property>16   <!--方言-->17   <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>18 19  <!--输出所有语句到控制台-->20  <property name="hibernate.show_sql">true</property>21 22   <!--在log和conlose中打印出更 格式化显示SQL-->23  <property name="hibernate.format_sql">true</property>24 25   <!-- hbm2ddl -->26   <property name="hibernate.hbm2ddl.auto">update</property>27 28   <!-- 指定当前session范围和上下文 thread指当前线程来跟踪管理 -->29   <property name="current_session_context_class">thread</property>30 31   <!-- 映射文件配置,注意配置文件名必须包含其相对于classpath的全路径 -->32   <mapping resource="Student.hbm."></mapping>33  </session-factory>34 </hibernate-configuration>
View Code

4.Student.hbm.

 1 <?'1.0' encoding='utf-8'?> 2  3 <!DOCTYPE hibernate-mapping PUBLIC 4   "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 5   ""> 6 <hibernate-mapping package="cn.happy.entity"> 7 <class name="Student" table="Student"><!--表名,不区分大小写--> 8  <id name="sid" column="sid"> 9 <generator class="native"></generator>10   <!--native :11   如果后台是Oracle,自动创建序列12      后台是MySQL,自动自增13 -->14  </id>15  <property name="name" type="string" column="name"/>16  <property name="age"/>17 </class>18 </hibernate-mapping>
View Code

5.测试类,使得数据使用Hibernate保存到数据库中

 1  @Test 2  public void testhibernate() { 3  4   //01准备一个学生对象 5   Student stu = new Student(); 6   stu.setSid(3); 7   stu.setName("白白"); 8   stu.setAge(44); 9 10   //02Hibernate 保存11   //读取大配置文件,获取连接的数据库信息12   Configuration cfg=new Configuration().configure();13 14   //3创建SessionFactory15   SessionFactory factory=cfg.buildSessionFactory();16 17   //加工session18  Session session=factory.openSession();19 20  //开启事务21   Transaction tx=session.beginTransaction();22 23  //5Hibernate24 25   session.save(stu);26 27   tx.commit();28   System.out.println("save ok");29  }
View Code

6.实现效果图