File tree 4 files changed +38
-3
lines changed
java/com/goeswhere/frameworkgame/blogapp
4 files changed +38
-3
lines changed Original file line number Diff line number Diff line change 27
27
<artifactId >spring-tx</artifactId >
28
28
<version >${spring.version} </version >
29
29
</dependency >
30
+ <dependency >
31
+ <groupId >org.springframework</groupId >
32
+ <artifactId >spring-jdbc</artifactId >
33
+ <version >${spring.version} </version >
34
+ </dependency >
30
35
</dependencies >
31
36
</dependencyManagement >
32
37
Original file line number Diff line number Diff line change 1
1
package com .goeswhere .frameworkgame .blogapp .controller ;
2
2
3
+ import org .springframework .beans .factory .annotation .Autowired ;
3
4
import org .springframework .stereotype .Controller ;
5
+ import org .springframework .transaction .annotation .Transactional ;
4
6
import org .springframework .ui .Model ;
5
7
import org .springframework .web .bind .annotation .RequestMapping ;
6
8
7
- import com .goeswhere .frameworkgame .blogapp .domain .Post ;
8
- import com .google .common .collect .Lists ;
9
+ import com .goeswhere .frameworkgame .blogapp .dao .PostDao ;
9
10
10
11
@ Controller
11
12
@ RequestMapping ("/" )
12
13
public class IndexController {
14
+ @ Autowired
15
+ PostDao posts ;
16
+
17
+ @ Transactional (readOnly = true )
13
18
@ RequestMapping
14
19
public String index (Model m ) {
15
- m .addAttribute ("posts" , Lists . newArrayList ( new Post ( "Ponies!" , "All about ponies" ) ));
20
+ m .addAttribute ("posts" , posts . all ( ));
16
21
return "index" ;
17
22
}
18
23
}
Original file line number Diff line number Diff line change 1
1
package com .goeswhere .frameworkgame .blogapp .dao ;
2
2
3
+ import java .util .List ;
4
+
5
+ import javax .persistence .EntityManager ;
6
+ import javax .persistence .PersistenceContext ;
7
+ import javax .persistence .criteria .CriteriaQuery ;
8
+
9
+ import org .springframework .stereotype .Repository ;
10
+ import org .springframework .transaction .annotation .Transactional ;
11
+
12
+ import com .goeswhere .frameworkgame .blogapp .domain .Post ;
13
+
14
+ @ Repository
3
15
public class PostDao {
4
16
17
+ @ PersistenceContext
18
+ EntityManager em ;
19
+
20
+ @ Transactional (readOnly = true )
21
+ public List <Post > all () {
22
+ final CriteriaQuery <Post > cq = em .getCriteriaBuilder ().createQuery (Post .class );
23
+ cq .from (Post .class );
24
+ return em .createQuery (cq ).getResultList ();
25
+ }
5
26
}
Original file line number Diff line number Diff line change 48
48
49
49
<tx : annotation-driven transaction-manager =" transactionManager" />
50
50
51
+ <bean id =" transactionManager" class =" org.springframework.orm.jpa.JpaTransactionManager" >
52
+ <property name =" entityManagerFactory" ref =" entityManagerFactory" />
53
+ </bean >
54
+
51
55
<bean id =" dataSource" class =" org.apache.commons.dbcp.BasicDataSource" destroy-method =" close" >
52
56
<property name =" driverClassName" value =" org.apache.derby.jdbc.EmbeddedDriver" />
53
57
<property name =" url" value =" jdbc:derby:blogapp;create=true" />
You can’t perform that action at this time.
0 commit comments