接口訪問參考:https://blog.csdn.net/hanjun0612/article/details/81625395html
PS:調用接口和跳轉網頁java
主要區別是mysql
1 調用接口是 @RestControllerweb
跳轉網頁是:@Controllerspring
2 跳轉網頁,須要增長sql
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
app
File-->New-->Project-->Spring Initializrspring-boot
而後選擇 web和web-starterui
而後咱們看一下搭建好的項目結構:this
先添加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>