Employee.java
@Entity
@Table(name="m_emp")
public class Employee {
@Id
@Column(name="empno")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String empname;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}
}
EmployeeRepository.java
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}
SampleApplication.java
@SpringBootApplication
public class SampleApplication {
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
}
Con.java
@RestController
public class Con {
@Autowired
EmployeeRepository empRepository;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(Model model) {
List<Employee> emplist=empRepository.findAll();
model.addAttribute("emplist", emplist);
return emplist.toString();
}
}
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/nom de la base de données
spring.datasource.username=root
//Aucun mot de passe requis si aucun mot de passe n'est spécifié dans l'installation de brassage
#spring.datasource.password=mysql
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.jpa.database=MYSQL
spring.jpa.hibernate.ddl-auto=update
build.gradle
plugins {
id 'org.springframework.boot' version '2.1.8.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('org.springframework.boot:spring-boot-devtools')
runtime('mysql:mysql-connector-java')
compileOnly('org.projectlombok:lombok')
implementation('org.springframework.boot:spring-boot-starter-tomcat')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
Recommended Posts