pom.xml的配置

打包方式

  1. <packaging>war</packaging>

依赖的导入

最基本的

  1. 连接数据库需要导入的
  2. <dependency>
  3. <groupId>mysql</groupId>
  4. <artifactId>mysql-connector-java</artifactId>
  5. <version>8.0.26</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.alibaba</groupId>
  9. <artifactId>druid</artifactId>
  10. <version>1.2.8</version>
  11. </dependency>
  12. <!-- Mybatis相关 -->
  13. <dependency>
  14. <groupId>org.mybatis</groupId>
  15. <artifactId>mybatis</artifactId>
  16. <version>3.5.7</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.mybatis</groupId>
  20. <artifactId>mybatis-spring</artifactId>
  21. <version>2.0.6</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>com.github.pagehelper</groupId>
  25. <artifactId>pagehelper</artifactId>
  26. <version>5.2.1</version>
  27. </dependency>
  28. <!-- SpringMVC相关-->
  29. <dependency>
  30. <groupId>org.springframework</groupId>
  31. <artifactId>spring-webmvc</artifactId>
  32. <version>5.3.9</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>com.fasterxml.jackson.core</groupId>
  36. <artifactId>jackson-databind</artifactId>
  37. <version>2.12.4</version>
  38. </dependency>
  39. <!-- Spring相关-->
  40. <dependency>
  41. <groupId>org.springframework</groupId>
  42. <artifactId>spring-jdbc</artifactId>
  43. <version>5.3.9</version>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework</groupId>
  47. <artifactId>spring-aspects</artifactId>
  48. <version>5.3.9</version>
  49. </dependency>
  50. <!-- Servlet-->
  51. <dependency>
  52. <groupId>javax.servlet</groupId>
  53. <artifactId>javax.servlet-api</artifactId>
  54. <version>4.0.1</version>
  55. <scope>provided</scope>
  56. </dependency>

其他jar包

  1. <!-- 其他jar-->
  2. <!-- 文件上传-->
  3. <dependency>
  4. <groupId>commons-fileupload</groupId>
  5. <artifactId>commons-fileupload</artifactId>
  6. <version>1.4</version>
  7. </dependency>
  8. <!--简化类(小辣椒插件)-->
  9. <dependency>
  10. <groupId>org.projectlombok</groupId>
  11. <artifactId>lombok</artifactId>
  12. <version>1.18.20</version>
  13. </dependency>
  14. <!-- excel导入导出-->
  15. <dependency>
  16. <groupId>com.alibaba</groupId>
  17. <artifactId>easyexcel</artifactId>
  18. <version>3.0.5</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.slf4j</groupId>
  22. <artifactId>slf4j-nop</artifactId>
  23. <version>1.7.6</version>
  24. </dependency>
  25. <!-- 糊涂-->
  26. <dependency>
  27. <groupId>cn.hutool</groupId>
  28. <artifactId>hutool-all</artifactId>
  29. <version>5.7.17</version>
  30. </dependency>

插件的导入

web.xml的配置

(1)头文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  5. version="3.1">
  6. </web-app>

(2)spring核心配置文件

  1. <display-name>ssm</display-name>
  2. <!-- 1.配置Spring的上下文对象 -->
  3. <!-- 1.1初始化参数加载Spring的配置文件 -->
  4. <context-param>
  5. <param-name>contextConfigLocation</param-name>
  6. <param-value>classpath:spring-*.xml</param-value>
  7. </context-param>
  8. <!-- 1.2.Spring的核心 监听器 监听ServletContext的生命周期,并创建上下文-->
  9. <listener>
  10. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  11. </listener>

(3)mvc核心文件

  1. <!--2.Springmvc的前端控制器的配置 Servlet -->
  2. <!-- 2.1.注册SpringMVC的前端控制器 就是Servlet的注册-->
  3. <servlet>
  4. <servlet-name>mvc</servlet-name>
  5. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  6. <!-- 2.2 局部参数:声明配置文件位置 -->
  7. <init-param>
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>classpath:springMVC.xml</param-value>
  10. </init-param>
  11. </servlet>
  12. <!-- 2.3 配置Servlet的触发路径-->
  13. <servlet-mapping>
  14. <servlet-name>mvc</servlet-name>
  15. <url-pattern>*.do</url-pattern>
  16. </servlet-mapping>
  17. <welcome-file-list>
  18. <welcome-file>/login.html</welcome-file>
  19. </welcome-file-list>

springMVC.xml(resources下)配置

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  12. <!-- 1.扫描控制层所在包,创建对应的类的对象,存储到IOC-->
  13. <context:component-scan base-package="com.qfedu.controller"></context:component-scan>
  14. <!-- 2.配置MVC相关信息-->
  15. <mvc:annotation-driven></mvc:annotation-driven>
  16. <!-- 放行静态资源-->
  17. <mvc:default-servlet-handler></mvc:default-servlet-handler>
  18. <!-- 配置文件上传-->
  19. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  20. <!-- 最大可上传的文件大小 单位:byte 超出后会抛出MaxUploadSizeExceededException异常,可以异常解析器捕获 -->
  21. <property name="maxUploadSize" value="1048576"></property>
  22. </bean>
  23. </beans>

spring-service.xml(resources下)配置文件

  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"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx.xsd">
  15. <!-- 5.扫描IOC注解所对应的包,一般扫描业务逻辑层-->
  16. <context:component-scan base-package="com.qfedu.service.impl"></context:component-scan>
  17. <!-- Spring声明式事务的配置-->
  18. <!-- 1.实现事务管理器的配置-->
  19. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  20. <property name="dataSource" ref="dataSource"></property>
  21. </bean>
  22. <!-- 2.实现事务增强通知 可以为匹配的方法设置事务信息-->
  23. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  24. <tx:attributes>
  25. <tx:method name="*" isolation="DEFAULT" propagation="REQUIRED"/>
  26. </tx:attributes>
  27. </tx:advice>
  28. <!-- 3.实现切面配置-->
  29. <aop:config proxy-target-class="true">
  30. <aop:pointcut id="pc" expression="execution(* com.qfedu.service.impl.*.*Tx(..))"/>
  31. <aop:advisor advice-ref="txAdvice" pointcut-ref="pc" ></aop:advisor>
  32. </aop:config>
  33. </beans>

spring-dao.xml(resources下)配置文件

  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"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <!-- 1.加载外部的配置,一般都是数据库连接信息 -->
  7. <!-- <context:property-placeholder location="classpath*:dbconfig.properties"></context:property-placeholder>-->
  8. <!-- 2.配置数据库连接信息,创建数据库连接池的对象-->
  9. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
  10. <property name="url" value="jdbc:mysql://110.40.192.129:3308/db_wangba?useUnicode=true&amp;serverTimezone=Asia/Shanghai&amp;characterEncoding=UTF8"></property>
  11. <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
  12. <property name="username" value="root"></property>
  13. <property name="password" value="zzjava"></property>
  14. </bean>
  15. <!-- 3.配置Mybatis的连接工厂信息-->
  16. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  17. <!-- 设置数据库连接池-->
  18. <property name="dataSource" ref="dataSource"></property>
  19. <!-- 配置映射文件所在的路径-->
  20. <property name="mapperLocations">
  21. <array>
  22. <value>classpath:mapper/*Mapper.xml</value>
  23. </array>
  24. </property>
  25. <property name="plugins">
  26. <array>
  27. <!-- 实现分页-->
  28. <bean class="com.github.pagehelper.PageInterceptor"></bean>
  29. </array>
  30. </property>
  31. </bean>
  32. <!-- 4.Mybatis的扫描的对象 指定持久层的包-->
  33. <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  34. <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
  35. <property name="basePackage" value="com.qfedu.dao"></property>
  36. </bean>
  37. </beans>

文件的基本结构

image.png
image.png