• 搭建springWeb保姆级教程


    经过我们对mybatis和spring框架的学习,我们即将要用框架进行前后端数据交互,已经脱离了那种用servlet的方式进行数据传输,今天让我们来搭建最基本的springweb框架!!!

    1.创建一个web项目

    1.

    2. 选择一个web项目

     2.将spring集成mybatis

    1.导入mybatis Jar包

    1. <dependency>
    2. <groupId>org.mybatisgroupId>
    3. <artifactId>mybatis-springartifactId>
    4. <version>1.3.1version>
    5. dependency>

    2.配置 sqlSessionFactory(在spring-mybatis.xml中)

    1. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    2. <property name="dataSource" ref="dataSource">property>
    3. <property name="configLocation" value="classpath:mybatis-config.xml">property>
    4. <property name="mapperLocations" value="classpath:mappers/*Mappers.xml">
    5. property>
    6. bean>
    7. <bean id="mapperFactory"
    8. class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    9. <property name="basePackage" value="com.ffyc.ssm.dao">property>
    10. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">
    11. property>
    12. bean>

    3.将比较常用的包导入到项目中

    1. <dependency>
    2. <groupId>mysqlgroupId>
    3. <artifactId>mysql-connector-javaartifactId>
    4. <version>8.0.16version>
    5. dependency>
    6. <dependency>
    7. <groupId>org.mybatisgroupId>
    8. <artifactId>mybatisartifactId>
    9. <version>3.4.2version>
    10. dependency>
    11. <dependency>
    12. <groupId>junitgroupId>
    13. <artifactId>junitartifactId>
    14. <version>4.12version>
    15. <scope>providedscope>
    16. dependency>
    17. <dependency>
    18. <groupId>com.alibabagroupId>
    19. <artifactId>druidartifactId>
    20. <version>1.1.10version>
    21. dependency>
    22. <dependency>
    23. <groupId>org.springframeworkgroupId>
    24. <artifactId>spring-contextartifactId>
    25. <version>5.2.2.RELEASEversion>
    26. dependency>
    27. <dependency>
    28. <groupId>org.springframeworkgroupId>
    29. <artifactId>spring-jdbcartifactId>
    30. <version>5.2.2.RELEASEversion>
    31. dependency>
    32. <dependency>
    33. <groupId>mysqlgroupId>
    34. <artifactId>mysql-connector-javaartifactId>
    35. <version>8.0.16version>
    36. dependency>
    37. <dependency>
    38. <groupId>org.springframeworkgroupId>
    39. <artifactId>spring-aspectsartifactId>
    40. <version>5.2.2.RELEASEversion>
    41. dependency>

    4.创建响应的包和类

     5.配置对应的mybatis-config.xml文件

    1. "1.0" encoding="UTF-8" ?>
    2. configuration
    3. PUBLIC "-//mybatis.org//DTD Config
    4. 3.0//EN"
    5. "http://mybatis.org/dtd/mybatis-3-config.dtd">
    6. <configuration>
    7. <settings>
    8. <setting name="logImpl" value="STDOUT_LOGGING"/>
    9. settings>
    10. <typeAliases>
    11. <package name="com.ffyc.ssm.model"/>
    12. typeAliases>
    13. configuration>

    6.配置spring.xml,开启全局扫描

    1. "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:aop="http://www.springframework.org/schema/aop"
    5. xmlns:context="http://www.springframework.org/schema/context"
    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/aop
    11. http://www.springframework.org/schema/aop/spring-aop.xsd">
    12. <context:component-scan base-package="com.ffyc.ssm"> context:component-scan>
    13. <aop:aspectj-autoproxy />
    14. beans>

    7.配置数据库db.xml

    1. "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:tx="http://www.springframework.org/schema/tx"
    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/tx
    11. http://www.springframework.org/schema/tx/spring-tx.xsd">
    12. <context:property-placeholder location="classpath:config.properties">context:property-placeholder>
    13. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    14. <property name="driverClassName" value="${driver}">property>
    15. <property name="url" value="${url}">property>
    16. <property name="username" value="${uname}">property>
    17. <property name="password" value="${pwd}">property>
    18. <property name="initialSize" value="5">property>
    19. <property name="maxActive" value="10">property>
    20. bean>
    21. <bean id="transactionManager"
    22. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    23. <property name="dataSource" ref="dataSource">property>
    24. bean>
    25. <tx:annotation-driven transaction-manager="transactionManager"/>
    26. beans>

    config.properties:

    1. driver=com.mysql.cj.jdbc.Driver
    2. url=jdbc:mysql://127.0.0.1:3306/数据库名?serverTimezone=Asia/Shanghai
    3. uname=账号 //不能写成username,可能会调用系统上的username,结果报错
    4. pwd=密码

    8.集成mybatis

    将数据库db.xml添加到spring-mybatis.xml中,spring-mybatis.xml添加到spring.xml进行全局扫描

    1. "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:aop="http://www.springframework.org/schema/aop"
    5. xmlns:context="http://www.springframework.org/schema/context"
    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/aop
    11. http://www.springframework.org/schema/aop/spring-aop.xsd">
    12. <import resource="classpath:db.xml">import>
    13. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    14. <property name="dataSource" ref="dataSource">property>
    15. <property name="configLocation" value="classpath:mybatis-config.xml">property>
    16. <property name="mapperLocations" value="classpath:mappers/*Mappers.xml">
    17. property>
    18. bean>
    19. <bean id="mapperFactory"
    20. class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    21. <property name="basePackage" value="com.ffyc.ssm.dao">property>
    22. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">
    23. property>
    24. bean>
    25. beans>

    9.导入 SpringWeb Jar包

    1. <dependency>
    2. <groupId>org.springframeworkgroupId>
    3. <artifactId>spring-webmvcartifactId>
    4. <version>5.2.2.RELEASEversion>
    5. dependency>

    10.在 web.xml 文件中配置 DispatcherServlet

    1. <servlet>
    2. <servlet-name>applicationservlet-name>
    3. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    4. <init-param>
    5. <param-name>contextConfigLocationparam-name>
    6. <param-value>classpath:spring.xmlparam-value>
    7. init-param>
    8. <load-on-startup>0load-on-startup>
    9. servlet>
    10. <servlet-mapping>
    11. <servlet-name>applicationservlet-name>
    12. <url-pattern>/url-pattern>
    13. servlet-mapping>

    11.开启 SpringMVC 注解

    新建一个xml文件,在spring.xml统一进行解析

    1. <mvc:annotation-driven>mvc:annotation-driven>

    12.配置过滤器

    1. <filter>
    2. <filter-name>characterEncodingFilterfilter-name>
    3. <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    4. <init-param>
    5. <param-name>encodingparam-name>
    6. <param-value>UTF-8param-value>
    7. init-param>
    8. filter>
    9. <filter-mapping>
    10. <filter-name>characterEncodingFilterfilter-name>
    11. <url-pattern>/*url-pattern>
    12. filter-mapping>

    13.添加 gson jar 包(json数据)

    1. <dependency>
    2. <groupId>com.google.code.gsongroupId>
    3. <artifactId>gsonartifactId>
    4. <version>2.8.6version>
    5. dependency>

    14.配置关于jackson Jar包

    1. <dependency>
    2. <groupId>com.fasterxml.jackson.coregroupId>
    3. <artifactId>jackson-databindartifactId>
    4. <version>2.13.3version>
    5. dependency>

    15.添加过滤器 Jar包

    1. <dependency>
    2. <groupId>com.thetransactioncompanygroupId>
    3. <artifactId>cors-filterartifactId>
    4. <version>2.5version>
    5. dependency>

    16.在 web.xml 中配置过滤器

    1. <filter>
    2. <filter-name>CORSfilter-name>
    3. <filter-class>com.thetransactioncompany.cors.CORSFilterfilter-class>
    4. filter>
    5. <filter-mapping>
    6. <filter-name>CORSfilter-name>
    7. <url-pattern>/*url-pattern>
    8. filter-mapping>

    17.注册拦截器

    1. <mvc:interceptors>
    2. <mvc:interceptor>
    3. <mvc:mapping path="/**"/>
    4. <mvc:exclude-mapping path="/statics/**"/>
    5. <mvc:exclude-mapping path="/loginCtl/checklogin"/>
    6. <bean id="demo" class="com.ff.springMVC.util.DemoInterceptor">bean>
    7. mvc:interceptor>
    8. mvc:interceptors>

    18.添加拦截器所需要的Jar包

    1. <dependency>
    2. <groupId>commons-fileuploadgroupId>
    3. <artifactId>commons-fileuploadartifactId>
    4. <version>1.3.3version>
    5. dependency>

    19.添加拦截器文件解析器

    1. 文件解析器
    2. <bean id="multipartResolver"
    3. class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    4. <property name="defaultEncoding" value="utf-8">property>
    5. <property name="maxUploadSize" value="10485760">property>
    6. bean>

     

  • 相关阅读:
    Python动态演示旋转矩阵的作用
    ObjectBox 初探
    狂神。SpringBoot学习(2)
    Solon Java 应用开发框架 v2.7.5 发布
    程序设计思路-球连球组成的群
    内网和热点同时连接使用配置
    CentOS同时安装mysql8.0
    Wireshark过滤DNS协议包语法实战
    ssssssssssssssssssssss
    抵押品管理服务市场现状及未来发展趋势分析
  • 原文地址:https://blog.csdn.net/dfdbb6b/article/details/128174352