Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
QZero_0
java-design-patterns
提交
d1bc28bb
J
java-design-patterns
项目概览
QZero_0
/
java-design-patterns
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
java-design-patterns
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d1bc28bb
编写于
8月 09, 2015
作者:
I
Ilkka Seppala
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
#84 Utilize Repository layer from another example
上级
1bcd822c
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
179 addition
and
1 deletion
+179
-1
layers/pom.xml
layers/pom.xml
+16
-0
layers/src/main/java/com/iluwatar/layers/App.java
layers/src/main/java/com/iluwatar/layers/App.java
+41
-1
layers/src/main/java/com/iluwatar/layers/Person.java
layers/src/main/java/com/iluwatar/layers/Person.java
+58
-0
layers/src/main/java/com/iluwatar/layers/PersonDao.java
layers/src/main/java/com/iluwatar/layers/PersonDao.java
+17
-0
layers/src/main/resources/META-INF/persistence.xml
layers/src/main/resources/META-INF/persistence.xml
+8
-0
layers/src/main/resources/applicationContext.xml
layers/src/main/resources/applicationContext.xml
+39
-0
未找到文件。
layers/pom.xml
浏览文件 @
d1bc28bb
...
@@ -11,6 +11,22 @@
...
@@ -11,6 +11,22 @@
<groupId>
com.iluwatar.layers
</groupId>
<groupId>
com.iluwatar.layers
</groupId>
<artifactId>
layers
</artifactId>
<artifactId>
layers
</artifactId>
<dependencies>
<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>
<dependency>
<groupId>
junit
</groupId>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<artifactId>
junit
</artifactId>
...
...
layers/src/main/java/com/iluwatar/layers/App.java
浏览文件 @
d1bc28bb
package
com.iluwatar.layers
;
package
com.iluwatar.layers
;
import
java.util.List
;
import
org.springframework.context.support.ClassPathXmlApplicationContext
;
public
class
App
{
public
class
App
{
public
static
void
main
(
String
[]
args
)
{
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
();
}
}
}
}
layers/src/main/java/com/iluwatar/layers/Person.java
0 → 100644
浏览文件 @
d1bc28bb
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
+
"]"
;
}
}
layers/src/main/java/com/iluwatar/layers/PersonDao.java
0 → 100644
浏览文件 @
d1bc28bb
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
);
}
layers/src/main/resources/META-INF/persistence.xml
0 → 100644
浏览文件 @
d1bc28bb
<?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>
layers/src/main/resources/applicationContext.xml
0 → 100644
浏览文件 @
d1bc28bb
<?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>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录