SSM+Maven+MySQL项目的搭建

Posted 火星的巧克力

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM+Maven+MySQL项目的搭建相关的知识,希望对你有一定的参考价值。

项目环境:Windows10+mysql5.7+Tomcat8.5+Maven3.6+IDER

Mysql / Tomcat / Maven的安装及配置在此省略

 

IDER创建一个maven-archetype-webapp项目

项目目录 

 

java文件夹存放代码

resources中存放配置文件

WEB-INF存放受保护的页面(WEB-INF中的资源不可以通过浏览器地址栏直接访问)

pom.xml中的依赖包

<?xml version="1.0" encoding="UTF-8"?>

<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.0</modelVersion> <groupId>cn.lzy</groupId> <artifactId>SSM_demo01</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>SSM_demo01 Maven Webapp</name> <!-- FIXME change it to the project\'s website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <!-- 指定jar版本 4.3.18.RELEASE --> <spring.version>4.3.18.RELEASE</spring.version> </properties> <dependencies>
  <!-- Spring核心 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency>
  <!-- Spring DAO --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency>
  <!-- Spring webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <!-- MyBatis所有基础类库 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.2</version> </dependency> <!-- mybatis-spring整合包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- mysql-connector-java用来支持JDBC与mysql的交互 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> <scope>runtime</scope> </dependency> <!-- 连接池所需jar包 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency>
<!-- json解析 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.8</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>SSM_demo01</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>

jdbc.properties

 

#MySQL 连接配置
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
jdbc.user=root
jdbc.password=root

 

 

 

applicationContext.xml 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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        ">

    <!--  开启组件扫描  -->
    <context:component-scan base-package="cn.lzy"></context:component-scan>
    <!--  开启标注形式的MVC -->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!-- 配置数据相关参数properties的属性:${url} -->
    <context:property-placeholder location="classpath:jdbc.properties" />
    <!--  MySQL连接池  -->
    <bean  id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass">
            <value>${jdbc.driverClass}</value>
        </property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!-- 创建sqlSessionFactory -->
    <bean id="sqlSessionFactory"  class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource"  ref="dataSource"></property>
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>
    <!-- 批量产生Mapper实现类的组件 -->
    <bean  id="mapperScanner"  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage"  value="cn.lzy.dao"></property>
    </bean>

    <!-- 配置ViewResolver -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"></property>
        <property name="suffix" value=".html"></property>
    </bean>

    <!-- 创建事务管理器  -->
    <bean  id="transactionManager"
           class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource"  ref="dataSource"></property>
    </bean>
    <!--  开启声明式事务  -->
    <tx:annotation-driven  transaction-manager="transactionManager"
                           proxy-target-class="true" />

    <!--  放行静态资源 -->
    <mvc:default-servlet-handler/>

</beans>

 

 web.xml配置 (一般默认的version=“2.3”  https://blog.csdn.net/molei199082/article/details/79956746 访问这个博客可以提供IDER修改web.xml版本的配置方法)

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <!-- 配置DispathcherServlet -->
    <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:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

  <!-- 指定匹配规则,默认将所有url请求都会转入到DispatcherServlet中去处理, 在spring-web中已经过滤掉一些请求了,因此这里可以直接这样配(/ 默认匹配所有请求) --> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

 

 这些就是SSM框架的基本搭建

 

以上是关于SSM+Maven+MySQL项目的搭建的主要内容,如果未能解决你的问题,请参考以下文章

Maven 搭建 SSM 项目 (oracle)

简单搭建一个SSM项目

SSM+Maven+eclipse环境搭建

day01-项目介绍+SSM环境搭建

Eclipse下SSM项目的搭建

SSM Spring+SpringMVC+mybatis+maven+mysql环境搭建