导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
配置yml
server:
port: 8090
spring:
datasource:
url: jdbc:mysql://localhost:3306/student
username: root
password: burst970919
jpa:
#自动创建、更新、验证数据库表结构
hibernate:
ddl-auto: update
#设置数据库引擎为InnoDB
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
#打印sql语句,方便调试
show-sql: true
package com.uncle.controller;
import com.uncle.bean.Guest;
import com.uncle.service.GuestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @program: SpringBoot-study
* @description: 嘉宾的控制层
* @author: 步尔斯特
* @create: 2021-03-03 14:22
**/
@Controller
@RequestMapping("/guest")
public class GuestController {
@Autowired
private GuestService guestService;
// @RequestMapping(method = RequestMethod.GET)
@GetMapping
public String list(Model model) {
List<Guest> list = guestService.list();
model.addAttribute("list", list);
return "list";
}
}
package com.uncle.service;
import com.uncle.bean.Guest;
import com.uncle.repository.GuestRepository;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @program: SpringBoot-study
* @description: 嘉宾业务接口的实现类
* @author: 步尔斯特
* @create: 2021-03-03 14:25
**/
@Service
public class GuestServiceImpl implements GuestService{
@Autowired
private GuestRepository guestRepository;
@Override
public List<Guest> list() {
return guestRepository.findAll();
}
}
package com.uncle.repository;
import com.uncle.bean.Guest;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* @program: SpringBoot-study
* @description: 数据仓库
* @author: 步尔斯特
* @create: 2021-03-10 13:28
**/
public interface GuestRepository extends JpaRepository<Guest,Long> {
}
package com.uncle.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.io.Serializable;
/**
* @program: SpringBoot-study
* @description: 嘉宾实体类
* @author: 步尔斯特
* @create: 2021-03-03 14:20
**/
@Entity
@Data@AllArgsConstructor@NoArgsConstructor
public class Guest implements Serializable {
@Id
private Long id;
@Column
private String name;
@Column
private String role;
}