1. 程式人生 > 其它 >自己動手搭建簡單的Spring Boot專案

自己動手搭建簡單的Spring Boot專案

搭建簡單的Spring Boot專案

1.引入web,mybatis,thymeleaf相關的jar包。

<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>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
   <!-- <scope>runtime</scope>-->//需要註釋掉,否則application.yml中driver-class-name報錯。
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>//注意引入的mybatis包。
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.20</version>
</dependency>

2.application.properties中配置,mysql 、mybatis

spring.datasource.url=jdbc:mysql://localhost:3306/springboot_tmp?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# 根據自己專案的情況進行配置
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.user.entity

3.專案結構如下:

java
  -com
    example
      user
      controller
      dao
      entity
      service
        -impl
resources
  mapper
  static
  templates

4.UserController.java

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public ModelAndView queryList(){
        ModelAndView  modelAndView= new ModelAndView();
        List<User> users = userService.queryList();
        modelAndView.addObject("users",users);
        modelAndView.setViewName("index");
        return modelAndView;
    }

}

UserServiceImpl.java

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    public List<User> queryList(){

        return userDao.queryList();
    }

}

UserDao.java

@Repository
//@Mapper 如果在啟動類新增掃描註解的話,可以不加此註解。
public interface UserDao {

    List<User> queryList();
}

User.java

@ToString
@Data
public class User {
    private Integer id;
    private String name;
    private Integer age;
    private String address;

}

Application

@SpringBootApplication
@MapperScan("com.example.user.dao")//注意如果不想每個資料庫持久層都加@Mapper的話,就加上這段掃描
public class Application {

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

}

5.index.html 頁面直接放在 templates 目錄下

<!DOCTYPE html>
<!-- 如果想讓頁面有提示,就加上thymeleaf的名稱空間-->
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>thymeleaf</title>
</head>
<body>
        <table>
            <tr>
                <th>#id</th>
                <th>#name</th>
                <th>#age</th>
                <th>#address</th>
            </tr>
            <tr th:each="user,stat : ${users}">
               <!-- <td th:text="${stat.count}"></td>-->
                <td th:text="${user.id}"></td>
                <td th:text="${user.name}"></td>
                <td th:text="${user.age}"></td>
                <td th:text="${user.address}"></td>
            </tr>
        </table>
</body>
</html>

6.訪問localhost:8080/user/list 路徑則頁面顯示,當然實際的專案開發中還有很多對請求的攔截和靜態頁面的過濾在這裡就不贅述了。