对于Java初学者而言,SSH框架还是比较复杂的,今天借用一个Web注册功能的案例给大家讲解下,主要是讲下开发模式与注意事项!
注册界面如下所示:
1.首先建库建表(我用的是Mysql数据库,大家可以参考其他关系型DB,在SSH框架中集成对应驱动访问即可)
/*
在Mysql的test库中执行如下创建表SQL语句
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(16) NOT NULL,
`password` varchar(16) NOT NULL,
`sex` int(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
2.创建Dynamic Web Project(我用的是Eclipse J2EE版本,大家也可以使用MyEclipse Java IDE开发环境)
3.将SSH框架中lib库及DB、日志相关库(jar包)拷贝至工程中lib目录
(我这里使用的Struts2 + Spring3 + Hibernate3集成框架)
备注:无需在Libraries中Add新增lib库,工程会自动加载
4.将struts.xml拷贝至src目录下(对应在classes目录也会生成该文件),将applicationContext.xml及web.xml拷贝至WEB-INF目录下
备注:这里路径很容易弄错,需要注意,因为排查起来比较麻烦
5.对工程结构分层:持久层 <- DAO层 <- 业务层 <- Web层(展现层),如下所示:
6.接下来就是逐层实现代码的过程(时间关系,直接上代码与注释吧):
(1)持久层
User.java
package hexieshangwang.ssh.beans;
/**
* @author:hexieshangwang
* @version 1.0
* @parameter
* @since
* @return
* @description:在持久层建立po与hbm.xml映射关系
*/
public class User {
private int id;
private String name;
private String password;
private int sex;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
}
User.hbm.xml
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="hexieshangwang.ssh.beans">
<class name="User" table="user">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name" type="java.lang.String"
not-null="true" length="16"></property>
<property name="password" column="password" type="java.lang.String"
not-null="true" length="16" />
<property name="sex" column="sex" type="java.lang.Integer"
length="1" />
</class>
</hibernate-mapping>
(2)DAO层
BaseDao.java
package hexieshangwang.ssh.dao;
import org.hibernate.HibernateException;
/**
* @author:hexieshangwang
* @version 1.0
* @parameter
* @since
* @return
* @description:在DAO层提供访问持久层(Hibernate)接口
*/
public interface BaseDao {
public void saveObject(Object obj) throws HibernateException;
}
UserDao.java
package hexieshangwang.ssh.daoImpl;
import org.hibernate.HibernateException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import hexieshangwang.ssh.dao.BaseDao;
/**
* @author:hexieshangwang
* @version 1.0
* @parameter
* @since
* @return
* @description:实现DAO层接口
*/
public class UserDao extends HibernateDaoSupport implements BaseDao{
@Override
public void saveObject(Object obj) throws HibernateException {
// TODO Auto-generated method stub
getHibernateTemplate().save(obj);
}
}
(3)业务层
UserService.java
package hexieshangwang.ssh.service;
import org.hibernate.HibernateException;
import hexieshangwang.ssh.forms.UserForm;
/**
* @author:hexieshangwang
* @version 1.0
* @parameter
* @since
* @return
* @description:在业务层提供访问DAO层Service接口
*/
public interface UserService {
public void regUser(UserForm user) throws HibernateException;
}
UserServiceImpl.java
package hexieshangwang.ssh.serviceImpl;
import org.hibernate.HibernateException;
import org.springframework.beans.BeanUtils;
import hexieshangwang.ssh.beans.User;
import hexieshangwang.ssh.dao.BaseDao;
import hexieshangwang.ssh.forms.UserForm;
import hexieshangwang.ssh.service.UserService;
/**
* @author:hexieshangwang
* @version 1.0
* @parameter
* @since
* @return
* @description:实现业务层Service接口
*/
public class UserServiceImpl implements UserService{
private BaseDao dao;
public void setDao(BaseDao dao) {
this.dao = dao;
}
@Override
public void regUser(UserForm userForm) throws HibernateException {
// TODO Auto-generated method stub
User user = new User();
BeanUtils.copyProperties(userForm, user);
dao.saveObject(user);
}
}
applicationContext.xml
备注:此处在Spring框架中整合了Hibernate配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 定义数据源的信息 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="jdbcUrl">
<value>jdbc:mysql://IP:3306/test</value>
</property>
<property name="user">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>
<property name="maxPoolSize">
<value>80</value>
</property>
<property name="minPoolSize">
<value>1</value>
</property>
<property name="initialPoolSize">
<value>1</value>
</property>
<property name="maxIdleTime">
<value>20</value>
</property>
</bean>
<!--定义Hibernate的SessionFactory -->
<!-- SessionFactory使用的数据源为上面的数据源 -->
<!-- 指定了Hibernate的映射文件和配置信息 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>hexieshangwang/ssh/beans/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="baseDao" class="hexieshangwang.ssh.daoImpl.UserDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!--用户注册业务逻辑类 -->
<bean id="userService" class="hexieshangwang.ssh.serviceImpl.UserServiceImpl">
<property name="dao">
<ref bean="baseDao" />
</property>
</bean>
<!-- 用户注册的Action -->
<bean id="regAction" class="hexieshangwang.ssh.action.RegisterAction">
<property name="userService">
<ref bean="userService" />
</property>
</bean>
<!-- more bean definitions go here -->
</beans>
(4)Web层
UserForm.java
package hexieshangwang.ssh.forms;
/**
* @author:hexieshangwang
* @version 1.0
* @parameter
* @since
* @return
* @description:在Web层实现表单Form
*/
public class UserForm {
private String name;
private String password;
private int sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
}
RegisterAction.java
package hexieshangwang.ssh.action;
import com.opensymphony.xwork2.ActionSupport;
import hexieshangwang.ssh.forms.UserForm;
import hexieshangwang.ssh.service.UserService;
/**
* @author:hexieshangwang
* @version 1.0
* @parameter
* @since
* @return
* @description:在Web层实现Action界面操作
*/
public class RegisterAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private UserForm user;
private UserService userService;
public UserForm getUser() {
return user;
}
public void setUser(UserForm user) {
this.user = user;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public String execute() {
try {
userService.regUser(user);
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml" />
<package name="hexieshangwang" extends="struts-default">
<action name="register" class="regAction">
<result name="submit">/Register.jsp</result>
<result name="success">/Success.jsp</result>
<result name="error">/Fail.jsp</result>
</action>
</package>
</struts>