hibernate h2变mysql_struts2-hibernate-mysql开发案例 -解道Jdon

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数据库管理:

6b552738ae585b1db2c1448e5b325363.png

创建数据库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创建项目

67d6427d00cd693f1f60cadf3bb1143f.png

‘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 ::

Album TitleMusic GenreArtist NamesTotal No of TracksDelete
">delete

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

bc4217a92e78d67106a1a1645a58594c.png

.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388737.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

P5024 保卫王国

传送门 我现在还是不明白为什么NOIPd2t3会是一道动态dp…… 首先关于动态dp可以看这里 然后这里就是把把矩阵给改一改,改成这个形式\[\left[dp_{i-1,0},dp_{i-1,1}\right]\times \left[\begin{matrix}\infty&ldp_{i,1}\\ldp_{i,0}&ldp_{i,1}\end{matrix}\ri…

提取图像感兴趣区域_从图像中提取感兴趣区域

提取图像感兴趣区域Welcome to the second post in this series where we talk about extracting regions of interest (ROI) from images using OpenCV and Python.欢迎来到本系列的第二篇文章,我们讨论使用OpenCV和Python从图像中提取感兴趣区域(ROI)。 As a rec…

解决java compiler level does not match the version of the installed java project facet

ava compiler level does not match the version of the installed java project facet错误的解决 因工作的关系,Eclipse开发的Java项目拷来拷去,有时候会报一个很奇怪的错误。明明源码一模一样,为什么项目复制到另一台机器上,就会…

php模板如何使用,ThinkPHP如何使用模板

到目前为止,我们只是使用了控制器和模型,还没有接触视图,下面来给上面的应用添加视图模板。首先我们修改下 Action 的 index 操作方法,添加模板赋值和渲染模板操作。PHP代码classIndexActionextendsAction{publicfunctionindex(){…

理解Windows窗体和WPF中的跨线程调用

你曾开发过Windows窗体程序,可能会注意到有时事件处理程序将抛出InvalidOperationException异常,信息为“ 跨线程调用非法:在非创建控件的线程上访问该控件”。这种Windows窗体应用程序中 跨线程调用时的一个最为奇怪的行为就是,有…

什么是嵌入式系统

在我们的日常生活中,我们经常使用许多使用嵌入式系统技术设计的电气和电子电路和套件。计算机,手机,平板,笔记本电脑,数字电子系统以及其他电子和电子设备都是使用嵌入式系统设计的。 什么是嵌入式系统?将硬…

面向数据科学家的实用统计学_数据科学家必知的统计数据

面向数据科学家的实用统计学Beginners usually ignore most foundational statistical knowledge. To understand different models, and various techniques better, these concepts are essential. These work as baseline knowledge for various concepts involved in data …

字符串、指针、引用、数组基础

1.字符串:字符是由单引号所括住的单个字母、数字或符号。若将单引号改为双引号,该字符就会变成字符串。它们之间主要的差别是:双引号的字符串“A”会比单引号的字符串’A’在字符串的最后补上一个结束符’\0’(Null字符&#xff0…

suse安装php,SUSE下安装LAMP

安装Apache可以看到编译安装Apache出错,rpm包安装gcc (首先要安装GCC)makemake install修改apache端口cd /home/sxit/apache2vi conf/httpd.confListen 8000启动 apache/home/root/apache2/bin/apachectl start(stop restart)http://localhost:8000安装一下PHP开发…

自己动手写事件总线(EventBus)

2019独角兽企业重金招聘Python工程师标准>>> 本文由云社区发表 事件总线核心逻辑的实现。 <!--more--> EventBus的作用 Android中存在各种通信场景&#xff0c;如Activity之间的跳转&#xff0c;Activity与Fragment以及其他组件之间的交互&#xff0c;以及在某…

viz::viz3d报错_我可以在Excel中获得该Viz吗?

viz::viz3d报错Have you ever found yourself in the following situation?您是否遇到以下情况&#xff1f; Your team has been preparing and working tireless hours to create and showcase the end product — an interactive visual dashboard. It’s a culmination of…

php 数组合并字符,PHP将字符串或数组合并到一个数组内方法

本文主要和大家分享PHP将字符串或数组合并到一个数组内方法&#xff0c;有两种方法&#xff0c;希望希望能帮助到大家。一般写法&#xff1a;<?php /*** add a string or an array to another array** param array|string $val* param array $array*/function add_val_to_a…

xcode 4 最低的要求是 10.6.6的版本,如果你是 10.6.3的版本,又不想升级的话。可以考虑通过修改版本号的方法进行安装

xcode 4 最低的要求是 10.6.6的版本&#xff0c;如果你是 10.6.3的版本&#xff0c;又不想升级的话。可以考虑通过修改版本号的方法进行安装。 一、打开控制台&#xff1b; 二、使用root用户&#xff1b; 命令&#xff1a;sudo -s 之后输入密码即可 三、编辑 /System/Library/C…

android 调试技巧

1.查看当前堆栈 Call tree new Exception(“print trace”).printStackTrace(); &#xff08;在logcat中打印当前函数调用关系&#xff09; 2.MethodTracing 性能分析与优&#xff08; 函数占用CPU时间&#xff0c; 调用次数&#xff0c; 函数调用关系&#xff09; a) 在程序…

Xml序列化

xml序列化 实现思路 通过程序生成一个xml文件来备份手机短信. 先获取手机短信的内容 —>通过xml备份.StringBuffer 代码如下public void click(View view) {StringBuffer sb new StringBuffer();sb.append("<?xml version\"1.0\" encoding\"UTF-8\…

java 添加用户 数据库,跟屌丝学DB2 第二课 建立数据库以及添加用户

在安装DB2 之后&#xff0c;就可以在 DB2 环境中创建自己的数据库。首先考虑数据库应该使用哪个实例。实例(instance) 提供一个由数据库管理配置(DBM CFG)文件控制的逻辑层&#xff0c;可以在这里将多个数据库分组在一起。DBM CFG 文件包含一组 DBM CFG 参数&#xff0c;可以使…

iphone视频教程

公开课介绍 本课程共28集 翻译至第15集 网易正在翻译16-28集 敬请关注 返回公开课首页 一键分享&#xff1a;  网易微博开心网豆瓣网新浪微博搜狐微博腾讯微博邮件 讲师介绍 名称&#xff1a;Alan Cannistraro 课程介绍 如果你对iPhone Development有兴趣&#xff0c;以下是入…

在Python中有效使用JSON的4个技巧

Python has two data types that, together, form the perfect tool for working with JSON: dictionaries and lists. Lets explore how to:Python有两种数据类型&#xff0c;它们一起构成了使用JSON的理想工具&#xff1a; 字典和列表 。 让我们探索如何&#xff1a; load a…

Vlan中Trunk接口配置

Vlan中Trunk接口配置 参考文献&#xff1a;HCNA网络技术实验指南 模拟器&#xff1a;eNSP 实验环境&#xff1a; 实验目的&#xff1a;掌握Trunk端口配置 掌握Trunk端口允许所有Vlan配置方法 掌握Trunk端口允许特定Vlan配置方法 实验拓扑&#xff1a; 实验IP地址 &#xff1a;…

django中的admin组件

Admin简介&#xff1a; Admin:是django的后台 管理的wed版本 我们现在models.py文件里面建几张表&#xff1a; class Author(models.Model):nid models.AutoField(primary_keyTrue)namemodels.CharField( max_length32)agemodels.IntegerField()# 与AuthorDetail建立一对一的关…