首页 > 代码库 > SSH整合——架构的历史(二)
SSH整合——架构的历史(二)
N层结构+Entity+Service+Impl+Hibernate+DAO+Impl
(1)将Service业务逻辑层抽象成一个接口
package com.zgy.registration.service;
import java.sql.SQLException;
import com.zgy.registration.model.User;
public interface UserManager {
public abstract boolean exists(User u) throws SQLException,
ClassNotFoundException;
public abstract void add(User u);
}
(2)Service层的实现UserManagerImpl
package com.zgy.registration.serviceImpl;
import java.sql.SQLException;
import com.zgy.registration.dao.UserDao;
import com.zgy.registration.daoImpl.UserDaoImpl;
import com.zgy.registration.model.User;
import com.zgy.registration.service.UserManager;
public class UserManagerImpl implements UserManager {
private UserDao userDao = new UserDaoImpl();
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
/* (non-Javadoc)
* @see com.zgy.registration.serviceImpl.UserManager#exists(com.zgy.registration.model.User)
*/
@Override
public boolean exists(User u) throws SQLException, ClassNotFoundException {
return userDao.checkUserExistsWithName(u.getUsername());
}
/* (non-Javadoc)
* @see com.zgy.registration.serviceImpl.UserManager#add(com.zgy.registration.model.User)
*/
@Override
public void add(User u) {
userDao.save(u);
}
}
(3)DAO层UserDao,封装数据库的操作
package com.zgy.registration.dao;
import com.zgy.registration.model.User;
public interface UserDao {
public void save(User u);
public boolean checkUserExistsWithName(String username);
}
(4)DAO的实现层UserDaoImpl,封装具体的数据库的实现
package com.zgy.registration.daoImpl;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import com.zgy.registration.dao.UserDao;
import com.zgy.registration.model.User;
import com.zgy.registration.util.HibernateUtil;
public class UserDaoImpl implements UserDao {
@Override
public void save(User u) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.getCurrentSession();
session.beginTransaction();
session.save(u);
session.getTransaction().commit();
}
@Override
public boolean checkUserExistsWithName(String username) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.getCurrentSession();
session.beginTransaction();
long count = (Long) session
.createQuery(
"select count(*) from User user "
+ "where user.username= :username")
.setString("username", username).uniqueResult();
session.getTransaction().commit();
if (count > 0) {
return true;
}
return false;
}
}
(5)修改测试类
package com.zgy.registration.service;
import java.sql.SQLException;
import junit.framework.Assert;
import org.junit.Test;
import com.zgy.registration.model.User;
import com.zgy.registration.serviceImpl.UserManagerImpl;
public class TestUserManager {
@Test
public void testExists() throws SQLException, ClassNotFoundException {
UserManager um = new UserManagerImpl();
User u = new User();
u.setUsername("a");
boolean exist = um.exists(u);
Assert.assertEquals(true, exist);
}
@Test
public void testAdd() throws SQLException, ClassNotFoundException {
UserManager um = new UserManagerImpl();
User u = new User();
u.setUsername("e");
u.setPassword("e");
boolean exist = um.exists(u);
if(!exist){
um.add(u);
Assert.assertEquals(true, um.exists(u));
}else{
Assert.fail("not added");
}
}
}
(6)数据存储成功。
(7)加入Struts,引入MVC的概念,使用Struts充当Controller角色,将业务层和View层分离
E、N层结构+Entity+Service+Impl+Hibernate+DAO+Impl+struts
(1)加入UserAction
package com.zgy.registration.action;
import com.zgy.registration.model.User;
import com.zgy.registration.service.UserManager;
import com.zgy.registration.serviceImpl.UserManagerImpl;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private String username;
private String password;
private String password2;
UserManager um = new UserManagerImpl();
public UserManager getUm() {
return um;
}
public void setUm(UserManager um) {
this.um = um;
}
@Override
public String execute() throws Exception {
User u = new User();
u.setUsername(username);
u.setPassword(password);
if(um.exists(u)) {
return "fail";
}
um.add(u);
return "success";
}
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;
}
public String getPassword2() {
return password2;
}
public void setPassword2(String password2) {
this.password2 = password2;
}
}
(2)配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="registration" extends="struts-default">
<action name="user" class="com.zgy.registration.action.UserAction">
<result name="success">/registerSuccess.jsp</result>
<result name="fail">/registerFail.jsp</result>
</action>
</package>
</struts>
SSH整合——架构的历史(二)