前置技能:

  • MySQL数据库
  • Spring、SpringMVC、MyBatis基本框架
  • Maven自动化构建工具
  • MyBatis插件:
    • 分页:pageHepler
    • 逆向工程(MBG):MyBatis Generator

软件版本:

JDK Tomcat Maven
1.8.0_291 8.5.69 3.8.1

创建工程

使用IDEAEclipse等IDE创建Maven项目的的动态web工程

资源引入

引入SSM项目依赖

数据库连接基本依赖

<!--数据库驱动-MySQL数据库-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.25</version>
</dependency>

<!--数据库连接池-德鲁伊(可选则其他连接池)-->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.6</version>
</dependency>

MyBatis框架相关依赖

<!--MyBatis-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.7</version>
</dependency>

<!--MyBatis整合Spring适配包-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.6</version>
</dependency>

Spring框架相关依赖

<!--SpringMVC-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.8</version>
</dependency>

<!--Spring事务控制-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.3.8</version>
</dependency>

<!--Spring面向切面编程-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>5.3.8</version>
</dependency>

web工程使用的基本依赖(注意依赖的作用范围)

<!--JSTL标准标签库-->
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

<!-- servlet-api(根据服务器支持的版本导入不同版本)-->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>

<!-- jsp-api-->
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.3</version>
    <scope>provided</scope>
</dependency>


<!--单元测试测试,4.12以上版本(项目创建时自动导入)-->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12/version> 
    <scope>test</scope>
</dependency>

其他依赖–>非必须

<!--MyBatis的代码生成器-->
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.4.0</version>
</dependency>


<!--SpringTest模块,注意使用范围-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.3.8</version>
    <scope>compile</scope>
</dependency>


<!--MyBatis第三方分页插件-->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.2.1</version>
</dependency>

<!--返回Json字符串的支持-->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.12.4</version>
</dependency>

<!--支持JSR303校验,注意:Tomcat7.0以下的服务器需要给服务器lib包替换新标准的el表达式-->
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.4.1.Final</version>
</dependency>

引入前端框架

框架选择:Bootstrap,当前版本3.4.1

使用方法:查看官网基本模板,参考者导入 Bootstrap核心CSS文件Bootstrap 核心 JavaScript文件,由于Bootstrap的所有JavaScript插件都依赖jQuery,需要导入jQuery文件,见官网说明

需要注意的标签属性

integrity:校验资源,防止CDN污染数据

crossorigin:支持跨域

<html>
<head>
  <%--  Bootstrap 核心 CSS 文件  --%>

  <link rel="stylesheet" href="static/bootstrap-3.4.1-dist/css/bootstrap.min.css"
        integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" 
        crossorigin="anonymous">

  <%-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) --%>

  <script src="static/js/jquery.min.js"
          integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ"
          crossorigin="anonymous"></script>

  <%-- Bootstrap 核心 JavaScript 文件 --%>

  <script src="static/bootstrap-3.4.1-dist/js/bootstrap.min.js"
          integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
          crossorigin="anonymous"></script>

  <title>SSM整合主页</title>
</head>
<body>
  没错,又是传统的Hello World!
</body>
</html>

以上引用资源路径根据自己的项目分级进行调整

编写SSM整合配置文件

Web配置文件编写

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         id="WebApp_ID" version="3.1">
  <!--配置Spring的容器-->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:config/Spring_config.xml</param-value>
  </context-param>
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>


  <!--配置字符编码过滤器,一定要放在所有过滤器之前-->
  <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>
      <init-param>
          <param-name>forceRequestEncoding</param-name>
          <param-value>true</param-value>
      </init-param>
      <init-param>
          <param-name>forceResponseEncoding</param-name>
          <param-value>true</param-value>
      </init-param>
  </filter>
  <filter-mapping>
      <filter-name>characterEncodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!--配置支持REST风格的过滤器,可以根据隐藏域中内容将请求从POST转换为DELETE与PUT-->
  <filter>
      <filter-name>HiddenHttpMethodFilter</filter-name>
      <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>HiddenHttpMethodFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>


  <!--
  将请求体中的数据解析包装成一个map,
  request被重新包装,request.getParameter()被重写,
  就会从自己封装的map中取数据可以直接使用PUT的请求
  -->
  <filter>
      <filter-name>formContentFilter</filter-name>
      <filter-class>org.springframework.web.filter.FormContentFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>formContentFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>


  <!--配置SpringMVC前端控制器-->
  <servlet>
      <servlet-name>DispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:config/SpringMVC_config.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>DispatcherServlet</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

Spring配置文件编写

<?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: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.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 http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

  <!--Spring配置文件 - - 业务逻辑相关配置-->


  <!--开启组件扫描(不扫描Controller层)-->
  <context:component-scan base-package="io.github.sheng1000">
      <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  </context:component-scan>

  <!-- 导入数据库配置信息,采用配置文件的方式,请根据自己的配置文件进行更改 -->
  <context:property-placeholder location="classpath:config/JdbcPool.properties"></context:property-placeholder>

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

  <!--事务控制配置-->
  <bean id="manager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <!--控制数据源-->
      <property name="dataSource" ref="dataSource"></property>
  </bean>

  <!--开启基于注解的事务,使用xml配置形式的事务(重要的使用配置式)-->
  <aop:config>
      <!--配置切入点-->
      <aop:pointcut id="txPoint" expression="execution(* io.github.sheng1000.service..*(..))"/>
      <!--配置切面-->
      <aop:advisor advice-ref="txPlusMethod" pointcut-ref="txPoint"></aop:advisor>
  </aop:config>

  <!--配置通知-->
  <tx:advice id="txPlusMethod" transaction-manager="manager">
      <tx:attributes>
          <!--指定切入点切入的所有方法均为事务方法-->
          <tx:method name="*"/>
          <!--切入点切入的get方法为事务方法,并且做了只读调优-->
          <tx:method name="get*" read-only="true"></tx:method>
      </tx:attributes>
  </tx:advice>

  <!--整合MyBatis-->
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

      <property name="dataSource" ref="dataSource"></property>

      <!--指定MyBatis全局配置文件的位置-->
      <property name="configLocation" value="classpath:config/MyBatis_config.xml"></property>

      <!--指定mapper文件位置-->
      <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
  </bean>

  <!--指定mybatis接口的实现加入Spring的IOC容器中,实现自动注入-->
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="basePackage" value="io.github.sheng1000.dao"></property>
  </bean>

  <!--创建一个可以执行批量操作的sqlSession-->
  <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
      <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
      <constructor-arg name="executorType" value="BATCH"></constructor-arg>
  </bean>
</beans>

SpringMVC配置文件编写

<?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: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">


  <!--SpringMVC配置文件 - - 管理网站网页跳转逻辑-->

  <!--开启组件扫描(只扫描Controller层)-->

  <context:component-scan base-package="io.github.sheng1000" use-default-filters="false">
      <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  </context:component-scan>

  <!--配置视图解析器-->

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


  <!--其他通用配置,处理静态资源等-->


  <!--将SpringMVC不能处理的请求交给服务器的默认处理器处理(注意服务器默认处理器名称)-->
  <mvc:default-servlet-handler></mvc:default-servlet-handler>
  <!--支持Spring高级功能,映射动态请求-->
  <mvc:annotation-driven></mvc:annotation-driven>
</beans>

MyBatis配置文件编写

编写MyBatis配置文件的方式有两种,本次采用xml文件的方式导入配置

<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
  <configuration>
      <settings>
          <!--开启驼峰命名法-->
          <setting name="mapUnderscoreToCamelCase" value="true"/>
      </settings>

      <!--配置分页插件-->
      <plugins>
          <plugin interceptor="com.github.pagehelper.PageInterceptor">
              <!--分页合理化参数-->
              <property name="reasonable" value="true"/>
          </plugin>
      </plugins>
  </configuration>

MyBatisMBG配置文件编写

代码生成器初体验,根据指定数据库表格生成相应的bean接口映射文件
提供Criteria类自定义SQL语句
MyBatis-PLUS中也提供了代码生成器,并且支持全java代码生成,不需要另外写配置文件,用起来更加舒服

<?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>
    <context id="MySQL8" targetRuntime="MyBatis3">

        <!--生成代码时不自动添加注释-->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--配置数据库信息-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:8848/ssm_study"
                        userId="root"
                        password="hrssheng">
        </jdbcConnection>

        <!--指定JDBC类型与Java类型转化规则,一般默认即可,不需要配置-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!--指定模型生成的位置-->
        <javaModelGenerator targetPackage="io.github.sheng1000.bean" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--指定sql映射文件生成的位置-->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--指定dao接口生成的位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="io.github.sheng1000.dao"
                              targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!--指定表的生成策略-->
        <table tableName="emp" domainObjectName="Employee"></table>
        <table tableName="dept" domainObjectName="Department"></table>
    </context>
  </generatorConfiguration>

配套的Java代码

public class MBGTest {
  public static void main(String[] args) throws Exception{
      List<String> warnings = new ArrayList<String>();
      boolean overwrite = true;
      //读取配置文件
      File configFile = new File("src/main/resources/config/MyBatisMBG_config.xml");
      ConfigurationParser cp = new ConfigurationParser(warnings);
      Configuration config = cp.parseConfiguration(configFile);
      DefaultShellCallback callback = new DefaultShellCallback(overwrite);
      MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
      myBatisGenerator.generate(null);
    }
}

后续

  1. 写一个测试用JSP动态页面
  2. ajax异步发送请求
  3. rest风格使用
  4. 前后端数据校验统一
  5. 经典Crud测试