spring+springmvc+mybatis configuration integration (1)

When tomcat starts a WEB project, the WEB container will read its configuration file web.xml, read the two nodes, and then the container creates a ServletContext (servlet context, global), the role of the ContextLoaderListener listener in the listener When the Web container is started, it monitors the changes of the servletContext object and obtains the servletContext object to automatically assemble the configuration information of the ApplicationContext.

So we need to configure web.xml first

<!--配置全局的监听器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>



  <!-- 字符过滤器 传值乱码-->
  <filter>
    <filter-name>encodingFilter</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>encodingFilter</filter-name>
    <url-pattern> / *</url-pattern>
  </filter-mapping>


  <!--配置springmvc前端控制器 进行请求分发 DispatcherServlet本质也是一个Servlet -->
  <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:mvc-dispatcher.xml</param-value>
    </init-param>
    <!--标记容器启动的时候就启动这个servlet-->
    <load-on-startup>1</load-on-startup>
  </servlet>

<!--让springmvc拦截所有请求-->
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

The next step is applicationContext.xml

    <!--配置数据库参数-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--数据库连接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--配置连接池属性-->
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <!--c3p0连接池私有属性-->
        <property name="maxPoolSize" value="${maxPoolSize}"/>
        <property name="minPoolSize" value="${minPoolSize}"/>
        <property name="autoCommitOnClose" value="false"/>
        <property name="checkoutTimeout" value="${checkoutTimeout}"/>
        <property name="acquireRetryAttempts" value="${acquireRetryAttempts}"/>
    </bean>

    <!-- 配置 SqlSessionFactory 对象,spring和MyBatis完美整合,不需要mybatis的配置映射文件-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入数据库连接池-->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置 MyBaties 全局配置⽂件:mybatis-config.xml -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!-- 扫描 model 包 使⽤别名 -->
        <property name="typeAliasesPackage" value="com.ac.model"/>
        <!-- 扫描 sql 配置⽂件:mapper 需要的 xml ⽂件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>
    <!-- 配置扫描 dao 接⼝包, 动态实现 dao 接⼝, 注⼊到 Spring 容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注⼊ sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 给出需要扫描 dao 接⼝包 -->
        <property name="basePackage" value="com.ac.dao"/>
    </bean>
    <!--spring-service-->
    <!-- 扫描 service 包下所有使⽤注解的类型 -->
    <context:component-scan base-package="com.ac.service"/>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注⼊数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

Next, only the configuration of springmvc remains

<!-- controller扫描器 -->
    <context:component-scan base-package="com.ac.controller"/>


    <!-- 配置注解驱动 -->
    <mvc:annotation-driven/>
    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/views/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--静态资源默认servlet配置
        (1)加⼊对静态资源的处理: js,gif,png
        (2)允许使⽤"/"做整体映射
    -->
    <mvc:default-servlet-handler/>

If we use primary key auto-increment in our database, then it is best to configure mybatis.xml

    <!--配置全局属性-->
    <settings>
        <!-- 使⽤jdbc的getGeneratedKeys获取数据库⾃增主键值 -->
        <setting name="useGeneratedKeys" value="true" />
        <!-- 使⽤列别名替换列名 默认:true -->
        <setting name="useColumnLabel" value="true" />
        <setting name="cacheEnabled" value="true"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

Guess you like

Origin blog.csdn.net/weixin_43911969/article/details/107186802