IDEA创建Spring+SpringMVC+MyBatis(SSM)极简入门(上)

1.  创建项目

2.  添加Controller

3.  pom+ properties+swager

4.  添加Mysql+ Mybatis

5.  调用Mybatis生成Mapper

1.创建项目

Web+MySQL;

2.添加Controller

默认端口8080

package com.eastmoney.emcc.controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/test")
public class TestController {
//<editor-fold desc="Get方法1.普通HTTP参数方式">
/**
* @param yourName 测试名称
* @return helloYouName
*/
@ApiOperation(value = "你的名字", notes = "根据传入名字返回hello to you!")//api简述,详细内容
//访问路径,访问方式 | http://localhost:8080/test/sayHelloToYou?yourName=gyb
@RequestMapping(value = "/sayHelloToYou", method = RequestMethod.GET)//普通HTTP参数GET方式
public String sayHelloToYou(@RequestParam String yourName) {
return "hello " + yourName;
}
//</editor-fold>

//<editor-fold desc="Get方法2.RestFul标准实现">
@ApiOperation(value = "你的生日年", notes = "根据传入生日年返回2019年岁数!")//api简述,详细内容
//http://localhost:8080/test/yourAgeTest/1 |RestFul标准实现
@RequestMapping(value = "/yourAgeTest/{yourBirthYear}", method = RequestMethod.GET)//RestFul标准实现,必填
public Integer countYourAge(@PathVariable(name = "yourBirthYear") Integer yourBirthYear) {
return 2019 - yourBirthYear;
}
//</editor-fold>
}

3.pom+ properties+swager

Mybatis、mapper、pagehelper 三个组件暂时不要开启

配置properties

#current env(dev,test,prod),当前环境
spring.profiles.active=test
#set Spring AOP support,面向切面
spring.aop.auto=true
#set aop-proxy mode: true(cglib) false(java JDK proxy)
#spring.aop.proxy-target-class=true
## server,端口,上下文路径,大小写敏感,http://localhost:8989/emcc/swagger-ui.html#/
server.port=8989
server.servlet.context-path=/emcc
spring.jackson.mapper.accept_case_insensitive_properties=true
#Swagger Configure Properties,swagger2使能设置,包扫描路径目录,swagger-ui标题,描述,版本
emcc.swagger.enable=true
emcc.swagger.packageScan=com.eastmoney.emcc.controller
emcc.swagger.title=Eastmoney Choice Club System API Document
emcc.swagger.description=Summary of emcc api
emcc.swagger.version=1.1

添加Swagger2 configuration

修改TestController

http://localhost:8989/emcc/swagger-ui.html#/

4.添加Mysql+ Mybatis

查看驱动路径

Copy所有表名

解禁pom中的mybatis依赖,添加properties中的配置

添加mybatis-config.XML

添加CommonMapper 、MyMapper、CommonMapper.xml

添加DynamicDataSource和MyBatisSpringConfig

Application类添加包扫描

@MapperScan(basePackages = "com.eastmoney.emcc.dao.mapper", markerInterface = MyMapper.class)//for mybatis
@SpringBootApplication(scanBasePackages = {"com.eastmoney.emcc"})//for mybatis

添加service及其实现类

添加TestController

运行调试swagger

5.调用Mybatis生成Mapper

添加mybatis-generator.xml

修改pom,build选项

刷新并创建

禁用pom中的build

原文地址:https://www.cnblogs.com/Chary/p/9791820.html