详细整合Spring+SpringMVC+MyBatis+logback(SSM)项目

整体目录结构

详细整合Spring+SpringMVC+MyBatis+logback(SSM)项目_第1张图片

表结构

详细整合Spring+SpringMVC+MyBatis+logback(SSM)项目_第2张图片

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/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>demo-mavenartifactId>
        <groupId>com.fugroupId>
        <version>1version>
    parent>
    <modelVersion>4.0.0modelVersion>
    <packaging>warpackaging>

    <artifactId>spring-springmvc-mybatis-demoartifactId>

    <dependencies>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
        dependency>
        
        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-classicartifactId>
        dependency>
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <scope>providedscope>
        dependency>
        
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
        dependency>
        
        
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>com.mysqlgroupId>
            <artifactId>mysql-connector-jartifactId>
            <version>8.0.33version>
            <scope>runtimescope>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.2.16version>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.13version>
            <scope>compilescope>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>2.1.1version>
        dependency>
        
        <dependency>
            <groupId>com.github.pagehelpergroupId>
            <artifactId>pagehelperartifactId>
            <version>5.3.2version>
        dependency>
    dependencies>

    <build>
        <finalName>ssmfinalName>
    build>

    
    <profiles>
        <profile>
            <id>devid>
            <build>
                <filters>
                    <filter>src/main/resources/environment/dev.propertiesfilter>
                filters>
            build>
            <activation>
                <activeByDefault>trueactiveByDefault>
            activation>
        profile>
        <profile>
            <id>testid>
            <build>
                <filters>
                    <filter>src/main/resources/environment/test.propertiesfilter>
                filters>
            build>
        profile>
        <profile>
            <id>prodid>
            <build>
                <filters>
                    <filter>src/main/resources/environment/prod.propertiesfilter>
                filters>
            build>
        profile>
    profiles>

project>

新建src/main/webapp/WEB-INF/web.xml


<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">
    
    <filter>
        <filter-name>CharacterEncodingFilterfilter-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>CharacterEncodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

    
    <filter>
        <filter-name>HiddenHttpMethodFilterfilter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
    filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

    
    <servlet>
        <servlet-name>springMVCservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath: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>

    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring.xmlparam-value>
    context-param>

web-app>

新建/src/main/resources/spring.xml


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

    
    <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init">
        <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="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="druidDataSource"/>
    bean>

    
    <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>

    
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="druidDataSource"/>
        
        <property name="typeAliasesPackage" value="com.fu.ssm.entity"/>
        
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        
        <property name="configurationProperties">
            <props>
                
                <prop key="mapUnderscoreToCamelCase">trueprop>
                <prop key="jdbcTypeForNull">NULLprop>
                <prop key="logImpl">SFL4Jprop>
                <prop key="loggingLevel">INFOprop>
            props>
        property>
        
        <property name="plugins">
            <array>
                
                <bean class="com.github.pagehelper.PageInterceptor"/>
                
                <bean class="com.fu.ssm.base.MyBatisSQLInterceptor"/>
            array>
        property>
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.fu.ssm.mapper"/>
    bean>

beans>

新建/src/main/resources/spring-mvc.xml


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

    
    <context:component-scan base-package="com.fu.ssm"/>

    
    <mvc:default-servlet-handler/>

    
    <mvc:annotation-driven/>

    
    

    
    
beans>

新建/src/main/resources/logback.xml


<configuration scan="true" scanPeriod="60 seconds" debug="false">    
    <property name="CONSOLE_LOG_PATTERN" value="[%d{HH:mm:ss} %p %.10t] %C{39}\.%M\\(\\): %m%n"/>

    
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder" charset="UTF-8">
            
            <pattern>${CONSOLE_LOG_PATTERN}pattern>
        encoder>
    appender>
    
    <logger name="com.fu.ssm.mapper" level="DEBUG" />

    <root level="INFO">
        <appender-ref ref="CONSOLE" />
    root>

configuration>

新建/src/main/resources/environment/dev.properties、test.properties、prod.properties

目前dev.properties、test.properties、prod.properties这三个文件的内容都是一样的,按需配置。

jdbc.driver=com.mysql.cj.jdbc.Driver
# 把&替换成&amp;(注意分号也是其中的一部分)
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true&amp;useSSL=false&amp;serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=123456

新建/src/main/java/com/fu/ssm/base/MyBatisSQLInterceptor.java

打印SQL拦截器,不太建议使用,建议使用logback logger配合properties区分环境使用

package com.fu.ssm.base;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

/**
 * MyBatis输出日志
 */
@Slf4j
@Intercepts({@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})})
public class MyBatisSQLInterceptor implements Interceptor {
    private static final ObjectMapper om = new ObjectMapper();

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        //如果输出中文乱码,则需要添加Tomcat JVM参数-Dfile.encoding=UTF-8
        Object[] args = invocation.getArgs();
        if (args != null && args.length > 1) {
            MappedStatement mappedStatement = (MappedStatement) args[0];
            Object parameter = args[1];
            BoundSql boundSql = mappedStatement.getBoundSql(parameter);
            String sql = boundSql.getSql();
            log.info("params:{}\nsql:{}", om.writeValueAsString(parameter), sql);
        }
        return invocation.proceed();
    }
}

新建/src/main/java/com/fu/ssm/base/PageDTO.java分页基类

用于DTO继承PageDTO实现分页

import com.github.pagehelper.IPage;
import lombok.Data;

@Data
public class PageDTO implements IPage {
    private Integer page;//起始页
    private Integer size;//每页数量
    private String orderBy;//排序

    /**
     * 初始化的时候就设置默认参数,如果不传则默认起始页为1,条数为10。
     */
    public PageDTO(){
        this.page=1;
        this.size=10;
    }

    @Override
    public Integer getPageNum() {
        return page;
    }

    @Override
    public Integer getPageSize() {
        return size;
    }

    @Override
    public String getOrderBy() {
        return orderBy;
    }

}

新建/src/main/java/com/fu/ssm/controller/TestController.java

标准的controller,省略了新增/修改/删除接口。

import com.fu.ssm.dto.TestDTO;
import com.fu.ssm.entity.Test;
import com.fu.ssm.service.TestService;
import com.github.pagehelper.PageSerializable;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("test")
@RequiredArgsConstructor
public class TestController {
    private final TestService testService;

    /**
     * 通过test表的id获取test信息
     * @param id 主键
     */
    @RequestMapping(value = "selectTestById", method = RequestMethod.POST)
    @ResponseBody
    public Test selectByTestId(@RequestBody Integer id) {
        return testService.selectByTestId(id);
    }

    /**
     * 分页查询test表记录
     * @param testDTO test数据传输对象
     */
    @RequestMapping(value = "selectTestPage", method = RequestMethod.POST)
    @ResponseBody
    public PageSerializable<Test> selectTestPage(@RequestBody TestDTO testDTO) {
        return testService.selectTestPage(testDTO);
    }

}

新建/src/main/java/com/fu/ssm/service/TestService.java

标准service接口

import com.github.pagehelper.PageSerializable;

public interface TestService {

    Test selectByTestId(Integer id);

    PageSerializable<Test> selectTestPage(TestDTO testDTO);
}

新建/src/main/java/com/fu/ssm/service/impl/TestServiceImpl.java

标准service接口实现类

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageSerializable;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class TestServiceImpl implements TestService {
    private final TestMapper testMapper;

    @Override
    public Test selectByTestId(Integer id) {
        log.debug("debug");
        log.info("info");
        log.warn("warn");
        log.error("warn");
        return testMapper.selectByTestId(id);
    }

    @Override
    public PageSerializable<Test> selectTestPage(TestDTO testDTO) {
        //PageHelper底层使用ThreadLocal防止并发造成数据问题,因此用完要关闭ThreadLocal。
        PageHelper.startPage(testDTO).close();
        //PageInfo会返回更为详细的内容,PageSerializable只返回list和total。
//        log.info("{}", PageInfo.of(testMapper.selectTestPage(testDTO)));
        return PageSerializable.of(testMapper.selectTestPage(testDTO));
    }

}

新建/src/main/java/com/fu/ssm/mapper/TestMapper.java

标准对象关系映射

import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TestMapper {
    Test selectByTestId(Integer id);

    List<Test> selectTestPage(TestDTO testDTO);
}

新建/src/main/resources/mapper/TestMapper.xml

mybatis动态SQL


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.fu.ssm.mapper.TestMapper">

    <select id="selectByTestId" parameterType="java.lang.Integer" resultType="com.fu.ssm.entity.Test">
        select id,name,age from test where id = #{id}
    select>

    <select id="selectTestPage" resultType="com.fu.ssm.entity.Test">
        select id,name,age from test
        <where>
            <if test="name != null and name != ''">
                <bind name="username" value="'%' + name + '%'"/>
                and name like #{username}
            if>
        where>
    select>

mapper>

新建/src/main/java/com/fu/ssm/entity/Test.java

实体类

import lombok.Data;

@Data
public class Test {
    private Integer id;
    private String name;
    private Integer age;
}

新建/src/main/java/com/fu/ssm/dto/TestDTO.java

实体类数据传输对象

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * test数据传输对象
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class TestDTO extends PageDTO {
    private String name;//名称
}

配置Tomcat

详细整合Spring+SpringMVC+MyBatis+logback(SSM)项目_第3张图片

虚拟机选项

-Xms256m
-Xmx512m
-XX:ReservedCodeCacheSize=256m
-Dfile.encoding=UTF-8

详细整合Spring+SpringMVC+MyBatis+logback(SSM)项目_第4张图片

部署

详细整合Spring+SpringMVC+MyBatis+logback(SSM)项目_第5张图片
详细整合Spring+SpringMVC+MyBatis+logback(SSM)项目_第6张图片

启动

详细整合Spring+SpringMVC+MyBatis+logback(SSM)项目_第7张图片

测试接口和SQL输出

详细整合Spring+SpringMVC+MyBatis+logback(SSM)项目_第8张图片

详细整合Spring+SpringMVC+MyBatis+logback(SSM)项目_第9张图片

多环境

详细整合Spring+SpringMVC+MyBatis+logback(SSM)项目_第10张图片
MyBatisSQLInterceptor和logback logger配置SQL输出二者选其一即可,建议用logback logger

你可能感兴趣的:(Java,spring,mybatis,logback)