接口访问参考: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>
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"; } }
<span th:text="${user.userName}"></span>
hello
</body>
springboot 简单搭建(thymeleaf 视图显示)
原文:https://www.cnblogs.com/hanjun0612/p/11060696.html