帶你快速入門SSM框架---零基礎搭建---詳解

SSM框架搭建流程

搭建Spring框架

創建一個maven的web項目

在這裏插入圖片描述
2、創建項目之後,會發生報錯,缺少web-inf下的web.xml文件
在這裏插入圖片描述
3、從其他項目中拷貝下來web.xml文件
在這裏插入圖片描述
項目不報錯拉。

MVC設計模式創建包路徑和目錄

利用MVC設計模式:
在這裏插入圖片描述
在這裏插入圖片描述

在這裏插入圖片描述
在這裏插入圖片描述

開始搭建spring框架

配置pom.xml文件:

<!-- 整合spring框架 -->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-webmvc</artifactId>
	<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-jdbc</artifactId>
	<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-aspects</artifactId>
	<version>4.1.3.RELEASE</version>
</dependency>

創建applicationContext.xml文件

在這裏插入圖片描述

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	


</beans>

測試spring框架

創建User對象類:

配置applicationContext.xml

<bean id="user" class="cn.tedu.pojo.User"></bean>

寫測試類

寫測試類:
public class TestSpring {

	@Test
	public void test() {
		//加載spring的文件
		ClassPathXmlApplicationContext cpx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
		//獲取bean對象
		User user = (User)cpx.getBean("user");
		System.out.println(user);
	}
	
}

運行結果

在這裏插入圖片描述

搭建SpringMVC框架

在這裏插入圖片描述

配置springmvc-config的文件

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc
						http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
						http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
						http://www.springframework.org/schema/context
          				http://www.springframework.org/schema/context/spring-context-4.0.xsd">



	
	<mvc:default-servlet-handler/>
	
	<!-- 註解驅動,識別註解 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!--  配置掃描包  -->
	<context:component-scan base-package="cn.tedu.controller">
	</context:component-scan>
	
	<!-- 配置內部資源的解析器
	prefix:路徑的前綴
	suffix:文件的後綴
	 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	
</beans>

配置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"
	id="WebApp_ID" version="2.5">
	<display-name>day_jsp</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>

	<!-- 配置springmvc, 將所有請求交給springmvc來處理 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 配置springmvc核心配置文件的位置,默認Springmvc的配置文件是在WEB-INF目錄下,默認的名字爲springmvc-servlet.xml,如果要放在其他目錄,則需要指定如下配置: -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/*.xml</param-value>
		</init-param>
	</servlet>
	<!-- 其中的斜槓(/)表示攔截所有請求(除JSP以外), 所有請求都要經過springmvc前端控制器 -->
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<!-- 亂碼處理過濾器 -->
	<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>
</web-app>

測試springmvc框架

寫一個TestSpringmvc的測試類:

@Controller
public class TestSpringmvc {
	
	@RequestMapping("/test")
	public String test() {
		return "test1";
	}
}

在pages下創建test1.jsp:

<%@ page pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title></title>
</head>
<body>
	
	<h1>test springmvc</h1>
	
</body>
</html>

發現報錯:

在這裏插入圖片描述
在這裏插入圖片描述
解決方法:
在這裏插入圖片描述
配置tomcat服務器:如上
保證項目不報錯。
在這裏插入圖片描述
將項目部署到tomcat服務器上:
在這裏插入圖片描述
右鍵運行tomcat:即可
在這裏插入圖片描述

瀏覽器輸入進行測試

在這裏插入圖片描述

搭建mybatis框架

pom.xml文件中添加依賴

	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis</artifactId>
		<version>3.2.8</version>
	</dependency>
	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis-spring</artifactId>
		<version>1.2.2</version>
	</dependency>

	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>5.1.32</version>
	</dependency>
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>druid</artifactId>
		<version>1.1.6</version>
	</dependency>

配置mybatis-config

<?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">
    
<!-- MyBatis的全局配置文件 -->
<configuration >
	<!-- 1.配置開發環境 -->
	<environments default="develop">
		<!-- 這裏可以配置多個環境,比如develop,test等 -->
		<environment id="develop">
			<!-- 1.1.配置事務管理方式:JDBC:將事務交給JDBC管理(推薦) -->
			<transactionManager type="JDBC"></transactionManager>
			<!-- 1.2.配置數據源,即連接池方式:JNDI/POOLED/UNPOOLED -->
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://localhost:3306/llxy_db?characterEncoding=utf-8"/>
				<property name="username" value="root"/>
				<property name="password" value="root"/>
			</dataSource>
		</environment>
	</environments>
	
	<!-- 2.加載Mapper配置文件,路徑以斜槓間隔: xx/xx/../xx.xml -->
	<mappers>
		<mapper resource="mybatis/mapper/UserMapper.xml"/>
	</mappers>
</configuration>

注意修改:"jdbc:mysql://localhost:3306/llxy_db?characterEncoding=utf-8"數據庫名稱。

利用之前的創建好的User對象:

package cn.tedu.pojo;

import java.util.Date;

/**
* @author  作者: bjzhangjian
* @version 創建時間:2020年5月19日 上午9:45:36
* @description 描述:pojo對象
*/
public class User {
	
	private Integer userid;
	private String username;
	private String password;
	private Date regtime;
	private String email;
	private String telephone;
	public Integer getUserid() {
		return userid;
	}
	public void setUserid(Integer userid) {
		this.userid = userid;
	}
	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;
	}
	public Date getRegtime() {
		return regtime;
	}
	public void setRegtime(Date regtime) {
		this.regtime = regtime;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getTelephone() {
		return telephone;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
	@Override
	public String toString() {
		return "User [userid=" + userid + ", username=" + username + ", password=" + password + ", regtime=" + regtime
				+ ", email=" + email + ", telephone=" + telephone + "]";
	}
	

}

新建UserMapper.xml

在這裏插入圖片描述

<?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>


</mapper>
<mapper namespace="cn.tedu.dao.UserMapper">


</mapper>

Dao層下新建UserMapper 接口

在這裏插入圖片描述

public interface UserMapper {

	
	public List<User> findAll();
	
}

在userMapper.xml–mapper標籤下中添加:

<!-- 查詢user表中的所有的信息 -->
	<select id="findAll" resultType="cn.tedu.pojo.User">
		select *from user
	</select>

寫測試類TestMybatis類(寫數據庫表設計)

數據庫準備

在這裏插入圖片描述

代碼實現

package cn.tedu.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import cn.tedu.dao.UserMapper;
import cn.tedu.pojo.User;

/**
 * @author  作者: bjzhangjian
 * @version 創建時間:2020年5月15日 上午10:46:07
 * @description 描述:
 */
public class TestMybatis {
	private static SqlSession session = null;
	static{
		//讀取核心配置文件mybatis-config.xml
		InputStream in;
		try {
			in = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
			//獲取一個sqlsessionFactory工廠對象
			SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in);
			//獲取一個sqlsession對象
			session = fac.openSession();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 接口開發
	 */
	@Test
	public void testFindById() {
		UserMapper mapper = session.getMapper(UserMapper.class);
		List<User> list = mapper.findAll();
		for (User user : list) {
			System.out.println(user);
		}
	}
	
}

運行結果

在這裏插入圖片描述

整合spring和mybatis

去除掉:mybatis-config中的配置信息

<?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">
    
<!-- MyBatis的全局配置文件 -->
<configuration >
	<!-- 1.配置開發環境 -->
		<!-- 這裏可以配置多個環境,比如develop,test等 -->
			<!-- 1.1.配置事務管理方式:JDBC:將事務交給JDBC管理(推薦) -->
			<!-- 1.2.配置數據源,即連接池方式:JNDI/POOLED/UNPOOLED -->
	<!-- 2.加載Mapper配置文件,路徑以斜槓間隔: xx/xx/../xx.xml -->
	
</configuration>

在applicationContext中:配置druid連接池

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	
	<!-- 1.加載jdbc.properties文件的位置 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	<!-- 2.配置druid連接池 ,id是固定值,class是druid連接池類的全路徑 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<!--配置連接數據庫的基本信息  -->
		<property name="driverClassName" value="${db.driverClassName}"></property>
		<property name="url" value="${db.url}"></property>
		<property name="username" value="${db.username}"></property>
		<property name="password" value="${db.password}"></property>
	</bean>
	
	<!-- 3.整合spring和mybatis框架	
		將SqlSession等對象的創建交給Spring容器
		id值(sqlSessionFactory)是固定值
	 -->
	<bean id="sqlSessionFactory" 
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 3.1.指定mybatis核心配置文件的位置 -->
		<property name="configLocation" 
				value="classpath:mybatis/mybatis-config.xml"></property>
		<!-- 3.2.配置連接池(數據源) ref指向連接池bean對象的id值 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 3.3、掃描所有的 XxxMapper.xml映射文件,讀取其中配置的SQL語句 -->
		<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/>
	</bean>
	
	<!-- 5、定義mapper接口掃描器 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 掃描所有XxxMapper接口,將接口實例的創建交給spring容器 -->
		
		<property name="basePackage" 
			value="cn.tedu.dao"/>
	</bean>
	
	<!-- 6.配置需要掃描的包(service層):spring自動去掃描 base-package下的類,
		如果掃描到的類上有 @Controller、@Service、@Component等註解,
		將會自動將類註冊爲bean(即由spring創建實例)
	 -->
	<context:component-scan 
		base-package="cn.tedu.service">
	</context:component-scan>
	
	<!-- 聲明bean對象 -->
	<bean id="user" class="cn.tedu.pojo.User"></bean>


</beans>

新建jdbc.properties

在resource下新建jdbc.properties

db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql:///mishop?characterEncoding=utf-8
db.username=root
db.password=root

編寫測試SSM的測試類

package cn.tedu.controller;

import java.util.List;

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

import cn.tedu.dao.UserMapper;
import cn.tedu.pojo.User;

/**
* @author  作者: bjzhangjian
* @version 創建時間:2020年5月19日 上午11:25:42
* @description 描述:
*/
@Controller
public class TestSSM {

	@Autowired //自動裝配:自動爲屬性賦值操作
	UserMapper mapper;
	
	@RequestMapping("/testssm")
	public String test() {
		
		List<User> list = mapper.findAll();
		for (User user : list) {
			System.out.println(user);
		}
		
		return "test";
	}
}

運行結果

在這裏插入圖片描述

SSM框架全部搞定搭建,初學者學習很適合的。歡迎關注。分享給身邊有需要的人。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章