Skip to content

Commit e8e9667

Browse files
committed
Example for Spring 4, Spring Boot and Spring Data
This is an example code for CRUD operation using Spring 4, Spring Boot and Spring data
1 parent cca0607 commit e8e9667

36 files changed

+757
-0
lines changed

.classpath

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
</attributes>
14+
</classpathentry>
15+
<classpathentry kind="src" path="src/main/resources"/>
16+
<classpathentry kind="src" path="src/test/resources"/>
17+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
18+
<attributes>
19+
<attribute name="maven.pomderived" value="true"/>
20+
</attributes>
21+
</classpathentry>
22+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
23+
<attributes>
24+
<attribute name="maven.pomderived" value="true"/>
25+
</attributes>
26+
</classpathentry>
27+
<classpathentry kind="output" path="target/classes"/>
28+
</classpath>

.project

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>example</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding//src/test/java=UTF-8
4+
encoding/<project>=UTF-8
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
12+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1

pom.xml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.lalit.productsale</groupId>
6+
<artifactId>example</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>example</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<mysql-connector-java.version>5.1.30</mysql-connector-java.version>
16+
<org.springframework.version>4.1.7.RELEASE</org.springframework.version>
17+
<hibernate.version>4.3.1.Final</hibernate.version>
18+
<junit.version>4.11</junit.version>
19+
</properties>
20+
21+
<parent>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-parent</artifactId>
24+
<version>1.2.5.RELEASE</version>
25+
</parent>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-jpa</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>mysql</groupId>
34+
<artifactId>mysql-connector-java</artifactId>
35+
<version>${mysql-connector-java.version}</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework</groupId>
39+
<artifactId>spring-test</artifactId>
40+
<version>${org.springframework.version}</version>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>junit</groupId>
45+
<artifactId>junit</artifactId>
46+
<version>${junit.version}</version>
47+
<scope>test</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.hsqldb</groupId>
51+
<artifactId>hsqldb</artifactId>
52+
<version>2.3.1</version>
53+
<scope>test</scope>
54+
</dependency>
55+
56+
</dependencies>
57+
58+
<build>
59+
<plugins>
60+
<plugin>
61+
<groupId>org.springframework.boot</groupId>
62+
<artifactId>spring-boot-maven-plugin</artifactId>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
</project>

sql/producttable.sql

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
create database if not exists productsaledb;
2+
use productsaledb;
3+
4+
-- create product table
5+
CREATE TABLE producttable (
6+
ProductID INT NOT NULL AUTO_INCREMENT,
7+
ProductName VARCHAR(45) NOT NULL,
8+
PRIMARY KEY (ProductID));
9+
10+
--create salesentry
11+
CREATE TABLE salesentry (
12+
SalesID INT NOT NULL AUTO_INCREMENT,
13+
DateOfSale DATETIME NULL,
14+
ProductID INT NULL,
15+
SalesAmount DECIMAL(12,2) NULL,
16+
SalesEntrycol VARCHAR(45) NULL,
17+
PRIMARY KEY (SalesID),
18+
INDEX FK_ProductID_idx (ProductID ASC),
19+
CONSTRAINT FK_ProductID
20+
FOREIGN KEY (ProductID)
21+
REFERENCES producttable (ProductID));
22+
23+
--populate producttable
24+
insert into producttable(ProductName)values('Product1');
25+
insert into producttable(ProductName)values('Product2');
26+
insert into producttable(ProductName)values('Product3');
27+
insert into producttable(ProductName)values('Product4');
28+
insert into producttable(ProductName)values('Product5');
29+
insert into producttable(ProductName)values('Product6');
30+
insert into producttable(ProductName)values('Product7');
31+
insert into producttable(ProductName)values('Product8');
32+
insert into producttable(ProductName)values('Product9');
33+
insert into producttable(ProductName)values('Product10');
34+
35+
--populate salesentry
36+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-31',1,10);
37+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-31',1,10);
38+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-31',1,20);
39+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-11-29',1,30);
40+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-11-30',2,40);
41+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-28',2,50);
42+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-12',3,60);
43+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-11',3,70);
44+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-09',4,80);
45+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-08',5,90);
46+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-04',6,100);
47+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-07',6,10);
48+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-06',7,20);
49+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-16',7,30);
50+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-17',8,40);
51+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-18',8,50);
52+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-22',8,60);
53+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-23',9,70);
54+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-24',9,80);
55+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-29',9,90);
56+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-11-14',10,100);
57+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-10-31',10,10);
58+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-01',10,20);
59+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-02',1,30);
60+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-10',2,40);
61+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-12',4,50);
62+
insert into salesentry(DateOfSale,ProductID,SalesAmount)values('2014-12-11',5,60);
63+
64+
commit;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.lalit.productsale;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.CommandLineRunner;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
10+
import com.lalit.productsale.output.ReportBuilder;
11+
import com.lalit.productsale.service.SaleRepository;
12+
13+
@SpringBootApplication
14+
public class Application implements CommandLineRunner{
15+
16+
@Autowired
17+
SaleRepository saleRepository;
18+
19+
@Autowired
20+
ReportBuilder reportBuilder;
21+
22+
public static void main(String[] args) {
23+
SpringApplication.run(Application.class);
24+
}
25+
26+
@Override
27+
public void run(String... args) throws Exception {
28+
List<Object[]> results = saleRepository.getSaleSummary();
29+
reportBuilder.printReport(results);
30+
}
31+
32+
33+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.lalit.productsale;
2+
3+
import javax.sql.DataSource;
4+
5+
import org.hibernate.dialect.MySQL5Dialect;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.ComponentScan;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
10+
import org.springframework.orm.jpa.JpaVendorAdapter;
11+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
12+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
13+
import org.springframework.transaction.annotation.EnableTransactionManagement;
14+
15+
import com.mysql.jdbc.Driver;
16+
17+
@Configuration
18+
@EnableTransactionManagement
19+
@ComponentScan("com.lalit.productsale")
20+
public class ApplicationConfig {
21+
22+
@Bean
23+
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
24+
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
25+
emf.setPackagesToScan("com.lalit.productsale");
26+
emf.setDataSource(dataSource());
27+
emf.setJpaVendorAdapter(jpaVendorAdapter());
28+
return emf;
29+
}
30+
31+
@Bean
32+
public DataSource dataSource() {
33+
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
34+
dataSource.setDriverClass(Driver.class);
35+
dataSource.setUrl("jdbc:mysql://localhost:3306/productsaledb");
36+
dataSource.setUsername("root");
37+
dataSource.setPassword("root");
38+
return dataSource;
39+
}
40+
41+
42+
private JpaVendorAdapter jpaVendorAdapter() {
43+
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
44+
jpaVendorAdapter.setShowSql(true);
45+
jpaVendorAdapter.setGenerateDdl(true);
46+
jpaVendorAdapter.setDatabasePlatform(MySQL5Dialect.class.getName());
47+
return jpaVendorAdapter;
48+
}
49+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.lalit.productsale.domain;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
import javax.persistence.Table;
9+
10+
import org.springframework.data.annotation.AccessType;
11+
import org.springframework.data.annotation.AccessType.Type;
12+
13+
@Entity
14+
@Table(name="producttable")
15+
@AccessType(Type.PROPERTY)
16+
public class Product {
17+
18+
private Integer productId;
19+
private String productName;
20+
21+
@Id
22+
@GeneratedValue(strategy=GenerationType.AUTO)
23+
@Column(name="ProductID")
24+
public Integer getProductId() {
25+
return productId;
26+
}
27+
public void setProductId(Integer productId) {
28+
this.productId = productId;
29+
}
30+
31+
@Column(name="ProductName")
32+
public String getProductName() {
33+
return productName;
34+
}
35+
public void setProductName(String productName) {
36+
this.productName = productName;
37+
}
38+
}

0 commit comments

Comments
 (0)