SSM整合实现用户登录

最近学完Spring,SpringMVC和MyBatis后,今天第一次来实现简单的SSM整合,完成简单的用户登录功能。

1. Maven目录结构

這是src/main/java和src/main/resource的目录结构
SSM整合实现用户登录_第1张图片
这是其他目录结构
SSM整合实现用户登录_第2张图片

2. 配置文件

对于初学者,玩这种项目很头疼的一点就是配置文件的问题。我在第一配置的由于对Maven的不熟,遇到了很多多问题,不过都不是啥大问题,搜搜都能解决。下面看看我配置文件的内容吧
web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>ssm</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
<!-- 1.启动Spring的容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
<!-- 2.SpringMvc的前端控制器,拦截所有请求 -->
	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- <init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param> -->
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
<!-- 3.字符编码过滤器,放在所有过滤器之前 -->
	<filter>
	    <filter-name>CharacterEncodingFilter</filter-name>
	    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
	    <init-param>
	        <param-name>encoding</param-name>
	        <param-value>utf-8</param-value>
	    </init-param>
	    <init-param>
	        <param-name>forceRequestEncoding</param-name>
	        <param-value>true</param-value>
	    </init-param>
	    <init-param>
	        <param-name>forceResponseEncoding</param-name>
	        <param-value>true</param-value>
	    </init-param>
	</filter>
	<filter-mapping>
	    <filter-name>CharacterEncodingFilter</filter-name>
	    <url-pattern>/*</url-pattern>
	</filter-mapping>

<!-- 下面这个配置对整个项目没什么用,可以不用,我懒的删了 -->
<!-- 4.使用Rest风格的URL,将页面普通的post请求转为指定的delete或者put请求 -->
	<filter>
	    <filter-name>HiddenHttpMethodFilter</filter-name>
	    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
	</filter>
	<filter-mapping>
	    <filter-name>HiddenHttpMethodFilter</filter-name>
	    <url-pattern>/*</url-pattern>
	</filter-mapping>
	<filter>
	    <filter-name>HttpPutFormContentFilter</filter-name>
	    <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
	</filter>
	<filter-mapping>
	    <filter-name>HttpPutFormContentFilter</filter-name>
	    <url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

spring mvc配置文件 dispatcherServlet-servlet.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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-5.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
	<context:component-scan base-package="com.my" use-default-filters="false">
	    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	    <property name="prefix" value="/"></property>
	    <property name="suffix" value=".jsp"></property>
	</bean>
	<mvc:default-servlet-handler/>
	<mvc:annotation-driven/>
</beans>

spring容器的配置文件 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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-5.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	<context:component-scan base-package="com.my">
	    <context:exclude-filter type="annotation"
	        expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
<!-- Spring的配置文件,这里主要配置和业务逻辑有关的 -->
<!-- 数据源,事务控制等等 -->
	<context:property-placeholder location="classpath:dbconfig.properties"/>
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	    <property name="jdbcUrl" value="${jdbc.url}"></property>
	    <property name="driverClass" value="${jdbc.driver}"></property>
	    <property name="user" value="${jdbc.username}"></property>
	    <property name="password" value="${jdbc.passowrd}"></property>
	</bean> 
	
<!-- 配置sqlSessionFactoryBean -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	    <property name="dataSource" ref="dataSource"></property>
	</bean>
	
	
<!-- 配置MapperScannnerConfigurer,Dao接口所在的包名,Spring会自动查找其下的类 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	    <property name="basePackage" value="com.my.dao"></property>
	    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>
	
<!-- 配置事务管理 -->	
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	    <property name="dataSource" ref="dataSource"></property>
	</bean>

<!-- 启动基于注解的声明式事务管理配置 -->	
	<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

对于mybatis的一些配置已经整合到Spring容器里面了,我就不打出dbConfigurer.properties文件的内容了,配自己的数据库信息即可

哦,这里提醒下,配置这些东西时,我碰到了一个很***的问题,时不时就跳出来。

org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document ‘http://www.springframework.org/schema/beans/spring-beans-2.0.xsd’, because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not xsd:schema.

就这些xsd的报错,这里给个链接,有关这些问题的原因和解决办法 链接

3. pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.my</groupId>
  <artifactId>ssm</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-web</artifactId>
  		<version>5.0.3.RELEASE</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-webmvc</artifactId>
  		<version>5.0.3.RELEASE</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-jdbc</artifactId>
  		<version>5.0.3.RELEASE</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-aspects</artifactId>
  		<version>5.0.3.RELEASE</version>
  	</dependency>
  	<dependency>
  		<groupId>javax.servlet</groupId>
  		<artifactId>javax.servlet-api</artifactId>
  		<version>3.1.0</version>
  	</dependency>
  	<dependency>
  		<groupId>org.mybatis</groupId>
  		<artifactId>mybatis</artifactId>
  		<version>3.4.5</version>
  	</dependency>
  	<dependency>
  		<groupId>org.mybatis</groupId>
  		<artifactId>mybatis-spring</artifactId>
  		<version>1.3.1</version>
  	</dependency>
  	<dependency>
  		<groupId>c3p0</groupId>
  		<artifactId>c3p0</artifactId>
  		<version>0.9.1.2</version>
  	</dependency>
  	<dependency>
  		<groupId>mysql</groupId>
  		<artifactId>mysql-connector-java</artifactId>
  		<version>5.1.45</version>
  	</dependency>
  	<dependency>
  		<groupId>javax.servlet</groupId>
  		<artifactId>jstl</artifactId>
  		<version>1.2</version>
  	</dependency>
  	<dependency>
  		<groupId>com.googlecode.json-simple</groupId>
  		<artifactId>json-simple</artifactId>
  		<version>1.1</version>
  	</dependency>
  	<dependency>
  		<groupId>commons-fileupload</groupId>
  		<artifactId>commons-fileupload</artifactId>
  		<version>1.3.3</version>
  	</dependency>
  	<dependency>
  		<groupId>com.google.code.gson</groupId>
  		<artifactId>gson</artifactId>
  		<version>2.8.2</version>
  	</dependency>
  	<dependency>
  		<groupId>org.apache.commons</groupId>
  		<artifactId>commons-lang3</artifactId>
  		<version>3.7</version>
  	</dependency>
  </dependencies>
</project>

4. 其他java类

UserInfo.java

package com.my.pojo;

public class UserInfo {
	private int id;
	private String userName;
	private String password;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "UserInfo[userName="+userName+",password="+password+"]";
	}
}

UserInfoDao.java

package com.my.dao;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.my.pojo.UserInfo;

public interface UserInfoDao {
	@Select("select * from user_info where userName = #{userName}")
	public UserInfo findUserInfoByCond(@Param("userName") String userName);
}

UserInfoService.java

package com.my.service;

import com.my.pojo.UserInfo;

public interface UserInfoService {
	public UserInfo login(String userName);
}

UserInfoServiceImpl.java

package com.my.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.my.dao.UserInfoDao;
import com.my.pojo.UserInfo;
import com.my.service.UserInfoService;

@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {
	@Autowired
	private UserInfoDao userInfoDao;
	
	public UserInfo login(String userName) {
		// TODO Auto-generated method stub
		return userInfoDao.findUserInfoByCond(userName);
	}
}

UserInfoController.java

package com.my.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.my.pojo.UserInfo;
import com.my.service.UserInfoService;

@Controller
@RequestMapping("/userinfo")
public class UserInfoController {
	@Autowired
	private UserInfoService userInfoService;
	@RequestMapping("/login")
	public String login(UserInfo ui) {
		UserInfo tempUi = userInfoService.login(ui.getUserName());
		System.out.println(ui.toString()+"bbb");
		System.out.println(tempUi.toString()+"aaaaaa");
		if(tempUi!=null&&tempUi.getPassword().equals(ui.getPassword())) {
			return "index";
		}else {
			System.out.println("登录错误");
			return "redirect:/login.jsp";
		}
	}
}

ok,最后看看运行效果
SSM整合实现用户登录_第3张图片
SSM整合实现用户登录_第4张图片
在这里插入图片描述

你可能感兴趣的:(spring,ssm,java)