SSM整合,基于SSM入门-Spring与MyBatis整合
基于Spring与MyBatis注解开发整合新增 依赖 SpringMvcConfig.java controller servletConfig.java
依赖SpringMvcConfig4.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
@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
测试



