Hibernate专题
struts2-hibernate-mysql开发案例与源码
源码下载
本案例展示使用Struts2,Hibernate和MySQL数据库开发一个个人音乐管理器Web应用程序。,可将您的音乐收藏添加到数据库中。功能有:显示一个添加记录的表单和所有的音乐收藏的列表。每行的记录可通过点击“删除”链接删除。Struts2灵活的J2EE框架之一,数据库是MySQL,使用Hibernate作为ORM工具。
工具:
Eclipse Indigo Java EE IDE for Web开发者
Struts 2
Hibernate 3
Hibernate Tools Eclipse Plugin Version 3.5.1
mysql JDBC jar (mysql-connector-java-5.1.23)
Tomcat 7
Step 1: 准备数据库
使用phpMyAdmin 作为MySQL数据库管理:
创建数据库music_manager ,加入下面SQL创建表 albumtbl:
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);
Step 2: 在Eclipse创建项目
‘businessobjects’ 包含了映射数据库表的POJO java beans
在 ‘dao’包下包含数据访问层的Java类,操作数据表
‘hbm’ 包含 *.hbm 用于 hibernate xml 到 数据表的字段映射
‘utils’包包含工具类
‘actions’ 是 Struts 2 action 类
‘delegates’ 扮演前端和Hibernate后端的桥梁。
‘forms’ 包是Struts 2可选,包含界面的表单对象
Step 3: 拷贝 jar文件到lib目录
从Tomcat安装目录拷贝servlet-api.jar 到项目lib目录。
Step 4: 增加Struts 2支持
在web.xml加入:
03
04
index.jsp
05
06
07
08
struts2
09
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
10
11
12
struts2
13
*.action
14
15
创建struts.xml内容:
/p>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
index.jsp
class="com.tctalk.apps.mmgr.web.actions.MusicManagerAction" method="getAllAlbumList" >
/WEB-INF/web/jsps/musicmgr.jsp
class ="com.tctalk.apps.mmgr.web.actions.MusicManagerAction" method="addAlbumToCollection" >
listAlbum
listAlbum
class ="com.tctalk.apps.mmgr.web.actions.MusicManagerAction" method="delAlbumFromCollection" >
listAlbum
Step 5: 增加 Hibernate 支持
增加Eclipse的Hibernate插件:
创建hibernate.cfg.xml
/p>
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/music_manager
root
org.hibernate.dialect.MySQLDialect
创建一个AlbumBO.java类,可由Hibernatw插件输出:
// 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;
}
Albumtbl.hbm.xml是AlbumBO和数据表Album的映射文件:
/p>
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
创建HibernateUtils.java 专门用于Hibernate的操作:
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();
}
}
}
创建MusicManagerDao.java and MusicManagerDaoImpl.java ,代码可见源码下载包。
Step 6:创建UI界面
Musicmngr.jsp:
pageEncoding="ISO-8859-1"%>
TechcubeTalk.com - Let's build apps from scratch series - Personal Music Manager Application:: TechcubeTalk.com - Personal Music Manager ::
Step 7: 增加 action类
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;
}
}
Step 8: 增加委托类
MusicManagerDelegate.java
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);
}
}
Step 9: 最后
在Eclipse点按项目,右键中选择 Export as war 文件,将项目输出一个WAR文件。也可以使用Gradle建立一个Java 项目,然后输出War文件。
将war文件拷贝到tomcat/webapps目录下,在浏览器访问http://localhost:8080/PersonalMusicManagerApp
.