手把手教你如何玩转SSM框架整合(非Maven版本)

      关于目前比较流行的框架,前面的文章都有进行了相关内容的介绍,所以在这里的话主要就是介绍一下关于SSM,即Spring,springMVC和Mybatis的整合知识点。

自身搭建环境:windows7+IDEA+Mysql 5.7

github项目源码地址https://github.com/qq496616246/SpringSpringMVCMybatis

如果你想看其他的环境搭建,那么请参考我的其余博文。

手把手教你阿里云服务器(Ubuntu系统)如何部署Web开发环境
地址:https://blog.csdn.net/cs_hnu_scw/article/details/79125582
手把手教你如何玩转SSH(Spring+Strus2+Hibernate)
地址:https://blog.csdn.net/cs_hnu_scw/article/details/77888051
手把手教你如何玩转SSH(Spring+SpringMVC+Hibernate)
地址:https://blog.csdn.net/cs_hnu_scw/article/details/78849772
手把手教你如何玩转SSM框架搭建(Maven版本)
地址:https://blog.csdn.net/cs_hnu_scw/article/details/80709822
手把手教你如何玩转SSM框架整合(非Maven版本)
地址:https://blog.csdn.net/cs_hnu_scw/article/details/78157672
手把手教你如何玩转SpringBoot整合Mybatis(mapper.xml方式开发)
地址:https://blog.csdn.net/cs_hnu_scw/article/details/80693248
手把手教你如何玩转SpringBoot整合MyBatis(mybatis全注解开发和热部署及其JSP配置详解)
地址:https://blog.csdn.net/cs_hnu_scw/article/details/78961232

整合Spring+SpringMVC+Mybatis

步骤:

一:首先:配置Web项目的相关内容

(1)通过IDEA创建一个Web工程,然后命令工程名字即可


(2)在创建后的项目的web目录下面,创建一个classes和jsp以及lib的文件目录


(3)配置文件夹路径。点击File-》Project Structure或者快捷键(ctrl + shift + Alt + s)选择Modules-> 选择Paths -> 选择“Use module compile out path” -> 将Outputpath 和Test output path 都设置为刚刚创建的classes文件夹。


(4)
1首先,选择当前窗口的Dependencies -> 将Module SDK选择为1.6 ->点击右边的 + 号 -> 选择 “1 JARS or directories ...”
2其次: 选择刚刚创建的lib文件夹 -> OK
3最后: 选择Jar Directory -> OK


结果如下图所示:


二:其次,下面的话就配置web项目运行所需要的Tomcat。

(1)点击IDEA的右上角


(2)其次,


(3)

然后选中我们的那个项目对应的explore即可。

(4)然后,再点击server进行配置下面的内容。


(5)到这个时候,我们的Web环境和Tomcat都配置好了,可以点击右上角的绿色按钮直接运行。如果一切配置正确的话,那么就会默认显示index.jsp(这个JSP页面是我们创建项目就默认生成有的)里面的内容。

三:关键整合的地方(请认真看):

首先,放一下整个的项目结构图,让大家有一个整体的了解!


首先,进行配置文件的配置内容。创建一个resouce文件目录,用于存放所有的配置文件,这可以方便我们以后对配置文件的管理。

********首先,要导包哦。这个可不能忘记了,具体的包我会放在最后源码上面。

(1)在resource目录下创建一个mapperxmlconfig文件,用于存放mybatis的mapper映射文件。

(2)在resource目录下创建一个mybatisconfig文件,用于配置Mybatis.xml文件,命名为SqlMapConfig.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>
    <!-- 这里面你可以配置一下关于JavaBean中类的一些别名,这样就方便在之后的Mapper文件中进行处理 -->
    <typeAliases>
        <package name="hnu.scw.pojo"/>
    </typeAliases>
</configuration>

(3)在resource目录下创建一个springconfig的文件,这里存放之后的关于spring的一些配置文件

(4)在springconfig文件中,创建applicationContex-dao.xml文件,主要是配置:加载properties文件,数据源,SqlSessionFactoryMapper扫描

(如果大家喜欢将所有的关于spring的内容都配置在一个xml中也是可以的,看个人喜好,如果想分开进行管理就分开创建不同的xml文件即可。)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!-- 配置 读取properties文件 jdbc.properties -->
    <context:property-placeholder location="classpath:resource/jdbc.properties" />
    <!-- 自动扫描所有的注解 -->
    <context:component-scan base-package="com.hnu.scw"/>

    <!-- 配置阿里的druid数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <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>

    <!-- 配置SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 设置数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 设置MyBatis核心配置文件 -->
        <property name="configLocation" value="classpath:resource/mybatisconfig/SqlMapConfig.xml" />
        <!-- 设置扫描mapper.xml文件 -->
       <property name="mapperLocations" value="classpath:resource/mapperxmlconfig/*Mapper.xml"/>
    </bean>

    <!-- 配置Mapper层java类扫描 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 设置Mapper扫描包 -->
        <property name="basePackage" value="com.hnu.scw.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
</beans>

(5)在springconfig文件中,创建管理service层的文件,命名为ApplicationContext-service.xml(这里其实主要就是配置一下扫描service的工作)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <!-- 配置Service扫描,不配置也可以,直接在dao层用一个总的扫描类即可 -->
    <context:component-scan base-package="com.hnu.scw.service" />
</beans>

(6)在springconfig文件中,创建管理transaction层的文件,主要就是对事务以及AOP切面的处理。(这都是选择性配置

注意:这里的配置还可以用注解的方式进行,但是习惯性是用这个了,看个人爱好吧。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    ">
    <!--添加spring-dao层配置文件-->
    <import resource="applicationContext-dao.xml" />
    <!-- 事务管理器 -->
    <bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 事务模板对象 -->
    <bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" >
        <property name="transactionManager" ref="transactionManager" />
    </bean>

    <!-- 通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 以方法为单位,指定方法应用什么事务属性
             isolation:隔离级别
             propagation:传播行为
             read-only:是否只读
          -->
            <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="add*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
            <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
            <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        </tx:attributes>
    </tx:advice>

    <!-- 切面 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.hnu.scw.service.impl.*.*(..))" id="txpc"/>
        <!-- 配置切面 : 通知+切点
            advice-ref:通知的名称
            pointcut-ref:切点的名称
        -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txpc" />
    </aop:config>

    <!--
    如果嫌弃上面的配置太麻烦,想在service不同的类进行不同的事务控制,那么就用下面的配置
    但是要注意:需要在service的类中进行手动的添加@Transactional注解,而用上面的话就不需要进行额外的配置了
    
     配置基于注解的声明式事务
    <tx:annotation-driven transaction-manager="transactionManager"/>
    -->
</beans>

      关于这些内容的含义,我就不多介绍了,如果有不懂的地方,可以看看我之前的文章,因为这些内容都介绍得非常非常的仔细了。

(7)在resource文件目录下,创建数据库的properties文件,这个就是连接数据库对应的内容

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456

(8)直接在与Java文件同级下,创建日志文件的properties文件,这个根据需要自行判断是否需要即可

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

(9)在resource目录下,创建springmvcconfig文件目录,用于配置springMVC的配置文件,并创建命名为springmvc.xml(主要:Controller扫描、注解驱动、视图解析器)

<?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-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <!-- 配置Controller扫描 -->
    <context:component-scan base-package="com.hnu.scw.controller" />

    <!-- 配置注解驱动 -->
    <mvc:annotation-driven />

    <!-- 配置视图解析器 -->
    <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- 后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

(10)配置web.xml文件(主要:配置SpringSpringMVC、解决post乱码问题)

<?xml version="1.0" encoding="UTF-8"?>
<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_3_1.xsd"
         version="3.1">

    <!--配置项目启动的时候加载spring的相关配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:resource/springconfig/applicationContex-*.xml</param-value>
    </context-param>
    <!--配置监听器加载spring-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--配置过滤器,解决post乱码问题-->
    <filter>
        <filter-name>encoding</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>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>*</url-pattern>
    </filter-mapping>

    <!--配置springmvc-->
    <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:resource/springmvcconfig/springmvc.xml</param-value>
        </init-param>
        <!-- 配置springmvc什么时候启动,参数必须为整数 -->
        <!-- 如果为0或者大于0,则springMVC随着容器启动而启动 -->
        <!-- 如果小于0,则在第一次请求进来的时候启动 -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

(11)上面这些的话,基本对整个的框架整合完成了,在里面配置applicationContext-dao.xml的时候,进行扫描mapper.xml的时候,会报错,那是因为在这个路径下面还没有内容,所以,别紧张。后面添加进去就可以了

最后,进行编写每一层的Java代码。我这都示例进行演示即可:

(1)创建pojo包,用于存放javabean实体。

package com.hnu.scw.pojo;
import java.io.Serializable;
/**
 * @ Author     :scw
 * @ Date       :Created in 下午 2:14 2018/6/15 0015
 * @ Description:定义一个‘人’的实体类
 * @ Modified By:
 * @Version: $version$
 */
public class Person implements Serializable{
    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

(2)创建mapper类,示例PersonMapper

package com.hnu.scw.mapper;
import com.hnu.scw.pojo.Person;
/**
 * @ Author     :scw
 * @ Date       :Created in 下午 3:21 2018/6/15 0015
 * @ Description:对应于person的mapper操作接口
 * @ Modified By:
 * @Version: $version$
 */
public interface PersonMapper {
    /**
     * 添加一个person实体
     * @param person
     */
    void addPerson(Person person);
}

(3)创建service接口,

package com.hnu.scw.service;
import com.hnu.scw.pojo.Person;
/**
 * @ Author     :scw
 * @ Date       :Created in 下午 3:17 2018/6/15 0015
 * @ Description:person服务
 * @ Modified By:
 * @Version: $version$
 */
public interface PersonService {
    /**
     * 添加一个person实体
     * @param person
     */
    void addPerson(Person person);
}

(4)创建service接口实现类

package com.hnu.scw.service.imp;
import com.hnu.scw.mapper.PersonMapper;
import com.hnu.scw.pojo.Person;
import com.hnu.scw.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * @ Author     :scw
 * @ Date       :Created in 下午 3:19 2018/6/15 0015
 * @ Description:person实体处理的service实现类
 * @ Modified By:
 * @Version: $version$
 */
@Service
public class PersonServiceImp implements PersonService {
    @Autowired
    private PersonMapper personMapper;
    @Override
    public void addPerson(Person person) {
        personMapper.addPerson(person);
    }
}

(5)创建controller层,

package com.hnu.scw.controller;
import com.hnu.scw.pojo.Person;
import com.hnu.scw.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * @ Author     :scw
 * @ Date       :Created in 下午 2:15 2018/6/15 0015
 * @ Description:${description}
 * @ Modified By:
 * @Version: $version$
 */
@Controller
public class MyTestController {

    @Autowired
    private PersonService personService;
    /**
     * 跳转到添加person页面
     * @return
     */
    @RequestMapping(value = "toaddperson")
    public String testMyFirst(){
        return "person_add";
    }

    /**
     * 添加person处理
     * @param person
     * @return
     */
    @RequestMapping(value = "addperson")
    public String addPerson(Person person){
        personService.addPerson(person);
        return "success";
    }
}

(6)在web/WEB-INF/jsp文件目录下,创建JSP页面

person_add.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>欢迎您的到来</title>
</head>
<body>
<h1>随便填一填呗</h1>
<form action="${pageContext.request.contextPath}/addperson" method="post">
    <table>
        <tr>
            <td>大声说出你的名字</td>
            <td>
                <input type="text" name="name" id="personname">
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="submit" id="submitform" name="submitform" value="提交">
            </td>
        </tr>
    </table>
</form>
</body>
</html>

success.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/6/15 0015
  Time: 下午 2:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加成功</title>
</head>
<body>
<h1>恭喜你,添加成功,赶紧看看数据库!</h1>
</body>
</html>

        好了,差不多就是这么个流程的整合,如果就是根据用户自身的需求,可能还需要配置什么内容,这都是可以根据需要进行添加的,但是正常的情况,一般的三个框架的整合就是完整的了哦。。。。

    这三个框架的整体构建就已经学会了,然后剩下的就是进行熟练度的练习啦,如果对这几个框架有不懂的,欢迎看我之前的文章,都有很详细的介绍的哦!!一起共同努力!!!!!

   如果需要SSM框架整合的Maven版本,那么可以阅读我的另外一篇文章哦。

评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值