ThinkBigWinBig 2014-11-24
SSH整合
最近没怎么手头上没什么事情,不过我感觉也是好事,毕竟一直忙碌刀会钝的。
所有最近收刀,静心磨刀,不为外面的什么各种项目所迷惑。收收心,毕竟磨刀不误砍柴工。总结下Spring整合Struts2和Hibernate。
上篇总结了Spring整合Struts2这次我把Hibernate加了进来,Spring这个非常强大的框架也是真真正正的算得上有个有艺术品位的框架,IOC和AOP完美结合,不仅仅可以用在WEB上面在其他的平台下运用也是非常的广泛。
下面开始做帖代码,这个例子实现了一个登陆和注册模块,用SSH做的一个简单的整合。
第一步建好一个web工程后添加jar包:
然后在WEB-INF下面创建applicationContext.xml用来配置Spring容器:
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 注册接口实现类 --> <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="datasource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQL5Dialect </prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="mappingResources"> <list> <value>/com/zdx/domain/User.hbm.xml</value> </list> </property> </bean> <bean id="userDao" class="com.zdx.dao.impl.UserDaoImpl"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="loginAction" class="com.zdx.action.LoginAction"> <property name="service" ref="userService"></property> </bean> <bean id="userService" class="com.zdx.service.UserService"> <property name="userDao" ref="userDao"></property> </bean> </beans>
2.修改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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Spring_Struts</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- listener是Servlet的监听器,他可以监听客户端的请求、服务器端的操作等, 通过监听器,可以自动激发一些操作,如监听到在线的数量。当增加一个HttpSession时, 就激发sessionCreated()方法。监听器需要知道application.xml配置文件的位置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
src下面创建struts.properties:
struts.objectFactory=spring
src下面创建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="default" extends="struts-default"> <action name="loginAction_*" class="loginAction" method="{1}"> <result name="success">/success.jsp</result> <result name="login">/index.jsp</result> <result name="registerError">/register.jsp</result> </action> </package> </struts>
创建User.java:
package com.zdx.domain; public class User { private Integer id; private String name; private String pwd; public User() { // TODO Auto-generated constructor stub } //get、set }
配置其hbm.xml如下:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping default-lazy="false"> <class name="com.zdx.domain.User" table="user"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <property name="pwd"></property> </class> </hibernate-mapping>
然后创建UserDao接口,实现类UserDaoImpl如下:
package com.zdx.dao.impl; import java.util.List; import org.hibernate.SessionFactory; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.zdx.dao.UserDao; import com.zdx.domain.User; public class UserDaoImpl extends HibernateDaoSupport implements UserDao { @Override public boolean insert(User user) { if (this.findByName(user.getName()) == null) { this.getHibernateTemplate().save(user); return true; } return false; } @Override public User findByName(String name) { User user = null; List<User> list = this.getHibernateTemplate().find( "from User where name=?", name); if (list.size() > 0) { user = list.get(0); } return user; } }
业务逻辑类UserService如下:
package com.zdx.service; import com.zdx.dao.UserDao; import com.zdx.domain.User; public class UserService { private UserDao userDao; public boolean register(User user){ return this.userDao.insert(user); } public User login(User user){ return this.userDao.findByName(user.getName()); } public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } }
控制层LoginAction如下:
package com.zdx.action; import com.opensymphony.xwork2.ActionSupport; import com.zdx.domain.User; import com.zdx.service.UserService; public class LoginAction extends ActionSupport { private User user; private UserService service; @Override public String execute() throws Exception { // TODO Auto-generated method stub return super.execute(); } public String login() { User userTmp = service.login(user); if (userTmp == null) { return LOGIN; } else if (this.user.getPwd().equals(userTmp.getPwd())) { return SUCCESS; } return LOGIN; } public String register() { if (service.register(user)) { return SUCCESS; } else { return "registerError"; } } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public UserService getService() { return service; } public void setService(UserService service) { this.service = service; } }
源码下载地址:
http://pan.baidu.com/s/1pJHleyR