SSH博文写的也差不多了,这篇主要是通过做一个完整简单的小例子,来整合下前面学到的东西,顺便把后面写的两篇Maven和Ant也加入进来,如果SSH或Maven、Ant这些东西不懂,可先看其他文章。项目用Maven来管理jar,我后面会把pom.xml贴出来,供大家参考,Maven之外的有:
Ant:1.8.4;
Xdoclet:1.2.3;(个人喜欢1.0版本)
JDK:1.6;
Tomcat:6;
数据库:MySQL;
首先新建一个web Project(tgb_ssh),右击项目选中Configure—Convert to Maven Project,Finish!下面是pom.xml配置:
4.0.0
tgb_ssh
tgb_ssh
1.0-SNAPSHOT
war
src
maven-compiler-plugin
3.0
1.6
maven-war-plugin
2.3
WebContent
false
org.hibernate
hibernate
3.2.7.ga
xdoclet
xdoclet
1.2.3
org.springframework
spring-orm
3.0.0.RELEASE
org.springframework
spring-orm
3.0.0.RELEASE
org.springframework
spring-web
3.0.0.RELEASE
struts
struts
1.2.9
javax.servlet
servlet-api
2.5
org.springframework
spring-aop
3.0.0.RELEASE
org.aspectj
aspectjweaver
1.7.1
mysql
mysql-connector-java
5.0.2
org.springframework
spring-struts
3.0.0.RELEASE
java
java official repository
http://download.java.net/maven/2/
下面是我整个项目包结构的一张截图,后面提及的类及配置文件请对号入座:
自底而上,先创建数据库:test_ssh,在domain包下创建一个实体类User.java:
package com.tgb.domain;
/**
* 用户实体类
* @author yuanfubiao
*@hibernate.class table="t_user"
*
*/
public class User {
private int id;
private String username;
private String password;
/**
* @hibernate.id generator-class="native"
*/
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
/**
* @hibernate.property
*/
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
/**
* @hibernate.property
*/
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
然后创建build.xml,配置好后就可以生成Hibernate的配置文件和映射文件了,build.xml:
上面利用Ant+Xdoclet生成方式不多做介绍,可参看我后面的博客,生成的映射文件和配置文件如下:
hibernate.cfg.xml
org.hibernate.dialect.MySQLDialect
true
false
root
1
com.mysql.jdbc.Driver
jdbc:mysql://127.0.0.1/test_ssh
update
User.hbm.xml
完成domain层之后,在dao包下分别创建一个接口UserDao.java和实现类UserDaoImpl.java,如下:
UserDao.java
package com.tgb.dao;
import com.tgb.domain.User;
public interface UserDao {
public void addUser(User user);
public User findUserByName(String userName);
}
UserDaoImpl.java
package com.tgb.dao;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.tgb.domain.User;
public class UserDaoImpl extends HibernateDaoSupport implements UserDao{
@Override
public void addUser(User user) {
getHibernateTemplate().save(user);
}
@Override
public User findUserByName(String userName) {
return (User)getHibernateTemplate().load(User.class, userName);
}
}
接下来创建manager包下的接口UserManager.java和实现类UserManagerImpl.java,如下:
UserManager.java
package com.tgb.manager;
import com.tgb.domain.User;
public interface UserManager {
public void addUser(User user);
public User findUserByName(String userName);
}
UserManagerImpl.java
package com.tgb.manager;
import com.tgb.dao.UserDao;
import com.tgb.domain.User;
public class UserManagerImpl implements UserManager {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void addUser(User user) {
userDao.addUser(user);
}
@Override
public User findUserByName(String userName) {
return userDao.findUserByName(userName);
}
}
Web包下分别创建UserAction和UserActionForm两个类,如下:
UserActionForm.java
package com.tgb.web;
import org.apache.struts.action.ActionForm;
public class UserActionForm extends ActionForm {
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserAction.java
package com.tgb.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.tgb.domain.User;
import com.tgb.manager.UserManager;
public class UserAction extends Action {
private UserManager userManager;
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserActionForm uaf = (UserActionForm)form;
User user = new User();
user.setUsername(uaf.getUserName());
user.setPassword(uaf.getPassword());
userManager.addUser(user);
return mapping.findForward("success");
}
}
Java类到此为止,这篇有点多,下篇文章继续。