• springboot 简单搭建(thymeleaf 视图显示)


    接口访问参考:https://blog.csdn.net/hanjun0612/article/details/81625395

    PS:调用接口和跳转网页

    主要区别是

    1 调用接口是 @RestController

       跳转网页是:@Controller

    2 跳转网页,需要增加

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    一,IDEA创建项目

    File-->New-->Project-->Spring Initializr

    然后选择 web和web-starter

    然后我们看一下搭建好的项目结构:

    二,搭建

    先添加POM依赖

    pom

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
        </dependencies>

    然后添加配置文件

    这里主要添加 application.yml和 application-dev.yml  就可以了

    application.yml:(它会通过active:dev去调用application-dev.yml)

    spring:
      profiles:
        active: dev
      datasource:
        url: jdbc:mysql://ip地址:3306/testdb?serverTimezone=UTC
        username: root
        password: root
      jpa:
        generate-ddl: false
        hibernate:
          ddl-auto: none
        show-sql: true
      thymeleaf:
        prefix: classpath:/templates/
        suffix: .html
        mode: HTML5
        encoding: UTF-8
        cache: false
        servlet:
          content-type: text/html

    application-dev.yml

    server:
      servlet:
        context-path:
      port: 8081

    DemoApplication:

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

    User

    @Entity
    @Table(name = "user", catalog = "testdb")
    @DynamicUpdate
    public class User {
        private Integer id;
        private String uuid;
        private String userName;
        private String password;
        private Date createTime;
        private Integer age;
     
        @Id
        @GeneratedValue
        @Column(name = "id")
        public Integer getId() {
            return id;
        }
     
        public void setId(Integer id) {
            this.id = id;
        }
     
        @Column(name = "uuid")
        public String getUuid() {
            return uuid;
        }
     
        public void setUuid(String uuid) {
            this.uuid = uuid;
        }
     
        @Column(name = "username")
        public String getUserName() {
            return userName;
        }
     
        public void setUserName(String userName) {
            this.userName = userName;
        }
     
        @Column(name = "password")
        public String getPassword() {
            return password;
        }
     
        public void setPassword(String password) {
            this.password = password;
        }
     
        @Column(name = "createtime")
        public Date getCreateTime() {
            return createTime;
        }
     
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
     
        @Column(name = "age")
        public Integer getAge() {
            return age;
        }
     
        public void setAge(Integer age) {
            this.age = age;
        }
    }

    UserRepository

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

    HelloController

    @Controller
    public class HelloController {
     
        @Autowired
        UserRepository userRepository;
     
        @RequestMapping("/index.do")
        public String say(ModelMap mode) {
            User user=userRepository.findById(3).get();
            mode.addAttribute("user", user);
            return "say";
        }
    }

    say.html

    <span th:text="${user.userName}"></span>
    hello
    </body>

     

    最后效果:

  • 相关阅读:
    List of all Oracle Server Parameters
    dbcc Trace
    站在巨人的肩膀上 书籍推荐 (zz)
    浅谈JS包装对象
    与 JavaScript 模块相关的所有知识点
    你真的懂Promise吗
    vue动态绑定class与style总结
    vue使用axios实现excel文件下载的实现
    CSRF 与 XSS
    推荐10个很棒的 JS 库
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/11060696.html
Copyright © 2020-2023  润新知