1. 使用maven搭建项目
<!-- 1. 添加parent--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> </parent>
- 2 pom.xml添加spring-boot-start-web和thymeleaf依赖
<!-- 2. 添加spring-boot依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency><!-- 3. thymeleaf 依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2. 创建spring-boot 启动类Application
@SpringBootApplicationpublic class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);; }
}
3. 创建controller, service, dao 等层,以及在resource文件夹下创建配置文件application.properties
server.port=8080
#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
- 2 在controller层下面编写SampleController
@Controller@RequestMapping("/demo")
public class SampleController {
@RequestMapping("/thymeleaf")
public String thymeleaf(Model model){
model.addAttribute("name", "weida"); return "hello"; }
}
4. mybatis
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version >
</dependency>
# mybatismybatis.type-aliases-package=com.imooc.miaosha.domainmybatis.configuration.map-underscore-to-camel-case=truemybatis.configuration.default-fetch-size=100mybatis.configuration.default-statement-timeout=3000mybatis.mapperLocations = classpath:com/imooc/miaosha/dao/*.xml
# 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
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.5</version>
</dependency>
4 controller
/** * 查询 * @return */@RequestMapping("/db/get")
@ResponseBodypublic Result<User> dbGet(){
User user = userService.getById(1); return Result.success(user);}
5 service
public User getById(int id) {
return userDao.getById(id);}
6 dao
@Select("select * from user where id = #{id}")
public User getById(@Param("id") int id);