手把手-快速搭建SpringBoot、Mybatis、Thymeleaf框架demo

SpringBoot入門知識

SpringBoot入門配置的相關知識在以下鏈接
https://blog.csdn.net/qq_41653935/article/details/104576132

快速搭建

目的:
學習springboot,熟悉快速搭建springboot基本配置
主要涉及SpringBootMybatisThymeleaf通用mapper

1、創建項目

new->project
在這裏插入圖片描述
在這裏插入圖片描述
之後就next->finish。

目標結構圖如下,

在這裏插入圖片描述

2、導入依賴

 <parent>
<!--    父工程    對各種常用依賴(並非全部)的版本進行了管理-->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
<!--            web啓動器-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
<!--            SpringBoot會自動爲Thymeleaf註冊一個視圖解析器-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
<!--            mybatis提供的與springboot整合包-->
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
<!--        通用mapper作者提供-->
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.4</version>
        </dependency>
        <dependency>
<!--          jdbc的啓動器,默認使用HikariCP連接池  -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
<!--            數據庫驅動-->
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.19</version>
        </dependency>
    </dependencies>

3、SpringBootApplication啓動類

@SpringBootApplication
@MapperScan("com.codeying.mapper")//掃描mapper包下的映射
public class SpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootApplication.class);
    }
}

4、application.yml配置文件

配置數據源、端口等

server:
  port: 8081
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/數據庫名?serverTimezone=UTC
    username: ***
    password: ***

5、controller-嘗試啓動

嘗試啓動springboot項目,編寫controller類,這時需要一個hello.html響應,在resources目錄下編寫hello.html隨便顯示一些東西
在這裏插入圖片描述

@Controller
@RequestMapping
public class Mycontroller {

    @RequestMapping("hello")
    public ModelAndView hello(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("hello");
        return mv;
    }
}

結果

在這裏插入圖片描述

6、實體類和通用mapper

創建以下實體類 並且在數據庫中創建相應數據庫,創建Account表和id,name,money字段,插入一些數據。
Account .java

public class Account {
    Integer id;
    String name;
    Double money;
    ……
    setter getter略

AccountMapper .java

public interface AccountMapper extends Mapper<Account> {
    
}

7、service

IAccountService 接口

public interface IAccountService {
    List<Account> findAll();
}

AccountServise

@Service
public class AccountServise implements IAccountService {

    @Autowired
    AccountMapper accountMapper;

    @Override
    public List<Account> findAll() {
        return accountMapper.selectAll();
    }
}

8、完善controller

添加如下代碼,基本的查詢賬戶功能以及完善,在hello.html中展示賬戶信息

	@Autowired
    AccountServise accountServise;
    
	@GetMapping("account")
    public ModelAndView account(){
        ModelAndView mv=new ModelAndView();
        List<Account> accounts=accountServise.findAll();
        mv.addObject("accounts",accounts);
        mv.setViewName("hello");
        return mv;
    }

完整hello.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>hello,codeying</h1>
<table class="list">
    <tr>
        <th>id</th>
        <th>姓名</th>
        <th>餘額</th>
    </tr>
    <tr th:each="account : ${accounts}">
        <td th:text="${account.id}">1</td>
        <td th:text="${account.name}">張三</td>
        <td th:text="${account.money}">張三</td>
    </tr>
</table>
</body>
</html>

測試成功
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章