spring+springmvc+mybatis-plus整合

web.xml

<display-name>Archetype Created Web Application</display-name>
  <!--Spring MVC 配置 并添加监听-->
  <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>/*
  

  
    org.springframework.web.context.ContextLoaderListener
  

  
  
    
    SpringMVC
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:mvc-dispatcher.xml
    
    
    1
  
  
    SpringMVC
    
    /
  

applicationContext.xml

<!--配置数据库参数-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--Druid数据库连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="url" value="${jdbc_url}" />
        <property name="username" value="${jdbc_user}" />
        <property name="password" value="${jdbc_password}" />

        <property name="filters" value="stat" />

        <property name="maxActive" value="20" />
        <property name="initialSize" value="1" />
        <property name="maxWait" value="60000" />
        <property name="minIdle" value="1" />

        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <property name="minEvictableIdleTimeMillis" value="300000" />

        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />

        <property name="poolPreparedStatements" value="true" />
        <property name="maxOpenPreparedStatements" value="20" />

        <property name="asyncInit" value="true" />
    </bean>

<!--     配置 SqlSessionFactory 对象,spring和MyBatis完美整合,不需要mybatis的配置映射文件-->
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <!--注入数据库连接池-->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置 MyBaties 全局配置⽂件:mybatis-config.xml -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!-- 扫描 model 包 使⽤别名 -->
<!--        <property name="typeAliasesPackage" value="com.demo.entity"/>-->
        <property name="typeAliasesPackage" value="com.swagger.entity"/>
        <!-- 扫描 sql 配置⽂件:mapper 需要的 xml ⽂件 -->
        <property name="mapperLocations" value="classpath:mappers/*.xml"/>
    </bean>

    <!-- 配置扫描 dao 接⼝包, 动态实现 dao 接⼝, 注⼊到 Spring 容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注⼊ sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 给出需要扫描 dao 接⼝包 -->
<!--        <property name="basePackage" value="com.demo.mapper"/>-->
        <property name="basePackage" value="com.swagger.mapper"/>
    </bean>

    <!--spring-service-->
    <!-- 扫描 service 包下所有使⽤注解的类型 -->
<!--    <context:component-scan base-package="com.demo.service"/>-->
    <context:component-scan base-package="com.swagger.service"/>
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注⼊数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

springmvc.xml

  <!-- controller扫描器 -->
<!--    <context:component-scan base-package="com.demo.web"/>-->
    <context:component-scan base-package="com.swagger.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/>

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>

你可能感兴趣的:(ssm)