SpringBoot + H2 + JPA + Thymeleaf 整合

SpringBoot + H2 + JPA  + Thymeleaf 整合 

maven依賴:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

yaml配置:

spring:
  datasource:
    platform: h2
    driver-class-name: org.h2.Driver
#    內存模式
#    url:  jdbc:h2:mem:base
#   AUTO_SERVER 混合模式 、DB_CLOSE_ON_EXIT、跟隨項目啓動
    url: jdbc:h2:file:./h2/base-db;AUTO_SERVER=TRUE;DB_CLOSE_ON_EXIT=TRUE
    username: root
    password: root
#    每次加載 , 只用於表格創建
#    schema: classpath:db/base.sql
  h2:
    console:
      settings:
        web-allow-others: true
      enabled: true
      path: /h2-console
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update
    generate-ddl: false
  mvc:
    view:
      prefix: /
      suffix: .html

  thymeleaf:
    prefix: classpath:/templates/
    check-template-location: true
    suffix: .html
    encoding: UTF-8
    servlet:
      content-type: text/html
    mode: HTML5
    cache: false

目錄截圖:

sql創建語句(base):

DROP TABLE IF EXISTS user;
CREATE TABLE user (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
 name varchar(35),
 PHONE varchar(35)
);
select * from user;
insert into user(id,name,PHONE) values(784,'在寫作協作','123456');
insert into user(id,name,phone) values(90,'是的','123456');

select * from user;
Thymeleaf(HTML)模板:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>TEMPLATES</title>
</head>
<body>
    Hello H2
    <p th:text="${users}"></p>
    <form action="/user/deleteUser" method="post">
        ID:<input name="id">
        <button type="submit">刪除</button>
    </form>

    <form action="/user/addUser" method="post">
        ID:<input name="id">
        NAME:<input name="name">
        PHONE:<input name="phone">
        <button type="submit">添加</button>
    </form>
</body>
</html>

Domain:

package com.domain;

import lombok.Data;

import javax.persistence.*;

@Entity
@Table(name="user")
@Data
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Column
    private String name;
    @Column
    private String phone;

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getPhone() {
        return phone;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

Dao:

package com.dao;

import com.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserDao extends JpaRepository<User, Integer> {

}

Controller:

package com.controller;

import com.domain.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;

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

    @Autowired
    private UserService userServiceImpl;

    @GetMapping("/getUsers")
    public String getUsers (ModelMap map){
        List<User> users = this.userServiceImpl.getUsers();
        map.addAttribute( "users",users );
        return "index";
    }

    @PostMapping("/addUser")
    public String addUser (ModelMap map,User user){
        if (this.userServiceImpl.addUser( user )!=null)
            return "redirect:/user/getUsers";
        else
            return "index";
    }

    @PostMapping("/deleteUser")
    public String deleteUser (Integer id){
        try {
            this.userServiceImpl.deleteUser( id );
            return "redirect:/user/getUsers";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "index";
    }
}

 

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