MyEclipse环境下搭建SSM框架实现登录功能

一、准备工作

MyEclipse10,下载安装破解

JDK8下载安装,配置环境变量

MySQL5下载安装,配置环境变量

Tomcat8下载安装,配置环境变量

maven3.5下载安装,配置环境变量

在MyEclipse中设置preferences将jdk、tomcat、maven等配置好。这些网上教程很多,就不详说了。

二、项目结构

三、代码实现

配置信息

3.0 pom.xml 配置 ---引入需要的jar包

<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.it.ssm</groupId>
  <artifactId>TestDoc</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
    <!-- 用来设置版本号 -->
	<properties>
		<srping.version>4.0.2.RELEASE</srping.version>
		<mybatis.version>3.2.8</mybatis.version>
		<slf4j.version>1.7.12</slf4j.version>
		<log4j.version>1.2.17</log4j.version>
	</properties>
	<!-- 用到的jar包 -->
	<dependencies>
		<!-- 单元测试 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<!-- 表示开发的时候引入,发布的时候不会加载此包 -->
			<scope>test</scope>
		</dependency>
		<!-- java ee包 -->
		<dependency>
			<groupId>javax</groupId>
			<artifactId>javaee-api</artifactId>
			<version>7.0</version>
		</dependency>
		<!-- spring框架包 start -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${srping.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${srping.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-oxm</artifactId>
			<version>${srping.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${srping.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${srping.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${srping.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${srping.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${srping.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>${srping.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${srping.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${srping.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${srping.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>${srping.version}</version>
		</dependency>
		<!-- spring框架包 end -->
		
		<!-- mybatis框架包 start -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>${mybatis.version}</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.2.2</version>
		</dependency>
		<!-- mybatis框架包 end -->
		<!-- 数据库驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.35</version>
		</dependency>
		<!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>
		<!-- jstl标签类 -->
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>1.1.2</version>
		</dependency>
		<!-- log start -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>${log4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<!-- log END -->
		<!-- Json -->
		<!-- 格式化对象,方便输出日志 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.6</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
			<version>1.9.13</version>
		</dependency>
		<!-- 上传组件包 start -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.1</version>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.4</version>
		</dependency>
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.10</version>
		</dependency>
		<!-- 上传组件包 end -->
		<!-- AL相关添加 -->
		<dependency>
			<groupId>net.sourceforge.jexcelapi</groupId>
			<artifactId>jxl</artifactId>
			<version>2.6</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.8</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.9</version>
		</dependency>
		<!-- AL相关添加 -->
	</dependencies>
  
  
  <build/>
</project>

3.1 jdbc.properties配置  ---数据库连接配置

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/document
username=root
password=123456

initialSize=0
maxActive=20
maxIdle=20
minIdle=1
maxWait=60000

3.2 log4j.properties配置  ---日志记录器配置

log4j.rootLogger=INFO,Console,File

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
log4j.appender.File = org.apache.log4j.RollingFileAppender
log4j.appender.File.File = logs/ssm.log
log4j.appender.File.MaxFileSize = 10MB
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

3.3 web.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0" id="WebApp_1558851176064">
	<display-name>TestDoc</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>

	
	<!-- 加载spring bean -->
    <context-param>
         <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mybatis.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
	<!-- 编码过滤器 -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<async-supported>true</async-supported>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- Spring MVC servlet -->
	<servlet>
		<servlet-name>SpringMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
		<async-supported>true</async-supported>
	</servlet>
	<servlet-mapping>
		<servlet-name>SpringMVC</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
</web-app>

3.4 spring-mybatis配置

<?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"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
                        http://www.springframework.org/schema/context    
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd    
                        http://www.springframework.org/schema/aop 
		                http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 
                        http://www.springframework.org/schema/mvc                  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
                        
<!-- 使用注解式注入 -->
	<context:annotation-config />
  <!-- 自动扫描 -->
    <context:component-scan base-package="com.it.ssm"/>
    <!-- 引入配置文件:1.2.3.4..-->
    <!-- 1.jdbc数据库连接配置 -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="location" value="classpath:jdbc.properties"/>
    </bean>  
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
        destroy-method="close">  
        <property name="driverClassName" value="${driver}" />  
        <property name="url" value="${url}" />  
        <property name="username" value="${username}" />  
        <property name="password" value="${password}" />  
        <!-- 初始化连接大小 -->  
        <property name="initialSize" value="${initialSize}"></property>  
        <!-- 连接池最大数量 -->  
        <property name="maxActive" value="${maxActive}"></property>  
        <!-- 连接池最大空闲 -->  
        <property name="maxIdle" value="${maxIdle}"></property>  
        <!-- 连接池最小空闲 -->  	
        <property name="minIdle" value="${minIdle}"></property>  
        <!-- 获取连接最大等待时间 -->  
        <property name="maxWait" value="${maxWait}"></property>  
    </bean>
    <!-- <bean id="dataSource_second" class="org.apache.commons.dbcp.BasicDataSource"  
        destroy-method="close">  
        <property name="driverClassName" value="${driver_2}" />  
        <property name="url" value="${url_2}" />  
        <property name="username" value="${username}" />  
        <property name="password" value="${password}" />  
    </bean> -->
    <!-- <bean id="dataSource" class="com.it1404.lc.util.MultipleDataSource">
	默认使用docapp数据库
		<property name="defaultTargetDataSource" ref="dataSource"></property>
		<property name="targetDataSources">
			<map>
				<entry key="dataSource_first" value-ref="dataSource_first"></entry>
				<entry key="dataSource_second" value-ref="dataSource_second"></entry>
			</map>
		</property>
	</bean> -->
    
    <!-- 2.spring+mybatis结合 -->
    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" /> 
                         <!-- 自动扫描mapping文件 -->
        <property name="mapperLocations" value="classpath:com/it/ssm/mapper/*.xml"></property>
    </bean>
    
    <!-- 3. spring+dao 配置(spring-dao.xml)  -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	 	<!--basePackage指定要扫描的包,在此包之下的映射器都会被搜索到。
		 可指定多个包,包与包之间用逗号或分号分隔-->
		<property name="basePackage" value="com.it.ssm.dao"/>
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>
	
	<!-- 4.事务管理 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	 
	 <!-- 切面 -->
	<!-- <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
	<bean id="dataSourceAspect" class="com.it1404.lc.util.DataSourceAspect"></bean>
		<aop:config>
			<aop:aspect ref="dataSourceAspect">
				拦截所有service方法,在dao层添加注解
				<aop:pointcut expression="execution(* com.it1404.lc.dao..*.*(..))" id="dataSourcePointcut"/>
				<aop:before method="intercept" pointcut-ref="dataSourcePointcut"/>
			</aop:aspect>
		</aop:config> -->
	 
 </beans>

3.5 spring-mvc配置

<?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"
	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-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

	 <!-- 添加注解驱动 -->
    <mvc:annotation-driven />
    <mvc:default-servlet-handler/>
    
    <!-- 设置使用注解的类所在的包 -->
	<context:component-scan base-package="com.it.ssm.controller"/>
	
	<!-- 静态资源处理  css js imgs -->
    <mvc:resources location="/resources/**" mapping="/resources"/>
    <mvc:resources mapping="/js/**" location="/js/"/>
    <mvc:resources mapping="/css/**" location="/css/"/>
    <mvc:resources mapping="/images/**" location="/images/"/> 
    <mvc:resources mapping="/plugin/**" location="/plugin/"/>
	
	<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
		<property name="prefix" value="/WEB-INF/jsp/"/>
		<property name="suffix" value=".jsp"/>
	</bean>

	<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器-->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <!-- 默认编码 -->
        <property name="defaultEncoding" value="utf-8"/>  
        <!-- 文件大小最大值 -->
        <property name="maxUploadSize" value="10485760000"/>  
        <!-- 内存中的最大值 -->
        <property name="maxInMemorySize" value="40960"/>
    </bean>
 

</beans>

至此配置文件全部完成,当然配置文件中的一些地址、接口等都还未写,在开发中也不是这样的设计顺序。这里知识为了观看方便直接贴出来了。

后台代码:

1.UserController.java(->com.it.ssm.controller包下)

package com.it.ssm.controller;


import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.it.ssm.entity.User;
import com.it.ssm.service.IUserService;

@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private IUserService userService;
	
	@RequestMapping("/login.do")
	@ResponseBody
	public String login(HttpServletRequest request){
		String username = request.getParameter("username");
		String pwd = request.getParameter("pwd");
		String msg = null;
		User user = userService.login(username);
		if (user.getPassword().equals(pwd)) {
			msg = "login successfully!";
		}else {
			msg = "please check your username and password!";
		}
		return msg;
	}
}

2.User.java(->com.it.ssm.entity包下)

package com.it.ssm.entity;

public class User {

	private Integer id;
	private String name;
	private String password;
	
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
	
}

3.IUserDao.java(->com.it.ssm.dao包下)

package com.it.ssm.dao;

import com.it.ssm.entity.User;

public interface IUserDao {

	public User getUserByName(String username);
	
}

4.UserMapper.xml(->com.it.ssm.mapper包下)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.it.ssm.dao.IUserDao">

	<resultMap id="UserResultMap" type="com.it.ssm.entity.User">
		<result column="id" property="id" jdbcType="INTEGER"/>
		<result column="name" property="name" jdbcType="VARCHAR"/>
		<result column="password" property="password" jdbcType="VARCHAR"/>
	</resultMap>

	<select id="getUserByName" resultMap="UserResultMap">
		select *
		from user
		where
		name = #{username}
	</select>

5.IUserService .java(->com.it.ssm.service包下)

package com.it.ssm.service;

import com.it.ssm.entity.User;

public interface IUserService {

	public User login(String username);

}

6.UserServiceImpl .java(com.it.ssm.service.Impl包下)

package com.it.ssm.service.Impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.it.ssm.dao.IUserDao;
import com.it.ssm.entity.User;
import com.it.ssm.service.IUserService;

@Service
public class UserServiceImpl implements IUserService {
	
	@Autowired
	private IUserDao userDao;

	@Override
	public User login(String username){
		User user = userDao.getUserByName(username);
		if (user != null) {
			return user;
		}else {
			return null;
		}
	}

}

前端页面:

1.首页 index.html ---登录页

<!DOCTYPE html>
<html>
  <head>
		<meta charset="utf-8" />
		<title>登录页面</title>
		<script src="js/jquery.min.js"></script>
		<script type="text/javascript">
			function login(){			
				var name = $('#username').val();
				var pwd = $('#pwd').val();
				alert("user:"+name+"  pwd:"+pwd);
				var rootPath = "/TestDoc";
				$.ajax({
					url:rootPath+'/user/login.do?username='+encodeURI(name)+"&pwd="+encodeURI(pwd),
					success:function(data){
						location.href="page/success.html";
					},
					error:function(data){
						alert(JSON.stringify(data));
					}
				});
			}
		</script>
	</head>
  
  <body bgcolor="#888888">
		<div style="width: 400;height: 60px;margin-left: 680px;margin-top: 200px;font-size:26px;color:orange;">SSM登录模块</div>
			
		<div style="position: absolute;width: 400px;height: 260px;background-color:#666666;left: 50%;top: 50%;margin-left: -200px;margin-top: -130px;" >
			<div>
				<input name="username" id="username" type="text" placeholder="用户名" style="width: 260px;height: 30px;margin-top: 55px;margin-left: 70px;text-align: center;"/>
			</div>
			<div>
				<input name="pwd" id="pwd" type="text" placeholder="密码" style="width: 260px;height: 30px;margin-top: 35px;margin-left: 70px;text-align: center;"/>
			</div>
			<div>
				<button onclick="login()" style="width: 80px;height: 35px;margin-top: 30px;margin-left: 160px;">登录</button>
			</div>
		</div>
	</body>
</html>

2.成功页 ---success.html

<!DOCTYPE html>
<html>
  <head>
    <title>登录成功</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  
  <body bgcolor="#888888">
   		<div style="position: absolute;width: 600px;height: 100px;color:darkorange;left: 50%;top: 50%;margin-left: -300px;margin-top: -50px;">
			<h1>Congratulations! Log in successfully!</h1>
		</div>
  </body>
</html>

好啦大功告成!展示一下页面效果吧。

 输入正确的用户名盒密码之后即可登录成功。

这里没有写数据库的信息,可以根据配置信息自己建一个数据库,填写相关字段就ok了,也可以将jdbc中的数据库配置为自己的数据库,改好SQL语句,连接好就行了。

 

以上!

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页