当前位置:   article > 正文

手把手教你如何玩转SSM框架整合(非Maven版本)_非maven的ssm项目

非maven的ssm项目

      关于目前比较流行的框架,前面的文章都有进行了相关内容的介绍,所以在这里的话主要就是介绍一下关于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。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  4. <configuration>
  5. <!-- 这里面你可以配置一下关于JavaBean中类的一些别名,这样就方便在之后的Mapper文件中进行处理 -->
  6. <typeAliases>
  7. <package name="hnu.scw.pojo"/>
  8. </typeAliases>
  9. </configuration>

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

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

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
  7. <!-- 配置 读取properties文件 jdbc.properties -->
  8. <context:property-placeholder location="classpath:resource/jdbc.properties" />
  9. <!-- 自动扫描所有的注解 -->
  10. <context:component-scan base-package="com.hnu.scw"/>
  11. <!-- 配置阿里的druid数据源 -->
  12. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  13. <property name="driverClassName" value="${jdbc.driver}" />
  14. <property name="url" value="${jdbc.url}" />
  15. <property name="username" value="${jdbc.username}" />
  16. <property name="password" value="${jdbc.password}" />
  17. </bean>
  18. <!-- 配置SqlSessionFactory -->
  19. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  20. <!-- 设置数据源 -->
  21. <property name="dataSource" ref="dataSource"/>
  22. <!-- 设置MyBatis核心配置文件 -->
  23. <property name="configLocation" value="classpath:resource/mybatisconfig/SqlMapConfig.xml" />
  24. <!-- 设置扫描mapper.xml文件 -->
  25. <property name="mapperLocations" value="classpath:resource/mapperxmlconfig/*Mapper.xml"/>
  26. </bean>
  27. <!-- 配置Mapper层java类扫描 -->
  28. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  29. <!-- 设置Mapper扫描包 -->
  30. <property name="basePackage" value="com.hnu.scw.mapper" />
  31. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  32. </bean>
  33. </beans>

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

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

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

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  6. 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
  7. ">
  8. <!--添加spring-dao层配置文件-->
  9. <import resource="applicationContext-dao.xml" />
  10. <!-- 事务管理器 -->
  11. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  12. <!-- 数据源 -->
  13. <property name="dataSource" ref="dataSource" />
  14. </bean>
  15. <!-- 事务模板对象 -->
  16. <bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" >
  17. <property name="transactionManager" ref="transactionManager" />
  18. </bean>
  19. <!-- 通知 -->
  20. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  21. <tx:attributes>
  22. <!-- 以方法为单位,指定方法应用什么事务属性
  23. isolation:隔离级别
  24. propagation:传播行为
  25. read-only:是否只读
  26. -->
  27. <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
  28. <tx:method name="add*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
  29. <tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
  30. <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
  31. <tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
  32. <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
  33. <tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
  34. <tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
  35. <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
  36. <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
  37. </tx:attributes>
  38. </tx:advice>
  39. <!-- 切面 -->
  40. <aop:config>
  41. <aop:pointcut expression="execution(* com.hnu.scw.service.impl.*.*(..))" id="txpc"/>
  42. <!-- 配置切面 : 通知+切点
  43. advice-ref:通知的名称
  44. pointcut-ref:切点的名称
  45. -->
  46. <aop:advisor advice-ref="txAdvice" pointcut-ref="txpc" />
  47. </aop:config>
  48. <!--
  49. 如果嫌弃上面的配置太麻烦,想在service不同的类进行不同的事务控制,那么就用下面的配置
  50. 但是要注意:需要在service的类中进行手动的添加@Transactional注解,而用上面的话就不需要进行额外的配置了
  51. 配置基于注解的声明式事务
  52. <tx:annotation-driven transaction-manager="transactionManager"/>
  53. -->
  54. </beans>

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

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

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

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

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

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  7. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
  9. <!-- 配置Controller扫描 -->
  10. <context:component-scan base-package="com.hnu.scw.controller" />
  11. <!-- 配置注解驱动 -->
  12. <mvc:annotation-driven />
  13. <!-- 配置视图解析器 -->
  14. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  15. <!-- 前缀 -->
  16. <property name="prefix" value="/WEB-INF/jsp/" />
  17. <!-- 后缀 -->
  18. <property name="suffix" value=".jsp" />
  19. </bean>
  20. </beans>

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  5. version="3.1">
  6. <!--配置项目启动的时候加载spring的相关配置文件-->
  7. <context-param>
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>classpath:resource/springconfig/applicationContex-*.xml</param-value>
  10. </context-param>
  11. <!--配置监听器加载spring-->
  12. <listener>
  13. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  14. </listener>
  15. <!--配置过滤器,解决post乱码问题-->
  16. <filter>
  17. <filter-name>encoding</filter-name>
  18. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  19. <init-param>
  20. <param-name>encoding</param-name>
  21. <param-value>UTF-8</param-value>
  22. </init-param>
  23. </filter>
  24. <filter-mapping>
  25. <filter-name>encoding</filter-name>
  26. <url-pattern>*</url-pattern>
  27. </filter-mapping>
  28. <!--配置springmvc-->
  29. <servlet>
  30. <servlet-name>springmvc</servlet-name>
  31. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  32. <init-param>
  33. <param-name>contextConfigLocation</param-name>
  34. <param-value>classpath:resource/springmvcconfig/springmvc.xml</param-value>
  35. </init-param>
  36. <!-- 配置springmvc什么时候启动,参数必须为整数 -->
  37. <!-- 如果为0或者大于0,则springMVC随着容器启动而启动 -->
  38. <!-- 如果小于0,则在第一次请求进来的时候启动 -->
  39. <load-on-startup>1</load-on-startup>
  40. </servlet>
  41. <servlet-mapping>
  42. <servlet-name>springmvc</servlet-name>
  43. <url-pattern>/</url-pattern>
  44. </servlet-mapping>
  45. </web-app>

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

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

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

  1. package com.hnu.scw.pojo;
  2. import java.io.Serializable;
  3. /**
  4. * @ Author :scw
  5. * @ Date :Created in 下午 2:14 2018/6/15 0015
  6. * @ Description:定义一个‘人’的实体类
  7. * @ Modified By:
  8. * @Version: $version$
  9. */
  10. public class Person implements Serializable{
  11. private Integer id;
  12. private String name;
  13. public Integer getId() {
  14. return id;
  15. }
  16. public void setId(Integer id) {
  17. this.id = id;
  18. }
  19. public String getName() {
  20. return name;
  21. }
  22. public void setName(String name) {
  23. this.name = name;
  24. }
  25. }

(2)创建mapper类,示例PersonMapper

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

(3)创建service接口,

  1. package com.hnu.scw.service;
  2. import com.hnu.scw.pojo.Person;
  3. /**
  4. * @ Author :scw
  5. * @ Date :Created in 下午 3:17 2018/6/15 0015
  6. * @ Description:person服务
  7. * @ Modified By:
  8. * @Version: $version$
  9. */
  10. public interface PersonService {
  11. /**
  12. * 添加一个person实体
  13. * @param person
  14. */
  15. void addPerson(Person person);
  16. }

(4)创建service接口实现类

  1. package com.hnu.scw.service.imp;
  2. import com.hnu.scw.mapper.PersonMapper;
  3. import com.hnu.scw.pojo.Person;
  4. import com.hnu.scw.service.PersonService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. /**
  8. * @ Author :scw
  9. * @ Date :Created in 下午 3:19 2018/6/15 0015
  10. * @ Description:person实体处理的service实现类
  11. * @ Modified By:
  12. * @Version: $version$
  13. */
  14. @Service
  15. public class PersonServiceImp implements PersonService {
  16. @Autowired
  17. private PersonMapper personMapper;
  18. @Override
  19. public void addPerson(Person person) {
  20. personMapper.addPerson(person);
  21. }
  22. }

(5)创建controller层,

  1. package com.hnu.scw.controller;
  2. import com.hnu.scw.pojo.Person;
  3. import com.hnu.scw.service.PersonService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. /**
  8. * @ Author :scw
  9. * @ Date :Created in 下午 2:15 2018/6/15 0015
  10. * @ Description:${description}
  11. * @ Modified By:
  12. * @Version: $version$
  13. */
  14. @Controller
  15. public class MyTestController {
  16. @Autowired
  17. private PersonService personService;
  18. /**
  19. * 跳转到添加person页面
  20. * @return
  21. */
  22. @RequestMapping(value = "toaddperson")
  23. public String testMyFirst(){
  24. return "person_add";
  25. }
  26. /**
  27. * 添加person处理
  28. * @param person
  29. * @return
  30. */
  31. @RequestMapping(value = "addperson")
  32. public String addPerson(Person person){
  33. personService.addPerson(person);
  34. return "success";
  35. }
  36. }

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

person_add.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <head>
  4. <title>欢迎您的到来</title>
  5. </head>
  6. <body>
  7. <h1>随便填一填呗</h1>
  8. <form action="${pageContext.request.contextPath}/addperson" method="post">
  9. <table>
  10. <tr>
  11. <td>大声说出你的名字</td>
  12. <td>
  13. <input type="text" name="name" id="personname">
  14. </td>
  15. </tr>
  16. <tr>
  17. <td></td>
  18. <td>
  19. <input type="submit" id="submitform" name="submitform" value="提交">
  20. </td>
  21. </tr>
  22. </table>
  23. </form>
  24. </body>
  25. </html>

success.jsp

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

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

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/200176
推荐阅读
相关标签
  

闽ICP备14008679号