一些想說的話:
2020的開端真的是太難了,發生了很多不好的事情,我也呆在家裏哪都去不了。但從另一方面講,這個假期對我也很充實,我過着一種我很喜歡的生活:學校借來的這本ssm的書能用,我很開心,很慶幸走之前遇到了這本書;學習厭煩了,可以和想見的人出去見一面,買點菜,聊聊天(有點像退休生活);不用學習自己不喜歡的東西,不用見自己不想見的人;可以學到想學的東西,可以見到想見的人,我又感覺有點小幸福的,希望2020一切都能好起來,不,是變得更好。
寫的有些亂,之所以寫這個,是因爲寒假開始時,我開始學習spring,從這一章開始,前面學的東西終於、馬上要投入使用了,這一章內容是整合ssm,下一章開始的三章就是項目練習,終於到這一階段了!!!!
最後,希望2020我能找到自己想找的實習,加油,奧裏給!
環境搭建
- 在Eclipse中,新建名爲ssm的Maven項目
新建好之後會有一些錯,別急,後面會調整好 - 設置項目ssm的相關特性
右鍵ssm項目,選擇properties
Dynamic Web Model先取消選中,然後應用,然後再選中,調整版本號爲3.1,然後點擊下面黃框的連接,會彈出這樣一個框,填寫
然後應用,關閉,這會就發現錯誤已經修正了
- 項目最初目錄結構分析
pom.xml是Maven項目的核心配置文件。
target目錄是所有工程編譯構建的輸出目錄
src目錄包含所有工程的源碼文件、配置文件、資源文件等等
main/java用於存放Java源文件
main/resources用於存放框架或其他工具的配置文件
test/java用於存放Java測試的源文件
test/resources用於存放測試的配置文件
main/webapp是web應用的目錄,裏面可以包含WEB-INF、js、css等內容 - 給項目添加SSM框架所需jar包
- 開始這步之前,可以改一下maven的setting.xml文件
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
<mirror>
<id>nexus-public-snapshots</id>
<mirrorOf>public-snapshots</mirrorOf>
<url>http://maven.aliyun.com/nexus/content/repositories/snapshots/</url>
</mirror>
不然下載會超級慢,我昨天放了一晚上他都沒整好,今天早上查着改了下配置,兩分鐘不到就好了,原文鏈接放這裏了,其實基本跟我這寫的一致,我再寫一遍主要是怕他把博客刪了,以後找不到了
maven下載jar包慢解決方法
編寫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>
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
<scope>provided</scope>
</dependency>
<!-- Spring Web -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<!-- Spring SpringMVC -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<!-- Spring JDBC -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<!-- Spring Aspects -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<!-- MyBatis -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!-- MyBatis Spring -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- C3P0 -->
<!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- MySQL驅動包 -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<!-- JSTL -->
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
</dependencies>
</project>
聯網環境下,保存後會自動下載jar包
編寫SSM整合的相關配置文件
1.編寫web.xmlwenjian
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<!-- 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的前端控制器,攔截所有請求,讀取dispatcherServlet.xml配置 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<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風格的URI,將頁面普通的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>
2.編寫Spring MVC配置文件
在src/main/webapp/WEB-INF目錄中,創建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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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 name="suffix" value=".jsp"/>
</bean>
<!--兩個標準配置 -->
<!-- 將springmvc不能處理的請求交給tomcat -->
<mvc:default-servlet-handler/>
<!-- 能支持springmvc更高級的一些功能,JSR303校驗,快捷的ajax...映射動態請求 -->
<mvc:annotation-driven/>
</beans>
一些具體的標籤功能,有心情了,複習的時候我再貼到這裏~
3.編寫Spring配置文件
在src/main/resources目錄中,創建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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.my">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- Spring的配置文件,這裏主要配置和業務邏輯有關的 -->
<!--=================== 數據源,事務控制,xxx ================-->
<context:property-placeholder location="classpath:dbconfig.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置MapperScannerConfigurer,Dao接口所在包名,Spring會自動查找其下的類 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.my.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 配置DataSourceTransactionManager(事務管理) -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 啓用基於註解的聲明式事務管理配置 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
在src/main/resources裏新加db.properties
創建實體類
在src/main/java目錄下新建一個pojo包,在包中新建一個實體類UserInfo
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;
}
}
數據庫訪問層開發
在src/main/java中新建一個com.my.dao的包,在包中新建一個UserInfoDao
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} and password = #{password}")
public UserInfo findUserInfoByCond(@Param("userName") String userName, @Param("password") String password);
}
業務邏輯層開發
在src/main/java目錄中,新建一個com.my.service包,用於存放業務邏輯層接口,在包中新建一個類UserInfoService
package com.my.service;
import com.my.pojo.UserInfo;
public interface UserInfoService {
public UserInfo login(String userName,String password);
}
在com.my.service.impl寫實現類
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;
@Override
public UserInfo login(String userName, String password) {
// TODO Auto-generated method stub
return userInfoDao.findUserInfoByCond(userName, password);
}
}
控制器開發
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(), ui.getPassword());
if (tempUi !=null && tempUi.getUserName() != null) {
return "index";
}
else {
return "redirect:/login.jsp";
}
}
}
表示層開發
<%@ page language="java" contentType="text/html; charset=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>Insert title here</title>
</head>
<body>
歡迎您,登錄成功!
</body>
</html>
<%@ page language="java" contentType="text/html; charset=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>Insert title here</title>
</head>
<body>
<form action="userinfo/login" method="post">
<table>
<tr>
<td>用戶名:</td>
<td><input type="text" name="userName" /></td>
</tr>
<tr>
<td>密 碼:</td>
<td><input type="text" name="password" /></td>
</tr>
<tr>
<td><input type="submit" value="登錄" /></td>
<td></td>
</tr>
</table>
</form>
</body>
</html>