java ssh做单表crud_ssh2 最简 crud tutorial

ssh2 最简 crud tutorial

2010-05-30 12:06

做一个最简ssh2组合的crud操作例子步骤:

1、环境:MyEclipse8.5, MySQL5

2、数据库建表及连接:

在数据库test里建表account

字段:id(int,主键,自增)

username(varchar(50))

password(varchar(50))

切换到 MyEclipse Hibernate 布局

右击视图 DB Browser空白处 选 New...

出现 Database Driver 对话框

Driver template: 选 MySQL Contector/J

Driver name: 可随便写: MySQL5

User name: root

Password: 111111

点按钮 Add JARs 加入 mysql-con-java.jar

如果没有这个文件可以去下载:

http://www.mysql.com/downloads/connector/j/

勾上 Save password

点按钮 Test Driver 看是否通过

点按钮 Finish

3、建立 Web 项目

切换到 MyEclipse Java Enterprise 布局

右击视图 Package Explorer 空白处 选 New > Web Project

出现New Web Project 对话框

Project Name: ssh2demo

勾上 Add Marven support

点按钮 Finish

出现提示:Incompatible Java Compliance Level

点按钮 Yes

完成建立 ssh2demo 项目

4、 导入 ssh2 需要的包

右击 ssh2demo 选 MyEclipse > Add Struts Capabilities...

点选 Struts2.1

点选 /*

点按钮 Next >

勾上 Struts 2 Spring Libraries

点按钮 Finish

右击 ssh2demo 选 MyEclipse > Add Spring Capabilities...

点按钮 Finish

右击 ssh2demo 选 MyEclipse > Add Hibernate Capabilities...

勾上 Spring 3.0 Persistence JDBC Libraries

点按钮 Next >

点选 Spring configuration file (applicationContext.xml)

点按钮 Next >

点选 Existing Spring configuration file

点按钮 Next >

DB Driver: 选 MySQL5 (这个是在步骤2里面建立的数据库连接)

点按钮 Next >

取消勾选 Create SessionFactory class

点按钮 Finish

5、写代码构建项目

修改 WebRoot/WEB-INF/web.xml

在 web-app 元素下加入:

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

位置不限,这个例子里是放在 welcome-file-list 之前。

保存并关闭 web.xml

右击 ssh2demo 选 Run As > MyEclipse Server Application

确认可以正确显示 index.jsp,关掉 MyEclipse Web Browser 视图

修改 WebRoot/index.jsp

在 This is my JSP page.
后加入:

Go Struts.

保存并关闭 index.jsp

右击 WebRoot 选 New > JSP (Advanced Templates)

出现 Create a new JSP page. 对话框

Template to use: 选 Just as HTML

点按钮 Finish

给 MyJsp.jsp 加入 Struts 标签库

在第二行空白处输入:

给 MyJsp.jsp 加入 CRUD 动作必须要素:

返回信息,提交表单,数据列表,编辑和删除的超链接

删除 This is my JSP page.

添加:

${message}

${id}${username}${password}编辑删除

保存并关闭 MyJsp.jsp

右击 src 选 New > Package

Name: 输入 cn.demo

点按钮 Finish

添加 实体类和DAO类:

切换到 MyEclipse Hibernate 布局

在 DB Browswer 视图中:

双击 MySQL5 打开数据库连接

展开 MySQL5 > Connected to MySQL5 > test > TABLE

右击 account 选 Hibernate Reverse Engineering...

出现 Hibernate Reverse Engineering 对话框

Java src folder: 点按钮 Browser...

选中 /ssh2demo/src

Java package: 点按钮 Browser...

选中 cn.demo

勾上 Create POJO<>DB Table mapping information

选中 Create a Hibernate mapping file (*.hbm.xml) for each database table

勾上 Update Hibernate configuration with mapping resource location

勾上 Java Data Object (POJO<>DB Table)

勾上 Java Data Access Object (DAO) (Hibernate 3 only)

选中 Spring DAO

点按钮 Finish

出现提示:Confirm Overwrite,点按钮 OK

切换到 MyEclipse Java Enterprise 布局

右击 src/cn.demo 选 New > Class

出现 New Java Class 对话框

Name: 输入 HelloAction

点按钮 Finish

双击 HelloAction.java

用以下内容替换原内容:

package cn.demo;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport {

private String message;

private Account account;

private AccountDAO accountDao;

private List accounts;

private int accountId;

public String getMessage() {

return message;

}

public void setMessage(String message) {

this.message = message;

}

public Account getAccount() {

return account;

}

public void setAccount(Account account) {

this.account = account;

}

public AccountDAO getAccountDao() {

return accountDao;

}

public void setAccountDao(AccountDAO accountDao) {

this.accountDao = accountDao;

}

public List getAccounts() {

return accounts;

}

public void setAccounts(List accounts) {

this.accounts = accounts;

}

public int getAccountId() {

return accountId;

}

public void setAccountId(int accountId) {

this.accountId = accountId;

}

public String execute() throws Exception {

accounts = accountDao.findAll();

return SUCCESS;

}

public String save() throws Exception {

if (null == account.getId()){

accountDao.save(account);

account = null;

message = "Create done!";

}

else{

accountDao.merge(account);

message = "Update done!";

}

return NONE;

}

public String edit() throws Exception {

account = accountDao.findById(getAccountId());

return SUCCESS;

}

public String delete() throws Exception {

account = accountDao.findById(getAccountId());

if (null != account){

accountDao.delete(account);

account = null;

}

message = "Delete done!";

return NONE;

}

}

保存并关闭 HelloAction.java

双击 src/applicationContext.xml

在 前面添加如下内容:

p:message="Retrieve done!">

保存并关闭 applicationContext.xml

双击 src/struts.xml

在 前面添加如下内容:

/MyJsp.jsp

hello

保存 struts.xml

点工具栏按钮:绿圆里有白三角(Run ssh2demo on MyEclipse Tomcat)

看到最终效果:可以进行 新增、查看、修改、删除 四个动作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值