S2SH簡單例項註解版——登入功能實現
阿新 • • 發佈:2018-10-31
第一步:Spring4 整合 Hibernate4
- Spring4 接管 Hibernate4 所有 Bean 例項,以及 SessionFactory,事務管理器;
- 泛型注入;
Entity
package com.java1234.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table;import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="t_user") public class User { private Integer id; private String userName; private String password; @Id @GenericGenerator(name = "generator", strategy = "native") @GeneratedValue(generator = "generator") @Column(name = "id", length=11)public Integer getId() { return id; } public void setId(Integer id) { this.id = id; }
@Column(name = "userName", length = 20) public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; }
@Column(name= "password", length = 20) public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
泛型注入DAO介面層
package com.java1234.dao; import java.io.Serializable; import java.util.List; //基礎資料庫操作類 public interface BaseDao<T> { public Serializable save(T o); // 儲存一個物件 public void delete(T o); // 刪除一個物件 public void update(T o); // 更新一個物件 public void saveOrUpdate(T o); // 儲存或更新物件 public List<T> find(String hql); // 查詢 public List<T> find(String hql, Object[] param); // 查詢集合 public List<T> find(String hql, List<Object> param); // 查詢集合 public List<T> find(String hql, Object[] param, Integer page, Integer rows); // 查詢集合(帶分頁) public List<T> find(String hql, List<Object> param, Integer page, Integer rows); // 查詢集合(帶分頁) public T get(Class<T> c, Serializable id); // 獲得一個物件 public T get(String hql, Object[] param); // 獲得一個物件 public T get(String hql, List<Object> param); // 獲得一個物件 public Long count(String hql); // select count(*) from 類 public Long count(String hql, Object[] param); // select count(*) from 類 public Long count(String hql, List<Object> param); // select count(*) from 類 public Integer executeHql(String hql); // 執行HQL語句 public Integer executeHql(String hql, Object[] param); // 執行HQL語句 public Integer executeHql(String hql, List<Object> param); // 執行HQL語句 }
註解版DAO實現層
package com.java1234.dao.impl; import java.io.Serializable; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import com.java1234.dao.BaseDao; @Repository("baseDao") @SuppressWarnings("all") public class BaseDaOImpl<T> implements BaseDao<T> { private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } @Autowired public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } private Session getCurrentSession() { return sessionFactory.getCurrentSession(); } public Serializable save(T o) { return this.getCurrentSession().save(o); } public void delete(T o) { this.getCurrentSession().delete(o); } public void update(T o) { this.getCurrentSession().update(o); } public void saveOrUpdate(T o) { this.getCurrentSession().saveOrUpdate(o); } public List<T> find(String hql) { return this.getCurrentSession().createQuery(hql).list(); } public List<T> find(String hql, Object[] param) { Query q = this.getCurrentSession().createQuery(hql); if (param != null && param.length > 0) { for (int i = 0; i < param.length; i++) { q.setParameter(i, param[i]); } } return q.list(); } public List<T> find(String hql, List<Object> param) { Query q = this.getCurrentSession().createQuery(hql); if (param != null && param.size() > 0) { for (int i = 0; i < param.size(); i++) { q.setParameter(i, param.get(i)); } } return q.list(); } public List<T> find(String hql, Object[] param, Integer page, Integer rows) { if (page == null || page < 1) { page = 1; } if (rows == null || rows < 1) { rows = 10; } Query q = this.getCurrentSession().createQuery(hql); if (param != null && param.length > 0) { for (int i = 0; i < param.length; i++) { q.setParameter(i, param[i]); } } return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list(); } public List<T> find(String hql, List<Object> param, Integer page, Integer rows) { if (page == null || page < 1) { page = 1; } if (rows == null || rows < 1) { rows = 10; } Query q = this.getCurrentSession().createQuery(hql); if (param != null && param.size() > 0) { for (int i = 0; i < param.size(); i++) { q.setParameter(i, param.get(i)); } } return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list(); } public T get(Class<T> c, Serializable id) { return (T) this.getCurrentSession().get(c, id); } public T get(String hql, Object[] param) { List<T> l = this.find(hql, param); if (l != null && l.size() > 0) { return l.get(0); } else { return null; } } public T get(String hql, List<Object> param) { List<T> l = this.find(hql, param); if (l != null && l.size() > 0) { return l.get(0); } else { return null; } } public Long count(String hql) { return (Long) this.getCurrentSession().createQuery(hql).uniqueResult(); } public Long count(String hql, Object[] param) { Query q = this.getCurrentSession().createQuery(hql); if (param != null && param.length > 0) { for (int i = 0; i < param.length; i++) { q.setParameter(i, param[i]); } } return (Long) q.uniqueResult(); } public Long count(String hql, List<Object> param) { Query q = this.getCurrentSession().createQuery(hql); if (param != null && param.size() > 0) { for (int i = 0; i < param.size(); i++) { q.setParameter(i, param.get(i)); } } return (Long) q.uniqueResult(); } public Integer executeHql(String hql) { return this.getCurrentSession().createQuery(hql).executeUpdate(); } public Integer executeHql(String hql, Object[] param) { Query q = this.getCurrentSession().createQuery(hql); if (param != null && param.length > 0) { for (int i = 0; i < param.length; i++) { q.setParameter(i, param[i]); } } return q.executeUpdate(); } public Integer executeHql(String hql, List<Object> param) { Query q = this.getCurrentSession().createQuery(hql); if (param != null && param.size() > 0) { for (int i = 0; i < param.size(); i++) { q.setParameter(i, param.get(i)); } } return q.executeUpdate(); } }
Service介面層
package com.java1234.service; import java.util.List; import com.java1234.entity.User; public interface UserService { public void saveUser(User user); public void updateUser(User user); public User findUserById(int id); public void deleteUser(User user); public List<User> findAllList(); public User findUserByNameAndPassword(User user); }
Service實現層
package com.java1234.service.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.java1234.dao.BaseDao; import com.java1234.entity.User; import com.java1234.service.UserService; @Service("userService") public class UserServiceImpl implements UserService{ @Resource private BaseDao<User> baseDao; @Override public void saveUser(User user) { baseDao.save(user); } @Override public void updateUser(User user) { baseDao.update(user); } @Override public User findUserById(int id) { return baseDao.get(User.class, id); } @Override public void deleteUser(User user) { baseDao.delete(user); } @Override public List<User> findAllList() { return baseDao.find("from User"); } @Override public User findUserByNameAndPassword(User user) { return baseDao.get("from User u where u.userName=? and u.password=?", new Object[]{user.getUserName(),user.getPassword()}); } }
Action層
package com.java1234.action; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.interceptor.ServletRequestAware; import org.springframework.stereotype.Controller; import com.java1234.entity.User; import com.java1234.service.UserService; import com.opensymphony.xwork2.ActionSupport; @Controller public class UserAction extends ActionSupport implements ServletRequestAware{ private static final long serialVersionUID = 1L; private HttpServletRequest request; @Resource private UserService userService; private User user; private String error; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getError() { return error; } public void setError(String error) { this.error = error; } public String login()throws Exception{ HttpSession session=request.getSession(); User currentUser=userService.findUserByNameAndPassword(user); if(currentUser!=null){ session.setAttribute("currentUser", currentUser); return SUCCESS; }else{ error="用後名或者密碼錯誤!"; return ERROR; } }
@Override public void setServletRequest(HttpServletRequest request) { // TODO Auto-generated method stub this.request=request; } }
第二步:Spring4 整合 Struts2
- Spring4 接管 Struts2 所有 Bean 例項;
struts.xml 配置
<package name="s2sh" namespace="/user" extends="struts-default"> <action name="user_*" method="{1}" class="com.java1234.action.UserAction"> <result name="success">/success.jsp</result> <result name="error">/index.jsp</result> </action> </package>
第三步:前臺
index.jsp
<form action="${pageContext.request.contextPath }/user/user_login.action" method="post"> userName:<input type="text" name="user.userName" value="${user.userName }"/><br/> password:<input type="password" name="user.password" value="${user.password }"><br/> <input type="submit" value="login"/><font color="red">${error }</font> </form>
success.jsp
歡迎:${currentUser.userName }