自己动手搭建简单的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 路径则页面显示,当然实际的项目开发中还有很多对请求的拦截和静态页面的过滤在这里就不赘述了。

posted @ 2021-07-05 16:22  小小酥oAo  阅读(130)  评论(0)    收藏  举报