Java Programming

CRUD Spring MVC

Tạo bảng Category

Sử dụng lệnh sau để tạo bảng Category như sau

--DROP TABLE Category;
GO
CREATE TABLE Category(
    CategoryId SMALLINT NOT NULL PRIMARY KEY IDENTITY(1, 1),
    CategoryName NVARCHAR(64) NOT NULL
);
GO
SET IDENTITY_INSERT Category ON;
INSERT INTO Category (CategoryId, CategoryName) VALUES
    (1, 'Laptop'),
    (2, 'Mouse'),
    (3, 'SSD');
SET IDENTITY_INSERT Category ON;

Tạo Model

Tạo class Category

Vào thư mục models trong java/webapp tạo Category.java và viết code như sau

@Entity(name = "Category")
public class Category {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "CategoryId")
    private short id;
    @Column(name = "CategoryName")
    private String name;
    public Category(){
    }
    public Category(short id, String name){
        this.id = id;
        this.name = name;
    }
    public short getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public void setId(short id) {
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    } 
}

Tạo interface CategoryRepository

Vào models tạo tiếp CategoryRepository.java viết code như sau

public interface CategoryRepository extends CrudRepository<Category,Short> {
}

Tạo CategoryController

Trong thư mục controllers ta tạo file HomeController.java như sau

@Controller
public class CategoryController {
    @Autowired
    CategoryRepository repository;

    @GetMapping("/category")
    public String index(Model model){
        model.addAttribute("list", repository.findAll());
        return "category/index";
    }
    @GetMapping("/category/delete/{id}")
    public String delete(@PathVariable("id") short id){
        repository.deleteById(id);
        return "redirect:/category";
    }
    @GetMapping("/category/add")
    public String add(){
        return "category/add";
    }
    @PostMapping("/category/add")
    public String add(Category obj){
        repository.save(obj);
        return "redirect:/category";
    }
    @GetMapping("/category/edit/{id}")
    public String edit(@PathVariable("id") short id, Model model){
        model.addAttribute("o", repository.findById(id).get());
        return "category/edit";
    }
    @PostMapping("/category/edit/{id}")
    public String edit(@PathVariable("id") short id, Category obj){
        repository.save(obj);
        return "redirect:/category";
    }
}

Tạo View cho CategoryController

View index.html

Trong thư mục templates, tạo thư mục category, rồi tạo index.html với nội dung sau:

<a href="/category/add">Add</a>
<table>
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Command</th>
        </tr>
    </thead>
    <tbody>
        <tr th:each="item : ${list}">
            <td th:text="${item.id}"></td>
            <td th:text="${item.name}"></td>
            <td>
                <a th:href="@{/category/edit/{id}(id=${item.id})}">Edit</a>
                <a th:href="@{/category/delete/{id}(id=${item.id})}">Delete</a>
            </td>
        </tr>
    </tbody>
</table>

View add.html

Trong templates/category tạo tiếp add.html, rồi nhập nội dung sau:

<form method="post">
    <div>
        <label for="name">Name</label>
        <div>
            <input type="text" name="name" id="name">
        </div>
    </div>
    <div>
        <button>Save Changes</button>
    </div>
</form>

View edit.html

<form method="post">
    <div>
        <label for="name">Name</label>
        <div>
            <input type="text" name="name" id="name" th:value="${o.name}">
            
        </div>
    </div>
    <div>
        <button>Save Changes</button>
    </div>
</form>