1、新建工程ssh01
2、添加hibernate包
|
3、添加spring 包
|
|
|
4、添加struts2包
|
|
|
|
|
5、 在web.Xml 配置spring contextConfigLocation
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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>/*</url-pattern> </filter-mapping>
<!-- spring配置contextConfigLocation --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> <!-- org.springframework.web.context.ContextLoaderListener.class --> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
|
6、建工程目录bean/ dao/ action/ seervice/ dao.impl/ service.impl/
7、通过生成bean
右键new
|
|
|
|
|
|
|
|
|
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.workit.bean.EmpBean" table="T_EMP" schema="WORKIT" lazy="false"> <id name="empno" type="java.lang.Short"> <column name="EMPNO" precision="4" scale="0" /> <generator class="sequence"> <param name="sequence">seq_emp</param> </generator> </id> <property name="ename" type="java.lang.String"> <column name="ENAME" length="10" /> </property> <property name="job" type="java.lang.String"> <column name="JOB" length="9" /> </property> <property name="sal" type="java.lang.Double"> <column name="SAL" precision="7" /> </property> <property name="comm" type="java.lang.Double"> <column name="COMM" precision="7" /> </property> <property name="deptno" type="java.lang.Byte"> <column name="DEPTNO" precision="2" scale="0" /> </property> </class> </hibernate-mapping>
| |
8、Dao的接口 | |
package com.workit.dao; import java.util.List; import com.workit.bean.UserBean; public interface UserDao { //登录 public UserBean isExistUser(UserBean user); //查询所有 public List<UserBean> findAllUser(); //查询当前 public UserBean findCurrUser(int userid); //添加 public void saveUser(UserBean user); //修改当前 public void updateUser(UserBean user); //删除 public void deleteUaser(int userid); } | |
9、编写dao 的实现类 | |
(1)继承HibernateDaoSupport、org.springframework.orm.hibernate3.support.HibernateDaoSupport (2)添加dao的接口、 (3)编写dao的实现类 package com.workit.dao.impl;
import java.util.List;
import org.hibernate.Session; import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.workit.bean.UserBean; import com.workit.dao.UserDao;
public class UserDaoImpl extends HibernateDaoSupport implements UserDao { //依赖注入sessionFactory public void deleteUser(int userid) { // TODO Auto-generated method stub UserBean userbean = null; userbean = findCurrByUser(userid); if(userbean != null) this.getHibernateTemplate().delete(userbean); } public List<UserBean> findAllUser() { // TODO Auto-generated method stub return this.getHibernateTemplate().find("from UserBean"); } public UserBean findCurrByUser(int userid) { // TODO Auto-generated method stub return this.getHibernateTemplate().get(UserBean.class, userid); } public UserBean isExsitUser(UserBean user) { // TODO Auto-generated method stub UserBean curruser = null; Session session = this.getHibernateTemplate().getSessionFactory().openSession(); String hql = "from UserBean user where user.loginname=? and user.password=?"; curruser = (UserBean)session.createQuery(hql) .setString(0, user.getLoginname()) .setString(1, user.getPassword()) .uniqueResult(); session.close(); return curruser; } public void saveUser(UserBean user) { // TODO Auto-generated method stub this.getHibernateTemplate().save(user); } public void updateUser(UserBean user) { // TODO Auto-generated method stub this.getHibernateTemplate().update(user); } }
| |
10、编写service的接口 package com.workit.service;
import java.util.List;
import com.workit.bean.UserBean;
public interface UserService { //登录验证 public UserBean isExsitUser(UserBean user); //查询所有 public List<UserBean> findAllUser(); //查询当前 public UserBean findCurrByUser(int userid); //添加 public void saveUser(UserBean user); //修改 public void updateUser(UserBean user); //删除 public void deleteUser(int userid); }
| |
11、编写Service的实现类 (1)创建Service的实现类并添加Service接口 (2) package com.workit.service.impl;
import java.util.List; import com.workit.bean.UserBean; import com.workit.dao.UserDao; import com.workit.service.UserService;
public class UserServiceImpl implements UserService { //注入userdao private UserDao userdao; public UserDao getUserdao() { return userdao; } public void setUserdao(UserDao userdao) { this.userdao = userdao; } public void deleteUser(int userid) { // TODO Auto-generated method stub userdao.deleteUser(userid); } public List<UserBean> findAllUser() { // TODO Auto-generated method stub return userdao.findAllUser(); } public UserBean findCurrByUser(int userid) { // TODO Auto-generated method stub return userdao.findCurrByUser(userid); } public UserBean isExsitUser(UserBean user) { // TODO Auto-generated method stub return userdao.isExsitUser(user); } public void saveUser(UserBean user) { // TODO Auto-generated method stub userdao.saveUser(user); } public void updateUser(UserBean user) { // TODO Auto-generated method stub userdao.updateUser(user); } }
| |
| |
12、创建Action package com.workit.action;
import java.util.List;
import com.opensymphony.xwork2.ActionContext; import com.workit.bean.UserBean; import com.workit.service.UserService;
public class UserAction { private List<UserBean> listuser; private UserBean user; private int userid; //注入UserService private UserService userservice; public UserService getUserservice() { return userservice; } public void setUserservice(UserService userservice) { this.userservice = userservice; } public List<UserBean> getListuser() { return listuser; } public void setListuser(List<UserBean> listuser) { this.listuser = listuser; } public UserBean getUser() { return user; } public void setUser(UserBean user) { this.user = user; } public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String login(){ UserBean curruser = null; curruser = userservice.isExsitUser(user); if(curruser != null){ ActionContext.getContext().getSession().put("curruser", curruser); return "loginsucc"; }else{ return "failer"; } } public String findalluser(){ listuser = userservice.findAllUser(); return "findalluser"; } public String findcurrbyuser(){ user = userservice.findCurrByUser(userid); return "findcurrbyuser"; } public String saveuser(){ userservice.saveUser(user); return "saveuser"; } public String updateuser(){ userservice.updateUser(user); return "updateuser"; } public String deleteuser(){ userservice.deleteUser(userid); return "deleteuser"; } } | |
9、配置applicationContext.xml (1)<?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.0.xsd"> <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property> <property name="url" value="jdbc:oracle:thin:@localhost:1521/oracle10"></property> <property name="username" value="workit"></property> <property name="password" value="workit"></property> <property name="maxActive" value="100"></property> <property name="maxIdle" value="30"></property> <property name="maxWait" value="500"></property> <property name="defaultAutoCommit" value="true"></property> </bean>
<!--<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean>--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop> <prop key="show_sql">true</prop> <prop key="format_sql">true</prop> </props> </property> <property name="mappingResources"> <list>
<value>com/workit/bean/UserBean.hbm.xml</value> </list> </property> </bean> <!-- 依赖注入用户 --> <bean id="userdao" class="com.workit.dao.impl.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="userservice" class="com.workit.service.impl.UserServiceImpl"> <property name="userdao" ref="userdao"></property> </bean> <bean id="useraction" class="com.workit.action.UserAction"> <property name="userservice" ref="userservice"></property> </bean> </beans>
| |
10、配置struts2文件 <?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> <include file="struts-default.xml"></include> <package name="workit" extends="struts-default"> <action name="UserAction_*" class="useraction" method="{1}"> <!--<result name="loginsucc">/meun.jsp</result>--> <result name="loginsucc" type="chain">UserAction_findalluser</result> <result name="failer">/failer.jsp</result> <result name="findalluser">/findalluser.jsp</result> <result name="findcurrbyuser">updateuser.jsp</result> <result name="updateuser" type="chain">UserAction_findalluser</result> <result name="deleteuser" type="chain">UserAction_findalluser</result> </action> </package> </struts>
| |
11、Hibernate.xml配置 <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration>
<session-factory> <!--<property name="dialect"> org.hibernate.dialect.Oracle9Dialect </property> <property name="connection.url"> jdbc:oracle:thin:@localhost:1521:oracle10 </property> <property name="connection.username">workit</property> <property name="connection.password">workit</property> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <property name="myeclipse.connection.profile">workitDB</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <mapping resource="com/workit/bean/UserBean.hbm.xml" /> --> </session-factory>
</hibernate-configuration> | |
12、登录 <form action="UserAction_login" method="post"> <table id="table" border="1"> <caption>用户登录</caption> <tr> <td>账号</td> <td><input type="text" name="user.loginname"/></td> </tr> <tr> <td>密码</td> <td><input type="password" name="user.password"/></td> </tr> <tr> <td><input type="reset" value="取消"/></td> <td><input type="submit" value="登录"></td> </tr> </table> </form> | |
13、查所有 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head>
<title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <link rel="stylesheet" href="css/user.css" type="text/css"></link> </head>
<body> <center>
<table id="table"> <caption>用户列表
<a href="saveuser.jsp">【添加用户】</a> </caption> <tr id="tr"> <td>序号</td> <td>用户姓名</td> <td>用户账号</td> <td>用户密码</td> <td>操作</td>
</tr> <s:iterator value="listuser" var="luser" status="st"> <tr id="truser"> <td id="td">${st.index+1 }</td> <td id="td">${luser.username }</td> <td id="td">${luser.loginname}</td> <td id="td">${luser.password }</td> <td id="td"> <a href="UserAction_deleteuser?userid=${luser.userid }">【删除】</a> <a href="UserAction_updateuser?userid=${luser.userid }">【修改】</a> <a href="UserAction_findcurrbyuser?userid=${luser.userid }">【详情】</a> </td>
</tr> </s:iterator> </table> </center> </body> </html>
| |
14、查当前 <form action="UserAction_findalluser" method="post"> <table id="table"> <caption>修改用户 </caption> <tr id="tr"> <td>序号</td> <td><input type="text" name="user.userid "/></td> </tr> <tr> <td>用户姓名</td> <td id="td"><input type="text" name="user.username " value="${user.username }"/></td> </tr> <tr> <td>用户账号</td> <td id="td"><input type="text" name="user.loginname" value="${user.loginname}"/></td> </tr> <tr> <td>用户密码</td> <td id="td"><input type="text" name="user.password" value="${user.password }"/></td> </tr> <tr> <td id="td" colspan="2"> <input type="reset" value="取消"/> <input type="submit" value="确定"/> </td> </tr>
</table> </form> | |
15、添加 <form action="UserAction_findalluser" method="post"> <table id="table"> <caption>添加用户</caption> <tr> <td>用户姓名</td> <td id="td"><input type="text" name="luser.username" value=""/></td> </tr> <tr> <td>用户账号</td> <td id="td"><input type="text" name="luser.loginname" value=""/></td> </tr> <tr> <td>用户密码</td> <td id="td"><input type="text" name="luser.password" value=""/></td> </tr> <tr> <td id="td" colspan="2"> <input type="reset" value="取消"/> <input type="submit" value="确定"/> </td>
</tr> </table> </form> |