Web开发:Struts2 Spring Hibernate整合(三)下——Hibernate的使用
现在在Struts2和Spring的基础上把Hibernate集成进去,这里主要是和Spring结合,首先依照上一篇介绍下载jar包。
采用Hibernate后的一般的web处理过程:action-》service-》dao-》数据库连接(sesstionFactory)-》怎么连接(dataSource)
(1)定义依赖关系:我们可以先修改前面的contextConfig.xml(Spring的配置文件,告诉它类之间的依赖关系)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://localhost:3306/example"> </property> <property name="username" value="mzuser"></property> <property name="password" value="123456"></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.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="mappingResources"> <value>com/mz/bean/UserBean.hbm.xml</value> </property> </bean> <bean id="userDao" class="com.mz.dao.UserDaoImpl"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <bean id="loginService" class="com.mz.service.LoginServiceImpl"> <property name="userDao"> <ref local="userDao"/> </property> </bean> <bean id="loginAction" class="com.mz.action.LoginAction"> <property name="loginService" > <ref local="loginService"/> </property> </bean> </beans>
显然可以看到其中sessionFactory和dataSource使用的是外部类,先放下,把我们自己需要完成的类完成
(2)修改LoginAction类:loginAction类调用loginService的方法,所以替换点前面的判断逻辑(改动很小):
package com.mz.action; import com.mz.service.ILoginService; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.ServletActionContext; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.UnsupportedEncodingException; /** * Created by hadoop on 15-9-7. */ public class LoginAction extends ActionSupport { private static final long serialVersionUID = 1L; private ILoginService loginService; public void setLoginService(ILoginService loginService) { this.loginService = loginService; } public ILoginService getLoginService(){ return this.loginService; } public String execute(){ return SUCCESS; } public String login() throws IOException { try { HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println(username + ":" + password); boolean login = loginService.userLogin(username, password); //主要修改内容 if(login){ response.getWriter().write("login success!"); response.getWriter().flush(); return SUCCESS; } else { response.getWriter().write("login failed!"); response.getWriter().flush(); return "login"; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); return "login"; } } }
(3)修改LoginServiceImpl类:该类需要调用Dao类访问数据库,所以定义内部属性IUserDao,并写set方法:
package com.mz.service; import com.mz.dao.IUserDao; /** * Created by hadoop on 15-9-8. */ public class LoginServiceImpl implements ILoginService { private IUserDao userDao; //IUserDao提供userLogin方法 public void setUserDao(IUserDao userDao) { //必须 this.userDao = userDao; } public boolean userLogin(String username, String password) { boolean login = userDao.userLogin(username,password); //修改部分 if(login){ return true; }else{ return false; } } }
(4)IUserDao接口
package com.mz.dao; /** * Created by hadoop on 15-9-9. */ public interface IUserDao { public boolean userLogin(String username, String password); }
(5)UserDaoImpl类:该类使用SessionFactory
package com.mz.dao; import com.mz.bean.UserBean; import org.hibernate.Session; import org.hibernate.SessionFactory; import javax.management.Query; import javax.transaction.Transaction; import java.util.List; /** * Created by hadoop on 15-9-9. */ public class UserDaoImpl implements IUserDao { private SessionFactory sessionFactory; // public void setSessionFactory(SessionFactory sessionFactory) { // this.sessionFactory = sessionFactory; } public boolean userLogin(String username, String password) { Session session = sessionFactory.openSession(); String hql = "from UserBean where username=? and password=?"; //hibernate查询语句 org.hibernate.Query query = session.createQuery(hql); query.setParameter(0, username); query.setParameter(1, password); //设置参数替换? System.out.println("from UserBean where username=" + username + " and password=" + password); List<UserBean> userBeans = query.list(); if(userBeans.size() != 0) { //有结果返回则登录成功 return true; } else { return false; } } }
(6)SessionFactory中在Hibernate的jar中有定义,为了能够使用dataResource类这里需要导入jar包,修改pom.xml
<dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency>
rebuild工程,jetty:start运行,能够根据数据库中的用户名密码判断登录情况
相关内容:
(1)Web开发:Struts2 Spring Hibernate整合(一)——Struts2的使用
(2)Web开发:Struts2 Spring Hibernate整合(二)——Spring的使用
(3)Web开发:Struts2 Spring Hibernate整合(三)上——Hibernate的使用