#84 Utilize Repository layer from another example
This commit is contained in:
		| @@ -11,6 +11,22 @@ | ||||
| 	<groupId>com.iluwatar.layers</groupId> | ||||
| 	<artifactId>layers</artifactId> | ||||
| 	<dependencies> | ||||
| 		<dependency> | ||||
| 			<groupId>org.springframework.data</groupId> | ||||
| 			<artifactId>spring-data-jpa</artifactId> | ||||
| 		</dependency> | ||||
| 		<dependency> | ||||
| 			<groupId>org.hibernate</groupId> | ||||
| 			<artifactId>hibernate-entitymanager</artifactId> | ||||
| 		</dependency> | ||||
| 		<dependency> | ||||
| 	  		<groupId>commons-dbcp</groupId> | ||||
| 	  		<artifactId>commons-dbcp</artifactId> | ||||
| 	  	</dependency>		 | ||||
| 		<dependency> | ||||
| 			<groupId>com.h2database</groupId> | ||||
| 			<artifactId>h2</artifactId> | ||||
| 		</dependency> | ||||
| 		<dependency> | ||||
| 			<groupId>junit</groupId> | ||||
| 			<artifactId>junit</artifactId> | ||||
|   | ||||
| @@ -1,8 +1,48 @@ | ||||
| package com.iluwatar.layers; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| import org.springframework.context.support.ClassPathXmlApplicationContext; | ||||
|  | ||||
| public class App { | ||||
| 	 | ||||
| 	public static void main(String[] args) { | ||||
| 		System.out.println("Hello World!"); | ||||
| 		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(); | ||||
| 	} | ||||
| } | ||||
|   | ||||
							
								
								
									
										58
									
								
								layers/src/main/java/com/iluwatar/layers/Person.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								layers/src/main/java/com/iluwatar/layers/Person.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,58 @@ | ||||
| package com.iluwatar.layers; | ||||
|  | ||||
| import javax.persistence.Entity; | ||||
| import javax.persistence.GeneratedValue; | ||||
| import javax.persistence.Id; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * Person entity | ||||
|  * | ||||
|  */ | ||||
| @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
									
								
								layers/src/main/java/com/iluwatar/layers/PersonDao.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								layers/src/main/java/com/iluwatar/layers/PersonDao.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | ||||
| package com.iluwatar.layers; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| import org.springframework.data.repository.CrudRepository; | ||||
| import org.springframework.stereotype.Repository; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * Person repository | ||||
|  * | ||||
|  */ | ||||
| @Repository | ||||
| public interface PersonDao extends CrudRepository<Person, Long> { | ||||
| 	 | ||||
| 	public List<Person> findBySurname(String surname); | ||||
| } | ||||
							
								
								
									
										8
									
								
								layers/src/main/resources/META-INF/persistence.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								layers/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> | ||||
							
								
								
									
										39
									
								
								layers/src/main/resources/applicationContext.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								layers/src/main/resources/applicationContext.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,39 @@ | ||||
| <?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"> | ||||
|  | ||||
| 	<jpa:repositories base-package="com.iluwatar" /> | ||||
|  | ||||
| 	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> | ||||
| 		<property name="entityManagerFactory" ref="entityManagerFactory" /> | ||||
| 	</bean> | ||||
|  | ||||
| 	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" | ||||
| 		destroy-method="close"> | ||||
| 		<property name="driverClassName" value="org.h2.Driver" /> | ||||
| 		<property name="url" value="jdbc:h2:~/databases/person" /> | ||||
| 		<property name="username" value="sa" /> | ||||
| 		<property name="password" value="sa" /> | ||||
| 	</bean> | ||||
|  | ||||
| 	<bean id="entityManagerFactory" | ||||
| 		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> | ||||
| 		<property name="dataSource" ref="dataSource" /> | ||||
| 		<property name="packagesToScan" value="com.iluwatar" /> | ||||
| 		<property name="persistenceProvider"> | ||||
| 			<bean class="org.hibernate.ejb.HibernatePersistence" /> | ||||
| 		</property> | ||||
| 		<property name="jpaProperties"> | ||||
| 			<map> | ||||
| 				<entry key="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" /> | ||||
| 				<entry key="hibernate.hbm2ddl.auto" value="create-drop" /> | ||||
| 			</map> | ||||
| 		</property> | ||||
| 	</bean> | ||||
| </beans> | ||||
		Reference in New Issue
	
	Block a user