频道栏目
首页 > 资讯 > 其他 > 正文

SSM框架搭建巨详细的过程

18-05-08        来源:[db:作者]  
收藏   我要投稿

前言 基础环境搭建 1.创建一个maven工程 2.Maven的一些设置 3.引入jar包 4.引入Bootstrap 5.编写ssm整合的关键配置文件 5.1web.xml的配置 5.2 SpringMVC.xml的配置 5.3 Spring的配置 5.4mybatis-config配置文件,使用MyBatis逆向工程 6.测试

前言

楼主搭建过很多次的SSM框架,但是都没有认真记录过,这次花了两天的时间记录下来了这篇博文,详细的介绍了如何搭建SSM框架,包括前端、后端、测试,希望大家多多支持呀!

基础环境搭建

1.创建一个maven工程

项目创建

创建一个maven project 跳过骨架,如果不跳过骨架选择,项目是不全的

填写坐标,打包方式为war

添加web.xml

项目–properties–Project Facets,将Dynamic Web Module前面的对勾去掉,点击Apply,再加上对勾,下方出现Further configuration,点击,然后设置路径,则会自动生成web.xml,然后点击ok,apply。

SEfL2.md.png

SE20z.png

?

2.Maven的一些设置

windows–Preferences–Maven–UserSettings,打开settings.xml

镜像处换成国内的阿里云


    
    
        nexus-aliyun
        central
        Nexus aliyun
        http://maven.aliyun.com/nexus/content/groups/public

设置编译器版本


    
      jdk-1.8
      
        true
        1.8
      
      
        1.8
        1.8
        1.8
       
    
  

3.引入jar包

引入项目依赖的jar包

spring springmvc mybatis 数据库连接池,驱动包 其他(jstl,servlet-api,junit)

    4.0.0
    com.alipay
    ssmcurd
    0.0.1-SNAPSHOT
    war
    
    
        
        
        
        
            org.springframework
            spring-webmvc
            4.3.7.RELEASE
        
        
        
        
            org.springframework
            spring-jdbc
            4.3.7.RELEASE
        
        
        
        
            org.springframework
            spring-aspects
            4.3.7.RELEASE
        
        
        
        
            org.mybatis
            mybatis
            3.4.2
        
        
        
        
            org.mybatis
            mybatis-spring
            1.3.1
        
        
        
        
            c3p0
            c3p0
            0.9.1.2
        
        
        
            mysql
            mysql-connector-java
            5.1.41
        
        
        
        
            jstl
            jstl
            1.2
        
        
        
            javax.servlet
            javax.servlet-api
            3.0.1
            provided
        
        
        
            junit
            junit
            4.12
            test
        

    

4.引入Bootstrap

下载用于生产环境的 Bootstrap 解压复制粘贴到src/main/static(新建) 同时在src/main/static/js下引入js文件 然后在页面中引入即可,以index.html为例



<script type="text/javascript" src="static/js/jquery-1.12.4.min.js"></script>

<script src="static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>

5.编写ssm整合的关键配置文件

web.xml,spring,springmvc,mybatis,使用mybatis的逆向工程生成对应的bean以及mapper

5.1web.xml的配置
web.xml配置Spring,修改路径为:classpath:applicationContext.xml



    contextConfigLocation
    classpath:applicationContext.xml




    org.springframework.web.context.ContextLoaderListener

在src/main/resources下新建文件applicationContext.xml



    


web.xml中配置SpringMVC

可以不指定路径,web.xml同级目录中新建springDispatcherServlet-servlet.xml 文件

url改为/表示拦截所有的请求


    
    
        springDispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        1
    
    
    
        springDispatcherServlet
        /
    




web.xml中添加过滤器

    
        CharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            utf-8
        
        
            forceRequestEncoding
            true
        
        
            forceResponseEncoding
            true
        
    
    
        CharacterEncodingFilter
        /*
    

    
    
        HiddenHttpMethodFilter
        org.springframework.web.filter.HiddenHttpMethodFilter
    
    
        HiddenHttpMethodFilter
        /*
    
    
        HttpPutFormContentFilter
        org.springframework.web.filter.HttpPutFormContentFilter
    
    
        HttpPutFormContentFilter
        /*
    
5.2 SpringMVC.xml的配置

在左下角namespace中勾选Context和mvc

SeJhR.png




    
    
        
        
    
    
    
        
        
    
    
    
     
    
    

5.3 Spring的配置

Spring的配置主要有以下三点:数据源、与MyBatis整合、事务控制

数据源

类路径下新建文件dbconfig.properties



    
    
    
    
        
    
    
    
    
    
        
        
        
        
    
    
    
    
        
        
        
        
        
    

    
    
        
        
    
    
    
    
        
        
    
    
    
    
        
        
        
        
    
    
    
        
            
            
            
            
        
    
    
5.4mybatis-config配置文件,使用MyBatis逆向工程

可以参考官网。

mybatis-config.xml文件的编写,全局配置文件



    
        
        
    
    
        
        
    

接下来就是mapper下的编写,此与dao下的接口一一对应

在数据库新建两张表tbl_emp tbl_dept

St35r.md.png

St5OY.md.png

在dao包下创建相应的接口[使用MyBatis的逆向工程生成对应的bean以及mapper]

MyBatis逆向工程使用如下:

pom.xml中添加依赖,可以选择不同的包



  org.mybatis.generator
  mybatis-generator-core
  1.3.5
新建配置文件MyBatisGenerator-config.xml 内容直接复制官网实例





      
    
        
    
    
    
    

    
        
    
    
    
        
        
    
    
    
        
    
    
    
        
    
    
生成
package com.alipay.curd.test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

public class MBGTest {
public static void main(String[] args) throws Exception {
    List warnings = new ArrayList();
    boolean overwrite = true;
    //指定配置文件名
    File configFile = new File("MyBatisGenerator-config.xml");
    ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = cp.parseConfiguration(configFile);
    DefaultShellCallback callback = new DefaultShellCallback(overwrite);
    MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
    myBatisGenerator.generate(null);
}
}
这样就生成对应的文件,可以根据业务的实际需求进行修改。

6.测试

推荐Spring项目使用Spring的单元测试,可以自动注入需要的组件

pom.xml 导入Spring TestContext Framework 依赖 ,注意版本一致


  org.springframework
  spring-test
  4.3.7.RELEASE
  test

@ContextConfiguration指定Spring配置文件的位置,使用Location @RunWith(SpringJUnit4ClassRunner.class),指定Spring测试 直接autowired要使用的组件即可 在applicationContext.xml中配置可以批量执行的语句


  
   
最终测试代码如下
/**
* 测试dao层的工作
* @author shaokang
*推荐Spring项目使用Spring的单元测试,可以自动注入需要的组件
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:applicationContext.xml"})
public class MapperTest {
@Autowired
DepartmentMapper departmentMapper;

@Autowired
EmployeeMapper  employeeMapper; 

@Autowired
SqlSession SqlSession;
/**
 * 测试DepartmentMapper
 */
@Test
public void testCuRD() {
    //原始方法,不再使用
    /*//1.创建SpringIOC容器
    ApplicationContext ioc=new ClassPathXmlApplicationContext("applicationContext.xml");
    //2.从容器中取mapper
    DepartmentMapper bean = ioc.getBean(DepartmentMapper.class);*/
    System.out.println(departmentMapper);

    //1.插入部门
    departmentMapper.insertSelective(new Department(null, "开发部"));
    departmentMapper.insertSelective(new Department(null, "测试部"));

    //2.生成员工数据,测试员工插入
    employeeMapper.insertSelective(new Employee(null, "Jerry", "M", "Jerry@163.com", 1));

    //3.批量插入多个员工,批量使用可以执行操作的sqlSession
    EmployeeMapper mapper = SqlSession.getMapper(EmployeeMapper.class);
    for(int i=0;i<100;i++) {
        String uid=UUID.randomUUID().toString().substring(0,5)+i;
        mapper.insertSelective(new Employee(null, uid, "M", uid+"@163.com", 1));
    }
}
}
相关TAG标签
上一篇:系统分析与设计使用 UML State Model练习
下一篇:Win10系统安装TensorFlow-GPU教程
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站