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./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 元素下加入:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
位置不限,这个例子里是放在 welcome-file-list 之前。
保存并关闭 web.xml
右击 ssh2demo 选 Run As > MyEclipse Server Application
确认可以正确显示 index.jsp,关掉 MyEclipse Web Browser 视图
修改 WebRoot/index.jsp
在 This is my JSP page. <br>后加入:
<a href="hello">Go Struts.</a>
保存并关闭 index.jsp
右击 WebRoot 选 New > JSP (Advanced Templates)
出现 Create a new JSP page. 对话框
Template to use: 选 Just as HTML
点按钮 Finish
给 MyJsp.jsp 加入 Struts 标签库
在第二行空白处输入:
<%@ taglib prefix="s" uri="/struts-tags" %>
给 MyJsp.jsp 加入 CRUD 动作必须要素:
返回信息,提交表单,数据列表,编辑和删除的超链接
删除 This is my JSP page. <br>
添加:
<h1>${message}</h1>
<s:form action="hello!save">
<s:textfield name="account.username" label="用户名"/>
<s:textfield name="account.password" label="密码"/>
<s:hidden name="account.id"/>
<s:submit/>
</s:form>
<table>
<s:iterator value="accounts">
<tr>
<td>${id}</td>
<td>${username}</td>
<td>${password}</td>
<td><a href='hello!edit?accountId=${id}'>编辑</a></td>
<td><a href='hello!delete?accountId=${id}'>删除</a></td>
</tr>
</s:iterator>
</table>
保存并关闭 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
在 </beans> 前面添加如下内容:
<bean id="helloAction" class="cn.demo.HelloAction"
p:message="Retrieve done!">
<property name="accountDao">
<ref bean="AccountDAO"/>
</property>
</bean>
保存并关闭 applicationContext.xml
双击 src/struts.xml
在 </struts> 前面添加如下内容:
<package name="asdf" extends="struts-default">
<action name="hello" class="helloAction">
<result>/MyJsp.jsp</result>
<result name="none" type="redirectAction">hello</result>
</action>
</package>
保存 struts.xml
点工具栏按钮:绿圆里有白三角(Run ssh2demo on MyEclipse Tomcat)
看到最终效果:可以进行 新增、查看、修改、删除 四个动作。
|