Struts2 mybatis spring 实现用户登录

来源:互联网 发布:js基本数据类型 编辑:程序博客网 时间:2024/06/01 08:06

前两天了解了一下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 0