搭建ssh框架項目(一)
阿新 • • 發佈:2018-10-25
pass figure 映射文件 ttext nsa Coding word map cti
一、創建web項目
二、導入jar包
三、創建數據庫(MySQL)
四、建立javaBean對象(ElecText.java),屬於持久層對象(PO對象)
package com.cppdy.ssh.domain; import java.util.Date; /** * * PO持久層對象,對應數據庫表Elec_Text * */ @SuppressWarnings("serial") public class ElecText implements java.io.Serializable{ private String textID; privateElecText.javaString textName; private Date textDate; private String textRemark; public String getTextID() { return textID; } public void setTextID(String textID) { this.textID = textID; } public String getTextName() { return textName; } public voidsetTextName(String textName) { this.textName = textName; } public Date getTextDate() { return textDate; } public void setTextDate(Date textDate) { this.textDate = textDate; } public String getTextRemark() { return textRemark; } public voidsetTextRemark(String textRemark) { this.textRemark = textRemark; } }
五、創建映射文件(ElecText.hbm.xml),建立PO對象與數據庫表Elec_Text的關聯關系
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.cppdy.ssh.domain.ElecText" table="Elec_Text"> <id name="textID" type="string"> <column name="textID" sql-type="varchar(50)" not-null="true"/> <generator class="uuid"/> </id> <property name="textName" type="string"> <column name="textName" sql-type="varchar(50)"/> </property> <property name="textDate" type="date"> <column name="textDate" length="50"/> </property> <property name="textRemark" type="string"> <column name="textRemark" sql-type="varchar(500)"/> </property> </class> </hibernate-mapping>ElecText.hbm.xml
六、創建hibernate.cfg.xml文件,配置連接數據庫的信息
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 數據庫用戶名 --> <property name="hibernate.connection.username">root</property> <!-- 數據庫密碼 --> <property name="hibernate.connection.password">root</property> <!-- 數據庫驅動 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 數據庫連接地址 --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ssh</property> <!-- 配置方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 無表自動創建,有表自動追加數據 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 顯示sql語句 --> <property name="hibernate.show_sql">true</property> <!-- 映射文件 --> <mapping resource="com/cppdy/ssh/domain/ElecText.hbm.xml"/> </session-factory> </hibernate-configuration>hibernate.cfg.xml
七、創建測試類(TestHibernate.java)
package junit; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; import com.cppdy.ssh.domain.ElecText; public class TestHibernate { @Test public void testElecText(){ Configuration config = new Configuration(); config.configure(); //創建sessionFactory對象 SessionFactory sf = config.buildSessionFactory(); //打開session,操作數據庫 Session session = sf.openSession(); //開啟事務 Transaction tr = session.beginTransaction(); //實例化ElecText對象,添加數據,執行保存操作 ElecText elecText = new ElecText(); elecText.setTextName("測試Hibernate名稱"); elecText.setTextDate(new Date()); elecText.setTextRemark("測試Hibernate備註"); //保存 session.save(elecText); //提交事務 tr.commit(); session.close(); } }TestHibernate.java
八、數據庫自動創建表並添加數據
項目結構:
搭建ssh框架項目(一)