第四步:创建 MVC 相关类

蒲公英 提交于 周四, 06/04/2020 - 22:57
Spring Boot、MVC 、Security + Mybatis + Thymeleaf

本教程架构当前较流行的 Java 技术:
Spring Boot、MVC 、Security + Mybatis + Mysql + Thymeleaf

 

后台我们仍采用通常分层方式:
Controller -> Service -> Dao(这里是 Mybatis 的 Mapper)

 

 

 

创建以下包:

  • com.example.demo.controller
  • com.example.demo.service
  • com.example.demo.entity
  • com.example.demo.mapper

追加 Entity 类:

package com.example.demo.entity;

import java.io.Serializable;

public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;
    
    private String username;
    
    private String password;
    
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
}
package com.example.demo.entity;

import java.io.Serializable;

public class Shirt implements Serializable {

    private static final long serialVersionUID = 1L;
    
    private String name;

    private int size;
    
    private String color;
    
    public String getName() {
        return name;
    }

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

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

}

追加 Mapper 类:

package com.example.demo.mapper;

import org.apache.ibatis.annotations.Mapper;
import com.example.demo.entity.User;

@Mapper
public interface UserMapper {
    User fetchUserByUsername(String username);
}
package com.example.demo.mapper;

import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.example.demo.entity.Shirt;

@Mapper
public interface ShirtMapper {
    List<Shirt> fetchAll();
}

追加 Service 类:

package com.example.demo.service;

import java.util.List;

import com.example.demo.entity.Shirt;

public interface ShirtService {
    List<Shirt> fetchAll();
}
package com.example.demo.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.entity.Shirt;
import com.example.demo.mapper.ShirtMapper;

@Service("shirtService")
public class ShirtServiceImpl implements ShirtService {
    @Autowired
    private ShirtMapper shirtMapper;

    @Override
    public List<Shirt> fetchAll() {
        return shirtMapper.fetchAll();
    }
}

追加 Controller 类:

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.demo.service.ShirtService;

@Controller
public class ShirtController {
    @Autowired
    private ShirtService shirtService;

    @RequestMapping("/admin")
    public String findAll(Model model){ 
        model.addAttribute("shirts", shirtService.fetchAll());
        
        return "shirts";
    }
}

resources/mapper 下追加 Mapper XML 文件:

UserMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.demo.mapper.UserMapper">
  <select id="fetchUserByUsername" resultType="User">
    SELECT * FROM `users` WHERE username = #{username}
  </select>
</mapper>

ShirtMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.demo.mapper.ShirtMapper">
  <select id="fetchAll" resultType="Shirt">
    SELECT * FROM `shirts`
  </select>
</mapper>

resources/templates 下追加 shirts.html 文件:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <title>Shirts</title>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<table>
  <tr>
    <th>Name</th>
    <th>Size</th>
    <th>Color</th>
  </tr>
  <tr th:each="shirt : ${shirts}">
    <td th:text="${shirt.name}"></td>
    <td th:text="${shirt.size}"></td>
    <td th:text="${shirt.color}"></td>
  </tr>
</table>
</body>
</html>