hibernate的demo

最近在學習ssh把一些小demo記錄下來以備日後查看
各位兄弟有什麼好的建議,那就請不要吝嗇,請多多指點。
java 代碼
 
  1. package com.tyq.pojo;  
  2.   
  3. public class User  implements java.io.Serializable {  
  4.   
  5.   
  6.     // Fields      
  7.   
  8.      private int id;  
  9.      private String name;  
  10.      private Password password = null;  
  11.   
  12.   
  13.     public Password getPassword() {  
  14.         return password;  
  15.     }  
  16.   
  17.     public void setPassword(Password password) {  
  18.         this.password = password;  
  19.     }  
  20.   
  21.     /** default constructor */  
  22.     public User() {  
  23.     }  
  24.   
  25.      
  26.     // Property accessors  
  27.   
  28.     public int getId() {  
  29.         return this.id;  
  30.     }  
  31.       
  32.     public void setId(int id) {  
  33.         this.id = id;  
  34.     }  
  35.   
  36.     public String getName() {  
  37.         return this.name;  
  38.     }  
  39.       
  40.     public void setName(String name) {  
  41.         this.name = name;  
  42.     }  
  43. }  
java 代碼
 
  1. package com.tyq.pojo;  
  2.   
  3.   
  4. public class Password  implements java.io.Serializable {  
  5.   
  6.   
  7.     // Fields      
  8.   
  9.      private int id;  
  10.      private String password;  
  11.      private User user = null;  
  12.   
  13.     // Constructors  
  14.   
  15.     /** default constructor */  
  16.     public Password() {  
  17.     }  
  18.   
  19.      
  20.     // Property accessors  
  21.   
  22.     public int getId() {  
  23.         return this.id;  
  24.     }  
  25.       
  26.     public void setId(int id) {  
  27.         this.id = id;  
  28.     }  
  29.   
  30.     public String getPassword() {  
  31.         return this.password;  
  32.     }  
  33.       
  34.     public void setPassword(String password) {  
  35.         this.password = password;  
  36.     }  
  37.   
  38.   
  39.     public User getUser() {  
  40.         return user;  
  41.     }  
  42.   
  43.   
  44.     public void setUser(User user) {  
  45.         this.user = user;  
  46.     }  
  47. }  
以上是兩個pojo類由於是學習用的就弄的簡單些把密碼和用戶名給分開,再次聲明這只是測試用真正的開發中這絕對是一bad smell
下面是對應的配置文件
xml 代碼
 
  1. <!--User.hbm.xml-->  
  2. <?xml version="1.0" encoding="utf-8"?>  
  3. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5. <!--  
  6.     Mapping file autogenerated by MyEclipse Persistence Tools 
  7. -->  
  8. <hibernate-mapping>  
  9.     <class name="com.tyq.pojo.User" table="user">  
  10.         <id name="id" type="java.lang.Integer">  
  11.             <column name="id" />  
  12.             <generator class="identity" />  
  13.         </id>  
  14.         <property name="name" type="java.lang.String">  
  15.             <column name="name" length="20" unique="true" />  
  16.         </property>  
  17.         <one-to-one name="password"  
  18.         class="com.tyq.pojo.Password"  
  19.         cascade="all"/>  
  20.     </class>  
  21. </hibernate-mapping>  
xml 代碼
 
  1. <1--Password.hbm.xml-->  
  2. <?xml version="1.0" encoding="utf-8"?>  
  3. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5. <!--  
  6.     Mapping file autogenerated by MyEclipse Persistence Tools 
  7. -->  
  8. <hibernate-mapping>  
  9.     <class name="com.tyq.pojo.Password" table="password">  
  10.         <id name="id" type="java.lang.Integer">  
  11.             <column name="id" />  
  12.             <generator class="identity"></generator>  
  13.         </id>  
  14.         <property name="password" type="java.lang.String">  
  15.             <column name="password" length="40" not-null="true" unique="true" />  
  16.         </property>  
  17.         <one-to-one name="user"  
  18.         class="com.tyq.pojo.User"  
  19.         constrained="true"/>  
  20.     </class>  
  21. </hibernate-mapping>  
下面就是hibernate 的配置文件拉
xml 代碼
 
  1. <!--hibernate.cfg.xml-->  
  2. <?xml version='1.0' encoding='UTF-8'?>  
  3. <!DOCTYPE hibernate-configuration PUBLIC  
  4.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  5.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  6.   
  7. <!-- Generated by MyEclipse Hibernate Tools.                   -->  
  8. <hibernate-configuration>  
  9.   
  10.     <session-factory>  
  11.         <property name="myeclipse.connection.profile">  
  12.             com.jdbc.mysql.Driver  
  13.         </property>  
  14.         <property name="connection.url">  
  15.             jdbc:mysql://localhost:3306/system  
  16.         </property>  
  17.         <property name="connection.username">yourname</property>  
  18.         <property name="connection.password">yourpassword</property>  
  19.         <property name="connection.driver_class">  
  20.             com.mysql.jdbc.Driver  
  21.         </property>  
  22.         <property name="dialect">  
  23.             org.hibernate.dialect.MySQLDialect  
  24.         </property>  
  25.         <property name="show_sql">true</property>  
  26.         <mapping resource="com/tyq/pojo/User.hbm.xml" />  
  27.         <mapping resource="com/tyq/pojo/Password.hbm.xml" />  
  28.   
  29.     </session-factory>  
  30.   
  31. </hibernate-configuration>  
下面是測試用類
java 代碼
 
  1. import junit.framework.TestCase;  
  2.   
  3. import org.hibernate.Session;  
  4. import org.hibernate.SessionFactory;  
  5. import org.hibernate.cfg.Configuration;  
  6.   
  7. import com.tyq.pojo.Password;  
  8. import com.tyq.pojo.User;  
  9.   
  10.   
  11. public class TestHibernate extends TestCase {  
  12.   
  13.     Session session = null;  
  14.     @Override  
  15.     protected void setUp() throws Exception {  
  16.         super.setUp();  
  17.         Configuration config = new Configuration().configure();  
  18.         SessionFactory factory = config.buildSessionFactory();  
  19.         session = factory.openSession();  
  20.     }  
  21.   
  22.     @Override  
  23.     protected void tearDown() throws Exception {  
  24.         if(session != null)  
  25.         {  
  26.             session.close();  
  27.         }  
  28.         super.tearDown();  
  29.     }  
  30.     public void testSelect(){  
  31.         User user = new User();  
  32.         Password password = new Password();  
  33.         user.setName("lql");  
  34.         password.setPassword("tyq");  
  35.         user.setPassword(password);  
  36.         password.setUser(user);  
  37.         session.save(user);  
  38.         session.beginTransaction().commit();  
  39.     }  
  40.   
  41.   
  42. }  
OK現在可以去數據庫裏看結果拉
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章