Complexité de la pile applicative
package fr.ensai.demo;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
# Start the Spring framework
SpringApplication.run(DemoApplication.class, args);
}
}
public class Command {
private Product product;
public Command() {
this.product = new Product(“something”);
}
}
public class Command (
private Product product;
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
// Passage de produit en paramètre, peu être une ss classe
this.product = product;
)
public Command() { } // Constructeur plus léger
}
@Component
public class FirstBean {
public String toString (){
return "Hello world"
};
}
@SpringBootApplication
public class DemoApplication {
@Autowired
private FirstBean firstBean;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
System.out.println(firstBean);
}
}
org.springframework.boot
spring-boot-starter-web
@RestController
@RequestMapping("/hello")
public class HelloWorldController {
@GetMapping("/world")
public String helloWorld() {
return "Hello world";
}
}
@Entity
@Table(name = "employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
private String mail;
private String password;
}
@Repository
public interface EmployeRepository extends CrudRepository < Employee, Long > {
}
Pour un CRUD de base pas besoin de plus de code !!!
@Service
public class EmployeeService {
@Autowired
private EmployeRepository employeRepository;
public Optional<Employee> getEmployee(final Long id) {
return employeRepository.findById(id);
}
public Iterable<Employee> getEmployees() {
return employeRepository.findAll();
}
public void deleteEmployee(final Long id) {
employeRepository.deleteById(id);
}
public Employee saveEmployee(Employee employee) {
Employee savedEmployee = employeRepository.save(employee);
return savedEmployee;
}
}
#H2 Database configuration
database=h2
spring.datasource.schema=classpath*:schema.sql
spring.datasource.data=classpath*:data.sql