javaASS4[1]
javaASS4[1]
PRN :- 122B1B238
Assignment No. 4
Open the pom.xml file and write the following XML code:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.codejava</groupId>
<artifactId>ExpenseApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. Create MySQL Database
Create a database named as testdb with one table expense like this:
You can execute the following MySQL script to create this table:
So our sample Spring Boot - Spring Data JPA project will manage expense information in this table.
1 spring.jpa.hibernate.ddl-auto=none
2 spring.datasource.url=jdbc:mysql://localhost:3306/testdb
3 spring.datasource.username=root
4 spring.datasource.password=password
5 logging.level.root=WARN
As you can see, the first line tells Hibernate not to make changes to the database structure:
1 spring.jpa.hibernate.ddl-auto=none
The next 3 lines specify the database connection properties - so change the value according to your MySQL
server.
And in the last line, we set logging level to WARN to avoid verbose output.
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Expense {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String item;
private float amount;
protected Expense() {
}
@Override
public String toString() {
return id + ". " + item + " - " + amount + " USD";
}
}
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
In the body of the interface, we define two custom methods. The first one is:
This method signature follows convention for a finder method findByXXX() where XXX is the property
name in the model class - it finds the exact match of the method's argument. Spring Data JPA will generate
implementation code at runtime.
And we use a custom query in the second method:
This method will return expense items whose amount is greater than a specified value.
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ExpenseApp implements CommandLineRunner {
@Autowired
ExpenseRepository repository;
@Override
public void run(String... args) throws Exception {
repository.save(new Expense("breakfast", 5));
repository.save(new Expense("coffee", 2));
repository.save(new Expense("New SSD drive", 200));
repository.save(new Expense("Tution for baby", 350));
repository.save(new Expense("Some apples", 5));
}
}
You see, an instance of ExpenseRespository will be injected to an instance of the ExpenseApp class at
runtime:
1 @Autowired
2 ExpenseRepository repository;
Then in the run() method we can use the repository to list all expenses, get the breakfast item and find
items with an amount greater than 200 USD.
You can package the application to an executable JAR file, either by:
- In Eclipse IDE: right-click on the project and select Run As > Maven build... Then enter the goal name as
package and hit Run button.
- Use Maven from command line: change the current working directory to the project directory and type:
1 mvn package
You should see the build result that looks like this:
Now type the following command to run the Spring Boot application: