這篇文章主要介紹了SSH Hibernate怎么配置的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇SSH Hibernate怎么配置文章都會有所收獲,下面我們一起來看看吧。
<!-- 指定Map.hbm.xml的位置--> <mapping resource="net/blogjava/nokiaguy/models/Map.hbm.xml"/>
一開始只是很基礎的引入了hibernat的jar包。后來在調試的時候發現很多jar包丟失了,引入了一個批處理。
比如表結構如下:
那么創建如下實體類:
[java] view plaincopy
<span style="font-size:12px;">package net.blogjava.nokiaguy .models;
公共類 MapEntity {
私有 int id;
私有字符串關鍵字;
私有字符串值;
public int getId() {
返回 id;
}
public void setId(int id) {
this.id = id;
}
公共字符串 getKeyword() {
返回關鍵字;
}
公共無效 setKeyword(字符串關鍵字) {
this.keyword = 關鍵字;
}
public String getValue() {
返回值;
}
public void setValue(String value) {
this.value = value;
}
}
</span>并為此實體類創建一個 xml 映射文件:
[html] view plaincopy<span style="font-size:12px;"><?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="net.blogjava.nokiaguy.models.MapEntity" table="t_map"> <! --將id屬性注入自增類型--> <id name="id" column="id" type="int"> <generator class="increment"/> </id> <property name="keyword" column="keyword"/> <property name=" value" column="value"/> </class> </hibernate-mapping></span>
后跟核心配置文件:hibernate.cfg.xml:
[html] view plaincopy<?xml version="1.0" encoding ="UTF-8"?> <!--表示解析XML文件的DTD文檔的位置。DTD是Document Type Definition的縮寫,是文檔類型的定義。XML 解析器使用 DTD 文檔來檢查 XML 文件的合法性。hibernate.sourceforge.net/hibernate-configuration-3.0dtd可以在Hibernate3.1.3軟件包的src\org\hibernate目錄下找到 --> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0 //EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- 表示下面的配置是為session-factory配置的,SessionFactory是Hibernate中的一個類,這個類主要負責保存HIbernate的配置信息,以及Session的操作 --> <hibernate -configuration> <!--配置數據庫驅動,Hibernate連接數據庫時需要使用數據庫驅動--> <session -factory> <!--設置數據庫的連接url:jdbc:mysql:// localhost/**, <property name="connection.url"> jdbc:mysql://localhost/testhibernate </property> <!--hibernate.dialect 只是Hibernate使用的數據庫方言,就是使用Hibernate來連接那種類型的數據庫服務器。--> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.username">root</property> <property name="connection.password"></property > <!-- 顯示hibernate產生的SQL語句--> <property name="show_sql">true</property> <property name="connection.driver_class"> </the session-Factory> </Hibernate-Configuration >
里面主要是配置連接的一些需要的參數和實體class_table的配置文件的位置。
[java] view plaincopy 調用操作數據庫public String execute()
throws Exception
{
Configuration configuration=new Configuration();
會話工廠會話工廠;
configuration.configure("/hibernate.cfg.xml");
sessionFactory=配置 .buildSessionFactory();
//啟動一個會話
Session session=sessionFactory.openSession();
交易transaction=session.beginTransaction();
//開始
交易 transaction.begin();
MapEntity mapEntity1=new MapEntity();
mapEntity1.setKeyword("ihu");
mapEntity1.setValue("bc...");
地圖實體地圖實體2 =新地圖實體();
mapEntity2.setKeyword("iou");
mapEntity2.setValue("bc...");
持久化兩個對象 session.save(mapEntity1);
session.save(mapEntity2);
//提交事務
transaction.commit();
session.close();
結果="保存成功";
返回“成功”;
}關于“SSH Hibernate怎么配置”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“SSH Hibernate怎么配置”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。