Struts2 mybatis spring 实现用户登录

前两天了解了一下Ibatis的简单配置和使用,今天主要是将ibatis和spring 、struts2整合在一起,实现用户登录的简单功能。以下是项目结构图,以及项目的部分代码。

这里写图片描述

创建ApplicationContext.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"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  

    <!--配置数据源属性文件  -->  
    <bean id="propertyConfigurer"  
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="location">  
            <value>/WEB-INF/configs/mySql.properties</value>  
        </property>  
    </bean>  

    <!--配置数据源  -->  
    <bean id="dataSource"  
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <property name="driverClassName">  
            <value>${jdbc.driver}</value>  
        </property>  
        <property name="url">  
            <value>${jdbc.url}</value>  
        </property>  
        <property name="username">  
            <value>${jdbc.user}</value>  
        </property>  
        <property name="password">  
            <value>${jdbc.pwd}</value>  
        </property>  
    </bean>  

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    
        <property name="configLocation" value="classpath:com/test/sqlMapper/mybatis_config.xml" />    
        <property name="dataSource" ref="dataSource" />    
    </bean>   


    <bean id="loginDao" class="org.mybatis.spring.mapper.MapperFactoryBean">  
        <property name="mapperInterface" value="com.test.dao.ILoginDao"/>  
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
    </bean>  

    <bean id="loginAction" class="com.test.action.LoginAction">  
        <property name="loginDao" ref="loginDao"></property>  
    </bean>  
</beans>  

mybatis的配置文件mybatis_config.xml

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">   
<configuration>  
    <typeAliases>  
        <typeAlias alias="user" type="com.test.entity.User"/>  
    </typeAliases>  
    <mappers>  
        <mapper resource="com/test/sqlMapper/loginMapper.xml"/>  
    </mappers>  
</configuration>

web.xml

<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="2.4"   
    xmlns="http://java.sun.com/xml/ns/j2ee"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  

<context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>/WEB-INF/applicationContext.xml</param-value>  
</context-param>  

<listener>  
        <listener-class>  
            org.springframework.web.context.ContextLoaderListener  
        </listener-class>  
</listener>  

  <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>    
</web-app>  

Action中的execute方法,对用户进行验证。

public String execute(){  
        String userName = getUsername();  
        String password = getPassword();  
        System.out.println("userName:"+userName+"\n"+"password:"+password);  
        List list = loginDao.getUser(userName);  
        if(list.size()>0){  
            return "success";  
        }else{  
            return "error";  
        }  

    }  

花费了几天时间去了解了ibatis以及spring的简单功能。明天打算开始学习jquery easyui的知识。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值