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

SSM入门-SSM整合(注解开发)

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

SSM入门-SSM整合(注解开发)

SSM整合-纯注解开发

SSM整合,基于SSM入门-Spring与MyBatis整合

基于Spring与MyBatis注解开发整合

新增 依赖 SpringMvcConfig.java controller servletConfig.java

依赖


    4.0.0

    org.example
    Spring02_Test
    1.0-SNAPSHOT

    
        8
        8
    

    
        
            mysql
            mysql-connector-java
            5.1.47
        

        
            com.alibaba
            druid
            1.1.16
        

        
            org.mybatis
            mybatis
            3.5.6
        

        
            org.springframework
            spring-jdbc
            5.2.10.RELEASE
        

        
            org.mybatis
            mybatis-spring
            2.0.2
        

        
            org.springframework
            spring-context
            5.2.10.RELEASE
        

        
        
            org.springframework
            spring-webmvc
            5.2.10.RELEASE
        

        
            org.springframework
            spring-test
            5.2.10.RELEASE
        

        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.9.8
        


        
        
            javax.servlet
            javax.servlet-api
            3.1.0
            provided
        



    


SpringMvcConfig
@Configuration  //设置为配置文件
@ComponentScan("com.xxxIt.controller")  //扫包mvc处理controller
@EnableWebMvc   //加入参数才能转换
public class SpringMvcConfig {
}
SerlvetConfig
package com.xxxIt.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    @Override
    protected Class[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}
Controller
package com.xxxIt.controller;

import com.xxxIt.pojo.User;
import com.xxxIt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController     //替代@Controller和ResponseBody
@RequestMapping("/users")   //所有请求的路径前缀
public class UserController {

    @Autowired  //自动装配
    private UserService userService;

    @GetMapping("/{id}")    //路径参数
    public User selectById(@PathVariable Integer id){
        return userService.selectById(id);
    }

}
启动maven 测试

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

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

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