集成lombok

idea下载插件lombok

pom.xml

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

集成mybatis

pom.xml

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.5</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.16</version>
</dependency>

application.yml

##数据库配置
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver #mysql驱动类名
    username: root #数据库用户名
    password: root #数据库密码
    url: jdbc:mysql://127.0.0.1:3306/testdb?serverTimezone=GMT&useUnicode=true&characterEncoding=utf-8&useSSL=false  #数据库连接url
 
#mybatis配置 
mybatis:
  mapper-locations: mapper/*.xml #mapper映射文件扫描路径

安装better-mybatis-generator插件

打开idea右侧Database控制面板,找到需要逆向的Table,右击选择mabatis-generate

参考下图的文字说明,配置mybatis-generate相关参数,点击OK,idea会自动逆向相对应的表生成相关的类和配置文件

在启动类上增加Mapper类的包扫描路径

@SpringBootApplication
@MapperScan(basePackages = {"com.jun.handlecache.dao"})
public class HandlecacheApplication {   

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

}

集成thymeleaf

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

application.yml

spring:
  thymeleaf:
    enabled: true #是否启用thymeleaf模板 
    cache: false  #是否缓存页面
    check-template-location: true #检测模板是否存在
    encoding: UTF-8	#模板的编码
    mode: HTML  #模板的模式
    prefix: classpath:/templates/  #模板存放位置
    suffix: .html  #模板文件后缀

编写测试页面test.html,一定要添加thymeleaf的命名空间

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
    <h2>Index Page</h2>
    <h4 th:text="${article.title}"></h4>
    <h4 th:text="${article.content}"></h4>
</body>
</html>

测试

编写Service类

@Service
public class ArticleService {

    @Resource
    private ArticleMapper articleMapper;


    public Article findArticle(Long id){
        Article article = articleMapper.selectByPrimaryKey(id);
        return article;
    }

}

编写controller测试类

@Controller
public class HelloController {

    @Autowired
    private ArticleService articleService;


    @GetMapping("/articles/{id}")
    public String hello(Model model, @PathVariable("id") Long id){
        model.addAttribute("article",articleService.findArticle(id));
        return "hello";
    }

}

运行项目查看是否显示index页面