首页 > 代码库 > SSM(springMVC-spring-mybatis)环境搭建-02-引入配置

SSM(springMVC-spring-mybatis)环境搭建-02-引入配置

整个配置在最后,主要记录如何找这些 jar 包 和 如何找这些 jar 包的依赖的包

1.为Maven项目引用 相关jar 包

  mybatis jar包和依赖包最容易找

  打开 https://mvnrepository.com/  , 在顶部搜索栏搜索 mybatis spring 关键词 结果如下

技术分享

 

点击第一个搜索项

技术分享

我选的 是 1.3.0 这个版本的 点击进去

技术分享

其他的 就不用多说了  接下来我直接贴出我的 pom.xml 的配置

 pom.xml

技术分享
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>banywl</groupId>
  <artifactId>ssm</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>ssm Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <!-- 公共配置 -->
  <properties>
    <spring.version>4.2.5.RELEASE</spring.version>
    <mysql.conn.version>5.1.38</mysql.conn.version>
    <dbcp.version>1.4</dbcp.version>
    <mybatis.version>3.4.1</mybatis.version>
    <cglib.version>3.2.2</cglib.version>
    <commons-logging.version>1.2</commons-logging.version>
    <log4j-core.version>2.3</log4j-core.version>
    <ognl.version>3.1.8</ognl.version>
    <javassist.version>3.20.0-GA</javassist.version>
    <log4j.version>1.2.17</log4j.version>
    <aspectjweaver.version>1.5.3</aspectjweaver.version>
    <aspectjrt.version>1.5.3</aspectjrt.version>
    <mybatis-spring.version>1.3.0</mybatis-spring.version>
    <servlet-api.version>2.5</servlet-api.version>
    <hibernate-validator.version>5.0.2.Final</hibernate-validator.version>
    <commons-fileupload.version>1.3.1</commons-fileupload.version>
    <commons-io.version>2.2</commons-io.version>
    <jackson-databind.version>2.8.9</jackson-databind.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <!--  Mysql 驱动 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql.conn.version}</version>
    </dependency>
    <!-- DBCP 连接池 -->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>${dbcp.version}</version>
    </dependency>
    <!-- MyBatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib</artifactId>
      <version>${cglib.version}</version>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>${commons-logging.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>${log4j-core.version}</version>
    </dependency>
    <dependency>
      <groupId>ognl</groupId>
      <artifactId>ognl</artifactId>
      <version>${ognl.version}</version>
    </dependency>
    <dependency>
      <groupId>org.javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>${javassist.version}</version>
    </dependency>
    <!-- 日志依赖 -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>
    <!-- spring -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>${aspectjweaver.version}</version>
    </dependency>
    <dependency>
      <groupId>aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>${aspectjrt.version}</version>
    </dependency>
    <!-- spring MVC -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!-- mybatis和spring整合 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>${mybatis-spring.version}</version>
    </dependency>
    <!-- servlet  -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>${servlet-api.version}</version>
      <scope>provided</scope>
    </dependency>
    <!--hibernate 校验-->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>${hibernate-validator.version}</version>
    </dependency>
    <!--文件上传-->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>${commons-fileupload.version}</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>${commons-io.version}</version>
    </dependency>
    <!--json 转换-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>${jackson-databind.version}</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>ssm</finalName>
    <resources>
      <!--编译之后包含xml-->
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
        </includes>
        <filtering>true</filtering>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <!--mybatis 逆向工具 Maven 插件-->
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.5</version>
        <configuration>
          <verbose>true</verbose>
          <overwrite>true</overwrite>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>
View Code

spring.xml

技术分享
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.2.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
       default-autowire="byName">

    <context:property-placeholder file-encoding="UTF-8" location="classpath:jdbc.properties" ignore-unresolvable="true"/>
    <!--注解支持 注意:一定要写在最前面-->
    <mvc:annotation-driven conversion-service="conversionService" validator="customerValidator" ></mvc:annotation-driven>

    <!-- 可扫描 controller、service -->
    <context:component-scan base-package="com.banywl.ssm.controller" />
    <context:component-scan base-package="com.banywl.ssm.service" />

    <!-- 数据源 使用 dbcp -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:mysql://localhost:3306/blqdv?characterEncoding=utf-8</value>
        </property>
        <property name="username" >
            <value>root</value>
        </property>
        <property name="password" >
            <value>root</value>
        </property>
        <!-- 其他配置 -->
        <property name="initialSize" value="5"/>
        <property name="maxIdle" value="20" />
        <property name="minIdle" value="5" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" scope="prototype">
        <!-- 加载 mybatis 配置文件 -->
        <!--<property name="configLocation" value="http://www.mamicode.com/classpath:mybatis.xml"></property>-->
        <!-- 批量指定类型别名 -->
        <property name="typeAliasesPackage" value="com.banywl.ssm.entity" />
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--
            自动扫描 Mapper
            遵循规范:需要将 mapper 接口类名 和 mapper.xml 映射文件名称保持一致,并放在一个目录中
            多个包用半角逗号分隔
            自动扫描出来的mapper的bean的id为mapper类名(首字母小写)
         -->
        <property name="basePackage" value="com.banywl.ssm.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

    <!--spring 声明式事务管理-->
    <!-- mybatis 事务管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 使用tx标签配置事务 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="modify*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="remove*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="*" propagation="REQUIRED" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="daoMethod" expression="execution(* com.banywl.ssm.service.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethod" />
    </aop:config>


    <!--静态资源解析-->
    <mvc:resources mapping="/js/**" location="/js/" />

    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp" />
    </bean>

    <!--自定义参数绑定-->
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean class="com.banywl.ssm.controller.converter.DateConverter" ></bean>
            </set>
        </property>
    </bean>

    <!-- 验证框架支持 -->
    <!-- 以下 validator  ConversionService 在使用 mvc:annotation-driven 会 自动注册-->
    <bean id="customerValidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
        <!-- 如果不加默认到 使用classpath下的 ValidationMessages.properties -->
        <property name="validationMessageSource" ref="messageSource"/>
    </bean>

    <!-- 国际化的消息资源文件(本系统中主要用于显示/错误消息定制) -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>classpath:message</value>
            </list>
        </property>
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="cacheSeconds" value="120"/>
    </bean>

    <!-- 全局异常处理器 -->
    <bean class="com.banywl.ssm.exception.ExceptionHandler"/>

    <!--文件上传-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--设置最大上传为5M-->
        <property name="maxUploadSize">
            <value>5242880</value>
        </property>
    </bean>

    <!--拦截器-->
    <mvc:interceptors>
        <mvc:interceptor>
            <!--/** 表示所有URL包括URL子路径-->
            <mvc:mapping path="/**"/>
            <bean class="com.banywl.ssm.interceptor.DemoInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>



</beans>
View Code

log4.properties

技术分享
log4j.rootLogger=DEBUG,Console,File

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} [%t] %p [%C] - %m%n
log4j.logger.org.apache.ibatis.logging.jdbc.BaseJdbcLogge=DEBUG
#log4j.logger.com.banywl.xcmp.dao=DEBUG

log4j.appender.File=org.apache.log4j.RollingFileAppender
log4j.appender.File.File=logs/ssm.log
log4j.appender.File.MaxFileSize=10MB

log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n
View Code

web.xml

技术分享
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
  <display-name>Archetype Created Web Application</display-name>

  <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:spring.xml</param-value>
    </init-param>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <!--
        1. *.action 解析指定路径
        2. / , 对于静态文件配置 不让 DispatcherServlet 解析
        3. /* , 使用这种配置 当 需要转发到 jsp 页面时 仍然会用 DispatcherServlet 解析jsp
      -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!--乱码处理过滤器-->
  <filter>
    <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
    <url-pattern>/</url-pattern>
  </filter-mapping>


</web-app>
View Code

generatorConfig.xml( mybatis逆向工具用,请勿自定义文件名 )

 

技术分享
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <!-- 驱动包路径! -->
    <classPathEntry location="E:\jar\mysql-connector-java-5.1.38.jar"/>
    <context id="MysqlTables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- jdbc url地址 ! -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/blqdv?generateSimpleParameterMetadata=http://www.mamicode.com/true" userId="root" password="root">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- targetPackage 生成后的路径 ,targetProject 绝对路径  该配置生成java代码  -->
        <javaModelGenerator targetPackage="com.banywl.ssm.entity" targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 该配置生成 mybatis 映射文件   -->
        <sqlMapGenerator targetPackage="com.banywl.ssm.mapper" targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成 java 接口代码  -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.banywl.ssm.mapper" targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--  表配置    tableName 表名      domainObjectName java类名,首字母必须大写,否则报字符串越界错误 -->
        <table tableName="sys_module" domainObjectName="Module" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
    </context>

</generatorConfiguration>
View Code

 

 

 

注:资源目录下需要建立message.properties文件供hibernate-validator使用

ssm 项目下载

SSM(springMVC-spring-mybatis)环境搭建-02-引入配置