hibernate连接mysql示范


 

hibernate连接mysql示范

今天用了一下java的数据库持久化-业务的hibernate框架。下面给出hibernate 连接mysql数据库示例

 

建表结构如下

 

mysql> desc test;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment | 
| username | varchar(100) | NO   |     | NULL    |                | 
+----------+--------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql>

 

 

hibernate配置文件 hibernate.cfg.xml

 

[xhtml] view plaincopy
  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  3. <hibernate-configuration>  
  4. <session-factory>  
  5. <property name="show_sql">true</property>  
  6. <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
  7. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  8. <property name="connection.url">jdbc:mysql://192.168.25.152/test</property>  
  9. <property name="connection.username">这里写用户名</property>  
  10. <property name="connection.password">密码</property>  
  11. <mapping resource="user.hbm.xml"/>  
  12. </session-factory>  
  13. </hibernate-configuration>  

 

表映射 user.hbm.xml

 

[xhtml] view plaincopy
  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  3. <hibernate-mapping>  
  4. <class name="com.TestDb" table="test">  
  5.     <id name="id" column="id">  
  6.         <generator class="increment"/>  
  7.     </id>  
  8.     <property name="username" column="username" />  
  9. </class>  
  10. </hibernate-mapping>  

 

表映射类

 

[java] view plaincopy
  1. package com;  
  2.   
  3. /** 
  4. * Created by IntelliJ IDEA. 
  5. * User: Administrator 
  6. * Date: 2006-2-6 
  7. * Time: 22:10:05 
  8. * To change this template use File | Settings | File Templates. 
  9. */  
  10. public class TestDb {  
  11. private String username;  
  12. private Long id;  
  13.   
  14. public Long getId() {  
  15. return id;  
  16. }  
  17.   
  18. public void setId(Long id) {  
  19. this.id = id;  
  20. }  
  21.   
  22. public String getUsername() {  
  23. return username;  
  24. }  
  25.   
  26. public void setUsername(String myname) {  
  27. this.username = myname;  
  28. }  
  29. }  

 

测试类

 

[java] view plaincopy
  1. package com;  
  2.   
  3.   
  4. import java.util.List;  
  5.   
  6. import org.hibernate.Query;  
  7. import org.hibernate.Session;  
  8. import org.hibernate.SessionFactory;  
  9. import org.hibernate.Transaction;  
  10. import org.hibernate.cfg.Configuration;  
  11.   
  12. public class test {  
  13.       
  14.     //遍历  
  15.     public static void all()  
  16.     {  
  17.         Query q = session.createQuery("select c.id,c.username from TestDb as c");  
  18.           
  19.         List l = q.list();  
  20.         for(int i=0;i<l.size();i++)  
  21.         {  
  22.             //TestDb user = (TestDb)l.get(i);  
  23.             //System.out.println(user.getUsername());  
  24.   
  25.               Object[] row = (Object[])l.get(i);;  
  26.               Long id = (Long)row[0];  
  27.               String name = (String)row[1];    
  28.               System.out.println(id+" "+name);  
  29.         }  
  30.     }  
  31.       
  32.     //读取  
  33.     public static void load()  
  34.     {  
  35.         TestDb obj = (TestDb) session.load(TestDb.classnew Long(2));  
  36.         System.out.println(obj.getUsername());  
  37.     }  
  38.       
  39.     //更新  
  40.     public static void update()  
  41.     {  
  42.         TestDb obj = (TestDb) session.load(TestDb.classnew Long(2));  
  43.         obj.setUsername("cg");  
  44.     }  
  45.       
  46.     //插入  
  47.     public static void insert()  
  48.     {  
  49.         TestDb user = new TestDb();  
  50.         user.setUsername("sb");  
  51.   
  52.         session.save(user);  
  53.     }  
  54.       
  55.     static SessionFactory sessionFactory;  
  56.     static Session session ;  
  57.     static Transaction tx ;  
  58.       
  59.     private static void init()  
  60.     {  
  61.         sessionFactory = new Configuration().configure().buildSessionFactory();  
  62.         session = sessionFactory.openSession();  
  63.         tx = session.beginTransaction();  
  64.     }  
  65.       
  66.     private static void close()  
  67.     {  
  68.         tx.commit();  
  69.         session.close();  
  70.         sessionFactory.close();  
  71.     }  
  72.       
  73.     public static void main(String[] args)   
  74.     {  
  75.         init();  
  76.         update();  
  77.         close();  
  78.     }  
  79. }  

 

文件结构

 

 

 

更多0

相关内容

    暂无相关文章