10-SSM框架整合

1. 框架整合

1.1 使用IDEA创建一个基于maven的web项目

项目创建后是一个不完整的目录结构,需要修改

10-SSM框架整合_第1张图片

1.2 完善项目的目录结构

10-SSM框架整合_第2张图片

1.3 导入需要依赖的坐标

<properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.8maven.compiler.source>
    <maven.compiler.target>1.8maven.compiler.target>

    <lombok.version>1.18.12lombok.version>
    <javaee.version>8.0javaee.version>
    <mysql.version>8.0.20mysql.version>
    <druid.version>1.1.23druid.version>
    <mybatis.version>3.5.5mybatis.version>
    <pagehelper.version>5.2.0pagehelper.version>
    <spring.version>5.2.7.RELEASEspring.version>
    <mybatis.spring.version>2.0.5mybatis.spring.version>
    <jackson.version>2.11.0jackson.version>
    <fileupload.version>1.4fileupload.version>
    <log4j.version>2.13.3log4j.version>
    <slf4j.version>1.7.30slf4j.version>
    <aspectjweaver.version>1.9.6aspectjweaver.version>
    <jstl.version>1.2jstl.version>
    <hibernate-validator.version>6.1.5.Finalhibernate-validator.version>
    <junit.version>4.12junit.version>

properties>

<dependencies>

    <dependency>
        <groupId>org.projectlombokgroupId>
        <artifactId>lombokartifactId>
        <version>${lombok.version}version>
    dependency>

    <dependency>
        <groupId>javaxgroupId>
        <artifactId>javaee-apiartifactId>
        <version>${javaee.version}version>
        <scope>providedscope>
    dependency>

    <dependency>
        <groupId>jstlgroupId>
        <artifactId>jstlartifactId>
        <version>${jstl.version}version>
    dependency>

    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>${junit.version}version>
        <scope>testscope>
    dependency>

    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>${mysql.version}version>
    dependency>

    <dependency>
        <groupId>com.alibabagroupId>
        <artifactId>druidartifactId>
        <version>${druid.version}version>
    dependency>

    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatisartifactId>
        <version>${mybatis.version}version>
    dependency>

    
    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatis-springartifactId>
        <version>${mybatis.spring.version}version>
    dependency>
    
    <dependency>
        <groupId>com.github.pagehelpergroupId>
        <artifactId>pagehelperartifactId>
        <version>${pagehelper.version}version>
    dependency>

    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-testartifactId>
        <version>${spring.version}version>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webmvcartifactId>
        <version>${spring.version}version>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-jdbcartifactId>
        <version>${spring.version}version>
    dependency>

    
    <dependency>
        <groupId>org.aspectjgroupId>
        <artifactId>aspectjweaverartifactId>
        <version>${aspectjweaver.version}version>
    dependency>


    
    <dependency>
        <groupId>com.fasterxml.jackson.coregroupId>
        <artifactId>jackson-databindartifactId>
        <version>${jackson.version}version>
    dependency>

    
    <dependency>
        <groupId>commons-fileuploadgroupId>
        <artifactId>commons-fileuploadartifactId>
        <version>${fileupload.version}version>
    dependency>

    
    <dependency>
        <groupId>org.hibernate.validatorgroupId>
        <artifactId>hibernate-validatorartifactId>
        <version>${hibernate-validator.version}version>
    dependency>

    
    <dependency>
        <groupId>org.apache.logging.log4jgroupId>
        <artifactId>log4j-coreartifactId>
        <version>${log4j.version}version>
    dependency>

    
    <dependency>
        <groupId>org.slf4jgroupId>
        <artifactId>slf4j-apiartifactId>
        <version>${slf4j.version}version>
    dependency>
    
    <dependency>
        <groupId>org.slf4jgroupId>
        <artifactId>slf4j-log4j12artifactId>
        <version>${slf4j.version}version>
    dependency>
    
    <dependency>
        <groupId>org.slf4jgroupId>
        <artifactId>slf4j-jclartifactId>
        <version>${slf4j.version}version>
    dependency>

dependencies>

1.4 添加配置文件

1.4.1 数据库连接的配置

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/forum?serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=root

1.4.2 日志的配置


<Configuration status="WARN">
   <Appenders>
      <Console name="Console" target="SYSTEM_OUT">
         <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
      Console>
   Appenders>
   
   <Loggers>
      <Root level="debug">
         <AppenderRef ref="Console" />
      Root>
      
   Loggers>
Configuration>

1.4.3 spring整合mybatis的配置



<configuration>

    <settings>
        <setting name="logImpl" value="LOG4J2"/>
    settings>


    
    <typeAliases>
        <package name="com.xuetang9.spring.forum.entity"/>
    typeAliases>

    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            
            <property name="helperDialect" value="mysql"/>
            
            <property name="pageSizeZero" value="true"/>
            
            <property name="reasonable" value="true"/>
        plugin>
    plugins>
configuration>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.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">

    

    
    <context:property-placeholder location="classpath:database.properties"/>

    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    bean>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/>
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <property name="basePackage" value="com.xuetang9.spring.forum.mapper"/>
    bean>

    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    bean>

    
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

    
    <aop:aspectj-autoproxy/>

    
    <context:component-scan base-package="com.xuetang9.spring.forum" use-default-filters="true">
        
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    context:component-scan>

beans>

1.4.4 springmvc的配置


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    bean>

    
    <mvc:default-servlet-handler/>

    
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    bean>

    
    <mvc:annotation-driven/>

    
    <context:component-scan base-package="com.xuetang9.spring.forum" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    context:component-scan>
beans>

1.1.5 web容器加载spring的配置


<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring/spring-mybatis.xmlparam-value>
    context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    
    <servlet>
        <servlet-name>SpringMVCservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring/spring-mvc.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>SpringMVCservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>

    
    <filter>
        <filter-name>encodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
        <init-param>
            <param-name>forceEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

web-app>

1.5 导入页面的静态资源

10-SSM框架整合_第3张图片

1.6 书写页面

1.7 书写Controller

1.8 书写Service

1.9 书写Mapper

1.10 书写Mapper的映射文件

1.11 添加扩展功能(日志)

2. tk.mapper的插件使用

2.1 在pom.xml中配置插件

<plugins>
    <plugin>
        
        <groupId>org.mybatis.generatorgroupId>
        <artifactId>mybatis-generator-maven-pluginartifactId>
        <version>1.3.5version>
        <configuration>
            <configurationFile>src/main/resources/generator-config.xmlconfigurationFile>
            <verbose>trueverbose>
            <overwrite>trueoverwrite>
        configuration>
        <executions>
            <execution>
                <id>generatorid>
                <goals>
                    <goal>generategoal>
                goals>
            execution>
        executions>
        <dependencies>
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>${mysql.version}version>
            dependency>
            <dependency>
                <groupId>tk.mybatisgroupId>
                <artifactId>mapperartifactId>
                <version>3.5.3version>
            dependency>
        dependencies>
    plugin>
plugins>

2.2 配置生成使用的配置



<generatorConfiguration>
    <properties resource="database.properties"/>

    <context id="mysql"
             targetRuntime="tk.mybatis.mapper.generator.TkMyBatis3SimpleImpl"
             defaultModelType="flat">
        
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            
            <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
        plugin>

        
        <jdbcConnection driverClass="${jdbc.driver}"
                        connectionURL="${jdbc.url}"
                        userId="${jdbc.username}"
                        password="${jdbc.password}">
            
            <property name="nullCatalogMeansCurrent" value="true"/>
        jdbcConnection>
        
        <javaModelGenerator targetPackage="${targetModelPackage}" targetProject="${targetJavaProject}"/>
        
        <sqlMapGenerator targetPackage="${targetXMLPackage}" targetProject="${targetResourcesProject}"/>
        
        <javaClientGenerator targetPackage="${targetMapperPackage}" targetProject="${targetJavaProject}" type="XMLMAPPER"/>

        <table tableName="%" mapperName="{0}Mapper">
            <generatedKey column="id" sqlStatement="mysql" identity="false"/>
        table>
    context>
generatorConfiguration>

2.3 运行插件生成实体类,Mapper及映射文件

10-SSM框架整合_第4张图片

你可能感兴趣的:(Java,EE,Java,ssm,java)