#55 Repository example skeleton
This commit is contained in:
parent
cd07c5cf60
commit
b22d2ddf89
@ -15,5 +15,20 @@
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-jpa</artifactId>
|
||||
<version>1.7.1.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-entitymanager</artifactId>
|
||||
<version><!-- 4.2.0.Final -->5.0.0.CR2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>9.4-1200-jdbc4</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -1,8 +1,52 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* Standalone application with Spring Data JPA, Hibernate and Maven
|
||||
*
|
||||
* @author DevCrumb.com
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main( String[] args ) {
|
||||
System.out.println( "Hello World!" );
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"applicationContext.xml");
|
||||
PersonDao dao = context.getBean(PersonDao.class);
|
||||
|
||||
Person peter = new Person("Peter", "Sagan");
|
||||
Person nasta = new Person("Nasta", "Kuzminova");
|
||||
|
||||
// Add new Person records
|
||||
dao.save(peter);
|
||||
dao.save(nasta);
|
||||
|
||||
// Count Person records
|
||||
System.out.println("Count Person records: " + dao.count());
|
||||
|
||||
// Print all records
|
||||
List<Person> persons = (List<Person>) dao.findAll();
|
||||
for (Person person : persons) {
|
||||
System.out.println(person);
|
||||
}
|
||||
|
||||
// Find Person by surname
|
||||
System.out.println("Find by surname 'Sagan': " + dao.findBySurname("Sagan"));
|
||||
|
||||
// Update Person
|
||||
nasta.setName("Barbora");
|
||||
nasta.setSurname("Spotakova");
|
||||
dao.save(nasta);
|
||||
|
||||
System.out.println("Find by id 2: " + dao.findOne(2L));
|
||||
|
||||
// Remove record from Person
|
||||
dao.delete(2L);
|
||||
|
||||
// And finally count records
|
||||
System.out.println("Count Person records: " + dao.count());
|
||||
|
||||
context.close();
|
||||
}
|
||||
}
|
||||
|
59
repository/src/main/java/com/iluwatar/Person.java
Normal file
59
repository/src/main/java/com/iluwatar/Person.java
Normal file
@ -0,0 +1,59 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
/**
|
||||
* Person entity
|
||||
*
|
||||
* @author DevCrumb.com
|
||||
*/
|
||||
@Entity
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
private String name;
|
||||
private String surname;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(String name, String surname) {
|
||||
this.name = name;
|
||||
this.surname = surname;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getSurname() {
|
||||
return surname;
|
||||
}
|
||||
|
||||
public void setSurname(String surname) {
|
||||
this.surname = surname;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person [id=" + id + ", name=" + name + ", surname=" + surname
|
||||
+ "]";
|
||||
}
|
||||
|
||||
}
|
17
repository/src/main/java/com/iluwatar/PersonDao.java
Normal file
17
repository/src/main/java/com/iluwatar/PersonDao.java
Normal file
@ -0,0 +1,17 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* Person dao interface
|
||||
*
|
||||
* @author DevCrumb.com
|
||||
*/
|
||||
@Repository
|
||||
public interface PersonDao extends CrudRepository<Person, Long> {
|
||||
|
||||
public List<Person> findBySurname(String surname);
|
||||
}
|
8
repository/src/main/resources/META-INF/persistence.xml
Normal file
8
repository/src/main/resources/META-INF/persistence.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<persistence version="1.0"
|
||||
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
|
||||
|
||||
<persistence-unit name="jpaData" />
|
||||
|
||||
</persistence>
|
51
repository/src/main/resources/applicationContext.xml
Normal file
51
repository/src/main/resources/applicationContext.xml
Normal file
@ -0,0 +1,51 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:security="http://www.springframework.org/schema/security"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
|
||||
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd">
|
||||
|
||||
<!-- Directory to scan for repository classes -->
|
||||
<jpa:repositories base-package="com.iluwatar" />
|
||||
|
||||
<bean id="dataSource"
|
||||
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
|
||||
<property name="driverClassName">
|
||||
<value>org.postgresql.Driver</value>
|
||||
</property>
|
||||
<property name="url">
|
||||
<value>jdbc:postgresql://localhost:5432/postgres</value>
|
||||
</property>
|
||||
<property name="username">
|
||||
<value>postgres</value>
|
||||
</property>
|
||||
<property name="password">
|
||||
<value>ile666</value>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="entityManagerFactory"
|
||||
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
<property name="persistenceUnitName" value="jpaData" />
|
||||
<property name="jpaVendorAdapter">
|
||||
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
|
||||
</property>
|
||||
<property name="jpaProperties">
|
||||
<props>
|
||||
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
|
||||
<prop key="hibernate.show_sql">false</prop>
|
||||
<prop key="hibernate.format_sql">false</prop>
|
||||
<prop key="hibernate.hbm2ddl.auto">create</prop>
|
||||
</props>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
|
||||
<property name="entityManagerFactory" ref="entityManagerFactory" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
Loading…
x
Reference in New Issue
Block a user