Skip to content

Instantly share code, notes, and snippets.

@mloza
Last active Dec 1, 2019
Embed
What would you like to do?
INSERT INTO task(name, description, budget, done) VALUES('Task 1', 'Description of task 1', 100.00, 1);
INSERT INTO task(name, description, budget, done) VALUES('Task 2', 'Description of task 2 Do', 100.00, 1);
INSERT INTO task(name, description, budget, done) VALUES('Task 3', 'Description of task 3 Do', 50.00, 0);
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
Hibernate: drop table task if exists
Hibernate: create table task (id bigint generated by default as identity, budget double, description clob, done boolean, name varchar(255), primary key (id))
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
@EnableAutoConfiguration
@ComponentScan
@EnableJpaRepositories(basePackageClasses = TaskRepository.class)
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
spring.datasource.url=jdbc:mysql://localhost/nazwa-bazy
spring.datasource.username=login
spring.datasource.password=haslo
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql = true
TaskEntity{id=1, name='Task 1', description='Test task', budget=123.55, done=true}
TaskEntity{id=2, name='Task 1', description='Test task', budget=123.55, done=true}
TaskEntity{id=3, name='Task 1', description='Test task', budget=123.55, done=true}
TaskEntity{id=4, name='Task 1', description='Test task', budget=123.55, done=true}
TaskEntity{id=5, name='Task 1', description='Test task', budget=123.55, done=true}
TaskEntity{id=6, name='Task 1', description='Test task', budget=123.55, done=true}
TaskEntity{id=1, name='Task 1', description='Description of task 1', budget=100.0, done=true}
TaskEntity{id=2, name='Task 2', description='Description of task 2 Do', budget=100.0, done=true}
TaskEntity{id=3, name='Task 3', description='Description of task 3 Do', budget=50.0, done=false}
TaskEntity{id=4, name='Task 1', description='Test task', budget=123.55, done=true}
Tasks with done set to true:
TaskEntity{id=1, name='Task 1', description='Description of task 1', budget=100.0, done=true}
TaskEntity{id=2, name='Task 2', description='Description of task 2 Do', budget=100.0, done=true}
Tasks with done set to false:
TaskEntity{id=3, name='Task 3', description='Description of task 3 Do', budget=50.0, done=false}
Tasks with "Do" in description:
TaskEntity{id=2, name='Task 2', description='Description of task 2 Do', budget=100.0, done=true}
TaskEntity{id=3, name='Task 3', description='Description of task 3 Do', budget=50.0, done=false}
@Entity
public class Task {
@GeneratedValue
@Id
private Long id;
@Column
private String name;
@Column
@Lob
private String description;
@Column
private Double budget;
@Column
private Boolean done;
//Gettery, Settery itp.
}
public interface TaskRepository extends CrudRepository<Task, Long> { }
public interface TaskRepository extends CrudRepository<task, long> {
public List findByDone(Boolean done);
@Query("select t from Task t where t.description like %?1%")
public List getByDescriptionLike(String search);
}
@Autowired
public TaskRepository taskRepository;
@RequestMapping("/db")
@ResponseBody
public String testMethod() {
StringBuilder response = new StringBuilder();
Task task = new Task()
.withName("Task 1")
.withDescription("Test task")
.withBudget(123.55)
.withDone(true);
taskRepository.save(task);
for(Task i: taskRepository.findAll()) {
response.append(i).append("<br>");
}
return response.toString();
}
@RequestMapping("/db2")
@ResponseBody
public String testMethod2() {
StringBuilder response = new StringBuilder();
response.append("Tasks with done set to true:\n");
for(Task i: taskRepository.findByDone(true)) {
response.append(i).append("\n");
}
response.append("Tasks with done set to false:\n");
for(Task i: taskRepository.findByDone(false)) {
response.append(i).append("\n");
}
response.append("Tasks with \"Do\" in description:\n");
for(Task i: taskRepository.getByDescriptionLike("Do")) {
response.append(i).append("\n");
}
return response.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment