added persistence

This commit is contained in:
jesus-dayo 2016-08-12 18:44:08 +08:00
parent 74509557a6
commit e479614eaa
23 changed files with 472 additions and 61 deletions

49
mapstruct/pom.xml Normal file
View File

@ -0,0 +1,49 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<artifactId>mapstruct</artifactId>
<name>mapstruct</name>
<groupId>com.baeldung</groupId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<org.mapstruct.version>1.0.0.Final</org.mapstruct.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>mapstruct</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,42 @@
package org.baeldung.dto;
public class EmployeeDTO {
private int employeeId;
private String employeeName;
private int divisionId;
private String divisionName;
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public int getDivisionId() {
return divisionId;
}
public void setDivisionId(int divisionId) {
this.divisionId = divisionId;
}
public String getDivisionName() {
return divisionName;
}
public void setDivisionName(String divisionName) {
this.divisionName = divisionName;
}
}

View File

@ -0,0 +1,24 @@
package org.baeldung.dto;
public class SimpleSource {
private String name;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

View File

@ -0,0 +1,33 @@
package org.baeldung.entity;
public class Division {
public Division() {
}
public Division(int id, String name) {
super();
this.id = id;
this.name = name;
}
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,33 @@
package org.baeldung.entity;
public class Employee {
private int id;
private String name;
private Division division;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Division getDivision() {
return division;
}
public void setDivision(Division division) {
this.division = division;
}
}

View File

@ -0,0 +1,24 @@
package org.baeldung.entity;
public class SimpleDestination {
private String name;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

View File

@ -0,0 +1,27 @@
package org.baeldung.mapper;
import org.baeldung.dto.EmployeeDTO;
import org.baeldung.entity.Employee;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
@Mapper
public interface EmployeeMapper {
@Mappings({
@Mapping(target="divisionId",source="entity.division.id"),
@Mapping(target="divisionName",source="entity.division.name"),
@Mapping(target="employeeId",source="entity.id"),
@Mapping(target="employeeName",source="entity.name")
})
EmployeeDTO employeeToEmployeeDTO(Employee entity);
@Mappings({
@Mapping(target="id",source="dto.employeeId"),
@Mapping(target="name",source="dto.employeeName"),
@Mapping(target="division",expression="java(new org.baeldung.entity.Division(dto.getDivisionId(),dto.getDivisionName()))")
})
Employee employeeDTOtoEmployee(EmployeeDTO dto);
}

View File

@ -0,0 +1,13 @@
package org.baeldung.mapper;
import org.baeldung.dto.SimpleSource;
import org.baeldung.entity.SimpleDestination;
import org.mapstruct.Mapper;
@Mapper
public interface SimpleSourceDestinationMapper {
SimpleDestination sourceToDestination(SimpleSource source);
SimpleSource destinationToSource(SimpleDestination destination);
}

View File

@ -0,0 +1,13 @@
package org.baeldung.mapper;
import org.baeldung.dto.SimpleSource;
import org.baeldung.entity.SimpleDestination;
import org.mapstruct.Mapper;
@Mapper(componentModel="spring")
public interface SimpleSourceDestinationSpringMapper {
SimpleDestination sourceToDestination(SimpleSource source);
SimpleSource destinationToSource(SimpleDestination destination);
}

View File

@ -0,0 +1,48 @@
package org.baeldung.mapper;
import static org.junit.Assert.*;
import org.baeldung.dto.EmployeeDTO;
import org.baeldung.entity.Division;
import org.baeldung.entity.Employee;
import org.junit.Test;
import org.mapstruct.factory.Mappers;
public class EmployeeMapperTest {
@Test
public void givenEmployeeDTOtoEmployee_whenMaps_thenCorrect(){
EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class);
EmployeeDTO dto = new EmployeeDTO();
dto.setDivisionId(1);
dto.setDivisionName("IT Division");
dto.setEmployeeId(1);
dto.setEmployeeName("John");
Employee entity = mapper.employeeDTOtoEmployee(dto);
assertEquals(dto.getDivisionId(), entity.getDivision().getId());
assertEquals(dto.getDivisionName(), entity.getDivision().getName());
assertEquals(dto.getEmployeeId(),entity.getId());
assertEquals(dto.getEmployeeName(),entity.getName());
}
@Test
public void givenEmployeetoEmployeeDTO_whenMaps_thenCorrect(){
EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class);
Employee entity = new Employee();
entity.setDivision(new Division(1,"IT Division"));
entity.setId(1);
entity.setName("John");
EmployeeDTO dto = mapper.employeeToEmployeeDTO(entity);
assertEquals(dto.getDivisionId(), entity.getDivision().getId());
assertEquals(dto.getDivisionName(), entity.getDivision().getName());
assertEquals(dto.getEmployeeId(),entity.getId());
assertEquals(dto.getEmployeeName(),entity.getName());
}
}

View File

@ -0,0 +1,44 @@
package org.baeldung.mapper;
import static org.junit.Assert.*;
import org.baeldung.dto.SimpleSource;
import org.baeldung.entity.SimpleDestination;
import org.junit.Test;
import org.mapstruct.factory.Mappers;
public class SimpleSourceDestinationMapperTest {
@Test
public void givenSimpleSourceToSimpleDestination_whenMaps_thenCorrect() {
SimpleSourceDestinationMapper simpleSourceDestinationMapper = Mappers
.getMapper(SimpleSourceDestinationMapper.class);
SimpleSource simpleSource = new SimpleSource();
simpleSource.setName("SourceName");
simpleSource.setDescription("SourceDescription");
SimpleDestination destination =
simpleSourceDestinationMapper.sourceToDestination(simpleSource);
assertEquals(simpleSource.getName(), destination.getName());
assertEquals(simpleSource.getDescription(), destination.getDescription());
}
@Test
public void givenSimpleDestinationToSourceDestination_whenMaps_thenCorrect() {
SimpleSourceDestinationMapper simpleSourceDestinationMapper = Mappers
.getMapper(SimpleSourceDestinationMapper.class);
SimpleDestination destination = new SimpleDestination();
destination.setName("DestinationName");
destination.setDescription("DestinationDescription");
SimpleSource source =
simpleSourceDestinationMapper.destinationToSource(destination);
assertEquals(destination.getName(), source.getName());
assertEquals(destination.getDescription(), source.getDescription());
}
}

View File

@ -30,6 +30,15 @@
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>

View File

@ -3,34 +3,34 @@ package org.baeldung.mock;
import java.util.ArrayList;
import java.util.List;
import org.baeldung.web.vo.Student;
import org.baeldung.web.entity.Student;
public class MockStudentData {
private static List<Student> studentList = new ArrayList<>();
static {
studentList.add(new Student("1", "Bryan", "Male", 20));
studentList.add(new Student("2", "Ben", "Male", 22));
studentList.add(new Student("3", "Lisa", "Female", 24));
studentList.add(new Student("4", "Sarah", "Female", 26));
studentList.add(new Student("5", "Jay", "Male", 20));
studentList.add(new Student("6", "John", "Male", 22));
studentList.add(new Student("7", "Jordan", "Male", 24));
studentList.add(new Student("8", "Rob", "Male", 26));
studentList.add(new Student("9", "Will", "Male", 20));
studentList.add(new Student("10", "Shawn", "Male", 22));
studentList.add(new Student("11", "Taylor", "Female", 24));
studentList.add(new Student("12", "Venus", "Female", 26));
studentList.add(new Student("13", "Vince", "Male", 20));
studentList.add(new Student("14", "Carol", "Female", 22));
studentList.add(new Student("15", "Joana", "Female", 24));
studentList.add(new Student("16", "Dion", "Male", 26));
studentList.add(new Student("17", "Evans", "Male", 20));
studentList.add(new Student("18", "Bart", "Male", 22));
studentList.add(new Student("19", "Jenny", "Female", 24));
studentList.add(new Student("20", "Kristine", "Female", 26));
}
// static {
// studentList.add(new Student("1", "Bryan", "Male", 20));
// studentList.add(new Student("2", "Ben", "Male", 22));
// studentList.add(new Student("3", "Lisa", "Female", 24));
// studentList.add(new Student("4", "Sarah", "Female", 26));
// studentList.add(new Student("5", "Jay", "Male", 20));
// studentList.add(new Student("6", "John", "Male", 22));
// studentList.add(new Student("7", "Jordan", "Male", 24));
// studentList.add(new Student("8", "Rob", "Male", 26));
// studentList.add(new Student("9", "Will", "Male", 20));
// studentList.add(new Student("10", "Shawn", "Male", 22));
// studentList.add(new Student("11", "Taylor", "Female", 24));
// studentList.add(new Student("12", "Venus", "Female", 26));
// studentList.add(new Student("13", "Vince", "Male", 20));
// studentList.add(new Student("14", "Carol", "Female", 22));
// studentList.add(new Student("15", "Joana", "Female", 24));
// studentList.add(new Student("16", "Dion", "Male", 26));
// studentList.add(new Student("17", "Evans", "Male", 20));
// studentList.add(new Student("18", "Bart", "Male", 22));
// studentList.add(new Student("19", "Jenny", "Female", 24));
// studentList.add(new Student("20", "Kristine", "Female", 26));
// }
public static List<Student> getMockDataStudents(){
return studentList;

View File

@ -0,0 +1,10 @@
package org.baeldung.web.dao;
import org.baeldung.web.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRepository extends JpaRepository<Student, Long> {
}

View File

@ -1,7 +1,12 @@
package org.baeldung.web.vo;
package org.baeldung.web.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Student implements Serializable {
/**
@ -12,25 +17,29 @@ public class Student implements Serializable {
public Student() {
}
public Student(String studentId, String name, String gender, Integer age) {
public Student(long id, String name, String gender, Integer age) {
super();
this.studentId = studentId;
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
private String studentId;
@Id
private long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String gender;
@Column(nullable = false)
private Integer age;
public String getStudentId() {
return studentId;
public long getId() {
return id;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
public void setId(long id) {
this.id = id;
}
public String getName() {

View File

@ -4,13 +4,13 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan("org.baeldung")
@Import(PersistenceConfig.class)
public class Application extends WebMvcConfigurerAdapter {

View File

@ -0,0 +1,37 @@
package org.baeldung.web.main;
import javax.sql.DataSource;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
@EnableJpaRepositories("org.baeldung")
@ComponentScan(basePackages = { "org.baeldung.web" })
@EntityScan("org.baeldung.web.entity")
@Configuration
public class PersistenceConfig {
@Bean
public JdbcTemplate getJdbcTemplate() {
return new JdbcTemplate(dataSource());
}
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder
.setType(EmbeddedDatabaseType.HSQL)
.addScript("db/sql/data.sql")
.build();
return db;
}
}

View File

@ -1,17 +1,14 @@
package org.baeldung.web.rest;
import org.baeldung.web.entity.Student;
import org.baeldung.web.service.StudentService;
import org.baeldung.web.vo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import static org.springframework.http.MediaType.APPLICATION_JSON;
@RestController
public class StudentDirectoryRestController {

View File

@ -4,6 +4,6 @@ import org.springframework.data.domain.Page;
public interface IOperations<T> {
Page<T> findPaginated(int page, int size);
public Page<T> findPaginated(final int page, final int size);
}

View File

@ -1,6 +1,6 @@
package org.baeldung.web.service;
import org.baeldung.web.vo.Student;
import org.baeldung.web.entity.Student;
public interface StudentService extends IOperations<Student>{

View File

@ -1,36 +1,24 @@
package org.baeldung.web.service;
import java.util.List;
import javax.transaction.Transactional;
import org.baeldung.mock.MockStudentData;
import org.baeldung.web.exception.MyResourceNotFoundException;
import org.baeldung.web.vo.Student;
import org.baeldung.web.dao.StudentRepository;
import org.baeldung.web.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
@Service
@Transactional
public class StudentServiceImpl implements StudentService {
private List<Student> mockDataStudent = MockStudentData.getMockDataStudents();
@Autowired
private StudentRepository dao;
@Override
public Page<Student> findPaginated(int page, int size){
Page<Student> studentPage = getPage(page, size);
return studentPage;
}
private Page<Student> getPage(int page, int size) {
page = page != 0?page - 1:page;
int from = Math.max(0, page * size);
int to = Math.min(mockDataStudent.size(), (page + 1) * size);
if(from > to){
throw new MyResourceNotFoundException("page number is higher than total pages.");
}
return new PageImpl<Student>(mockDataStudent.subList(from, to),
new PageRequest(page,size),
mockDataStudent.size());
public Page<Student> findPaginated(int page, int size) {
return dao.findAll(new PageRequest(page,size));
}
}

View File

@ -1 +1,3 @@
server.contextPath=/
server.contextPath=/
spring.h2.console.enabled=true
logging.level.org.hibernate.SQL=debug

View File

@ -0,0 +1,9 @@
CREATE TABLE student (
id INTEGER PRIMARY KEY,
name VARCHAR(30),
gender VARCHAR(10),
age INTEGER
);
INSERT INTO student (id,name,gender,age)
VALUES (1,'Bryan', 'Male',20);