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

SSM——SSM整合

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

SSM——SSM整合

文章目录

一. 准备工作

1. 原始方式整合2. 创建Maven工程3. 导入Maven坐标 二. 编写实体类三. 编写Mapper接口四. 编写Service接口和实现类五. 编写Controller六. 编写添加页面七. 编写列表页面八. 编写相应配置文件

1. MyBatis核心文件2. MyBatis映射文件3. spring核心配置文件4. 配置spring-mvc核心配置文件5. 配置web.xml

一. 准备工作 1. 原始方式整合
create database ssm;
create table account(
	id int primary key auto_increment,
	name varchar(100),
	money double(7,2)
);

2. 创建Maven工程

3. 导入Maven坐标



    4.0.0

    com.itheima
    itheima_ssm
    1.0-SNAPSHOT
    war

    itheima_ssm Maven Webapp
    
    http://www.example.com

    
        UTF-8
        1.8
        1.8
    

    
        
        
            org.springframework
            spring-context
            5.0.5.RELEASE
        
        
            org.aspectj
            aspectjweaver
            1.8.7
        
        
            org.springframework
            spring-jdbc
            5.0.5.RELEASE
        
        
            org.springframework
            spring-tx
            5.0.5.RELEASE
        
        
            org.springframework
            spring-test
            5.0.5.RELEASE
        
        
            org.springframework
            spring-webmvc
            5.0.5.RELEASE
        

        
        
            javax.servlet
            servlet-api
            2.5
        
        
            javax.servlet.jsp
            jsp-api
            2.0
        

        
        
            org.mybatis
            mybatis
            3.4.5
        
        
            org.mybatis
            mybatis-spring
            1.3.1
        
        
            mysql
            mysql-connector-java
            5.1.6
        
        
            c3p0
            c3p0
            0.9.1.2
        

        
            junit
            junit
            4.12
        
        
            jstl
            jstl
            1.2
        

    

    
        itheima_ssm
        
            
                
                    maven-clean-plugin
                    3.0.0
                
                
                
                    maven-resources-plugin
                    3.0.2
                
                
                    maven-compiler-plugin
                    3.7.0
                
                
                    maven-surefire-plugin
                    2.20.1
                
                
                    maven-war-plugin
                    3.2.0
                
                
                    maven-install-plugin
                    2.5.2
                
                
                    maven-deploy-plugin
                    2.8.2
                
            
        
    


二. 编写实体类

三. 编写Mapper接口

四. 编写Service接口和实现类

五. 编写Controller

package com.itheima.controller;

import com.itheima.domain.Account;
import com.itheima.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.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    //保存
    @RequestMapping(value = "/save",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String save(Account account){
        accountService.save(account);
        return "保存成功";
    }

    //查询
    @RequestMapping("/findAll")
    public ModelAndView findAll(){
        List accountList = accountService.findAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("accountList",accountList);
        modelAndView.setViewName("accountList");
        return modelAndView;
    }

}

六. 编写添加页面

七. 编写列表页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


    Title


    

展示账户数据列表

账户id 账户名称 账户金额
${account.id} ${account.name} ${account.money}
八. 编写相应配置文件

1. MyBatis核心文件

由于MyBatis中大部分配置都交给spring 来管理,所以原来的sqlMapConfig.xml不需要了,现在只写一个sqlMapConfig-spring.xml即可,里面具体的配置放在spring核心配置文件中即可。





    
    
        
        
        
    


2. MyBatis映射文件

3. spring核心配置文件




    
    
        
        
    

    
    

    
    
        
        
        
        
    

    
    
        
        
        
    

    
    
    
        
    


    
    
    
        
    

    
    
        
            
        
    

    
    
        
    




4. 配置spring-mvc核心配置文件




    
    
    
    
    
    
        
        
    
    
    



5. 配置web.xml



    
    
        contextConfigLocation
        classpath:applicationContext.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    

    
    
        DispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:spring-mvc.xml
        
        1
    
    
        DispatcherServlet
        /
    

    
    
        CharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
    
    
        CharacterEncodingFilter
        /*
    



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

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

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