一起学习SpringBoot项目
后续会持续更新和springboot相关的代码,本人都是本地运行成功不报错再发帖子,后续会集成各种中间件以及第三方Api欢迎关注一起学习!
第一课:在ide新建springbot项目并运行!
1.File->new->project
2.设置JDK版本
3.项目名称,打包方式,JDK版本,唯一标识等
4.SpringBoot项目需要依赖,暂不勾选可在POM.XML 引入Mave依赖
5.application.properties配置端口号等信息,把我的配置拷贝过来:
#配置端口号
server.port=8988
#数据配置
spring.datasource.url=jdbc:mysql://xx.xx.xx.xx/readapple?useSSL=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&&serverTimezone=CTT
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.password=zhangyuan
spring.datasource.username=root
#配置静态资源路径
spring.resources.static-locations=classpath:/statics/,classpath:/templates/
#mybatis配置别名
mybatis.type-aliases-package=com.st.pojo
mybatis.mapper-locations=classpath:mapper/*.xml
#最后记得开启打印sql语句,方便测试
logging.level.com.st.mapper=debug
logging.level.root=info
logging.config=classpath:logback.xml
#thymeleaf
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.mode: HTML
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
#读取缓存配置文件
spring.cache.ehcache.config=classpath:ehcache.xml
#最后记得开启打印sql语句,方便测试
spring.servlet.multipart.enabled=true
#图片上传
file.location.path=D:/upload
6.启动类方法
7.编写测试Controller
package com.st.controller;
import com.st.pojo.SlickApp;
import com.st.pojo.User;
import com.st.service.SlickAppService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.*;
import java.util.List;
@RestController
@RequestMapping("/json")
public class JsonController {
private static final Logger logger = LoggerFactory.getLogger(JsonController.class);
@Resource
private SlickAppService slickAppService;
@RequestMapping("/user")
public User getUser() {
return new User(1, "孙悟空", "123456");
}
@RequestMapping("/list")
public List<User> getUserList() {
List<User> userList = new ArrayList<>();
User user1 = new User(1, "鲁智深", "123456");
user1.getId();
User user2 = new User(2, "学习课", "123456");
userList.add(user1);
userList.add(user2);
logger.info("aaaaaaaaaaaaaaa");
logger.debug("aass");
return userList;
}
}
浏览器输入http://localhost:8988/json/list:
顺利完成!!!