使用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";
}
编写页面
templates 测试页
测试网页
修改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";
}
页面
templates 测试页
Hello
访问页面