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

IDEA配置SSM

Java 更新时间:发布时间: 百科书网 趣学号
搭建整合环境 1. 搭建整合环境

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

2. 整合的思路

  1. 先搭建整合的环境
  2. 先把Spring的配置搭建完成
  3. 再使用Spring整合SpringMVC框架
  4. 最后使用Spring整合MyBatis框架

3. 创建数据库和表结构

语句

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

4. 创建maven的工程


  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
  
  
  
    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
  
  
      org.thymeleaf
      thymeleaf-spring4
      3.0.9.RELEASE
  


  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. 编写实体类,在ssm_domain项目中编写

package com.qcby.entity;

import java.io.Serializable;

public class Account implements Serializable {
    // 主键
    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接口和实现类

package com.qcby.service;

import com.qcby.entity.Account;
import java.util.List;

public interface AccountService {

    //查询所有
    public List findAll();
}
package com.qcby.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;
    }
}
Spring框架代码的编写 1. 搭建和测试Spring的开发环境

1. 在ssm_web项目中创建spring.xml的配置文件,编写具体的配置信息。



    
    

2. 在ssm_web项目中编写测试方法,进行测试

package com.qcby.demo;

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框架 1. 搭建和测试SpringMVC的开发环境

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



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



    encodingFilter
    
    @RequestMapping(path = "/hello")
    public String sayHello(Model model){
        System.out.println("入门方法执行了2...");
        List accounts = accountService.findAll();
        // 向模型中添加属性msg与值,可以在html页面中取出并渲染
        model.addAttribute("msg","hello,SpringMVC");
        // 配置了视图解析器后,写法
        return "suc";
    }
}
Spring整合SpringMVC的框架

1. 目的:在controller中能成功的调用service对象中的方法。

2. 在项目启动的时候,就去加载spring.xml的配置文件,在web.xml中配置ContextLoaderListener监听器(该监听器默认加载WEB-INF目录下的applicationContext.xml的配置文件,所以要配置全局的变量加载类路径下的配置文件)


Archetype Created Web Application

  org.springframework.web.context.ContextLoaderListener



  contextConfigLocation
  classpath:spring.xml

3. 在controller中注入service对象,调用service对象的方法进行测试

package com.qcby.controller;

import com.qcby.entity.Account;
import com.qcby.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {
    //依赖注入
    @Autowired
    private AccountService accountService;
    
    @RequestMapping("/findAll")
    public ModelAndView findAll(){
        System.out.println("表现层:查询所有");
        //调用service的方法
        List list = accountService.findAll();
        ModelAndView mv = new ModelAndView();
        mv.setViewName("suc");
        return mv;
    }
}
Spring整合MyBatis框架 1. Spring整合MyBatis框架

1. 目的:把SqlMapConfig.xml配置文件中的内容配置到spring.xml配置文件中;整合原理就是把Dao生成的代理对象,存入到IOC容器中

2.jdbc.properties

连接到自己的数据库

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false
jdbc.username=root
jdbc.password=2020

3.在spring.xml文件中配置mybatis







    
    
    
    




    
    

    
    


    
     

6.Index.html




    
    主页



入门入门程序


7.suc.html




    
    Title


Hello 

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

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号