美文网首页
拉Jar包方式SSM框架搭建

拉Jar包方式SSM框架搭建

作者: 把大象放进冰箱 | 来源:发表于2018-12-18 22:54 被阅读0次

目录主题帖传送门:Spring SpringMVC MyBatis 整合-重复的轮子造的不亦乐乎 - 简书

1、环境

Eclipse + JDK1.8 + Tomcat 9.x,环境一定要统一,否则后患无穷,很多bug都是环境不统一造成的。

2、SSM搭建

第一步:就是找Jar包,说实话,找的很辛苦,网上很多Maven和Gradle的,里面加载了很多废包,Coder的洁癖告诉我要自己动手丰衣足食。

项目里的包如上图

spring-XXX开头的是Spring+SpringMVC常用包,按图索骥找到所有包是从Spring官网下载的,由于Spring改版,SpringBoot主推,没了下载地址,github上下源码是不可能下了,那么就用Index of libs-release-local/org/springframework/spring 这个地址试试

commons-XXX开头的是Apache-commons项目的用于数据库连接池、文件IO相关的,在Apache官网找的链接地址下载Apache Commons – Apache Commons

mybatis-XXX开头的包,是Mybatis官网mybatis – MyBatis 3 | 入门找到的github地址Releases · mybatis/mybatis-3 · GitHub

Jackson是用于JSON收发的,是通过maven下载的,怎么下传送门:Eclipse Maven下载依赖包 - 简书

JSTL和Standard库是JSP标准标签库,刀耕火种没什么不好,不是么,官网地址Index of /dist/jakarta/taglibs/standard/binaries

Mysql-connect-XXX是mysql官网的包,mysql链接,各自根据对应的数据库找驱动包

annotations-api这个库其实是从tomcat9的包里拷贝出来的,在tomcat9-lib包里第一个,它是我拷贝出来解决注解异常的,应用包的规则是先找应用本地的,找不到就找tomcat的。这个包实际不用copy出来,因为可以通过add lib到classPath下面解决异常,后面会讲。

找到这些包,包的版本之间是有差异的,如果不想管这些差异和异常,可以直接按照截图中的jar包版本号按图索骥,后面会省去很多麻烦。比如Spring5.0以上的包需要搭配Jackson2.6以上的包才行,否则报错找不到jackson中的一个类,再比如mysql5.0驱动和8.0驱动在className上有个小差异,会导致一个warning。

框架整合不困难,困难的是如何选择正确的版本。

懒得逐个找包的用完整的Jar包分享百度盘:SSMJars.zip_免费高速下载|百度网盘-分享无限制

第二步:建项目

Eclipse建一个Dynamic Web项目,勾选生成web.xml

将如上找到的jar包拷贝到WebContent > WEB-INF > lib文件夹下,刷新项目

添加Server,在Eclipse > Servers添加Server-Tomcat9

打开项目properties > Java Build Path > Libraries > ClassPath下Add Library > Server Runtime选择tomcat9 Add,如果这里看不到tomcat9,那就回到上面一句话添加Server 

上面做完,其实annotations-api这个可以从你的项目lib中删除了。删不删随你,如果你开始就没拷贝,就更好了。不删的话tomcat版本不一致会导致报错说annotation类重复。

第三步:文件目录

结构都是自己建的,我的如下:

WebContent > WEB-INF > web.xml

WebContent下其他可以建可以不建,images放网站UI图片切片,JavaScripts放自己的JS,lib放jquery等三方UI控件,stylesheets放CSS,upload用于存放上传的文件,当然有条件可以用文件服务代替本地这种存放。

resources 是自己建的包,里面放Spring、SpringMVC和数据库配置文件,我嫌麻烦所以数据库配置直接写Spring.xml里了,所以比那些格式化的项目少了一个properties文件,要不要无所谓。

SRC下

com.sidi.controller 存放控制类

com.sidi.dao 存放Mybatis数据库操作接口,因为用注解SQL方式,所以不需要MapperXML

com.sidi.entity 存放Mybatis数据库实体对象,由MybatisGenerator生成,传送门:Eclipse中使用MyBatis Generator - 简书

com.sidi.service 服务接口

com.sidi.service.impl 服务接口实现类

com.sidi.util 工具类

第四步:配置文件

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"

id="WebApp_ID" version="3.0">

<display-name>SIDI</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>

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

</filter>

<filter-mapping>

<filter-name>CharacterEncodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath*:/spring.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

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

</servlet>

<servlet-mapping>

<servlet-name>springMVC</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

</web-app>

web.xml修改建议:暂时无需修改

Spring.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:tx="http://www.springframework.org/schema/tx"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation="http://www.springframework.org/schema/beans   

    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd   

    http://www.springframework.org/schema/tx   

    http://www.springframework.org/schema/tx/spring-tx-4.2.xsd   

    http://www.springframework.org/schema/context   

    http://www.springframework.org/schema/context/spring-context-4.2.xsd 

    http://www.springframework.org/schema/aop

    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 

    http://www.springframework.org/schema/mvc   

    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd

    http://www.springframework.org/schema/task

    http://www.springframework.org/schema/task/spring-task-4.2.xsd">

<context:component-scan base-package="com.sidi">

<context:exclude-filter type="annotation"

expression="org.springframework.stereotype.Controller" />

</context:component-scan>

<bean id="dataSource"

class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />

<property name="url" value="jdbc:mysql://localhost:3306/testDB?characterEncoding=utf-8" />

<property name="username" value="username" />

<property name="password" value="password" />

</bean>

<bean id="sqlSessionFactory"

class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

        <property name="sqlSessionFactory" ref="sqlSessionFactory" />

        <property name="basePackage" value="com.sidi.dao" />

    </bean>

<bean id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource" />

</bean>

</beans>

Spring.xml修改建议:

driverClassName:我用的connect是8.0 所以是com.mysql.cj.jdbc.Driver,如果5.0的请改为com.mysql.jdbc.Driver

dataSource:里面的链接参数自己替换

context:component-scan base-package="com.sidi" 包名根据实际src目录替换

sqlSessionFactory下的<property name="basePackage" value="com.sidi.dao" /> 根据实际src中dao目录替换

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:tx="http://www.springframework.org/schema/tx"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation="http://www.springframework.org/schema/beans   

    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd   

    http://www.springframework.org/schema/tx   

    http://www.springframework.org/schema/tx/spring-tx-4.2.xsd   

    http://www.springframework.org/schema/context   

    http://www.springframework.org/schema/context/spring-context-4.2.xsd 

    http://www.springframework.org/schema/aop

    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 

    http://www.springframework.org/schema/mvc   

    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd

    http://www.springframework.org/schema/task

    http://www.springframework.org/schema/task/spring-task-4.2.xsd">

<!-- Spring MVC配置 -->

    <context:annotation-config />

<!-- 扫描注解 -->

    <context:component-scan base-package="com.sidi"  use-default-filters="false">

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />

</context:component-scan>

<!-- 默认的mvc注解映射的支持 -->

    <mvc:annotation-driven />

<!-- 静态资源配置 -->

    <mvc:resources mapping="/stylesheets/**" location="/stylesheets/" /> 

    <mvc:resources mapping="/images/**" location="/images/" />

    <mvc:resources mapping="/lib/**" location="/lib/" />

    <mvc:resources mapping="/javascripts/**" location="/javascripts/" />

    <mvc:resources mapping="/upload/**" location="/upload/" />

<!-- 定义视图解析器  -->

    <bean id="viewResolver" 

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/view/" />

<property name="suffix" value=".jsp" />

</bean>

<!-- 文件上传解析器  -->

    <bean id="multipartResolver"

        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

        <property name="maxUploadSize" value="-1"/>

        <property name="defaultEncoding" value="UTF-8" />

    </bean>

</beans>

Spring-MVC.xml修改建议:

扫描注解包名根据src实际目录修改

静态资源配置根据webContent目录下结构修改,主要是加载静态资源不拦截

定义视图解析器我的跳转是通过定义的包/WEB-INF/view/XXX.jsp文件完成,其中suffix可以不写兼容HTML,JSP,FreeMaker或者自定义的等格式

至此,SSM拉包搭建完毕,Add进Server跑起来,应该没有任何报错,这里不像其他教程写Hello是因为,后面会写,这里只保证SSM的Jar包完整,配置正确即可。数据库连接没有就在Spring.xml注释配置,后面有数据库再加。启动Tomcat如下图不报错即可。

下一篇:Spring注解开发MVC基本范例 - 简书

相关文章

  • 拉Jar包方式SSM框架搭建

    目录主题帖传送门:Spring SpringMVC MyBatis 整合-重复的轮子造的不亦乐乎 - 简书 1、环...

  • SpringMVC利用全局异常数据校验

    异常拦截-数据校验 SSM框架集成数据校验: 导入相关jar包 SSM集成所有jar包+以下jar包 数据校验工具...

  • SSM搭建过程

    SSM搭建步骤 手动导入jar包 spring + springmvc的jar包 mybatisjar包 myba...

  • 2018-06-14

    在ssm搭建框架时,根据我们的需求会用到各种jar包,下面就简单介绍一下我在搭建框架时常用的各种jar的作用:系转...

  • 第十章 MyBatis和Spring的整合

    整合环境搭建 1、Spring框架所需的JAR包 2、MyBatis框架所需的JAR包 mybatis-3.4.2...

  • maven-ssm整合

    创建根目录 配置pom中的jar包依赖,即SSM框架依赖的jar包 通过properties属性配置版本号的引用关...

  • 4.SSM配置初始化

    一.SSM整合步奏 1.导入jar包 2.搭建SpringMVC 3.Spring、mybatis整合

  • Maven管理的Spring + SpringMVC + Myb

    一、配置maven的pom.xml加载jar包 为了后续开发的方便,将SSM框架所有需要的jar包一并加载进来 p...

  • Spring 入门1

    1 Spring 框架搭建 Spring Jar包地址 http://repo.springsource.org/...

  • 一个案例带你入门SSM框架开发

    SSM框架快速入门 关于SSM框架环境搭建,请点击这里前往我的博客:SSM框架整合之环境搭建由于本项目采用了mav...

网友评论

      本文标题:拉Jar包方式SSM框架搭建

      本文链接:https://www.haomeiwen.com/subject/wwirkqtx.html