From db3317e5a89d0ee6cf638124c3886d69af623f9e Mon Sep 17 00:00:00 2001 From: sxyx2008 Date: Fri, 4 Jul 2014 16:00:58 +0800 Subject: [PATCH] spring4 hibernate4 example --- .gitignore | 4 +- pom.xml | 1 + spring4-hibernate4-example/pom.xml | 194 ++++++++++++++++++ .../com/studerw/spring/IndexController.java | 16 ++ .../com/studerw/spring/model/Account.java | 39 ++++ .../src/main/resources/log4j.dtd | 166 +++++++++++++++ .../src/main/resources/log4j.xml | 44 ++++ .../src/main/resources/spring/app.properties | 5 + .../resources/spring/applicationContext.xml | 16 ++ .../src/main/resources/spring/db-config.xml | 49 +++++ .../WEB-INF/bootstrap-dispatcher-servlet.xml | 26 +++ .../src/main/webapp/WEB-INF/pages/index.jsp | 5 + .../src/main/webapp/WEB-INF/web.xml | 38 ++++ .../src/main/webapp/index.jsp | 3 + .../com/studerw/spring/HibernateTest.java | 44 ++++ .../java/com/studerw/spring/WebAppTest.java | 38 ++++ .../src/test/resources/log4j.xml | 48 +++++ .../src/test/resources/spring/test.properties | 5 + .../src/test/resources/spring/testContext.xml | 16 ++ 19 files changed, 756 insertions(+), 1 deletion(-) create mode 100644 spring4-hibernate4-example/pom.xml create mode 100644 spring4-hibernate4-example/src/main/java/com/studerw/spring/IndexController.java create mode 100644 spring4-hibernate4-example/src/main/java/com/studerw/spring/model/Account.java create mode 100644 spring4-hibernate4-example/src/main/resources/log4j.dtd create mode 100644 spring4-hibernate4-example/src/main/resources/log4j.xml create mode 100644 spring4-hibernate4-example/src/main/resources/spring/app.properties create mode 100644 spring4-hibernate4-example/src/main/resources/spring/applicationContext.xml create mode 100644 spring4-hibernate4-example/src/main/resources/spring/db-config.xml create mode 100644 spring4-hibernate4-example/src/main/webapp/WEB-INF/bootstrap-dispatcher-servlet.xml create mode 100644 spring4-hibernate4-example/src/main/webapp/WEB-INF/pages/index.jsp create mode 100644 spring4-hibernate4-example/src/main/webapp/WEB-INF/web.xml create mode 100644 spring4-hibernate4-example/src/main/webapp/index.jsp create mode 100644 spring4-hibernate4-example/src/test/java/com/studerw/spring/HibernateTest.java create mode 100644 spring4-hibernate4-example/src/test/java/com/studerw/spring/WebAppTest.java create mode 100644 spring4-hibernate4-example/src/test/resources/log4j.xml create mode 100644 spring4-hibernate4-example/src/test/resources/spring/test.properties create mode 100644 spring4-hibernate4-example/src/test/resources/spring/testContext.xml diff --git a/.gitignore b/.gitignore index edcc8c9..fca2ff7 100644 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,9 @@ target *.db .lock *.iml +*.springBeans .gradle build dependency-reduced-pom.xml -/src \ No newline at end of file +/src + diff --git a/pom.xml b/pom.xml index 2b8c8e1..6375f6e 100644 --- a/pom.xml +++ b/pom.xml @@ -69,6 +69,7 @@ simple-osgi-webapp flyway-example workflow-activiti-example + spring4-hibernate4-example diff --git a/spring4-hibernate4-example/pom.xml b/spring4-hibernate4-example/pom.xml new file mode 100644 index 0000000..fb398d0 --- /dev/null +++ b/spring4-hibernate4-example/pom.xml @@ -0,0 +1,194 @@ + + 4.0.0 + + net.aimeizi + maven-framework-project + 1.0.0 + + spring4-hibernate4-example + war + + + UTF-8 + 4.0.4.RELEASE + 4.3.5.Final + + + + + org.springframework + spring-context + ${spring.version} + + + org.springframework + spring-webmvc + ${spring.version} + + + cglib + cglib + 2.2.2 + + + org.springframework + spring-jdbc + ${spring.version} + + + org.springframework + spring-orm + ${spring.version} + + + org.springframework + spring-tx + ${spring.version} + + + + + org.postgresql + postgresql + 9.3-1101-jdbc41 + + + + com.h2database + h2 + 1.4.179 + + + + + com.jolbox + bonecp + 0.8.0.RELEASE + + + + + org.aspectj + aspectjrt + 1.7.4 + + + + + javax.servlet + javax.servlet-api + 3.1.0 + provided + + + javax.servlet.jsp + jsp-api + 2.2 + provided + + + javax.servlet + jstl + 1.2 + + + + org.springframework + spring-webmvc + ${spring.version} + + + org.hibernate + hibernate-core + ${hibernate.version} + + + org.hibernate + hibernate-entitymanager + ${hibernate.version} + + + org.javassist + javassist + 3.18.0-GA + + + + com.google.guava + guava + 17.0 + + + + + log4j + log4j + 1.2.17 + + + + + javax.validation + validation-api + 1.0.0.GA + + + org.hibernate + hibernate-validator + 4.1.0.Final + + + + + org.springframework + spring-test + ${spring.version} + test + + + + junit + junit + 4.11 + test + + + + + spring4-hibernate4-example + + + maven-compiler-plugin + + 1.6 + 1.6 + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.14.1 + + + + org.apache.tomcat.maven + tomcat7-maven-plugin + 2.2 + + / + 8080 + 8009 + true + true + + -Xms512m -Xmx1024m -XX:MaxPermSize=1024m + true + + + + + + + + \ No newline at end of file diff --git a/spring4-hibernate4-example/src/main/java/com/studerw/spring/IndexController.java b/spring4-hibernate4-example/src/main/java/com/studerw/spring/IndexController.java new file mode 100644 index 0000000..c71311d --- /dev/null +++ b/spring4-hibernate4-example/src/main/java/com/studerw/spring/IndexController.java @@ -0,0 +1,16 @@ +package com.studerw.spring; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +@Controller +@RequestMapping("/index.htm") +public class IndexController { + @RequestMapping(method = RequestMethod.GET) + public String printWelcome(ModelMap model) { + model.addAttribute("message", "Hello world!"); + return "index"; + } +} \ No newline at end of file diff --git a/spring4-hibernate4-example/src/main/java/com/studerw/spring/model/Account.java b/spring4-hibernate4-example/src/main/java/com/studerw/spring/model/Account.java new file mode 100644 index 0000000..0b5129c --- /dev/null +++ b/spring4-hibernate4-example/src/main/java/com/studerw/spring/model/Account.java @@ -0,0 +1,39 @@ +package com.studerw.spring.model; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity @Table(name="ACCOUNT") +public class Account { + @Id @GeneratedValue + private long id; + private double cashBalance; + private String name; + + public long getId() { + return id; + } + public void setId(long id) { + this.id = id; + } + public double getCashBalance() { + return cashBalance; + } + public void setCashBalance(double cashBalance) { + this.cashBalance = cashBalance; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "id: " + id + ", balance: " + cashBalance + ", name: " + name; + + } +} \ No newline at end of file diff --git a/spring4-hibernate4-example/src/main/resources/log4j.dtd b/spring4-hibernate4-example/src/main/resources/log4j.dtd new file mode 100644 index 0000000..d92a6e7 --- /dev/null +++ b/spring4-hibernate4-example/src/main/resources/log4j.dtd @@ -0,0 +1,166 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring4-hibernate4-example/src/main/resources/log4j.xml b/spring4-hibernate4-example/src/main/resources/log4j.xml new file mode 100644 index 0000000..be66795 --- /dev/null +++ b/spring4-hibernate4-example/src/main/resources/log4j.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring4-hibernate4-example/src/main/resources/spring/app.properties b/spring4-hibernate4-example/src/main/resources/spring/app.properties new file mode 100644 index 0000000..f80ef41 --- /dev/null +++ b/spring4-hibernate4-example/src/main/resources/spring/app.properties @@ -0,0 +1,5 @@ +hibernate.dialect=org.hibernate.dialect.H2Dialect +jdbc.driver=org.h2.Driver +jdbc.url=jdbc\:h2\:file\:./target/spring4hibernate4;AUTO_SERVER\=TRUE +jdbc.user=sa +jdbc.pw= diff --git a/spring4-hibernate4-example/src/main/resources/spring/applicationContext.xml b/spring4-hibernate4-example/src/main/resources/spring/applicationContext.xml new file mode 100644 index 0000000..28d91cd --- /dev/null +++ b/spring4-hibernate4-example/src/main/resources/spring/applicationContext.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring4-hibernate4-example/src/main/resources/spring/db-config.xml b/spring4-hibernate4-example/src/main/resources/spring/db-config.xml new file mode 100644 index 0000000..9ba8182 --- /dev/null +++ b/spring4-hibernate4-example/src/main/resources/spring/db-config.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + hibernate.format_sql=true + hibernate.show_sql=true + hibernate.hbm2ddl.auto=update + + + + + + + + + + + + diff --git a/spring4-hibernate4-example/src/main/webapp/WEB-INF/bootstrap-dispatcher-servlet.xml b/spring4-hibernate4-example/src/main/webapp/WEB-INF/bootstrap-dispatcher-servlet.xml new file mode 100644 index 0000000..bf553e9 --- /dev/null +++ b/spring4-hibernate4-example/src/main/webapp/WEB-INF/bootstrap-dispatcher-servlet.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring4-hibernate4-example/src/main/webapp/WEB-INF/pages/index.jsp b/spring4-hibernate4-example/src/main/webapp/WEB-INF/pages/index.jsp new file mode 100644 index 0000000..228bb09 --- /dev/null +++ b/spring4-hibernate4-example/src/main/webapp/WEB-INF/pages/index.jsp @@ -0,0 +1,5 @@ + + +

${message}

+ + \ No newline at end of file diff --git a/spring4-hibernate4-example/src/main/webapp/WEB-INF/web.xml b/spring4-hibernate4-example/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..66cac61 --- /dev/null +++ b/spring4-hibernate4-example/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,38 @@ + + + + Spring 4 and Hibernate 4 Example + + + Spring 4 and Hibernate 4 + + + contextConfigLocation + classpath:spring/applicationContext.xml + + + + org.springframework.web.context.ContextLoaderListener + + + + bootstrap-dispatcher + org.springframework.web.servlet.DispatcherServlet + 1 + + + + bootstrap-dispatcher + / + + + + index.jsp + + + + COOKIE + + \ No newline at end of file diff --git a/spring4-hibernate4-example/src/main/webapp/index.jsp b/spring4-hibernate4-example/src/main/webapp/index.jsp new file mode 100644 index 0000000..95399af --- /dev/null +++ b/spring4-hibernate4-example/src/main/webapp/index.jsp @@ -0,0 +1,3 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%-- Redirected because we can't set the welcome page to a virtual URL. --%> + diff --git a/spring4-hibernate4-example/src/test/java/com/studerw/spring/HibernateTest.java b/spring4-hibernate4-example/src/test/java/com/studerw/spring/HibernateTest.java new file mode 100644 index 0000000..8822465 --- /dev/null +++ b/spring4-hibernate4-example/src/test/java/com/studerw/spring/HibernateTest.java @@ -0,0 +1,44 @@ +package com.studerw.spring; + +import org.apache.log4j.Logger; +import org.hibernate.Query; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.transaction.TransactionConfiguration; +import org.springframework.transaction.annotation.Transactional; + +import com.studerw.spring.model.Account; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("classpath:spring/testContext.xml") +@TransactionConfiguration(defaultRollback = true) +public class HibernateTest { + private static final Logger log = Logger.getLogger(HibernateTest.class); + @Autowired + SessionFactory sessionFactory; + + + @Test + @Transactional + public void createAccount() { + Session session = sessionFactory.getCurrentSession(); // not part of a transaction, so we need to open a session manually + Account account = new Account(); + account.setName("blah"); + account.setCashBalance(12.23); + long id = (Long)session.save(account); + log.debug("saved new account, id=" + id); + } + + @Test @Transactional + public void updateAccount() { + Session session = sessionFactory.getCurrentSession(); + Query query = session.createQuery("from Account a"); + Account a = (Account) query.uniqueResult(); + log.debug(a); + } +} diff --git a/spring4-hibernate4-example/src/test/java/com/studerw/spring/WebAppTest.java b/spring4-hibernate4-example/src/test/java/com/studerw/spring/WebAppTest.java new file mode 100644 index 0000000..f925f6e --- /dev/null +++ b/spring4-hibernate4-example/src/test/java/com/studerw/spring/WebAppTest.java @@ -0,0 +1,38 @@ +package com.studerw.spring; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.web.context.WebApplicationContext; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration("file:src/main/webapp/WEB-INF/bootstrap-dispatcher-servlet.xml") +public class WebAppTest { + private MockMvc mockMvc; + + @Autowired + protected WebApplicationContext wac; + + @Before + public void setup() { + this.mockMvc = webAppContextSetup(this.wac).build(); + } + + @Test + public void simple() throws Exception { + mockMvc.perform(get("/index.htm")) + .andExpect(status().isOk()) + .andExpect(view().name("index")); + } +} diff --git a/spring4-hibernate4-example/src/test/resources/log4j.xml b/spring4-hibernate4-example/src/test/resources/log4j.xml new file mode 100644 index 0000000..4feb8f9 --- /dev/null +++ b/spring4-hibernate4-example/src/test/resources/log4j.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring4-hibernate4-example/src/test/resources/spring/test.properties b/spring4-hibernate4-example/src/test/resources/spring/test.properties new file mode 100644 index 0000000..f80ef41 --- /dev/null +++ b/spring4-hibernate4-example/src/test/resources/spring/test.properties @@ -0,0 +1,5 @@ +hibernate.dialect=org.hibernate.dialect.H2Dialect +jdbc.driver=org.h2.Driver +jdbc.url=jdbc\:h2\:file\:./target/spring4hibernate4;AUTO_SERVER\=TRUE +jdbc.user=sa +jdbc.pw= diff --git a/spring4-hibernate4-example/src/test/resources/spring/testContext.xml b/spring4-hibernate4-example/src/test/resources/spring/testContext.xml new file mode 100644 index 0000000..b5d2331 --- /dev/null +++ b/spring4-hibernate4-example/src/test/resources/spring/testContext.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + \ No newline at end of file