SSM中的模板

提示:此为观看动力节点的教学视频后的笔记 ·上

文章目录

  • 前言
  • 一、pom.xml
  • 二、jdbc.properties
  • 三、SqlMapConfig.xml
  • 四、applicationContext_mapper.xml
  • 总结


前言

Spring SpringMVC Mybatis:

在使用SSM时,我们常常会用到很多模板,这篇文章便来记录这些常用的模板。


以下是本篇文章正文内容,下面模板可供参考

一、pom.xml

maven项目中的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">
  <modelVersion>4.0.0modelVersion>

  <groupId>自定义groupId>
  <artifactId>maven项目名artifactId>
  <version>1.0version>
  <packaging>warpackaging>

  
  <properties>
    
    <junit.version>4.12junit.version>
    
    <spring.version>5.2.5.RELEASEspring.version>
    
    <mybatis.version>3.5.1mybatis.version>
    
    <mybatis.spring.version>1.3.1mybatis.spring.version>
    
    <mybatis.paginator.version>1.2.15mybatis.paginator.version>
    
    <mysql.version>8.0.16mysql.version>
    
    <slf4j.version>1.6.4slf4j.version>
    
    <druid.version>1.1.12druid.version>
    
    <pagehelper.version>5.1.2pagehelper.version>
    
    <jstl.version>1.2jstl.version>
    
    <servlet-api.version>3.0.1servlet-api.version>
    
    <jsp-api.version>2.0jsp-api.version>
    
    <jackson.version>2.9.6jackson.version>
  properties>

  <dependencies>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-beansartifactId>
      <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.springframeworkgroupId>
      <artifactId>spring-aspectsartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jmsartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-context-supportartifactId>
      <version>${spring.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.miemiedevgroupId>
      <artifactId>mybatis-paginatorartifactId>
      <version>${mybatis.paginator.version}version>
    dependency>
    <dependency>
      <groupId>com.github.pagehelpergroupId>
      <artifactId>pagehelperartifactId>
      <version>${pagehelper.version}version>
    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>junitgroupId>
      <artifactId>junitartifactId>
      <version>${junit.version}version>
      <scope>testscope>
    dependency>

    
    <dependency>
      <groupId>jstlgroupId>
      <artifactId>jstlartifactId>
      <version>${jstl.version}version>
    dependency>
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>3.0.1version>
      <scope>providedscope>
    dependency>
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>jsp-apiartifactId>
      <version>${jsp-api.version}version>
      <scope>providedscope>
    dependency>

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

    
    <dependency>
      <groupId>commons-iogroupId>
      <artifactId>commons-ioartifactId>
      <version>2.4version>
    dependency>
    <dependency>
      <groupId>commons-fileuploadgroupId>
      <artifactId>commons-fileuploadartifactId>
      <version>1.3.1version>
    dependency>
  dependencies>

  
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-compiler-pluginartifactId>
        <configuration>
          <source>1.8source>
          <target>1.8target>
          <encoding>UTF-8encoding>
        configuration>
      plugin>
    plugins>

    
    <resources>
      <resource>
        <directory>src/main/javadirectory>
        <includes>
          <include>**/*.xmlinclude>
          <include>**/*.propertiesinclude>
        includes>
        <filtering>falsefiltering>
      resource>
      <resource>
        <directory>src/main/resourcesdirectory>
        <includes>
          <include>**/*.xmlinclude>
          <include>**/*.propertiesinclude>
        includes>
        <filtering>falsefiltering>
      resource>
    resources>
  build>

project>

二、jdbc.properties

jdbc.properties文件放在resources文件夹中

位置src / main / java / resources / jdbc.properties

注意:jdbc.properties存放的是连接池用到的值,我这里的值用于连接MySQL8.0
(MySQL8.0以上和MySQL5.x在连接配置上有差别,请自行上网了解)

jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmuser?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
jdbc.username=root
jdbc.password=root

三、SqlMapConfig.xml

MyBatis要用的配置文件,放到resources文件夹中


DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

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

configuration>

四、applicationContext_mapper.xml

resources文件夹


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

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

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

    
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource"/>
        
        <property name="configLocation" value="classpath:SqlMapConfig.xml"/>
        
        <property name="typeAliasesPackage" value="包路径.pojo包"/>
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="包路径.mapper包"/>
    bean>
beans>

总结

ssm项目需要用到的配置文件比较多,总会有遗漏的地方,这时通过模板来补遗,是个很不错的选择。

此处为笔记(上),下期的笔记(下)包含三个:

  • applicationContext_service.xml
  • springmvc.xml
  • web.xml

你可能感兴趣的:(maven,spring,intellij,idea)