Skip to content
nimamoosavi edited this page Aug 14, 2021 · 7 revisions

this project is a small modular and expandable library that allows you to eliminate the writing of boilerplate code for CRUD operations in the development of Spring applications that work with databases and microservices. It implements a full set of base operations to Create, Read, Update and Delete your entities. Currently, it works with JPA databases in Jdbc project and MongoDB project and micro client project but you can expand it to work with other databases.

Requirements

The library works with Java 8+, ladder framework 1.0.1+ and repository implementation such as mongo client project , jdbc client project or micro client or your implementation

Structure

CRUD Project this project create default crud and used mapstruct library for casting object to another object

maven

<properties>
        <java.version>1.8</java.version>
        <org.mapstruct.version>1.3.0.Final</org.mapstruct.version>
        <org.projectlombok.version>1.18.8</org.projectlombok.version>
</properties>

<dependency>
            <groupId>app.ladderproject</groupId>
            <artifactId>crud</artifactId>
            <version>0.0.1-Released</version>
        </dependency>
        <dependency>
            <groupId>app.ladderproject</groupId>
            <artifactId>Jdbc-client</artifactId>
            <version>0.0.1-Released</version>
</dependency>
            
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.0.RELEASE</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version> <!-- or newer version -->
                <configuration>
                    <source>11</source> <!-- depending on your project -->
                    <target>11</target> <!-- depending on your project -->
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${org.projectlombok.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

BaseController

a simple abstract implementation of REST controller that support CRUD operations, and which you can use in your application

method

@PostMapping

public ResponseEntity<BaseDTO> create(@Valid @RequestBody S s)

s is the object of request model

return ResponseEntity<BaseDTO < R>> that R the view model you must add to controller

this method save data to DataBase that you must implement in repository layer

@PutMapping

public ResponseEntity<BaseDTO> update(@Valid @RequestBody S s, @Valid @RequestParam I id)

s is the object of request model

id is your IncrementalId of DataBase

ResponseEntity<BaseDTO < R>> that R the view model you must add to controller

apiNote this method save data to DataBase that you must implement in repository layer

@DeleteMapping

public ResponseEntity<BaseDTO> deleteById(@Valid @RequestParam I id)

id is your IncrementalId of DataBase

ResponseEntity<BaseDTO < Boolean>> is the true or false result in BaseDTO pattern

used for delete an entity from database

@GetMapping

public ResponseEntity<BaseDTO> findByID(@Valid @RequestParam I id)

id is your IncrementalId of DataBase

ResponseEntity<BaseDTO < R>> that R the view model you must add to controller

this method used for get object from Identify number of data base

@GetMapping(value = "/all")

public ResponseEntity<BaseDTO<List>> findAll()

@return ResponseEntity<BaseDTO < List < R>>> that R the view model you must add to controller

used for getAll data from database , you must know that the cost of this method is high and you can used findListByPagination Or findByPagination for fetch data

@GetMapping(value = "/all/pagination")

public ResponseEntity<BaseDTO<PageDTO<List>>> findListByPagination(@Valid @RequestParam Integer page, @RequestParam Integer pageSize)

page is the number of page you need to fetch

pageSize is the sizable page of data

ResponseEntity<BaseDTO < PageDTO < List < R>>>> this methode return PageDTO that is all data in it

@PostMapping(value = "/all/pagination")

public ResponseEntity<BaseDTO<PageDTO<List>>> findListByPagination(@Valid @RequestParam Integer page, @RequestParam Integer pageSize, @RequestBody String orders)

page is the number of page you need to fetch

pageSize is the sizable page of data

orders is the list of fields and your direction such as Asc and Desc for Sorting

return ResponseEntity<BaseDTO < PageDTO < List < R>>>> this methode return PageDTO that is all data in it

@GetMapping(value = "/all/pagination/detail")

public ResponseEntity<BaseDTO<PageDTO<List>>> findByPagination(@Valid @RequestParam Integer page, @RequestParam Integer pageSize)

page is the number of page you need to fetch

pageSize is the sizable page of data

return ResponseEntity<BaseDTO < PageDTO < List < R>>>> this methode return PageDTO that is all data in it

@PostMapping(value = "/all/pagination/detail")

public ResponseEntity<BaseDTO<PageDTO<List>>> findByPagination(@Valid @RequestParam Integer page, @RequestParam Integer pageSize, @RequestBody String orders)

page is the number of page you need to fetch

pageSize is the sizable page of data

orders is the list of fields and your direction such as Asc and Desc for Sorting

return ResponseEntity<BaseDTO < PageDTO < List < R>>>> this methode return PageDTO that is all data in it

@GetMapping(value = "/exists/ById")

public ResponseEntity<BaseDTO> existsById(@Valid @RequestParam I id)

id is your IncrementalId of DataBase

return the Boolean of result

@GetMapping(value = "/count")

public ResponseEntity<BaseDTO> count()

return the number of objects

apiNote this controller used for the count of data

Example

@RestController
@RequestMapping(value = "/rest/integration/v1/affair")
@Api(value = "Integration", protocols = "HTTP")
@SwaggerDefinition(tags = {@Tag(name = "Integration", description = "سرویس مدیریت اطلاعات")})
@RequiredArgsConstructor
public class AffairController extends BaseController<Affair, AffairReqVM, AffairResVM, Long> {
}

GeneralService

a simple abstract implementation of Service that support CRUD operations, and which you can use in your application

method

BaseDTO< R > save(@NotNull S s)

s is the Request view Model that you can save it in Data Base

return the Response View Model that you must set in base class

this method used for save in Data Base

BaseDTO< List< R > > saveAll(List< S > sList)

sList is the list of Request view Model that you can save it in Data Base

return the list of Response of view model

this method used for save batch in Data base

BaseDTO< R > update(@NotNull S s, @NotNull I id)

s is the Request view Model that you can save it in Data Base

id is the incrementalId of dataBase

return the result of view Model

this method used for update the Data

BaseDTO< Boolean > deleteById(@NotNull I id)

id is the incrementalId of dataBase

return the result such as true or false

this methode used for delete Data with the incrementalId

BaseDTO findById(@NotNull I id)

id is the incrementalId of dataBase

BaseDTO< R > is the result of find that you can give it the Response View Model

this method used for fetch data from dataBase with the incrementalId of object

BaseDTO< Boolean > existsById(@NotNull I id)

id is the incrementalId of dataBase

return the result such as true or false

used for to know that this incrementalId is in Data Base Or Not

BaseDTO< List< R > > getAll()

return BaseDTO< List < R > > the list of response view model Data

this method used for get all data from dataBse that you must know that the cost of this method is very expensive you can choose the method findListByPagination(...) and findByPagination(..) for fetch by pagination

BaseDTO< PageDTO < List < R >>> findListByPagination(int page, int pageSize)

page is the number of page you need to fetch

pageSize is the sizable page of data

return BaseDTO<PageDTO < List < R>>> this methode return PageDTO that is all data in it

BaseDTO<PageDTO<List>> findListByPagination(int page, int pageSize, String orders)

page is the number of page you need to fetch

pageSize is the sizable page of data

orders is the list of fields and your direction such as Asc and Desc

return BaseDTO<PageDTO < List < R>>> this methode return PageDTO that is all data in it

BaseDTO<PageDTO<List>> findByPagination(int page, int pageSize)

page is the number of page you need to fetch

pageSize is the sizable page of data

return BaseDTO<PageDTO < List < R>>> this methode return PageDTO that is all data in it

this method call count method and return the count of data

BaseDTO<PageDTO<List>> findByPagination(int page, int pageSize, String orders)

page is the number of page you need to fetch

pageSize is the sizable page of data

orders are the list of fields and your direction such as Asc and Desc

return BaseDTO<PageDTO < List < R>>> this methode return PageDTO that is all data in it

this method call count method and return the count of data

BaseDTO count()

return the number of data

this method used for count of data objects

Example

@Service
public class AffairService extends GeneralService<Affair, AffairReqVM, AffairResVM, Long> {
}

GeneralMapper

Mappers are used to convert DTOs to entities and vice versa. This is done automatically thanks to MapStruct framework

this class used for map compile time and used is the protocol that implement by mapStruct library version 1.3.1.Final you must create interface and extended it you must create interface and extended it

methode

public abstract T requestToBaseObject(S source);

source is the source of request view model

return the T is the Object

method used for cast request to BaseObject

public abstract S toRequestModel(T target)

target the BaseObject of object

return the Request view model

method used for cast BaseObject to Response View Model

public abstract List< T > requestToBaseObject(List< S > sourceList)

sourceList the list of Request view Model

return the list of BaseObject

method used for cast List Response View Model to List BaseObject

public abstract List< S > toRequestModel(List< R > targetList)

targetList the list of Response View Model

return the List Of Request View Model

method used for cast List Response View Model to List Request View Model

public abstract List< S > toRequestModels(List< T > targetList)

targetList the List Of BaseObject

return the List of Response View Model

method used for cast Iterable Of BaseObject to List Of Request View Model

public abstract T responseToBaseObject(R source)

source the Response View Model

return the BaseObject

method used for cast Response To BaseObject

public abstract R toResponseModel(T target)

target the BaseObject Object

return the Response View Model

method used for cast BaseObject to Response

public abstract List< T > responseToBaseObject(List< R > sourceList)

sourceList the List Of Response View Model

return the list Of BaseObject

used for cast Iterable of Response to List Of BaseObject

public abstract List< R > toResponseModel(List< T > targetList)

targetList the List of BaseObject

return the List Of Response View Model

used for cast to Iterable of BaseObject to List Of Response

Example

@Mapper(componentModel = "spring")
@Component
public abstract class AffairMapper extends GeneralMapper<Affair, AffairReqVM, AffairResVM, Long> {
}

GeneralRepository

this interface used for connect crud to database and implement in jdbc client Project and Mongo client project and micro client Project you must add the project to your pom file and use Repository layer

method

T save(T t);

t the Entity View Model that you must Add To Data Base

return the Optional Of Entity that save it in dataBase

this method used for save Data in Data base

T update(I id, T t)

id the incrementalId of dataBase Object

t the Entity View Model that you must Add To Data Base

return the Optional Of Entity that save it in dataBase

this method used for Update data Base Object

List saveAll(List tList)

tList the list of Entity that you must save it in Data base

return the Optional List Of Entity and their Ids

used for save the batch of Data in Data base

Optional< T > findById(I id)

id the incrementalId of dataBase Object

the Optional Of Entity that save it in database

used for fetch Data By IncrementalId

List findAll()

return the List Of Entities

this methode uses for Fetch All Data

List findAll(int page, int pageSize)

page the page number that you must fetch it

pageSize the page Size of that you need to split Data

return the Optional List Of Entity from Response Of Data Base

List findAll(int page, int pageSize, String orders)

return the Number Of data

method used for get the count Of Data

void deleteById(I id)

id is the incrementalId of Object that you need too remove it from Data Base

throws RuntimeException has throw if Delete Method Not Acceptable

Used for delete Object From Incremental Id

Example

used for jdbc project for implement general Repository layer

@Service
public class AffairJdbcService extends JdbcServiceImpl<Affair, Long> {
}

@Repository
public interface AffairRepository extends JdbcRepository<Affair, Long> {
}
Clone this wiki locally