web.xml

web.xml配置基本的信息

  1. 指定spring配置文件
  2. 指定springmvc配置文件,
  3. 配置全局字符编码过滤器
  4. rest风格filter设置,用于将POST请求转为DELETE或者PUT ```xml <!DOCTYPE web-app PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN” “http://java.sun.com/dtd/web-app_2_3.dtd“ > Archetype Created Web Application

    contextConfigLocation classpath:applicationContext.xml

    characterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding utf-8 forceRequestEncoding true forceResponseEncoding true hiddenHttpMethodFilter org.springframework.web.filter.HiddenHttpMethodFilter

    characterEncodingFilter / hiddenHttpMethodFilter /

    org.springframework.web.context.ContextLoaderListener

    dispatcherServlet org.springframework.web.servlet.DispatcherServlet 1 dispatcherServlet /

  1. <a name="NrxDL"></a>
  2. ## springMVC.xml
  3. springMVC的配置文件,在web.xml中可以指定springMVC的配置文件路径,有两种方式指定
  4. 1. 显示通过contextConfigLocation属性指定处理
  5. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/21630109/1624090602403-0c52b9b3-5a0e-4dd9-b1b3-2ebe18fa32b5.png#clientId=u7db80866-a1a4-4&from=paste&height=247&id=u29a3dc91&margin=%5Bobject%20Object%5D&name=image.png&originHeight=493&originWidth=1270&originalType=binary&ratio=2&size=65020&status=done&style=none&taskId=u3d46a704-64da-46f9-8d19-840f5d27f18&width=635)
  6. 2. 不指定contextConfigLocation属性,默认会在web.xml同级目录下找${servlet-name}-servlet.xml文件,用作springmvc的配置文件
  7. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/21630109/1624090695177-a05acbe0-3740-4306-8f07-e18257f44277.png#clientId=u7db80866-a1a4-4&from=paste&height=344&id=ub4f95b0e&margin=%5Bobject%20Object%5D&name=image.png&originHeight=688&originWidth=2009&originalType=binary&ratio=2&size=128570&status=done&style=none&taskId=uca8dd552-c959-44b1-ba3c-b3cdf1cae6e&width=1004.5)<br />对springmvc做基本的配置
  8. 1. 配置要扫描的包
  9. 1. 配置视图解析器
  10. 1. springmvc无法处理的请求交给tomcat处理
  11. 1. 配置路径自动映射等功能
  12. ```xml
  13. <?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16. xmlns:context="http://www.springframework.org/schema/context"
  17. xmlns:mvc="http://www.springframework.org/schema/mvc"
  18. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
  19. <!--spring mvc 的配置文件, 包含网站跳转逻辑的控制,配置-->
  20. <!--配置扫描的包-->
  21. <context:component-scan base-package="com.lyc" use-default-filters="false">
  22. <!--只扫码@Controller配置的类-->
  23. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  24. </context:component-scan>
  25. <!--配置视图解析器-->
  26. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  27. <property name="prefix" value="/WEB-INF/views/"/>
  28. <property name="suffix" value=".jsp"/>
  29. </bean>
  30. <!--两个标准配置-->
  31. <!--将springmvc不能处理的请求交给tomcat-->
  32. <mvc:default-servlet-handler/>
  33. <!--能支持springmvc更高级的功能,如JSR303校验、快捷的Ajax、映射动态请求-->
  34. <mvc:annotation-driven/>
  35. </beans>

spring.xml

配置spring

  1. 需要扫描的包
  2. 数据源配置
  3. 整合mybatis
  4. 事务

    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" xmlns:tx="http://www.springframework.org/schema/tx"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    7. <!--spring 的配置文件-->
    8. <!--配置要扫描的包,除了controller包归springmvc管,其它包都是spring扫描管-->
    9. <context:component-scan base-package="com.lyc">
    10. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    11. </context:component-scan>
    12. <!--配置数据源-->
    13. <context:property-placeholder location="classpath:dbconfig.properties"/>
    14. <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="comboPooledDataSource">
    15. <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
    16. <property name="driverClass" value="${jdbc.driverClass}"/>
    17. <property name="user" value="${jdbc.user}"/>
    18. <property name="password" value="${jdbc.password}"/>
    19. </bean>
    20. <!--整合mybatis-->
    21. <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
    22. <!--指定mybatis配置文件位置-->
    23. <property name="configLocation" value="classpath:mybatis-config.xml"/>
    24. <!--指定数据源-->
    25. <property name="dataSource" ref="comboPooledDataSource"/>
    26. <!--指定mapper文件的路径-->
    27. <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    28. </bean>
    29. <!--配置mybatis的dao接口扫码路径-->
    30. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    31. <!--扫描所有dao接口,放入IOC容器中-->
    32. <property name="basePackage" value="com.lyc.crud.dao"/>
    33. </bean>
    34. <!--配置事务-->
    35. <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
    36. <!--要控制的数据源-->
    37. <property name="dataSource" ref="comboPooledDataSource"/>
    38. </bean>
    39. <!--要AOP事务切入的方法-->
    40. <aop:config>
    41. <!--配置要切入的方法-->
    42. <!--所有访问权限控制 com.lyc.crud.service.. 包下的所有包,*(..) 所有类下的任意参数方法-->
    43. <aop:pointcut id="txPointcut" expression="execution(* com.lyc.crud.service..*(..))"/>
    44. <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    45. </aop:config>
    46. <!--配置事务增强-->
    47. <tx:advice id="txAdvice" transaction-manager="transactionManager">
    48. <tx:attributes>
    49. <!--所有方法都是事务方法-->
    50. <tx:method name="*"/>
    51. <!--所有get开头的方法,read-only,查询方法提高效率-->
    52. <tx:method name="get*" read-only="true"/>
    53. </tx:attributes>
    54. </tx:advice>
    55. </beans>

    mybatis.xml

    mybatis配置可以在spring的配置文件中配置,但是为了清晰一点,有的配置还是由专门的mybatis配置文件来配置

    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. <settings>
    7. <setting name="mapUnderscoreToCamelCase" value="true"/>
    8. </settings>
    9. <typeAliases>
    10. <package name="com.lyc.crud.dao"/>
    11. </typeAliases>
    12. </configuration>

    mybatis generator

    mybatis的生成器,通过配置,可以快速生成Java bean对象和基本的CRUD语句
    引入jar包

    1. <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
    2. <dependency>
    3. <groupId>org.mybatis.generator</groupId>
    4. <artifactId>mybatis-generator-core</artifactId>
    5. <version>1.4.0</version>
    6. </dependency>

    配置生成规则 ```xml <?xml version=”1.0” encoding=”UTF-8”?> <!DOCTYPE generatorConfiguration

    1. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    2. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

    1. <!-- 生成的Java文件的编码 -->
    2. <property name="javaFileEncoding" value="UTF-8" />
    3. <!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; -->
    4. <property name="beginningDelimiter" value="`" />
    5. <property name="endingDelimiter" value="`" />
    6. <!-- 注释生成器 -->
    7. <commentGenerator>
    8. <property name="suppressDate" value="true" />
    9. <property name="suppressAllComments" value="true" />
    10. </commentGenerator>
    11. <!-- 必须要有的,使用这个配置链接数据库 @TODO:是否可以扩展 -->
    12. <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
    13. connectionURL="jdbc:mysql://localhost:3306/ssm_crud"
    14. userId="root"
    15. password="root">
    16. </jdbcConnection>
    17. <javaTypeResolver>
    18. <property name="forceBigDecimals" value="false"/>
    19. </javaTypeResolver>
    20. <!-- 生成javabean 对象目录 -->
    21. <javaModelGenerator targetPackage="com.lyc.crud.entity" targetProject=".\src\main\java">
    22. <property name="enableSubPackages" value="true" />
    23. </javaModelGenerator>
    24. <!-- 指定SQL MAPPER目录 -->
    25. <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
    26. <property name="enableSubPackages" value="true" />
    27. </sqlMapGenerator>
    28. <!-- 指定生成的DAO目录 -->
    29. <javaClientGenerator targetPackage="com.lyc.crud.dao" type="XMLMAPPER"
    30. targetProject=".\src\main\java">
    31. <property name="enableSubPackages" value="true" />
    32. </javaClientGenerator>
  1. <!-- ============================================================================== -->
  2. <!--domainObjectName="User"-->
  3. <table tableName="tbl_emp" domainObjectName="Employ">
  4. </table>
  5. <table tableName="tbl_dept" domainObjectName="Department"></table>
  6. <!-- ============================================================================= -->
  7. </context>

  1. 新建一个类,启动该类main方法,执行下列代码
  2. ```java
  3. public class MBG {
  4. public static void main(String[] args) throws Exception {
  5. List<String> warnings = new ArrayList<String>();
  6. boolean overwrite = true;
  7. File configFile = new File("mybatis-generator-config.xml");
  8. ConfigurationParser cp = new ConfigurationParser(warnings);
  9. Configuration config = cp.parseConfiguration(configFile);
  10. DefaultShellCallback callback = new DefaultShellCallback(overwrite);
  11. MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
  12. myBatisGenerator.generate(null);
  13. }
  14. }