栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SSM 框架 搭建整合(IDEA)保姆级

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SSM 框架 搭建整合(IDEA)保姆级

一:搭建整合环境

1. 搭建整合环境

1. 整合说明:SSM整合可以使用多种方式,咱们会选择XML + 注解的方式

2. 整合的思路:

  •  先搭建整合的环境
  •  先把Spring的配置搭建完成
  •  再使用Spring整合SpringMVC框架
  •  最后使用Spring整合MyBatis框架

3. 创建数据库和表结构:

create database ssm;
create table account(
id int primary key auto_increment,
name varchar(20),
money double
);

4. 创建maven的工程:

 我这里改成了自己的maven  , idea自带的可能会有问题

 创建好后把pom.xml文件中部分内容删掉 剩下:,在光标处,再粘贴复制进去下方这些


    5.0.2.RELEASE
    1.6.6
    1.2.12
    5.1.6
    3.4.5
    2.9.4
  
  
    
    
      org.aspectj
      aspectjweaver
      1.6.8
    
    
      org.springframework
      spring-aop
      ${spring.version}
    
    org.springframework
    spring-context
    ${spring.version}
  
    
      org.springframework
      spring-web
      ${spring.version}
    
    
      org.springframework
      spring-webmvc
      ${spring.version}
    
    
      org.springframework
      spring-test
      ${spring.version}
    
    
      org.springframework
      spring-tx
      ${spring.version}
    
    
      org.springframework
      spring-jdbc
      ${spring.version}
    
    
      junit
      junit
      4.12
      test
    
    
      mysql
      mysql-connector-java
      ${mysql.version}
    
    
      javax.servlet
      servlet-api
      2.5
      provided
    
    
      javax.servlet.jsp
      jsp-api
      2.0
      provided
    
    
      jstl
      jstl
      1.2
    
    
    
      log4j
      log4j
      ${log4j.version}
    
    
      org.slf4j
      slf4j-api
      ${slf4j.version}
    
    
      org.slf4j
      slf4j-log4j12
      ${slf4j.version}
    
    
    
      org.mybatis
      mybatis
      ${mybatis.version}
    
    
      org.mybatis
      mybatis-spring
      1.3.0
    
    
    
      org.thymeleaf
      thymeleaf-spring4
      3.0.9.RELEASE
    
    
      com.alibaba
      druid
      1.1.10
    
      org.mybatis.generator
      mybatis-generator-core
      1.3.5
    
    
      com.fasterxml.jackson.core
      jackson-core
      ${jackson.version}
    
    
      com.fasterxml.jackson.core
      jackson-databind
      ${jackson.version}
    
    
      com.github.pagehelper
      pagehelper
      5.1.10
    
    
      cn.hutool
      hutool-all
      5.2.3
    
  
  
    ssm
    
      
        
          org.apache.maven.plugins
          maven-compiler-plugin
          3.2
          
            1.8
            1.8
            UTF-8
            true
          
        
      
    
    
      
        org.mybatis.generator
        mybatis-generator-maven-plugin
        1.3.5
        
        
          src/main/resources/mbg.xml
          true
          true
        
      
    
  

 5. 编写实体类。

        在src下的main目录下创建 java 和resource 两个目录,在java目录中新建软件包,再在这个新建的软件包中新建  controller ,dao,entity,service 软件包。

。每一个数据库的表,对应一个实体,实体中 get set和toString方法都要写上。(根据本篇最开始的数据库编写字段)。在entity中新建Account类,写好

public class Account{
    // 主键
    private int id;
    // 账户名称
    private String name;
    // 账号的金额
    private Double money;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", money=" + money +
                '}';
    }
}

6. 编写service接口和实现类,在service中新建AccountService 接口

import com.qcby.entity.Account;

import java.util.List;

public interface AccountService {

    //查询所有
    public List findAll();
}

7.写一个实现类,在service下新建软件包 serviceimpl,在其中新建实现类AccountServiceImpl

实现AccountService接口,加上@service注解,实现相应方法

import com.qcby.entity.Account;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {
    //查询所有
    @Override
    public List findAll() {
        System.out.println("业务层:查询所有");
        return null;
    }
}

8.在resources 目录下创建spring配置,就叫spring就行

光标出 写入开启注解扫描,然后 光标放在

    
    

 如果不会。。就直接粘贴下方 进去




    
    

不过要注意 base-package= "com.qcby",这里的com.qcby是java下写的大的那个软件包的名称。我写的是 com.qcby,注意写成自己的。

9.测试一下,main下新建个Test目录,作为测试,其中新建com.qcby 其中创建一个测试类  TestSpring

import com.qcby.service.AccountService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {
    @Test
    public void run(){
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring.xml");
        AccountService service = ac.getBean(AccountService.class);
        service.findAll();
    }
}

 以上是对Spring最简单的配置


 下面配置 SpringMVC

Spring整合SpringMVC框架

1. 搭建和测试SpringMVC的开发环境

1. 在web.xml中配置DispatcherServlet前端控制器

注意一定要知道,DispatcherServlet

 打开web.xml 全删除掉里边内容,再粘贴进,(因为他自己的版本太老了)


   
 

 再加入



    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    true
    
        encoding
        UTF-8
    



    encodingFilter
    
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest,
                                         HttpServletResponse httpServletResponse, Object o, Exception e) {
        e.printStackTrace(); //在命令行打印异常信息在程序中出错的位置及原因。
        // 做强转
        SysException exception = null;
        // 判断
        if(e instanceof SysException){
            exception = (SysException)e;
        }else{
            exception = new SysException("系统正在维护,请联系管理员");

        }
        ModelAndView mv = new ModelAndView();
        mv.addObject("errorMsg",exception.getMessage());
        // 设置跳转的页面
        mv.setViewName("error");
        return mv;
    }
}

这里会先报错,去创建实体对象  entity中新建 类叫 SysException(出错时向前台返回实体对象),

public class SysException extends Exception{
    // 提示消息
    private String message;
    public SysException(String message){
        this.message = message;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return "SysException{" +
                "message='" + message + ''' +
                '}';
    }
}

springmvc.xml 当中-配置异常处理器-


    

运行tomcat 访问。并不会报 500错误。


结束!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/884558.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号