Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 1778|回复: 0

[默认分类] 如何自己成功搭建一个SSM框架的WEB项目

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-3-20 14:02:04 | 显示全部楼层 |阅读模式

    工作中我们或多或少都需要自己搭建一个框架,现在常见的java开源框架组合方式主要为:SSH,spring+SpringMVC+JDBC,SSM
    其中SSM目前无论是培训机构培训亦或是招聘。都会将会使用SSM框架作为一个重要能力来作为培训或是招聘的重要目标之一,下面我将自己自学时搭建的一个SSM项目分享出来,供初学者参阅。
    1.第一步,我们需要搭建好自己的开发环境(IDE) 笔者使用的是myeclipse+tomcat+mysql

    2.第二步创建一个web工程 工程名自定义,创建好了之后按照MVC设计模式创建好所有的包或文件夹(domain用于存放javabean对象,config用于存放所有的配置文件),并将SSM框架所需要的所有jar包导入到项目中

    3.编写项目的配置文件(配置文件中每部分的含义,有详细的注释说明)
    a.spring的配置文件application-context.xml配置文件

    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <beans
    3.       xmlns="http://www.springframework.org/schema/beans"
    4.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5.       xmlns:context="http://www.springframework.org/schema/context"
    6.       xmlns:aop="http://www.springframework.org/schema/aop"
    7.       xmlns:tx="http://www.springframework.org/schema/tx"
    8.           xmlns:mvc="http://www.springframework.org/schema/mvc"
    9.                
    10.       xsi:schemaLocation="
    11.        
    12.           http://www.springframework.org/schema/beans
    13.           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    14.           
    15.           http://www.springframework.org/schema/context
    16.       http://www.springframework.org/schema/context/spring-context-3.0.xsd
    17.           
    18.           http://www.springframework.org/schema/aop
    19.           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    20.           
    21.           http://www.springframework.org/schema/tx
    22.       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    23.    
    24.       http://www.springframework.org/schema/mvc
    25.       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    26.       ">
    27.       <!-- 1.启用spring的注解扫描器 -->
    28.               <!--
    29.                       为了加载service层中的事务能够成功被spring管理
    30.                       需要设置spring的配置文件中的注解扫描器不扫描控制层,同时设置springMVC的配置文件不扫描service层。
    31.                       如果不做此设置,事务无法开启
    32.                -->
    33.       <context:component-scan base-package="scmweb">
    34.               <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/><!-- expression对应的是注解对象的全类名,而不是开发人员创建的控制层的全类名 -->
    35.       </context:component-scan>
    36.       <!-- 2.配置数据源 -->
    37.       <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    38.               <property name="locations">
    39.                       <value>classpath:config/jdbc.properties</value>
    40.               </property>
    41.       </bean>
    42.       <bean id="c3p0datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  destroy-method="close">
    43.               <property name="driverClass" value="${jdbc.driverClassName}"></property>
    44.               <property name="jdbcUrl" value="${jdbc.url}"></property>
    45.               <property name="user" value="${jdbc.username}"></property>
    46.               <property name="password" value="${jdbc.password}"></property>
    47.       </bean>
    48.       <!-- 3.配置mybatis相关的东西 -->
    49.       <!-- 3.1 配置mybatis核心sqlsessionfactory -->
    50.       <bean name="sqlsessionfactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    51.               <!-- 配置mybatis的主配置文件 -->
    52.               <property name="configLocation" value="classpath:config/mybatis.xml"></property>
    53.               <property name="dataSource" ref="c3p0datasource"></property>
    54.               <!-- 设置自动将指定包下所有的xxxMapper.xml文件引入mybatis -->
    55.               <property name="mapperLocations" value="classpath:scmweb/log/scmdao/*.xml"></property>
    56.       </bean>
    57.       <!-- 3.2 配置sqlSessionTemplate持久化模版(包含了增删查改的模版方法,
    58.                              如果不配置的话需要利用sqlsessionfactory来生成sqlsession对象实现对数据库的操作)
    59.        -->
    60. <!--       <bean name="sqlSessionTemplate"  class="org.mybatis.spring.SqlSessionTemplate"> -->
    61. <!--               <constructor-arg index="0" ref="sqlsessionfactory"></constructor-arg> -->
    62. <!--       </bean> -->
    63.       <!-- 4.配置事务相关得东西 -->
    64.       <!-- 4.1 配置事务管理器 -->
    65.       <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    66.               <property name="dataSource" ref="c3p0datasource"></property>
    67.       </bean>
    68.       <!-- 4.2 配置事务的通知  配置为那种类型的方法加上事务-->
    69.       <tx:advice id="tx" transaction-manager="transactionManager">
    70.               <tx:attributes>
    71.                       <tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/><!-- 设置了rollback-for属性  那么只要出现异常(无论是否被手动捕获)都会回滚 -->
    72.                       <tx:method name="update*" propagation="REQUIRED"/>
    73.                       <tx:method name="insert*" propagation="REQUIRED"/>
    74.                       <tx:method name="*"  propagation="SUPPORTS"/>
    75.               </tx:attributes>
    76.       </tx:advice>
    77.       <!-- 4.3 配置事务的切面 -->
    78.       <aop:config>
    79.               <aop:pointcut id="acut"  expression="execution(* scmweb.log.scmservice.*.*(..))" />
    80.               <aop:advisor advice-ref="tx" pointcut-ref="acut"/><!-- 这里应该是用pointcut-ref属性  而不是pointcut关联切入点 -->
    81.       </aop:config>
    82.      
    83.     <!--
    84.             配置mybatis的转换器,
    85.                   目的:通过该配置,可以使得mybatis.xml文件中不必再关联实体映射配置文件(xxxMapper.xml)了,
    86.                           并最终实现了dao层只需要 接口+xxxMapper.xml配置文件
    87.                 原理:对于在basePackage设置的包(包括子包)下的接口类,扫描所有xxxMapper.xml文件,如果某个文件的名称空间是该接口的全名,
    88.                                 那么存在与名称空间相同的接口将被转换成spring的BEAN,在调用 的地方通过@Autowired方式将可以注入接口实例,
    89.                                 我们在service实现层调用dao接口的方法时,则会自动将接口的全类名当作名称空间,方法名当作sql语句的id值,来执行
    90.                                 对应的sql语句,并返回相应结果(因此daoimpl层就没有作用了)
    91.                 结束配置之后 3.2步便可以不再配置了
    92.         -->
    93.                 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    94. <!--                 <property name="sqlSessionFactory" ref="sqlsessionfactory" /> 该配置不用手动配,否则会报错,spring底层会自动注入-->
    95.                         <property name="basePackage" value="scmweb.log.scmdao"/>
    96.                 </bean>
    97.      
    98. </beans>
    复制代码
    b.jdbc.property配置文件


    1. jdbc.driverClassName=com.mysql.jdbc.Driver
    2. jdbc.url=jdbc\:mysql\://localhost\:3306/scm?useUnicode\=true&characterEncoding\=UTF-8
    3. jdbc.username=root
    4. jdbc.password=root
    复制代码
    c.MyBatis.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.         <typeAliases>
    6.                  <!--
    7.                          通过package标签设置实现了将某一个包下的所有的实体类都以简单类名作为别名的设置,
    8.                          这样避免了分别为每一个实体类设置别名的麻烦(配置完成之后,xxxMapper.xml中不用再写全类名了直接写别名即可)
    9.                   -->
    10.                  
    11.                  <package name="scmweb.log.domain"/>
    12.         </typeAliases>
    13.        
    14.         <!-- 这种方式在这里引入 xxxMapper.xml文件比较麻烦,且有多少个映射文件就要引入多少次
    15.                 为了解决这个事情,可以在sqlSessionFactory的配置中,设置扫描放置xxxMapper.xml文件的包
    16.                 这样可以自动扫描包下面的所有实体映射文件
    17.         -->
    18. <!--         <mappers>  -->
    19. <!--                         <mapper resource="scmweb/log/domain/emploeemapper.xml"/>  -->
    20. <!--          </mappers> -->
    21. </configuration>
    复制代码

    d.springMVC.xml配置文件


    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans
    3.       xmlns="http://www.springframework.org/schema/beans"
    4.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5.           xmlns:mvc="http://www.springframework.org/schema/mvc"
    6.           xmlns:context="http://www.springframework.org/schema/context"
    7.       xsi:schemaLocation="
    8.        
    9.           http://www.springframework.org/schema/beans
    10.           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    11.    
    12.       http://www.springframework.org/schema/mvc
    13.       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    14.       
    15.           http://www.springframework.org/schema/context
    16.       http://www.springframework.org/schema/context/spring-context-3.0.xsd
    17.       ">
    18.       <!-- 设置annotation-driven注解之后,便支持了返回json格式数据到前台页面 -->
    19.       <mvc:annotation-driven/>
    20.       <context:component-scan base-package="scmweb">
    21. <!--               <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> -->
    22.               <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    23.       </context:component-scan>
    24.       <!-- 映射器 与xml形式的解析器不一样-->
    25. <!--       <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping.class"></bean> -->
    26.      
    27.       <!-- 适配器 与xml形式的解析器不一样-->
    28.       <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
    29.       
    30.        <!-- xml版的视图解析器和注解版的视图解析器是一样的 -->
    31.       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    32.               <!-- 由于这里是为了视图通过逻辑地址访问,因此这里视图解析器为必选配置 -->
    33.               <!-- 配置路径前缀 -->
    34.               <property name="prefix" value="/WEB-INF/view/"></property>
    35.               <!-- 配置路径后缀 -->
    36.               <property name="suffix" value=".jsp"></property>
    37.       </bean>
    38.       
    39.        
    40.       
    41. <!--         
    42.         当 springMVC配置文件中  通过注解引入了spring的配置文件时,则web.xml文件中便可以不配置spring的xml配置文件了
    43.         <import resource="classpath:config/application-context.xml"/>
    44. -->
    45. </beans>
    复制代码

    e.web.xml配置文件中需要配置spring和springMVC的相关内容


    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <web-app version="3.0"
    3.         xmlns="http://java.sun.com/xml/ns/javaee"
    4.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5.         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    6.         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    7.   <display-name></display-name>       
    8.   <welcome-file-list>
    9.     <welcome-file>index.jsp</welcome-file>
    10.   </welcome-file-list>
    11.   <!-- 配置spring -->
    12.   <listener>
    13.                   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    14.   </listener>
    15.   <!-- 当 springMVC配置文件中  通过注解引入了spring的配置文件时,则可以配置spring的xml配置文件-->       
    16.   <context-param>
    17.                 <param-name>contextConfigLocation</param-name>
    18.                 <param-value>classpath:config/application-context.xml</param-value>
    19.   </context-param>
    20.   
    21.   <!-- 配置springMVC -->
    22.   <servlet>
    23.           <servlet-name>DispatcherServlet</servlet-name>
    24.           <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    25.           <init-param>
    26.                   <param-name>contextConfigLocation</param-name>
    27.                   <param-value>classpath:config/springMVC.xml</param-value><!-- 该配置文件一定是包含springMVC所有属性配置得配置文件 -->
    28.           </init-param>
    29.   </servlet>
    30.   <servlet-mapping>
    31.           <servlet-name>DispatcherServlet</servlet-name>
    32.           <url-pattern>*.action</url-pattern>
    33.   </servlet-mapping>
    34.     <filter>
    35.           <filter-name>CharacterEncodingFilter</filter-name>
    36.           <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    37.           <init-param>
    38.                   <param-name>encoding</param-name>
    39.                   <param-value>UTF-8</param-value>
    40.           </init-param>
    41.   </filter>
    42.   <filter-mapping>
    43.           <filter-name>CharacterEncodingFilter</filter-name>
    44.           <url-pattern>/*</url-pattern>
    45.   </filter-mapping>
    46.   <!-- 配置springMVC结束 -->
    47.   
    48.   
    49. </web-app>
    复制代码

    配置到这一步之后,会发现,现在需要手动创建javabean实例、dao层的接口以及javabean对应表格的mapper配置文件,因此,这里用到了一个mybatis逆向工程的插件,用于自动生成上述三个部分
    4.配置mybatis的逆向工程插件
    a.首先需要在myeclipse的安装目录中添加逆向工程
    首先找见安装目录中的MyEclipse 10下创建add-plugins\mybatis-generater文件夹,然后将逆向工程压缩包解压之后的两个文件夹放到改目录下

    D:\myprogram1\myeclipse10\MyEclipse 10\add-plugins\mybatis-generater

    其次在myeclipse的安装目录D:\myprogram1\myeclipse10\MyEclipse 10\dropins下创建mybatis.link文件(后缀为.link),文件内容为:path=D:\\myprogram1\\myeclipse10\\MyEclipse 10\\add-plugins\\mybatis-generater
    重启myecplise,将中generatorConfig.xml添加到项目中
    b.generatorConfig.xml配置文件

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE generatorConfiguration
    3.   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    4.   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
    5. <!--
    6.         mybatis的逆向工程插件,用于帮助开发人员自动生成数据库中指定表格所对应的:
    7.         javabean、xxxMapper.xml文件(javabean与表格的映射文件)和dao层的接口类
    8.         这样开发人员不用自己去一张一张的建表格对应的各种类和配置文件了。
    9.        
    10.         当配置结束之后,右击文件名会有generator Mybatis/Ibatis artifacts选项   点击即可顺利运行
    11.          执行结果为:数据库中一张表对应:一个javabean,一个dao接口对象,一个xxxMapper.xml文件
    12. -->
    13. <generatorConfiguration>
    14. <!--
    15.         <properties resource="conn.properties" />
    16.           -->
    17.           <!-- 处理1 -->
    18.          <classPathEntry location="E:\mybatis\mysql-connector-java-5.1.7-bin.jar"/>
    19.         <!-- 指定运行环境是mybatis3的版本 -->
    20.         <context id="testTables" targetRuntime="MyBatis3">
    21.                 <commentGenerator>
    22.                 <!-- 是否取消注释 -->
    23.                         <property name="suppressAllComments" value="true" />
    24.                          <!-- 是否生成注释代时间戳 -->
    25.                         <property name="suppressDate" value="true" />
    26.                 </commentGenerator>
    27.                 <!-- 处理2   jdbc 连接信息 -->
    28.                 <jdbcConnection driverClass="com.mysql.jdbc.Driver"
    29.                         connectionURL="jdbc:mysql://localhost:3306/scm?useUnicode=true&characterEncoding=UTF-8" userId="root" password="root">
    30.                 </jdbcConnection>
    31.                
    32.         <!--处理3   targetPackage指定模型(即表所对应的实体类)生成在哪个包 ,targetProject指定项目的src目录,-->        
    33.                 <javaModelGenerator targetPackage="scmweb.log.domain"
    34.                         targetProject="SCM/src">
    35.                         <!-- 去除字段前后空格 -->
    36.                         <property name="trimStrings" value="false" />
    37.                 </javaModelGenerator>
    38.                 <!--处理4   配置xxxMapper.xml文件生成到项目中哪个包中 -->
    39.                 <sqlMapGenerator targetPackage="scmweb.log.scmdao"
    40.                         targetProject="SCM/src" />
    41.                 <!-- 处理5   配置dao接口生成信息(由mybatis逆向工程生成的dao接口类全是以Mapper结尾的)-->
    42.                 <javaClientGenerator type="XMLMAPPER" targetPackage="scmweb.log.scmdao" targetProject="SCM/src" />
    43.                
    44.                 <!-- 指定要为数据库中哪些表格生成对应的接口对象和实体类(javabean)以及对应的xxxMapper.xml文件
    45.                         切忌一张表格只能自动生成一次,如果多次生成的话,会造成生成的事物内部属性一直重复
    46.                 -->
    47.                 <table tableName="account" domainObjectName="Account"/>
    48.                        
    49.                 <table tableName="supplier" domainObjectName="Supplier"/>
    50.         </context>
    51. </generatorConfiguration>
    复制代码

    当配置结束之后,右击文件名会有generator Mybatis/Ibatis artifacts选项&nbsp;&nbsp; 点击即可顺利运行

    &nbsp;执行结果为:数据库中一张表对应:一个javabean,一个dao接口对象,一个xxxMapper.xml文件



    5.如果项目搭建好了之后POST表单提交的数据出现乱码,可以查看我得另一篇日志SSM框架Jsp页面POST提交的中文数据保存到数据库变成乱码问题的分析
    6.项目源码地址:https://gitee.com/willbeahero/ssmproject


      
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-21 08:01 , Processed in 0.394252 second(s), 51 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表