首页 > 代码库 > Struts2+Spring3.1+Hibernate3.3的整个项目
Struts2+Spring3.1+Hibernate3.3的整个项目
经过一天的折腾,终于在MyEclipse2013下搭建出一个Struts2+Spring3.1+Hibernate3.3整合的项目,具体过程如下,仅供新手学习,大神勿喷
首先新建Web项目:
直接finish完成即可!
加入Spring支持:
弹出如下窗口:
此处将Spring配置文件的名称改为applicationContext-hibernate.xml用来专门管理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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> </beans>
直接点击Finish完成即可!至此Spring支持成功加入了!
加入Struts2的支持:
此处需要注意的是在加Spring支持的时候,已经加入了antlr-2.7.6.jar的包,所以此处要勾选高级选项,将antlr-2.7.2.jar的复选框取消,点击finish完成即可!至此Struts2的支持加好!
最后一步:加入Hibernate支持:
在加Hibernate支持之前,先要配置好数据库的链接:
此界面找不到的朋友还请自行百度查询!
此处1的地方要勾选上!
2的地方就是将Hibernate交给Spring管理!
3的
选择刚才创建好的数据库链接!
点击finish完成即可!
此时各配置文件配置如下:
applicationContext-hibernate.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com.ssh.dao.impl,com.ssh.service.impl"></context:component-scan> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="http://www.mamicode.com/com.mysql.jdbc.Driver"></property> <property name="url" value="http://www.mamicode.com/jdbc:mysql://localhost:3306/ssh"></property> <property name="username" value="http://www.mamicode.com/root"></property> <property name="password" value="http://www.mamicode.com/admin"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> </props> </property> <property name="mappingResources"> <list> <value>com/ssh/pojo/SysRole.hbm.xml</value> <value>com/ssh/pojo/SysUser.hbm.xml</value></list> </property> </bean> </beans>
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> </struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>SSH</display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-*.xml</param-value> </context-param> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> </web-app>
框架支持加完之后,下面正式进入登录功能的开发!
数据库中我预先准备了一些数据,SQL代码如下:
/* Navicat MySQL Data Transfer Source Server : 本机服务器 Source Server Version : 50533 Source Host : localhost:3306 Source Database : platform Target Server Type : MYSQL Target Server Version : 50533 File Encoding : 65001 Date: 2014-05-09 20:12:53 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for sys_role -- ---------------------------- DROP TABLE IF EXISTS `sys_role`; CREATE TABLE `sys_role` ( `id` int(11) NOT NULL AUTO_INCREMENT, `roleName` varchar(255) NOT NULL COMMENT ‘角色名称‘, `depart` int(11) DEFAULT NULL COMMENT ‘权限(部门)‘, `roleNum` varchar(2000) DEFAULT NULL COMMENT ‘角色权限‘, `field1` varchar(255) DEFAULT NULL COMMENT ‘备用字段1‘, `field2` varchar(255) DEFAULT NULL COMMENT ‘备用字段2‘, `field3` varchar(255) DEFAULT NULL COMMENT ‘备用字段3‘, `field4` varchar(255) DEFAULT NULL COMMENT ‘备用字段4‘, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of sys_role -- ---------------------------- -- ---------------------------- -- Table structure for sys_user -- ---------------------------- DROP TABLE IF EXISTS `sys_user`; CREATE TABLE `sys_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userName` varchar(255) NOT NULL COMMENT ‘用户名‘, `userPwd` varchar(255) NOT NULL COMMENT ‘密码‘, `userNike` varchar(255) NOT NULL COMMENT ‘用户昵称‘, `userMail` varchar(255) DEFAULT NULL COMMENT ‘邮箱‘, `userPhone` varchar(255) DEFAULT NULL COMMENT ‘手机‘, `userTel` varchar(255) DEFAULT NULL COMMENT ‘电话‘, `userLastTime` varchar(255) DEFAULT NULL COMMENT ‘最后登录时间‘, `userLastIp` varchar(255) DEFAULT NULL COMMENT ‘最后登录IP‘, `userCompany` varchar(255) DEFAULT NULL COMMENT ‘单位名称‘, `roleid` int(11) DEFAULT NULL, `field1` varchar(255) DEFAULT NULL COMMENT ‘备用字段1‘, `field2` varchar(255) DEFAULT NULL COMMENT ‘备用字段2‘, `field3` varchar(255) DEFAULT NULL COMMENT ‘备用字段3‘, `field4` varchar(255) DEFAULT NULL COMMENT ‘备用字段4‘, PRIMARY KEY (`id`), KEY `roleid` (`roleid`), CONSTRAINT `sys_user_ibfk_1` FOREIGN KEY (`roleid`) REFERENCES `sys_role` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of sys_user -- ---------------------------- INSERT INTO `sys_user` VALUES (‘1‘, ‘admin‘, ‘admin‘, ‘管理员‘, null, null, null, null, null, null, null, null, null, null, null);
通过Hibernate生产映射:
生成映射之后,在src目录下会有如下的包:
SysRole.java
package com.ssh.pojo; @SuppressWarnings("serial") public class SysRole implements java.io.Serializable { private Integer id; private String roleName; private Integer depart; private String roleNum; private String field1; private String field2; private String field3; private String field4; public SysRole() { } public SysRole(String roleName) { this.roleName = roleName; } public SysRole(String roleName, Integer depart, String roleNum, String field1, String field2, String field3, String field4) { this.roleName = roleName; this.depart = depart; this.roleNum = roleNum; this.field1 = field1; this.field2 = field2; this.field3 = field3; this.field4 = field4; } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getRoleName() { return this.roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } public Integer getDepart() { return this.depart; } public void setDepart(Integer depart) { this.depart = depart; } public String getRoleNum() { return this.roleNum; } public void setRoleNum(String roleNum) { this.roleNum = roleNum; } public String getField1() { return this.field1; } public void setField1(String field1) { this.field1 = field1; } public String getField2() { return this.field2; } public void setField2(String field2) { this.field2 = field2; } public String getField3() { return this.field3; } public void setField3(String field3) { this.field3 = field3; } public String getField4() { return this.field4; } public void setField4(String field4) { this.field4 = field4; } }
SysUser.java
package com.ssh.pojo; @SuppressWarnings("serial") public class SysUser implements java.io.Serializable { private Integer id; private SysRole sysRole; private String userName; private String userPwd; private String userNike; private String userMail; private String userPhone; private String userTel; private String userLastTime; private String userLastIp; private String userCompany; private String field1; private String field2; private String field3; private String field4; public SysUser() { } public SysUser(String userName, String userPwd, String userNike) { this.userName = userName; this.userPwd = userPwd; this.userNike = userNike; } public SysUser(SysRole sysRole, String userName, String userPwd, String userNike, String userMail, String userPhone, String userTel, String userLastTime, String userLastIp, String userCompany, String field1, String field2, String field3, String field4) { this.sysRole = sysRole; this.userName = userName; this.userPwd = userPwd; this.userNike = userNike; this.userMail = userMail; this.userPhone = userPhone; this.userTel = userTel; this.userLastTime = userLastTime; this.userLastIp = userLastIp; this.userCompany = userCompany; this.field1 = field1; this.field2 = field2; this.field3 = field3; this.field4 = field4; } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public SysRole getSysRole() { return this.sysRole; } public void setSysRole(SysRole sysRole) { this.sysRole = sysRole; } public String getUserName() { return this.userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPwd() { return this.userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } public String getUserNike() { return this.userNike; } public void setUserNike(String userNike) { this.userNike = userNike; } public String getUserMail() { return this.userMail; } public void setUserMail(String userMail) { this.userMail = userMail; } public String getUserPhone() { return this.userPhone; } public void setUserPhone(String userPhone) { this.userPhone = userPhone; } public String getUserTel() { return this.userTel; } public void setUserTel(String userTel) { this.userTel = userTel; } public String getUserLastTime() { return this.userLastTime; } public void setUserLastTime(String userLastTime) { this.userLastTime = userLastTime; } public String getUserLastIp() { return this.userLastIp; } public void setUserLastIp(String userLastIp) { this.userLastIp = userLastIp; } public String getUserCompany() { return this.userCompany; } public void setUserCompany(String userCompany) { this.userCompany = userCompany; } public String getField1() { return this.field1; } public void setField1(String field1) { this.field1 = field1; } public String getField2() { return this.field2; } public void setField2(String field2) { this.field2 = field2; } public String getField3() { return this.field3; } public void setField3(String field3) { this.field3 = field3; } public String getField4() { return this.field4; } public void setField4(String field4) { this.field4 = field4; } }
applicationContext-hibernate多出如下代码,用于让项目启动时加载到实体类
<property name="mappingResources"> <list> <value>com/ssh/pojo/SysRole.hbm.xml</value> <value>com/ssh/pojo/SysUser.hbm.xml</value></list> </property>
SysUserDAO.java
package com.ssh.dao; public interface SysUserDAO <K ,V>{ /** * 登录方法 * @param userName 用户名 * @param passWord 密码 * @return */ public V login(String userName,String passWord); }
在DAOImpl中要注入HibernateTemplate
因此ApplicationContext-hibernate.xml加入如下配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com.ssh.dao.impl,com.ssh.service.impl"></context:component-scan> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="http://www.mamicode.com/com.mysql.jdbc.Driver"></property> <property name="url" value="http://www.mamicode.com/jdbc:mysql://localhost:3306/ssh"></property> <property name="username" value="http://www.mamicode.com/root"></property> <property name="password" value="http://www.mamicode.com/admin"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> </props> </property> <property name="mappingResources"> <list> <value>com/ssh/pojo/SysRole.hbm.xml</value> <value>com/ssh/pojo/SysUser.hbm.xml</value></list> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> </beans>
SysUserDAOImpl.java
package com.ssh.dao.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.ssh.dao.SysUserDAO; import com.ssh.pojo.SysUser; public class SysUserDAOImpl extends HibernateDaoSupport implements SysUserDAO<Integer, SysUser> { @Autowired public SysUserDAOImpl(HibernateTemplate hibernateTemplate){ super.setHibernateTemplate(hibernateTemplate); } /** * 登录 */ @SuppressWarnings("unchecked") public SysUser login(String userName, String passWord) { String hql = "FROM SysUser where userName = ? and userPwd = ?"; List<SysUser> list = super.getHibernateTemplate().find(hql , userName , passWord); if(list.size() > 0){ return list.get(0); }else{ return null; } } }
SysUserService.java
package com.ssh.service; import com.ssh.pojo.SysUser; public interface SysUserService { /** * 登录 * @param userName 用户名 * @param passWord 密码 * @return */ public SysUser login(String userName , String passWord); }
SysUserServiceImpl.java
package com.ssh.service.impl; import com.ssh.dao.SysUserDAO; import com.ssh.pojo.SysUser; import com.ssh.service.SysUserService; public class SysUserServiceImpl implements SysUserService { private SysUserDAO<Integer, SysUser> sysUserDAO; public SysUser login(String userName, String passWord) { return sysUserDAO.login(userName, passWord); } //通过set方法注入dao public void setSysUserDAO(SysUserDAO<Integer, SysUser> sysUserDAO) { this.sysUserDAO = sysUserDAO; } }
加入两个新的配置用来注入DAO和Service
applicationContext-action.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id="login" class="com.ssh.action.UserAction"> <property name="sysUserService" ref="sysUserService"></property> </bean> </beans>
applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!-- 首字母不要大写,会报错 --> <bean id="sysUserDAO" class="com.ssh.dao.impl.SysUserDAOImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="sysUserService" class="com.ssh.service.impl.SysUserServiceImpl"> <property name="sysUserDAO" ref="sysUserDAO"></property> </bean> </beans>
编写Action:
UserAction.java
package com.ssh.action; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; import com.ssh.pojo.SysUser; import com.ssh.service.SysUserService; @SuppressWarnings("serial") public class UserAction extends ActionSupport{ private HttpServletRequest request = ServletActionContext.getRequest(); private String userName; private String passWord; private SysUserService sysUserService; public String login(){ SysUser user = sysUserService.login(userName, passWord); if(user != null){ request.getSession().setAttribute("user", user); //登录成功之后将登录用户放入session return "success"; }else{ return "input"; //返回错误页 } } public void setSysUserService(SysUserService sysUserService) { this.sysUserService = sysUserService; } 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 HttpServletRequest getRequest() { return request; } public void setRequest(HttpServletRequest request) { this.request = request; } }
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="http://www.mamicode.com/"> <title>登录</title> </head> <body> <form action="login.action" method="post"> 用户名:<input name="userName" type="text" /> 密码:<input name="passWord" type="password" /> <input type="submit" value="http://www.mamicode.com/login" /> </form> </body> </html>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="http://www.mamicode.com/"> </head> <body> 欢迎${user.userNike}登录!!!<br> </body> </html>
本文出自 “小理子” 博客,请务必保留此出处http://menglihy.blog.51cto.com/6979810/1409147