Posted by My Blog on Saturday, November 19, 2016
package com.unitomo.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Mahasiswa {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String nama;
private String nim;
public Mahasiswa() {
}
public Mahasiswa(String nama){
this.nama=nama;
}
public Mahasiswa( String nama, String nim) {
this.nama = nama;
this.nim = nim;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
public String getNim() {
return nim;
}
public void setNim(String nim) {
this.nim = nim;
}
}
package com.unitomo.repository;
/**
*
* @author Della Emilia
*/
import com.unitomo.entities.Mahasiswa;
import java.util.List;
import java.io.Serializable;
import org.springframework.data.jpa.repository.JpaRepository;
/**
*
* @author Della Emilia
*/
public interface MahasiswaRepository extends JpaRepository<Mahasiswa, Long>{
List<Mahasiswa>findByNama (String nama);
}
package com.unitomo;
import com.unitomo.entities.Mahasiswa;
import com.unitomo.repository.MahasiswaRepository;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
/**
*
* @author Della Emilia
*/
@Component
public class IsiDataMahasiswa implements CommandLineRunner {
private final MahasiswaRepository mhsRepository;
@Autowired
public IsiDataMahasiswa(MahasiswaRepository mhsRepository) {
this.mhsRepository = mhsRepository;
}
@Override
public void run(String... strings) throws Exception {
List<Mahasiswa> mahasiswas = new ArrayList<>();
mahasiswas.add(new Mahasiswa("2013420001"));
mahasiswas.add(new Mahasiswa("2013420002", "Fulan2"));
mahasiswas.add(new Mahasiswa("2013420003", "Fulan3"));
mahasiswas.add(new Mahasiswa("2013420004", "Fulan4"));
mhsRepository.save(mahasiswas);
}
}
dan kemudian pada application.properties
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/fw
spring.datasource.username=postgres
spring.datasource.password=postgres