当前位置:Gxlcms > 数据库问题 > Hibernate连接MySQL

Hibernate连接MySQL

时间:2021-07-01 10:21:17 帮助过:48人阅读

   技术分享技术分享
  1. <!DOCTYPE hibernate-configuration PUBLIC  
  2.  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  3.  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  4. <hibernate-configuration>  
  5. <session-factory>  
  6.     <!--程序执行的时候是否显示真正的sql语句-->  
  7.     <property name="show_sql">true</property>  
  8.     <!--使用的SQL对应的“方言”,此处是Oracle11的“方言”-->  
  9.     <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect  
  10.     </property>  
  11.     <!--连接数据库的Driver-->  
  12.     <property name="connection.driver_class">  
  13.         com.mysql.jdbc.Driver  
  14.     </property>  
  15.     <!--数据库连接url-->  
  16.     <property name="connection.url">  
  17.         jdbc:mysql://localhost:3306/test  
  18.     </property>  
  19.     <!--用户名-->  
  20.     <property name="connection.username">root</property>  
  21.     <!--密码-->  
  22.     <property name="connection.password">123456</property>  
  23.     <mapping resource="Student.hbm.xml"/>  
  24. </session-factory>  
  25. </hibernate-configuration>  

 

 

(2)在src目录下的com.abc包中添加Student.java

 

[java] view plain copy    技术分享技术分享
  1. package com.abc;  
  2.   
  3. public class Student {  
  4.     private String NO;  
  5.     private String name;  
  6.    
  7.     public String getNO() {  
  8.         return NO;  
  9.     }  
  10.     public void setNO(String NO) {  
  11.         this.NO = NO;  
  12.     }  
  13.     public String getName() {  
  14.         return name;  
  15.     }  
  16.     public void setName(String name) {  
  17.         this.name = name;  
  18.     }  
  19. }  

 

 

(3)在src目录下添加Student.hbm.xml

 

[java] view plain copy    技术分享技术分享
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!DOCTYPE hibernate-mapping  
  3.         PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5. <hibernate-mapping package="com.abc">  
  6.     <class name="Student" table="Student">  
  7.         <id name="NO">  
  8.             <generator class="assigned" />  
  9.         </id>  
  10.         <property name="name" column="name" type="java.lang.String" />  
  11.     </class>  
  12. </hibernate-mapping>  

 

 

(4)Test.java

 

[java] view plain copy    技术分享技术分享
  1. package com.abc;  
  2.   
  3. import org.hibernate.*;  
  4. import org.hibernate.cfg.*;  
  5.   
  6. public class Test {  
  7.     public static void main(String[] args) {  
  8.         try {  
  9.             //通过Configuration获得一个SessionFactory对象  
  10.             SessionFactory sf = new Configuration().configure().buildSessionFactory();  
  11.             //打开一个Session  
  12.             Session session = sf.openSession();  
  13.             //开始一个事务  
  14.             Transaction tx = session.beginTransaction();  
  15.             //创建一个Student对象  
  16.             Student stu = new Student();  
  17.             //通过session的save()方法将Student对象保存到数据库中  
  18.             stu.setNO("2016003");  
  19.             stu.setName("Zhang San");  
  20.             session.save(stu);  
  21.             //提交事务  
  22.             tx.commit();  
  23.             //关闭会话  
  24.             session.close();  
  25.         } catch(Exception e) {  
  26.             e.printStackTrace();  
  27.         }  
  28.     }  
  29. }  

 

 

7 验证

(1)运行Test.java,结果为

Hibernate: insert into Student (name, NO)values (?, ?)

 

(2)从MySQL中查询数据

技术分享

Hibernate连接MySQL

标签:显示   use   title   char   main   lib   sig   ace   except   

人气教程排行