Czy mógłbym prosić kogoś aby wytłumaczył mi jakoś bardzo łopatologicznie jak działa Spring DAO?
Mój kod:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.List;
@RestController
@RequestMapping("/web")
@Component
public class RunAtStart {
private final AccountRepository accountRepository;
public RunAtStart(AccountRepository accountRepository) {
this.accountRepository = accountRepository;
}
@GetMapping("searchall")
public List<Account> searchall() {
return accountRepository.findAll();
}
@GetMapping("user/firstname/{firstName}")
public List<Account> getByFirstName(@PathVariable("firstName") String firstName) {
return accountRepository.findByFirstNameIgnoreCase(firstName);
}
@GetMapping("user/lastname/{lastName}")
public List<Account> getByLastName(@PathVariable("lastName") String lastName) {
return accountRepository.findByLastNameIgnoreCase(lastName);
}
@GetMapping("user/id/{id}")
public Account getById(@PathVariable("id") long id) {
return accountRepository.findById(id);
}
@GetMapping("users/newsalary/{newsalary}")
public int setSalaryForAll(@PathVariable("newSalary") int newSalary) {
return accountRepository.setSalaryForAll(newSalary);
}
@GetMapping(value = "users/add")
Account addEmployee(
@RequestParam("name") String name,
@RequestParam("last") String last,
@RequestParam("salary") BigDecimal salary
) {
Account account = new Account();
account.setFirstName(name);
account.setLastName(last);
account.setSalary(salary);
accountRepository.save(account);
return account;
}
}
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.List;
@Repository
public interface AccountRepository extends JpaRepository<Account, Long> {
List<Account> findByFirstNameIgnoreCase(String firstName);
List<Account> findByLastNameIgnoreCase(String lastName);
List<Account> findBySalaryBetween(BigDecimal from, BigDecimal to);
Account findById(long id);
@Transactional
@Modifying
@Query(value = "UPDATE Account a SET a.salary = :newSalary")
int setSalaryForAll(@Param("newSalary") int newSalar);
}
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.math.BigDecimal;
@Entity
@Table(name = "PRACOWNIK")
public class Account {
@Id
@GeneratedValue
private Long id;
private String firstName;
private String lastName;
private BigDecimal salary;
@Override
public String toString() {
return "Account{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", salary=" + salary +
'}';
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public BigDecimal getSalary() {
return salary;
}
public void setSalary(BigDecimal salary) {
this.salary = salary;
}
}
Chodzi mi o to, że przerabiam tutorial Artura Owczarka i on sobie działa na Spring Boocie.
Łączę się z bazą, mam konto Employee oraz EmployeeRepository.
EmployeeRepository dziedziczy po JpaRepository a Employee jest zwykłą klasą tyle, że jeszcze ma adnotacje Entity aby stworzyć tabelę.
W moim "springowym" mainie wszystko sobbie działa na obiekcie EmployeeRepository i wszystko śmiga, bez frontu bo jSon ale działa...
Dzisiaj włączyłem tutorial z internetu, tam najczęściej znajuje się lekko inna konfiguracja.
W linku:
http://www.dineshonjava.com/2012/12/spring-mvc-with-hibernate-crud-example.html#.WAig3fmLRhE
Są kody, serwis oraz jego implementacja, dao oraz implementacja.
Na zdjęciu:
https://1.bp.blogspot.com/-3fkVVG_HUb4/UN3xTEZJ4JI/AAAAAAAABvg/DEZTnn4Jymo/s1600/spring3-hibernate-application-architecture.png
Pokazane jest, że Dao idzie z bazy do serwisu i potem z serwisu do kontrolera.
Teraz moje pytanka.
- Czy to Spring Boot tak ogranicza kod? Złożoność mojego kodu jest dużo mniejsza (nie patrząc na web) i dlaczego tak jest?
- Do czego używa się te implementacje dao oraz serwis? W kodzie zamieszczonym przeze mnie jest już implementacja w kontrolerze a tu wszystko rozbite?
Czy mógłbym prosić aby ktoś poświecił chwilkę i mi to wytłumaczył bardzo prostym sposobem? Jak dziecku, dopiero startuje i nie chcę się uczyć źle a to zapewne podstawa podstaw.
Z góry dziękuję,
Pozdrawiam.