项目压缩包:
链接:https://pan.baidu.com/s/1o2g_UwdCZcbdOi5rDoUS6w
提取码:ybi4
项目导入包:
链接:https://pan.baidu.com/s/1PpxDo4PgqyXkzy7HYDK92Q
提取码:3zup
1、基本概念
1.1、Spring
Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
1.2、SpringMVC
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
1.3、MyBatis
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。MyBatis是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
2、SSM整体及导包
相信大家已经eclipse相对熟悉,都配置好了apache和jdk,我配置的环境是apache—tomcat9.0和jdk1.8.
2.1、先给大家看一下整体项目
2.2、至少需要导入的的包
(1)apache-tomcat9.0,这个不用介绍配置了创建就会导入。
(2)jdk1.8,同理。
需要自己导入的包有(等下介绍在哪里导入):
(3)mybatis
(4)mysql-connector-java
(5)spring-framework.RELEASE-dist
图示
可能某些暂时没用到。。。。
3、SSM整合
3.1、创建Web工程
file->new->other->Dynamic Web Project,点击OK输入名字,其它默认(或者根据自己想法修改修改),后面也可以更改,然后一直next,最后勾选上web.xml的建立,Web项目就创建好了。
3.2、配置文件
先在ssm->Java Resources下创建一个resource文件夹,类型Resource Folder。
3.2.1、先配置数据库文件jdbc.properties(我的只包含了必须内容,想要扩充可以自行百度)
先是创建一个file文件,然后修改名字为jdbc.properties即可(当时我配置时始终找不到后缀为.properties的文件,百度了好久才明白。。。。)
#根据自己实际情况配置,修改一下
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3305/ssm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
3.2.2、再配置日志文件log4j.properties(我的也是很简单的显示简单报错结果和日期,想要扩充可以自行百度)
#Global logging configuration
log4j.rootLogger=DEBUG, stdout
#Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
接下来才是重要的部分
3.2.3、配置spring的规范操作sqlMapConfig.xml(我的就开起了别名,再mapper数据库操作中就可以省略部分路径)
这个直接创建后缀为xml的文件即可
<?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">
<configuration>
<!--别名
在mapper.xml中各种type类型只需要直接写dao包中的名称,type="USER"
否则,需要写全部路径,type="ssm.dao.USER"
-->
<typeAliases>
<package name="ssm.dao"/>
</typeAliases>
</configuration>
3.2.4、视图解析和配置驱动文件springMvc.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: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.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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置注解驱动 -->
<!-- Spring 来扫描指定包下的类,并注册被@Component,@Controller,@Service,@Repository等注解标记的组件 -->
<mvc:annotation-driven />
<!-- 配置Controller扫描 -->
<context:component-scan base-package="ssm.*" />
<!-- 对静态资源放行 -->
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
3.2.5、连接配置文件applicationContext.xml(我的就是简单文件整合到一起配置少,但大型项目会分开好多连接,分别配置解析不同内容,对于后期维护有很大的帮助,例如:
数据库连接:applicationContext-sql.xml
数据库的事务管理:applicationContext-trans.xml
配置注解驱动@Component,@Controller,@Service,@Repository:applicationContext-dao.xml
配置Mapper扫描:applicationContext-mapper
)
<?xml version="1.0" encoding="UTF-8"?>
<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/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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 配置扫描 -->
<context:component-scan base-package="ssm.*" />
<!-- 配置数据库 -->
<!-- 配置 读取properties文件 jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置 数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置SqlSessionFactory -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 设置MyBatis核心配置文件 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml" />
<!-- 设置数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置Mapper扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 设置Mapper扫描包 -->
<property name="basePackage" value="ssm.mapper" />
</bean>
</beans>
3.2.6、最后还要配置web.xml文件(主要是实现过滤、加载springMvc和springMybatis(applicationContext文件))
<?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"
id="WebApp_ID" version="3.0">
<display-name>Hello</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
<!-- 配置spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置监听器加载spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置过滤器,解决post的乱码问题 -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置SpringMVC -->
<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:springMvc.xml</param-value>
</init-param>
<!-- 配置springmvc什么时候启动,参数必须为整数 -->
<!-- 如果为0或者大于0,则springMVC随着容器启动而启动 -->
<!-- 如果小于0,则在第一次请求进来的时候启动 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<!--
*.do 拦截.dao结尾的请求
/ 拦截所有路径,除jsp
/* 拦截所有路径,包括jsp
所有的请求都进入springMVC -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4、注意事项(走过的坑)
4.1、导入需要的包,最好导入WebContent/WEB-INF/lib,因为这样项目具有移植性,不用换地方就重新配置,只需要从lib中导入即可
4.2、版本要兼容,以下提供改版本的方法,提高版本很简单,直接进入/项目/properties/project facets修改
如果想要改低就要修改文件/项目/.settings/org.eclipse.wst.common.project.facet.core.xml
4.3、找不到配置文件source和主文件src,估计是创建项目时没有默认加入,需要自己添加,导致可能路径自己默认的是/。
原因:class path:是WEB-INF/classes。
同时也要在/项目/properties/Deployment Assembly再导入jar包
4.4、配置项目路径(服务器的server.xml文件)
<Context docBase="ssm" path="/ssm" reloadable="true" source="org.eclipse.jst.jee.server:ssm"/></Host>
修改为,不用一直要输入项目名
<Context docBase="ssm" path="/" reloadable="true" source="org.eclipse.jst.jee.server:ssm"/></Host>
其它估计就没有什么容易出错的地方了。
下面是结果展示