springboot 构建

springboot 构建,第1张

一、用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/*.xml

配置druid

# druid
spring.datasource.url=jdbc:mysql://localhost:3306/miaosha?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.filters=stat
spring.datasource.maxActive=1000
spring.datasource.initialSize=100
spring.datasource.maxWait=60000
spring.datasource.minIdle=500
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=select 'x'
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxOpenPreparedStatements=20

jdbc


    mysql
    mysql-connector-java
    runtime

5.controller

/**
 *  查询
 * @return
 */

@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.运行结果

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/793263.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-05
下一篇 2022-05-05

发表评论

登录后才能评论

评论列表(0条)

保存