使用idea, 搭建Gradle+springboot+thymeleaf 项目

本文介绍了使用IDEA 2019进行Spring Boot开发的过程。涉及gradle配置、application.properties配置,使用Thymeleaf编写Controller和页面并测试。还阐述了使用MyBatis访问数据库,包括修改build.gradle和配置文件,编写实体类等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

使用IDEA 2019 

 

 

 gradle的相关配置可以参考

创建完成之后,目录结构如下

 

查看 build.gradle

plugins {
    id 'org.springframework.boot' version '2.1.5.RELEASE'
    id 'java'
    id 'war'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

配置application.properties

spring.thymeleaf.cache=false

编写Controller ,测试是否成功

 

package com.example.demo.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

    @RequestMapping("/")
    @ResponseBody
    public String index(){
        return "hello world!";
    }
}

 

OK,下面开始使用thymeleaf 

编写Controller

    @RequestMapping("/user")
    public String getUser(HttpServletRequest request){
        request.setAttribute("user","thymeleaf User! ");
        return "index";
    }

编写页面 

 

<!DOCTYPE html>
<html  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>templates 测试页</title>
</head>
<body>
<h1 th:text="${user}"></h1>
</body>
</html>

测试网页

 

使用mybatis访问数据库

修改build.gradle

plugins {
    id 'org.springframework.boot' version '2.1.5.RELEASE'
    id 'java'
    id 'war'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.38'

    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.1'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

修改配置文件


spring.thymeleaf.cache=false

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/life_master
spring.datasource.username=root
spring.datasource.password=mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

 

实体类

package com.example.demo.domain;

import org.springframework.stereotype.Component;

import java.io.Serializable;

@Component
public class User implements Serializable {
    private int id;
    private String name;
    private String password;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}
package com.example.demo.mapper;

import com.example.demo.domain.User;
import org.apache.ibatis.annotations.*;


@Mapper
public interface UserDao {
    @Select("SELECT * FROM users where id = #{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "password", column = "password")
    })
    User findById(@Param("id") int id);
}

controller, userdao处会有提示,但是不影响程序运行

 @Autowired
    UserDao userdao;
    @GetMapping("/{id}")
    public String findById(HttpServletRequest request , @PathVariable(value = "id") String id){

        User u = userdao.findById(Integer.parseInt(id));
        System.out.println(u);
        request.setAttribute("user",u.toString());
        return "home";
    }

页面

<!DOCTYPE html>
<html  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>templates 测试页</title>
</head>
<body>
<h1 th:text="${user}">Hello</h1>
</body>
</html>

访问页面

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hero_孙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值