使用eclipse搭建SSM框架整合Maven(超详细!)

今天药忘吃喽~ 2022-05-16 14:08 527阅读 0赞

工作以来,一直在写前端,很久没碰后端了,今天突然发现,后端的东西得慢慢捡起来了,毕竟以后还是得靠后端吃饭的。要写后端,首先得有环境吧,那就从搭框架开始吧,今天先搭一个SSM框架,方便以后需要时直接用。下次有时间再写一个搭建SSH的。文章大部分内容参考传智播客教学视频,本文只列出搭建流程,一些固定代码还是能copy就copy吧^_^

1.使用eclipse新建一个Maven工程

70

70 1

2.填写项目的Maven坐标

70 2

3.添加项目依赖,完整pom.xml如下:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <parent>
  5. <groupId>cn.itcast.parent</groupId>
  6. <artifactId>itcast-parent</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. </parent>
  9. <groupId>cn.itcast.usermanage</groupId>
  10. <artifactId>itcast-usermanage</artifactId>
  11. <version>1.0.0-SNAPSHOT</version>
  12. <packaging>war</packaging>
  13. <dependencies>
  14. <!-- 单元测试 -->
  15. <dependency>
  16. <groupId>junit</groupId>
  17. <artifactId>junit</artifactId>
  18. <scope>test</scope>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring-webmvc</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework</groupId>
  26. <artifactId>spring-jdbc</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework</groupId>
  30. <artifactId>spring-aspects</artifactId>
  31. </dependency>
  32. <!-- Mybatis -->
  33. <dependency>
  34. <groupId>org.mybatis</groupId>
  35. <artifactId>mybatis</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.mybatis</groupId>
  39. <artifactId>mybatis-spring</artifactId>
  40. </dependency>
  41. <!-- MySql -->
  42. <dependency>
  43. <groupId>mysql</groupId>
  44. <artifactId>mysql-connector-java</artifactId>
  45. </dependency>
  46. <dependency>
  47. <groupId>org.slf4j</groupId>
  48. <artifactId>slf4j-log4j12</artifactId>
  49. </dependency>
  50. <!-- Jackson Json处理工具包 -->
  51. <dependency>
  52. <groupId>com.fasterxml.jackson.core</groupId>
  53. <artifactId>jackson-databind</artifactId>
  54. </dependency>
  55. <!-- 连接池 -->
  56. <dependency>
  57. <groupId>com.jolbox</groupId>
  58. <artifactId>bonecp-spring</artifactId>
  59. </dependency>
  60. <!-- JSP相关 -->
  61. <dependency>
  62. <groupId>jstl</groupId>
  63. <artifactId>jstl</artifactId>
  64. </dependency>
  65. <dependency>
  66. <groupId>javax.servlet</groupId>
  67. <artifactId>servlet-api</artifactId>
  68. <scope>provided</scope>
  69. </dependency>
  70. <dependency>
  71. <groupId>javax.servlet</groupId>
  72. <artifactId>jsp-api</artifactId>
  73. <scope>provided</scope>
  74. </dependency>
  75. <!-- Apache工具组件 -->
  76. <dependency>
  77. <groupId>org.apache.commons</groupId>
  78. <artifactId>commons-lang3</artifactId>
  79. </dependency>
  80. <dependency>
  81. <groupId>org.apache.commons</groupId>
  82. <artifactId>commons-io</artifactId>
  83. </dependency>
  84. <dependency>
  85. <groupId>com.github.pagehelper</groupId>
  86. <artifactId>pagehelper</artifactId>
  87. </dependency>
  88. <dependency>
  89. <groupId>com.github.jsqlparser</groupId>
  90. <artifactId>jsqlparser</artifactId>
  91. </dependency>
  92. <dependency>
  93. <groupId>com.github.abel533</groupId>
  94. <artifactId>mapper</artifactId>
  95. </dependency>
  96. </dependencies>
  97. <build>
  98. <plugins>
  99. <!-- 配置Tomcat插件 -->
  100. <plugin>
  101. <groupId>org.apache.tomcat.maven</groupId>
  102. <artifactId>tomcat7-maven-plugin</artifactId>
  103. <configuration>
  104. <port>80</port>
  105. <!-- http://127.0.0.1:{port}/{path} -->
  106. <path>/</path>
  107. </configuration>
  108. </plugin>
  109. </plugins>
  110. </build>
  111. </project>

4.编写web.xml配置文件。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <display-name>itcast-usermanage</display-name>
  4. <context-param>
  5. <param-name>contextConfigLocation</param-name>
  6. <param-value>classpath:spring/applicationContext*.xml</param-value>
  7. </context-param>
  8. <!--Spring的ApplicationContext 载入 -->
  9. <listener>
  10. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  11. </listener>
  12. <!-- 编码过滤器,以UTF8编码 -->
  13. <filter>
  14. <filter-name>encodingFilter</filter-name>
  15. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  16. <init-param>
  17. <param-name>encoding</param-name>
  18. <param-value>UTF8</param-value>
  19. </init-param>
  20. </filter>
  21. <filter-mapping>
  22. <filter-name>encodingFilter</filter-name>
  23. <url-pattern>/*</url-pattern>
  24. </filter-mapping>
  25. <!-- 解决PUT请求无法提交表单数据的问题 -->
  26. <filter>
  27. <filter-name>HttpMethodFilter</filter-name>
  28. <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
  29. </filter>
  30. <filter-mapping>
  31. <filter-name>HttpMethodFilter</filter-name>
  32. <url-pattern>/*</url-pattern>
  33. </filter-mapping>
  34. <!--
  35. 将POST请求转化为DELETE或者是PUT
  36. 要用_method指定真正的请求方法
  37. -->
  38. <filter>
  39. <filter-name>HiddenHttpMethodFilter</filter-name>
  40. <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  41. </filter>
  42. <filter-mapping>
  43. <filter-name>HiddenHttpMethodFilter</filter-name>
  44. <url-pattern>/*</url-pattern>
  45. </filter-mapping>
  46. <!-- 配置SpringMVC框架入口 -->
  47. <servlet>
  48. <servlet-name>itcast-usermanage</servlet-name>
  49. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  50. <init-param>
  51. <param-name>contextConfigLocation</param-name>
  52. <param-value>classpath:spring/itcast-usermanage-servlet.xml</param-value>
  53. </init-param>
  54. <load-on-startup>1</load-on-startup>
  55. </servlet>
  56. <servlet-mapping>
  57. <servlet-name>itcast-usermanage</servlet-name>
  58. <!--
  59. 可行:/、*.xxx、/xxx/*
  60. 不行:/*
  61. -->
  62. <url-pattern>/rest/*</url-pattern>
  63. </servlet-mapping>
  64. <welcome-file-list>
  65. <welcome-file>index.jsp</welcome-file>
  66. </welcome-file-list>
  67. </web-app>

5.添加db.properties连接数据库的配置文件。本文连接的是mysql数据库。

70 3

6.编写spring容器配置文件:applicatonContext.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  3. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  7. 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
  8. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  9. <!-- 使用spring自带的占位符替换功能 -->
  10. <bean
  11. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  12. <!-- 允许JVM参数覆盖 -->
  13. <!-- java -Djdbc.url=123 -jar xxx.jar -->
  14. <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
  15. <!-- 忽略没有找到的资源文件 -->
  16. <property name="ignoreResourceNotFound" value="true" />
  17. <!-- 配置资源文件 -->
  18. <property name="locations">
  19. <list>
  20. <value>classpath:jdbc.properties</value>
  21. </list>
  22. </property>
  23. </bean>
  24. <!-- 扫描包 -->
  25. <context:component-scan base-package="cn.itcast"/>
  26. <!-- 定义数据源 -->
  27. <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
  28. destroy-method="close">
  29. <!-- 数据库驱动 -->
  30. <property name="driverClass" value="${jdbc.driverClassName}" />
  31. <!-- 相应驱动的jdbcUrl -->
  32. <property name="jdbcUrl" value="${jdbc.url}" />
  33. <!-- 数据库的用户名 -->
  34. <property name="username" value="${jdbc.username}" />
  35. <!-- 数据库的密码 -->
  36. <property name="password" value="${jdbc.password}" />
  37. <!-- 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 -->
  38. <property name="idleConnectionTestPeriod" value="60" />
  39. <!-- 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 -->
  40. <property name="idleMaxAge" value="30" />
  41. <!-- 每个分区最大的连接数 -->
  42. <!--
  43. 判断依据:请求并发数
  44. -->
  45. <property name="maxConnectionsPerPartition" value="100" />
  46. <!-- 每个分区最小的连接数 -->
  47. <property name="minConnectionsPerPartition" value="5" />
  48. </bean>
  49. </beans>

7.编写spring事物配置文件:applicationContext-transaction.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  3. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  7. 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
  8. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  9. <!-- 定义事务管理器 -->
  10. <bean id="transactionManager"
  11. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  12. <property name="dataSource" ref="dataSource" />
  13. </bean>
  14. <!-- 定义事务策略 -->
  15. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  16. <tx:attributes>
  17. <!--所有以query开头的方法都是只读的 -->
  18. <tx:method name="query*" read-only="true" />
  19. <!--其他方法使用默认事务策略 -->
  20. <tx:method name="*" />
  21. </tx:attributes>
  22. </tx:advice>
  23. <aop:config>
  24. <!--pointcut元素定义一个切入点,execution中的第一个星号 用以匹配方法的返回类型,
  25. 这里星号表明匹配所有返回类型。 com.abc.dao.*.*(..)表明匹配cn.itcast.mybatis.service包下的所有类的所有
  26. 方法 -->
  27. <aop:pointcut id="myPointcut" expression="execution(* cn.itcast.usermanage.service.*.*(..))" />
  28. <!--将定义好的事务处理策略应用到上述的切入点 -->
  29. <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" />
  30. </aop:config>
  31. </beans>

8.编写springMVC配置文件:itcast-usermanage-servlet.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  7. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
  9. <!-- 注解驱动 -->
  10. <mvc:annotation-driven/>
  11. <!-- 扫描Controller -->
  12. <context:component-scan base-package="cn.itcast.usermanage.controller"/>
  13. <!-- 视图解析器 -->
  14. <!--
  15. Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" -> "/WEB-INF/jsp/test.jsp"
  16. -->
  17. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  18. <property name="prefix" value="/WEB-INF/views/"/>
  19. <property name="suffix" value=".jsp"/>
  20. </bean>
  21. </beans>

9.编写mybatis和spring的整合文件:applicationContext-mybatis.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  3. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  7. 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
  8. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  9. <bean class="org.mybatis.spring.SqlSessionFactoryBean">
  10. <!-- 数据源 -->
  11. <property name="dataSource" ref="dataSource"/>
  12. <!-- 配置Mybatis的全局配置文件 -->
  13. <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
  14. <!-- 配置mapper.xml文件 -->
  15. <!-- <property name="mapperLocations" value="classpath:mybatis/mappers/*.xml"/> -->
  16. <!-- 别名包 -->
  17. <property name="typeAliasesPackage" value="cn.itcast.usermanage.pojo"/>
  18. </bean>
  19. <!-- mapper接口的扫描器 -->
  20. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  21. <property name="basePackage" value="cn.itcast.usermanage.mapper"/>
  22. </bean>
  23. </beans>

注:以上四个文件可以写成一个配置文件,但是为了方便后期维护,分开写较好。

10.编写mybatis全局配置文件:mybatis-config.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <plugins>
  7. <!-- 分页助手 -->
  8. <plugin interceptor="com.github.pagehelper.PageHelper">
  9. <property name="dialect" value="mysql" />
  10. <!-- 该参数默认为false -->
  11. <!-- 设置为true时,使用RowBounds分页会进行count查询 -->
  12. <property name="rowBoundsWithCount" value="true" />
  13. </plugin>
  14. <!-- 通用mapper -->
  15. <plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
  16. <!--主键自增回写方法,默认值MYSQL,详细说明请看文档 -->
  17. <property name="IDENTITY" value="MYSQL" />
  18. <!--通用Mapper接口,多个通用接口用逗号隔开 -->
  19. <property name="mappers" value="com.github.abel533.mapper.Mapper" />
  20. </plugin>
  21. </plugins>
  22. </configuration>

至此,SSM框架就搭建完成啦,剩下的就是往里面填东西了。其实框架这东西,就是搭建起来配置文件多比较麻烦,搭建完成后,用起来还是比较舒服的!要是嫌SSM框架太繁琐,可以用springboot微服务框架,少了很多复杂的配置文件,用起来也很方便!博客有搭建流程,有兴趣可以看看!

发表评论

表情:
评论列表 (有 0 条评论,527人围观)

还没有评论,来说两句吧...

相关阅读