Skip to content

Instantly share code, notes, and snippets.

View RestController.java
@Controller
@RequestMapping("rest")
public class RestController {
@Autowired
RestService restService;
@GetMapping("/{id}")
public ResponseEntity<RestResult> getById(@PathVariable("id") int id){
RestResult rest = restService.getRestById(id);
return new ResponseEntity<RestResult>(rest, HttpStatus.OK);
View RestServiceImp.java
@Service
public class RestServiceImp implements RestService{
@Autowired
private RestDao restDao;
@Override
public List<RestResult> getAll() {
return restDao.getAll();
}
View RestDaoImp.java
@Transactional
@Repository
public class RestDaoImp implements RestDao{
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<RestResult> getAll() {
String sql = "select * from rest order by id asc";
List<RestResult> rest = jdbcTemplate.query(sql, new RestMapper());
View RestMapper.java
public class RestMapper implements RowMapper<Rest>{
@Override
public Rest mapRow(ResultSet rs, int rowNum) throws SQLException {
Rest rest = new Rest();
rest.setId(rs.getInt("id"));
rest.setKey(rs.getString("key"));
rest.setValue(rs.getString("value"));
RestResult restResult = new RestResult();
View BukuController.java
package com.example.springbook.controller;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
View BukuRepository.java
package com.example.springbook.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.example.springbook.model.Buku;
View SpringBookApplication.java
package com.example.springbook;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@SpringBootApplication
@EnableJpaAuditing
public class SpringBookApplication {
View Buku.java
package com.example.springbook.model;
import org.hibernate.validator.constraints.NotBlank;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
You can’t perform that action at this time.