以登录案例从零搭建SSM环境详细教程

一、安装Java环境

Java环境安装是比较简单的,百度一下都会有详细步骤,不过这个还是解释一下其中各个部分的作用:

JVM:Java Virtual Machine(Java虚拟机)的缩写,负责加载.class并运行之,Java语言编译后的字节码本身并不是跨平台的,但因为JVM底层是c语言写的,可以将Java的.class文件解析成各个平台的机器语言运行,因此Java语言的跨平台其实是依靠JVM实现的;

JRE:Java Runtime Environment(Java运行时环境),包括JVM以及一些运行时类库,它是运行Java程序的最小单元,如果只需要运行Java程序,那么有它就够了;

JDK:Java Development Kit(Java开发工具包),包括JRE以及一些编译命令工具,它是开发Java程序的最小单元,如果需要开发Java程序,那么你需要安装JDK;

安装JDK步骤:

1.下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

2.配置环境变量:https://jingyan.baidu.com/article/ff41162596a77912e4823716.html

二、安装开发工具eclipse或者idea:https://www.eclipse.org/downloads/packages/

三、下载maven和Tomcat并解压到本地,并配置

1.配置maven

1)路径配置:

2)setting.xml配置:

<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

	<pluginGroups>
	</pluginGroups>

	<proxies>
	</proxies>

	<servers>
	</servers>

	<mirrors>
	<!-- 阿里云 Maven 镜像服务器配置 -->

		<mirror>
			<id>aliyun</id>
			<name>aliyun Maven</name>
			<mirrorOf>*</mirrorOf>
			<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
		</mirror>

	</mirrors>
	<profiles>

	</profiles>
	<activeProfiles>
	</activeProfiles>


</settings>

 

2.Tomcat配置

四、搭建SSM环境

1.创建maven项目

注:在J2EE模式下会出现以上项目结构,开始没有web.xml文件,需要点击Deployment xxx然后点击Generate xxx生成项目部署文件即web.xml文件

2.在pom.xml中添加SSM框架所需的jar包,具体依赖坐标可以访问http://maven.aliyun.com/mvn/search搜索

 

 

3.编写dao层

项目结构如下:

1)在数据库创建表并插入一条信息

2)配置连接数据库的属性文件db.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf8
username=root
password=123456

3)编写UserMapper接口

package com.isoftstone.demo.mapper;
import com.isoftstone.demo.bean.User;
public interface UserMapper {
	public User selectUser(String username);
}

4)编写UserMapperpe配置文件,用于编写动态SQL

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" 
	"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<!-- namespace代表命名空间,值必须时接口的全限定名 -->
<!-- 实现类没有,mybatis会生成接口的实现类 -->
<mapper namespace="com.isoftstone.demo.mapper.UserMapper">
	<!-- 根据用户名查询用户 -->
	<!-- User selectUser(String username) -->
	<select id="selectUser" resultType="com.isoftstone.demo.bean.User">
		select 
			username,
			password
		from 
			t_user
		where
			username=#{username} 
	</select>
</mapper>

5)编写User实体类作为Java程序和数据库表的映射

package com.isoftstone.demo.bean;
import java.io.Serializable;
public class User implements Serializable{
	private static final long serialVersionUID = 7446221460818106351L;
	private String username;
	private String password;
	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() {
		return "User [username=" + username + ", password=" + password + "]";
	}	
}

6)编写dao层spring配置文件application-dao.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:context="http://www.springframework.org/schema/context" 
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
 	<!-- 属性文件 -->
 	<util:properties id="dbprop" location="classpath:db.properties"/>
 	<!-- 数据库连接池 -->
 	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
 		<property name="driverClassName" value="#{dbprop.driverClassName}"/>
 		<property name="url" value="#{dbprop.url}"/>
 		<property name="username" value="#{dbprop.username}"/>
 		<property name="password" value="#{dbprop.password}"/>
 	</bean>
 	<!-- spring和mybatis的整合 -->
 	<!-- 扫描持久层的包 -->
 	<bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 		<property name="basePackage" value="com.isoftstone.demo.mapper"/>
 	</bean>
 	<!-- SqlSessionFactoryBean工厂 -->
 	<bean id="factoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
 		<!-- 配置映射文件 -->
 		<property name="mapperLocations" value="classpath:UserMapper.xml"/>
 		<!-- 依赖注入dataSource -->
 		<property name="dataSource" ref="dataSource"/>
 	</bean>
 </beans>

7)编写测试类

import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.isoftstone.demo.bean.User;
import com.isoftstone.demo.mapper.UserMapper;

public class TestDemo {
	@Test
	public void testSelectUser(){
		AbstractApplicationContext ac = new ClassPathXmlApplicationContext("application-dao.xml");
		UserMapper um = ac.getBean("userMapper",UserMapper.class);
		User user = um.selectUser("zhangsan");
		System.out.println(user);
		ac.close();
	}
}

8)执行测试类,结果如下表示正确

4.编写service层

项目结构如下:

1)编写UserService接口

package com.isoftstone.demo.service;

public interface UserService {
	public boolean checkUser(String username,String password);
}

2)编写UserServiceImpl实现类

package com.isoftstone.demo.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.isoftstone.demo.bean.User;
import com.isoftstone.demo.mapper.UserMapper;
@Service
public class UserServiceImpl implements UserService {
	@Resource
	private UserMapper userMapper;
	public boolean checkUser(String username, String password) {
		User user = userMapper.selectUser(username);
		if(user!=null&&user.getPassword().equals(password)){
			return true;
		}
		return false;
	}
}

3)编写service层spring配置文件

<?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:context="http://www.springframework.org/schema/context" 
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
 	<!-- 注解扫描 -->
 	<context:component-scan base-package="com.isoftstone.demo.service"/>
</beans>

4)编写测试方法

        @Test
	public void testCheckUser(){
		AbstractApplicationContext ac = new ClassPathXmlApplicationContext("application-dao.xml","application-service.xml");
		UserService us = ac.getBean("userServiceImpl",UserService.class);
		System.out.println(us.checkUser("zhangsan", "test"));
		ac.close();
	}

4)结果如下表示正确

5.编写controller层

项目结构如下:

1)编写UserController类

package com.isoftstone.demo.controller;
import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.isoftstone.demo.service.UserService;

@Controller
public class UserController {
	@Resource
	private UserService userServiceImpl;
	@RequestMapping("/login.do")
	public String login(){
		return "login";
	}
	@RequestMapping("/checkLogin.do")
	public String checkLogin(String username,String password){
		if(userServiceImpl.checkUser(username, password)){
			return "success";
		}else{
			return "failed";
		}
	}
}

2)编写JSP

<%@ page language="java" contentType="text/html; utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>SSM登录案例测试</title>
</head>
<body>
	<form action="${pageContext.request.contextPath}/checkLogin.do" method="post">
		<table>
			<tr>
				<td>username:</td>
				<td><input type="text" name="username"></td>
			</tr>
			<tr>
				<td>password:</td>
				<td><input type="password" name="password"></td>
			</tr>
			<tr>
				<td colspan="2"><input type="submit" value="login in"></td>
			</tr>
		</table>
	</form>
</body>
</html>

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_2_5.xsd" version="2.5">
  <display-name>SSMDemo</display-name>
  <!-- 配置上下文的初始化参数 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:application-*.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>
  		<!-- 设置初始化参数,指定字符集 -->
  	<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>
  	<!-- 前端控制器 -->
  <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:spring-mvc.xml</param-value> 
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

3)启动程序,访问

 

4)编写spring-mvc.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:context="http://www.springframework.org/schema/context" 
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
  	<!-- 注解扫描包,使用高版本注解驱动 -->
  <mvc:annotation-driven/>
  	<!-- 组件扫描 -->
  <context:component-scan base-package="com.isoftstone.demo.controller"/>
  	<!-- 视图解析器 -->
  <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  	<property name="prefix" value=""/>
  	<property name="suffix" value=".jsp"/>
  </bean>
</beans>

4)结果如下表示正确,到此,一个简单的SSM项目搭建完成!

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值