一、用maven构建
1.在Project Object Model(pom)中添加parent
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.7</version> <relativePath/> <!-- lookup parent from repository --> </parent>
添加spring-boot-start-web和thymeleaf依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
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
依赖
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency>
配置文件添加
# 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
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope>
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.运行结果