ssm快速入门框架使用,一篇解决环境搭建快速上手,拿来即用

Posted 吃螃蟹的小孩

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssm快速入门框架使用,一篇解决环境搭建快速上手,拿来即用相关的知识,希望对你有一定的参考价值。

SSM整合配置

开发规划

环境要求

Idea,mysql5.7,Tomcat 9,Maven 3.8,Lombok


配置流程

项目搭建->MySQL->Mybatis->Spring->SpringMVC->web.xml


搭建项目目录

  • com.cxy.pojo 实体类(映射数据表)
  • com.cxy.mapper 数据操作层(数据库CRUD)
  • com.cxy.service 业务层
  • com.cxy.controller 控制层

配置文件

项目搭建

  • pom.xml中导入jar包
  • pom.xml中设置Maven资源过滤

MySQL

  • 搭建数据库和数据表

Mybatis

mybatis-config.xml

配置、连接、绑定、操作数据库

  • 用于配置数据源(交给Spring管理)
  • 日志输出
  • 数据库相关配置
  • 数据关联:数据表与pojo关联
  • 数据操作:与mapper(Dao)层的mapper.xml关联CRUD操作

database.properties

用于配置数据库连接的用户名和密码


Spring

applicationContext.xml

Spring 整合配置文件

  • 用于整合spring-dao.xml,spring-service.xml,spring-mvc.xml

spring-dao.xml

与dao层关联

  • 关联数据库配置文件:database.properties
  • 配置数据库连接池
  • 配置SqlSessionFactory对象,绑定mybatis-config.xml文件
  • 配置扫描dao层包,动态实现dao接入注入spring容器

spring-service.xml

与service层关联

  • 配置扫描service层包
  • 将service层注入实现类注入IOC容器
  • 配置事务管理器
  • 配置AOP事务支持

SpringMVC

spring-mvc.xml

与controller关联

  • 开启注解扫描,解决json乱码问题
  • 配置静态资源默认servlet处理
  • 配置视图解析器
  • 配置扫描controller层包

web.xml

  • 配置SpringMVC分发器:DispatcherServlet
  • 配置乱码过滤
  • 配置Session过期时间

配置数据库

在这里插入图片描述

database.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/student_manager?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456

搭建项目目录

在这里插入图片描述

用maven导入jar包

<dependencies>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.18</version>
        </dependency>
        <!--Junit白盒测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!--数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <!-- 数据库连接池 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>

        <!--Servlet - JSP -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!--Mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>

        <!--Spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.4</version>
        </dependency>
        <!--AOP-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.9</version>
        </dependency>
        <!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
<!--        log4j-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

Mybatis

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
        <!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" />
        <setting name="logImpl" value="LOG4J" />
    </settings>
    <typeAliases>
        <package name="com.cxy.pojo"/>
    </typeAliases>
    <mappers>
        <mapper resource="com/cxy/mapper/mapperXml/UsersMapper.xml"/>
        <mapper resource="com/cxy/mapper/mapperXml/InstituteMapper.xml"/>
        <mapper resource="com/cxy/mapper/mapperXml/DomainsMapper.xml"/>
    </mappers>
</configuration>

Spring

spring-dao.xml

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

        <!-- 配置整合mybatis -->
        <!-- 1.关联数据库文件 -->
        <context:property-placeholder location="classpath:database.properties"/>

        <!-- 2.数据库连接池 -->
        <!--数据库连接池
        dbcp 半自动化操作 不能自动连接
        c3p0 自动化操作(自动的加载配置文件 并且设置到对象里面)
-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <!-- 配置连接池属性 -->
            <property name="driverClass" value="${jdbc.driver}"/>
            <property name="jdbcUrl" value="${jdbc.url}"/>
            <property name="user" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>

            <!-- c3p0连接池的私有属性 -->
            <property name="maxPoolSize" value="30"/>
            <property name="minPoolSize" value="10"/>
            <!-- 关闭连接后不自动commit -->
            <property name="autoCommitOnClose" value="false"/>
            <!-- 获取连接超时时间 -->
            <property name="checkoutTimeout" value="10000"/>
            <!-- 当获取连接失败重试次数 -->
            <property name="acquireRetryAttempts" value="2"/>
        </bean>

        <!-- 3.配置SqlSessionFactory对象 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- 注入数据库连接池 -->
            <property name="dataSource" ref="dataSource"/>
            <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
            <property name="configLocation" value="classpath:mybatis-config.xml"/>

        </bean>

        <!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!-- 注入sqlSessionFactory -->
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
            <!-- 给出需要扫描Dao接口包 -->
            <property name="basePackage" value="com.cxy.mapper"/>
        </bean>
</beans>

spring-service.xml

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

">

        <!-- 扫描service相关的bean -->
        <context:component-scan base-package="com.cxy.service" />

        <!--把ServiceImpl注入到IOC容器中-->
        <bean id="usersServiceImpl" class="com.cxy.service.impl.UsersServiceImpl">
            <property name="usersMapper" ref="usersMapper"/>
        </bean>
        <bean id="domainsServiceImpl" class="com.cxy.service.impl.DomainsServiceImpl">
            <property name="domainsMapper" ref="domainsMapper"/>
        </bean>
        <bean id="instituteServiceImpl" class="com.cxy.service.impl.InstituteServiceImpl">
            <property name="instituteMapper" ref="instituteMapper"/>
        </bean>

        <!-- 配置事务管理器 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!-- 注入数据库连接池 -->
            <property name="dataSource" ref="dataSource" />
        </bean>

</beans>

spring-mvc.xml

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

        <!-- 配置SpringMVC -->
        <!-- 1.开启SpringMVC注解驱动 -->
        <mvc:annotation-driven >
            <mvc:message-converters>
                <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html;charset=UTF-8</value>
                            <value>application/json;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>

        <!-- 2.静态资源默认servlet配置-->
        <mvc:default-servlet-handler/>

        <!-- 3.配置jsp 显示ViewResolver视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
         <property name="prefix" value="/WEB-INF/jsp/"/>
         <property name="suffix" value=".jsp"/>
        </bean>

        <!-- 4.

以上是关于ssm快速入门框架使用,一篇解决环境搭建快速上手,拿来即用的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot 快速入门案例

AngularJS入门之如何快速上手

使用Springboot快速搭建SSM框架

Maix Bit(K210)保姆级入门上手教程---环境搭建

WPF 通过 Windows Template Studio 快速搭建项目框架和上手项目

App逆向入门