Skip to content

Commit 453708d

Browse files
committed
HOLY SHIT IT RAN SQL
1 parent ee70dc7 commit 453708d

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
<artifactId>spring-tx</artifactId>
2828
<version>${spring.version}</version>
2929
</dependency>
30+
<dependency>
31+
<groupId>org.springframework</groupId>
32+
<artifactId>spring-jdbc</artifactId>
33+
<version>${spring.version}</version>
34+
</dependency>
3035
</dependencies>
3136
</dependencyManagement>
3237

Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
package com.goeswhere.frameworkgame.blogapp.controller;
22

3+
import org.springframework.beans.factory.annotation.Autowired;
34
import org.springframework.stereotype.Controller;
5+
import org.springframework.transaction.annotation.Transactional;
46
import org.springframework.ui.Model;
57
import org.springframework.web.bind.annotation.RequestMapping;
68

7-
import com.goeswhere.frameworkgame.blogapp.domain.Post;
8-
import com.google.common.collect.Lists;
9+
import com.goeswhere.frameworkgame.blogapp.dao.PostDao;
910

1011
@Controller
1112
@RequestMapping("/")
1213
public class IndexController {
14+
@Autowired
15+
PostDao posts;
16+
17+
@Transactional(readOnly = true)
1318
@RequestMapping
1419
public String index(Model m) {
15-
m.addAttribute("posts", Lists.newArrayList(new Post("Ponies!", "All about ponies")));
20+
m.addAttribute("posts", posts.all());
1621
return "index";
1722
}
1823
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
package com.goeswhere.frameworkgame.blogapp.dao;
22

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
315
public class PostDao {
416

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+
}
526
}

src/main/resources/spring/appContext-main.xml

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848

4949
<tx:annotation-driven transaction-manager="transactionManager" />
5050

51+
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
52+
<property name="entityManagerFactory" ref="entityManagerFactory" />
53+
</bean>
54+
5155
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
5256
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver" />
5357
<property name="url" value="jdbc:derby:blogapp;create=true" />

0 commit comments

Comments
 (0)