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

springboot 构建

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

springboot 构建

一、用maven构建

1.在Project Object Model(pom)中添加parent

 


    org.springframework.boot
    spring-boot-starter-parent
    2.6.7
     

添加spring-boot-start-web和thymeleaf依赖


    org.springframework.boot
    spring-boot-starter-thymeleaf


    org.springframework.boot
    spring-boot-starter-web

2.创建启动类Application

 

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

3.创建controller, service, dao 等层,配置文件application.properties

application.properties:

#thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
spring.thymeleaf.content-type=text/html
spring.thymeleaf.enabled=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
pageCache.enbale=true
#log
logging.level.com.imooc.miaosha=DEBUG
logging.level.org.mybatis=DEBUG
logging.level.com.ibatis=DEBUG
logging.level.com.alibaba.druid=DEBUG

编写sampleController

@RequestMapping({"/db/get", "/demo"})
@Controller
public class SampleController {

    @RequestMapping("/thymeleaf")
    public String thymeleaf(Model model){
        model.addAttribute("name", "fanziqi");
        return "hello~";
    }

4.mybatis

依赖


    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    2.2.2

配置文件添加

# mybatis
mybatis.type-aliases-package=com.imooc.miaosha.domain
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.default-fetch-size=100
mybatis.configuration.default-statement-timeout=3000
mybatis.mapperLocations = classpath:com/imooc/miaosha/dao

@ResponseBody
@RequestMapping({"/db/get", "/demo"})
@Controller
public class SampleController {

    @RequestMapping("/thymeleaf")
    public String thymeleaf(Model model){
        model.addAttribute("name", "fanziqi");
        return "hello~";
    }

}

6.service

public User getById(int id) {
    return UserDao.getById(id);
}

7.dao

public class UserDao {
    public static User getById(int id) {
        return UserDao.getById(id);
    }
}

8.运行结果

 

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

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

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