1. get full list - all
http://localhost:8011/myapp/lov/all
This will have
- Controller
- Service and Impl
- Repository
- Model class
Controller
package com.sample.controller;
import com.sample.model.Lov;
import com.sample.service.LovServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/myapp/lov")
public class LovController {
@Autowired
LovServiceImpl lovService;
@GetMapping("/all")
public ResponseEntity<List<Lov>> retrieveAllValues() {
return new ResponseEntity<List<Lov>>(lovService.retrieveAllValues(), HttpStatus.OK);
}
@GetMapping("/fieldName")
public ResponseEntity<List<Lov>> findByFieldName(@RequestParam String fieldName) {
return new ResponseEntity<List<Lov>>(lovService.findByFieldName(fieldName), HttpStatus.OK);
}
@GetMapping("/fieldName1")
public ResponseEntity<List<Lov>> findLovByFieldName(@RequestParam String fieldName) {
return new ResponseEntity<List<Lov>>(lovService.findLovByFieldName(fieldName), HttpStatus.OK);
}
@GetMapping("/fieldId")
public ResponseEntity<List<Lov>> findByFieldId(@RequestParam String fieldId) {
return new ResponseEntity<List<Lov>>(lovService.findByFieldId(fieldId), HttpStatus.OK);
}
}
-----------------
Model class
package com.sample.model;
import lombok.Data;
import javax.persistence.*;
@Entity
@Table(name = "lov")
@Data
public class Lov {
@Id
private int lovId;
@Column(name = "fieldName")
private String fieldName;
@Column(name = "fieldId")
private String fieldId;
@Column(name = "value")
private String value;
}
--------------------------------------------------------------------------------------
Service interface
package com.sample.service;
import com.sample.model.Lov;
import com.sample.repository.LovRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
public interface LovService {
List<Lov> retrieveAllValues();
List<Lov> findByFieldName(String fieldName);
List<Lov> findLovByFieldName(String fieldName);
List<Lov> findByFieldId(String fieldId);
}
--------------------------------
Service Impl
package com.sample.service;
import com.sample.model.Lov;
import com.sample.repository.LovRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class LovServiceImpl implements LovService {
private static final Logger logger = LoggerFactory.getLogger(LovServiceImpl.class);
@Autowired
LovRepository lovRepository;
public List<Lov> retrieveAllValues() {
return lovRepository.findAll();
}
public List<Lov> findByFieldName(String fieldName) {
return lovRepository.findByFieldName(fieldName);
}
public List<Lov> findLovByFieldName(String fieldName) {
return lovRepository.findLovByFieldName(fieldName);
}
public List<Lov> findByFieldId(String fieldId) {
return lovRepository.findByFieldId(fieldId);
}
}
-----------------------------------------
Repository
package com.sample.repository;
import com.sample.model.Lov;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface LovRepository extends JpaRepository<Lov,Integer> {
List<Lov> findLovByFieldName(String fieldName);
List<Lov> findByFieldName(String fieldName);
List<Lov> findByFieldId(String fieldId);
}
Properties
server.port=8011
#your database name is mydb
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=false
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
logging.level.root=warn
logging.level.com.sample=debug
Dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
No comments:
Post a Comment