SSH-使用注解整合实现简单的登录功能

SSH架构

SSH-使用注解整合实现简单的登录功能_第1张图片


实现思路

1. 添加3个框架所需的jar包
2. 在spring中配置数据源对象和回话工厂
3. 实现并配置DAO
4. 实现并配置Service
5. 为业务层添加事务管理
6. 实现并配置Action
7. 创建JSP测试页面


1.导入基本jar包

链接:http://pan.baidu.com/s/1eSy9SEA 密码:b1b1

2.配置applicationContext.xml


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
    
    <context:component-scan base-package="cn">context:component-scan>
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">property>
        <property name="url" value="jdbc:oracle:thin:@192.168.208.1:1521:orcl">property>
        <property name="username" value="bdqn">property>
        <property name="password" value="bdqn">property>
    bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource">property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialectprop>
                <prop key="show_sql">trueprop>
                <prop key="format_sql">trueprop>
                <prop key="javax.persistence.validation.mode">noneprop>
            props>
        property>
        <property name="packagesToScan" value="cn.pojo">property>
    bean>

    
    <bean id="txManager" 
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    bean>
    <tx:annotation-driven transaction-manager="txManager"/>

beans>

3.配置web.xml


<web-app version="3.0" 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_3_0.xsd">
    <display-name>display-name>
    <welcome-file-list>
        <welcome-file>login.jspwelcome-file>
    welcome-file-list>

    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext.xmlparam-value>
    context-param>

    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListenerlistener-class>
    listener>
    
    <filter>
        <filter-name>OpenSessionInViewFilterfilter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilterfilter-class>
        <init-param>
            <param-name>sessionFactoryBeanNameparam-name>
            <param-value>sessionFactoryparam-value>
        init-param>

    filter>

    <filter-mapping>
        <filter-name>OpenSessionInViewFilterfilter-name>
        <url-pattern>*.actionurl-pattern>
    filter-mapping>

    <filter>
        <filter-name>Struts2filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
    filter>
    <filter-mapping>
        <filter-name>Struts2filter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
web-app>

4.配置Struts.xml



<struts>
    


    


    
    

    


    <package name="default" namespace="/" extends="struts-default">
        <action name="login" class="SysEmployeeAction" method="login">
            <result name="login">/index.jspresult>
            <result name="input">/login.jspresult>
        action>

    package>
struts>

5.编写实体层
可以用DB Browser工具自动生成

package cn.pojo;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * SysEmployee entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "SYS_EMPLOYEE", schema = "BDQN")
public class SysEmployee implements java.io.Serializable {

    // Fields

    private String sn;
    private SysDepartment sysDepartment;
    private Long positionId;
    private String name;
    private String password;
    private String status;
    private Set bizClaimVouchersForNextDealSn = new HashSet(
            0);
    private Set bizClaimVouchersForCreateSn = new HashSet(
            0);

    // Constructors

    /** default constructor */
    public SysEmployee() {
    }

    /** minimal constructor */
    public SysEmployee(String sn, SysDepartment sysDepartment, Long positionId,
            String name, String password, String status) {
        this.sn = sn;
        this.sysDepartment = sysDepartment;
        this.positionId = positionId;
        this.name = name;
        this.password = password;
        this.status = status;
    }

    /** full constructor */
    public SysEmployee(String sn, SysDepartment sysDepartment, Long positionId,
            String name, String password, String status,
            Set bizClaimVouchersForNextDealSn,
            Set bizClaimVouchersForCreateSn) {
        this.sn = sn;
        this.sysDepartment = sysDepartment;
        this.positionId = positionId;
        this.name = name;
        this.password = password;
        this.status = status;
        this.bizClaimVouchersForNextDealSn = bizClaimVouchersForNextDealSn;
        this.bizClaimVouchersForCreateSn = bizClaimVouchersForCreateSn;
    }

    // Property accessors
    @Id
    @Column(name = "SN", unique = true, nullable = false, length = 50)
    public String getSn() {
        return this.sn;
    }

    public void setSn(String sn) {
        this.sn = sn;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "DEPARTMENT_ID", nullable = false)
    public SysDepartment getSysDepartment() {
        return this.sysDepartment;
    }

    public void setSysDepartment(SysDepartment sysDepartment) {
        this.sysDepartment = sysDepartment;
    }

    @Column(name = "POSITION_ID", nullable = false, precision = 10, scale = 0)
    public Long getPositionId() {
        return this.positionId;
    }

    public void setPositionId(Long positionId) {
        this.positionId = positionId;
    }

    @Column(name = "NAME", nullable = false, length = 50)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "PASSWORD", nullable = false, length = 50)
    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Column(name = "STATUS", nullable = false, length = 20)
    public String getStatus() {
        return this.status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "sysEmployeeByNextDealSn")
    public Set getBizClaimVouchersForNextDealSn() {
        return this.bizClaimVouchersForNextDealSn;
    }

    public void setBizClaimVouchersForNextDealSn(
            Set bizClaimVouchersForNextDealSn) {
        this.bizClaimVouchersForNextDealSn = bizClaimVouchersForNextDealSn;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "sysEmployeeByCreateSn")
    public Set getBizClaimVouchersForCreateSn() {
        return this.bizClaimVouchersForCreateSn;
    }

    public void setBizClaimVouchersForCreateSn(
            Set bizClaimVouchersForCreateSn) {
        this.bizClaimVouchersForCreateSn = bizClaimVouchersForCreateSn;
    }

}

6.编写daoImpl

package cn.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import cn.pojo.SysEmployee;

@Repository("SysEmployeeDao")
public class SysEmployeeDaoImpl extends HibernateDaoSupport implements SysEmployeeDao  {

public SysEmployeeDaoImpl(){

    }

    @Autowired
    public SysEmployeeDaoImpl(
            @Qualifier("sessionFactory")
            SessionFactory sessionFactory){
        this.setSessionFactory(sessionFactory);

    }

    @Override
    public SysEmployee getSysEmployee(String sn, String password) {
        // TODO Auto-generated method stub
        List emps = this.getHibernateTemplate()
                                    .find("from SysEmployee where sn=? and password=?",sn,password);
        if(emps.size()>0)
            return emps.get(0);
        else
            return null;
    }

}

7.编写ServiceImpl

package cn.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.dao.SysEmployeeDao;
import cn.pojo.SysEmployee;
@Service("SysEmployeeService")
@Transactional
public class SysEmployeeServiceImpl implements SysEmployeeService{
    @Resource
    private SysEmployeeDao empDao;



    @Override
    public SysEmployee login(String sn, String password) {
        // TODO Auto-generated method stub
        return empDao.getSysEmployee(sn, password);
    }

}

8.编写action

package cn.action;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;



import cn.pojo.SysEmployee;
import cn.service.SysEmployeeService;

@Controller("SysEmployeeAction")
public class SysEmployeeAction{

    @Resource
    private SysEmployeeService empService;

    private String sn;
    private String password;



    public SysEmployeeService getEmpService() {
        return empService;
    }
    public void setEmpService(SysEmployeeService empService) {
        this.empService = empService;
    }
    public String getSn() {
        return sn;
    }
    public void setSn(String sn) {
        this.sn = sn;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public String login(){
        SysEmployee emp =empService.login(sn, password);
        Map map =  ActionContext.getContext().getSession();
        String name = emp.getName();
        map.put("emp", emp);
        if(null!=emp){
            return "login";
        }else{
            return "input";
        }

    }

}

10.基本搭建
SSH-使用注解整合实现简单的登录功能_第2张图片

你可能感兴趣的:(SSH-使用注解整合实现简单的登录功能)