日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

使用Struts2,Hibernate和MySQL创建个人MusicManager Web应用程序的研讨会

發(fā)布時(shí)間:2023/12/3 数据库 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Struts2,Hibernate和MySQL创建个人MusicManager Web应用程序的研讨会 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

概述:

在本研討會(huì)教程中,我們將使用Struts 2,Hibernate和MySQL數(shù)據(jù)庫開發(fā)一個(gè)個(gè)人音樂管理器應(yīng)用程序。 該Web應(yīng)用程序可用于將您的音樂收藏添加到數(shù)據(jù)庫中。 我們將顯示用于添加唱片的表格,并在下面顯示所有音樂收藏。 通過單擊“刪除”鏈接,可以從每一行中刪除記錄。 我們選擇Struts2是因?yàn)樗庆`活的J2EE框架之一。 該數(shù)據(jù)庫是MySQL,我們已使用Hibernate作為ORM工具。

使用的工具:

  • 面向Web開發(fā)人員的Eclipse Indigo Java EE IDE
  • Struts2
  • 休眠3
  • Hibernate Tools Eclipse插件版本3.5.1
  • mysql JDBC jar(mysql-connector-java-5.1.23)
  • 雄貓7
  • 步驟1:準(zhǔn)備數(shù)據(jù)庫

    我們使用phpMyAdmin設(shè)計(jì)數(shù)據(jù)庫并向其中添加表。 您可以使用任何工具在MySQL中創(chuàng)建表。 下面是表格的屏幕截圖。

    我們創(chuàng)建的數(shù)據(jù)庫是music_manager,并且表albumtbl將用于存儲(chǔ)音樂數(shù)據(jù)。 暫時(shí)忽略genretbl,因?yàn)樵诒窘坛痰暮竺娌糠种?#xff0c;我們將使用此表作為主表來存儲(chǔ)所有音樂流派。

    CREATE TABLE IF NOT EXISTS `albumtbl` (`music_id` INT(4) NOT NULL AUTO_INCREMENT,`album_title` VARCHAR(255) NOT NULL,`album_genre` VARCHAR(255) NOT NULL,`album_artists` text NOT NULL,`no_of_tracks` INT(2) NOT NULL,PRIMARY KEY (`music_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;INSERT INTO `albumtbl` (`music_id`, `album_title`, `album_genre`, `album_artists`, `no_of_tracks`) VALUES (1, 'Trouble-Akon', 'Hip Hop', 'Akon', 11), (3, 'Savage Island', 'Contemporary R&B', 'Savage, Ganxstardd, Soulja Boy, David Dallas, Sean P, Pitbull', 16), (4, 'Kiss (Carly Rae Jepsen album)', 'Pop', 'Carly Rae Jepsen, Justin Bieber, Owl City', 12), (5, 'Taylor Swift (album)', 'Pop', 'Taylor Swift', 15);

    步驟2:在Java源代碼中創(chuàng)建軟件包,如屏幕快照所示

    • “ businessobjects”包將包含數(shù)據(jù)庫表中字段的POJO Java bean。
    • 在“ dao”包中將包含數(shù)據(jù)訪問層java類。 我們有一個(gè)接口dao及其實(shí)現(xiàn)版本。 該Dao實(shí)際上將完成交談和更新表的工作。
    • 'hbm'軟件包包含* .hbm文件,用于將Hibernate xml映射到表字段。
    • 'utils'軟件包包含所有要由其他類使用的Utility類。
    • “動(dòng)作”將具有所有Struts 2動(dòng)作類。
    • 'delegates'包具有委托java類,它將充當(dāng)前端層和休眠dao層之間的橋梁。
    • 'forms'包在Struts 2中是可選的,因?yàn)樗鼪]有ActionForm概念。 但是,為了簡(jiǎn)單起見和可維護(hù)性,我們將所有g(shù)etter設(shè)置程序保留在此處,并且該類將從其擴(kuò)展。

    步驟3:將jar文件復(fù)制到lib文件夾中

    您將需要從Tomcat安裝目錄中獲取的servlet-api.jar文件。 我的Tomcat位于C:\ Java \ Tomcat \ tomcat7文件夾中。

    步驟4:添加Struts 2支持

    包括Struts2.xml并將引用放在web.xml中以支持struts2。

    web.xml

    <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>PersonalMusicManagerApp</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>*.action</url-pattern></filter-mapping> </web-app>

    struts.xml

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts><constant name="struts.enable.DynamicMethodInvocation" value="false" /><constant name="struts.devMode" value="false" /><package name="default" extends="struts-default" namespace="/"><default-action-ref name="index" /><action name="index"><result>index.jsp</result></action><action name="listAlbum" class="com.tctalk.apps.mmgr.web.actions.MusicManagerAction" method="getAllAlbumList" ><result name="success">/WEB-INF/web/jsps/musicmgr.jsp</result></action><action name="addAlbum" class ="com.tctalk.apps.mmgr.web.actions.MusicManagerAction" method="addAlbumToCollection" ><result name="input">listAlbum</result><result name="success" type="redirectAction">listAlbum</result></action><action name="delAlbum" class ="com.tctalk.apps.mmgr.web.actions.MusicManagerAction" method="delAlbumFromCollection" ><result name="success" type="redirectAction">listAlbum</result></action></package></struts>

    步驟5:添加Hibernate支持

    為了與Hibernate一起使用,我們使用了Jboss Elipse Hibernate插件來為此生成hbm文件。 該插件是可選的,因?yàn)樗鼘⒐?jié)省一些時(shí)間,以方便地手動(dòng)生成“休眠配置”和hbm文件。 我們已經(jīng)有一個(gè)不錯(cuò)的分步教程,介紹如何使用此插件自動(dòng)生成hbm和java文件。 單擊此處轉(zhuǎn)到教程– http://www.techcubetalk.com/2013/04/step-by-step-auto-code-generation-for-pojo-domain-java-classes-and-hbm-files-使用橢圓休眠的插件/

    您可以從Eclipse版本從jboss.org下載該插件。

    步驟5a:創(chuàng)建hibernate.cfg.xml-這將具有所有數(shù)據(jù)庫身份驗(yàn)證信息

    <?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 name=""><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql://localhost:3306/music_manager</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password"></property><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><mapping resource="com/tctalk/apps/mmgr/db/hbm/Albumtbl.hbm.xml" /></session-factory> </hibernate-configuration>

    步驟5b:AlbumBO.java – POJO對(duì)應(yīng)于表albumtbl

    package com.tctalk.apps.mmgr.db.businessobjects; // Generated by Hibernate Tools 3.4.0.CR1/*** AlbumtblBO generated by hbm2java*/public class AlbumtblBO implements java.io.Serializable {private static final long serialVersionUID = -1445059679188116334L;private int musicId;private String albumTitle;private String albumGenre;private String albumArtists;private int noOfTracks;public AlbumtblBO() {}public AlbumtblBO(int musicId, String albumTitle, String albumGenre,String albumArtists, int noOfTracks) {this.musicId = musicId;this.albumTitle = albumTitle;this.albumGenre = albumGenre;this.albumArtists = albumArtists;this.noOfTracks = noOfTracks;}public int getMusicId() {return this.musicId;}public void setMusicId(int musicId) {this.musicId = musicId;}public String getAlbumTitle() {return this.albumTitle;}public void setAlbumTitle(String albumTitle) {this.albumTitle = albumTitle;}public String getAlbumGenre() {return this.albumGenre;}public void setAlbumGenre(String albumGenre) {this.albumGenre = albumGenre;}public String getAlbumArtists() {return this.albumArtists;}public void setAlbumArtists(String albumArtists) {this.albumArtists = albumArtists;}public int getNoOfTracks() {return this.noOfTracks;}public void setNoOfTracks(int noOfTracks) {this.noOfTracks = noOfTracks;}}

    步驟5c:Albumtbl.hbm.xml –它包含表Albumtbl表的字段和POJO類AlbumBO的字段之間的映射。

    <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated Mar 17, 2013 11:53:52 PM by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping><class name="com.tctalk.apps.mmgr.db.businessobjects.AlbumtblBO" table="albumtbl" catalog="music_manager"><id name="musicId" type="int"><column name="music_id" /><generator class="assigned" /></id><property name="albumTitle" type="string"><column name="album_title" not-null="true" /></property><property name="albumGenre" type="string"><column name="album_genre" not-null="true" /></property><property name="albumArtists" type="string"><column name="album_artists" length="65535" not-null="true" /></property><property name="noOfTracks" type="int"><column name="no_of_tracks" not-null="true" /></property></class> </hibernate-mapping>

    步驟5d:HibernateUtils.java –這是休眠會(huì)話處理實(shí)用程序類
    MusicMgrConstant.java也具有要從中引用的hibernate.cfg.xml的位置/路徑。

    package com.tctalk.apps.mmgr.utils;import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;public class HibernateUtils {private static SessionFactory hbmSessionFactory;static {try {Configuration cfg = new Configuration().configure(MusicMgrConstant._HIBERNATE_CONFIG_LOCATION);hbmSessionFactory = cfg.buildSessionFactory();} catch (RuntimeException ex) {System.out.println("********* Error occurred while reading config file *********");ex.printStackTrace();}}/*** getSession creates hibernate Session & returns it*/public static Session getSession() {return hbmSessionFactory.openSession();}/*** closeSession closes the session, if it exists*/public static void closeSession(Session inSession) {if (inSession != null) {inSession.close();}} }package com.tctalk.apps.mmgr.utils;public interface MusicMgrConstant {String _HIBERNATE_CONFIG_LOCATION = "hibernate.cfg.xml"; }

    步驟5e??:MusicManagerDao.java和MusicManagerDaoImpl.java –這些是DAO類,具有列出,添加和刪除數(shù)據(jù)庫中數(shù)據(jù)的方法。

    package com.tctalk.apps.mmgr.db.dao;import java.util.List;import com.tctalk.apps.mmgr.db.businessobjects.AlbumtblBO;public interface MusicManagerDao {public List getAllMusicAlbumsFromCollection();public boolean addAlbum(AlbumtblBO album);public boolean delAlbum(int albumId); }package com.tctalk.apps.mmgr.db.dao;import java.util.List;import org.hibernate.Criteria; import org.hibernate.Session;import com.tctalk.apps.mmgr.db.businessobjects.AlbumtblBO; import com.tctalk.apps.mmgr.utils.HibernateUtils;public class MusicManagerDaoImpl implements MusicManagerDao {public List getAllMusicAlbumsFromCollection() {List albumList = null;Session hbmSession = null;try {hbmSession = HibernateUtils.getSession();Criteria criteria = hbmSession.createCriteria(AlbumtblBO.class);albumList = criteria.list();} catch (Exception ex) {ex.printStackTrace();} finally {HibernateUtils.closeSession(hbmSession);}return albumList;}public boolean addAlbum(AlbumtblBO album) {Session hbmSession = null;boolean STATUS_FLAG = true;try {hbmSession = HibernateUtils.getSession();hbmSession.beginTransaction();//add the album to the hibernate session to savehbmSession.save(album);hbmSession.getTransaction().commit();} catch (Exception ex) {ex.printStackTrace();STATUS_FLAG = false;} finally {HibernateUtils.closeSession(hbmSession);}return STATUS_FLAG;}public boolean delAlbum(int albumId) {Session hbmSession = null;boolean STATUS_FLAG = true;try {hbmSession = HibernateUtils.getSession();hbmSession.beginTransaction();//first retrieve the album corresponds to that idAlbumtblBO albumObj = (AlbumtblBO)hbmSession.load(AlbumtblBO.class, albumId);hbmSession.delete(albumObj);hbmSession.getTransaction().commit();} catch (Exception ex) {ex.printStackTrace();STATUS_FLAG = false;} finally {HibernateUtils.closeSession(hbmSession);}return STATUS_FLAG;}}

    步驟6:在“ WebContent”部分中創(chuàng)建UI層

    我們創(chuàng)建了“ web”文件夾,以將所有與UI相關(guān)的文件保留在那里。 由于此應(yīng)用程序只有一個(gè)JSP,因此我們將創(chuàng)建jsp文件夾并復(fù)制musicmngr.jsp。
    在中,我們將表單數(shù)據(jù)發(fā)布到操作addAlbum。 在此之下,我們遍歷表中的專輯列表以列出音樂專輯。

    Musicmngr.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>TechcubeTalk.com - Let's build apps from scratch series - Personal Music Manager Application</title> </head> <body> <h2>:: TechcubeTalk.com - Personal Music Manager ::</h2> <div style="margin-bottom: 25px;"> <s:form action="addAlbum" method="POST"><s:textfield label="Album Title" name="album.albumTitle"/><s:textfield label="Music Genre" name="album.albumGenre"/><s:textarea label="Artist Names" name="album.albumArtists" cols="40" rows="10"/><s:textfield label="Total No of Tracks" name="album.noOfTracks"/><s:submit value="Add Music Album" align="center"/> </s:form> </div> <div><table style="border: 1px dotted black;"><tr><th style="background-color:#ABDCFF;">Album Title</th><th style="background-color:#ABDCFF;">Music Genre</th><th style="background-color:#ABDCFF;">Artist Names</th><th style="background-color:#ABDCFF;">Total No of Tracks</th><th style="background-color:#ABDCFF;">Delete</th></tr><s:iterator value="albumList" var="album"><tr><td><s:property value="albumTitle"/></td><td><s:property value="albumGenre"/></td><td><s:property value="albumArtists"/></td><td><s:property value="noOfTracks"/></td><td><a href="delAlbum.action?musicId=<s:property value="musicId"/>">delete</a></td></tr></s:iterator></table> </div> </body> </html>

    步驟7:添加動(dòng)作類

    動(dòng)作類MusicManagerAction.java將處理jsp的所有數(shù)據(jù)處理。 此類擴(kuò)展了MusicManagerForm,這只是一個(gè)POJO,以使所有g(shù)etter / setter對(duì)應(yīng)于jsp的所有表單值和albumList。

    • getAllAlbumList()方法通過MusicManagerDelegate從數(shù)據(jù)庫中檢索音樂專輯列表,并在albumList變量中進(jìn)行設(shè)置。
    • addAlbumToCollection()方法將音樂專輯添加到數(shù)據(jù)庫中。
    • delAlbumFromCollection()從數(shù)據(jù)庫中刪除基于musicId的特定專輯。

    MusicManagerAction.java

    package com.tctalk.apps.mmgr.web.actions;import java.util.List;import com.tctalk.apps.mmgr.db.businessobjects.AlbumtblBO; import com.tctalk.apps.mmgr.web.delegates.MusicManagerDelegate; import com.tctalk.apps.mmgr.web.forms.MusicManagerForm;public class MusicManagerAction extends MusicManagerForm {private static final long serialVersionUID = 9168149105719285096L;private MusicManagerDelegate musicMgrDelegate = new MusicManagerDelegate();public String getAllAlbumList(){List albumList = musicMgrDelegate.getAllMusicAlbums();String returnString = ERROR;if(albumList != null) {setAlbumList(albumList);returnString = SUCCESS;}return returnString;}public String addAlbumToCollection(){String returnString = ERROR;AlbumtblBO album = getAlbum();if(musicMgrDelegate.addAlbumToCollection(album)){returnString = SUCCESS;}return returnString;}public String delAlbumFromCollection(){String returnString = ERROR;int albumId = getMusicId();if(musicMgrDelegate.delAlbumFromCollection(albumId)) {returnString = SUCCESS;}return returnString;} }

    MusicManagerForm.java

    package com.tctalk.apps.mmgr.web.forms;import java.util.List;import com.opensymphony.xwork2.ActionSupport; import com.tctalk.apps.mmgr.db.businessobjects.AlbumtblBO;public class MusicManagerForm extends ActionSupport {private static final long serialVersionUID = 706337856877546963L;private List albumList = null;private AlbumtblBO album = null;private int musicId;public AlbumtblBO getAlbum() {return album;}public void setAlbum(AlbumtblBO album) {this.album = album;}public List getAlbumList() {return albumList;}public void setAlbumList(List albumList) {this.albumList = albumList;}public int getMusicId() {return musicId;}public void setMusicId(int musicId) {this.musicId = musicId;}}

    步驟8:添加代表類

    委托類充當(dāng)表示層和數(shù)據(jù)庫處理層之間的橋梁。 它接受輸入并傳遞到數(shù)據(jù)庫層(dao類)以在數(shù)據(jù)庫中添加/刪除數(shù)據(jù)。 同樣,它從數(shù)據(jù)庫中獲取數(shù)據(jù)并顯示在頁面中。

    MusicManagerDelegate.java

    package com.tctalk.apps.mmgr.web.delegates;import java.util.List;import com.tctalk.apps.mmgr.db.businessobjects.AlbumtblBO; import com.tctalk.apps.mmgr.db.dao.MusicManagerDao; import com.tctalk.apps.mmgr.db.dao.MusicManagerDaoImpl;public class MusicManagerDelegate {MusicManagerDao mmgrDao = (MusicManagerDao) new MusicManagerDaoImpl();public List getAllMusicAlbums() { return mmgrDao.getAllMusicAlbumsFromCollection();}public boolean addAlbumToCollection(AlbumtblBO albumobj) {return mmgrDao.addAlbum(albumobj);}public boolean delAlbumFromCollection(int albumId) {return mmgrDao.delAlbum(albumId);} }

    步驟9:最終整合

    整合完成后,最終的包裝結(jié)構(gòu)將如下所示-


    生成項(xiàng)目并右鍵單擊它,然后選擇“導(dǎo)出為戰(zhàn)爭(zhēng)文件”并保存在已知文件夾中。 打開Tomcat管理器控制臺(tái)應(yīng)用程序,然后瀏覽WAR文件以進(jìn)行安裝。

    安裝完成后,以(http:// localhost:8080 / PersonalMusicManagerApp – URL可能因Tomcat安裝文件夾/端口等而異)運(yùn)行該應(yīng)用程序。

    如果一切順利運(yùn)行,將顯示屏幕–

    • 從GitHub存儲(chǔ)庫中以zip和war下載項(xiàng)目

    參考: TechCubeTalk博客上的JCG合作伙伴 Suvoraj Biswas提供的關(guān)于 使用Struts2,Hibernate和MySQL創(chuàng)建Personal MusicManager Web應(yīng)用程序的研討會(huì) 。

    翻譯自: https://www.javacodegeeks.com/2013/12/workshop-on-creating-a-personal-musicmanager-web-application-with-struts2-hibernate-and-mysql.html

    總結(jié)

    以上是生活随笔為你收集整理的使用Struts2,Hibernate和MySQL创建个人MusicManager Web应用程序的研讨会的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。