spring4 hibernate4 example
This commit is contained in:
+3
-1
@@ -6,7 +6,9 @@ target
|
||||
*.db
|
||||
.lock
|
||||
*.iml
|
||||
*.springBeans
|
||||
.gradle
|
||||
build
|
||||
dependency-reduced-pom.xml
|
||||
/src
|
||||
/src
|
||||
|
||||
|
||||
@@ -69,6 +69,7 @@
|
||||
<module>simple-osgi-webapp</module>
|
||||
<module>flyway-example</module>
|
||||
<module>workflow-activiti-example</module>
|
||||
<module>spring4-hibernate4-example</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
|
||||
@@ -0,0 +1,194 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>net.aimeizi</groupId>
|
||||
<artifactId>maven-framework-project</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<artifactId>spring4-hibernate4-example</artifactId>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<spring.version>4.0.4.RELEASE</spring.version>
|
||||
<hibernate.version>4.3.5.Final</hibernate.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cglib</groupId>
|
||||
<artifactId>cglib</artifactId>
|
||||
<version>2.2.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-orm</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Database -->
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>9.3-1101-jdbc41</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.4.179</version>
|
||||
</dependency>
|
||||
|
||||
<!-- BoneCP -->
|
||||
<dependency>
|
||||
<groupId>com.jolbox</groupId>
|
||||
<artifactId>bonecp</artifactId>
|
||||
<version>0.8.0.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<!-- AspectJ -->
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjrt</artifactId>
|
||||
<version>1.7.4</version>
|
||||
</dependency>
|
||||
|
||||
<!--Servlet -->
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet.jsp</groupId>
|
||||
<artifactId>jsp-api</artifactId>
|
||||
<version>2.2</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-entitymanager</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.javassist</groupId>
|
||||
<artifactId>javassist</artifactId>
|
||||
<version>3.18.0-GA</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>17.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Logging -->
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>1.2.17</version>
|
||||
</dependency>
|
||||
|
||||
<!-- JSR 303 with Hibernate Validator -->
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>1.0.0.GA</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>4.1.0.Final</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.11</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>spring4-hibernate4-example</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.14.1</version>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.tomcat.maven</groupId>
|
||||
<artifactId>tomcat7-maven-plugin</artifactId>
|
||||
<version>2.2</version>
|
||||
<configuration>
|
||||
<path>/</path>
|
||||
<port>8080</port>
|
||||
<ajpPort>8009</ajpPort>
|
||||
<useTestClasspath>true</useTestClasspath>
|
||||
<contextReloadable>true</contextReloadable>
|
||||
<systemProperties>
|
||||
<JAVA_OPTS>-Xms512m -Xmx1024m -XX:MaxPermSize=1024m</JAVA_OPTS>
|
||||
<log4j.debug>true</log4j.debug>
|
||||
</systemProperties>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
|
||||
<!-- Authors: Chris Taylor, Ceki Gulcu. -->
|
||||
|
||||
<!-- Version: 1.2 -->
|
||||
|
||||
<!-- A configuration element consists of optional renderer
|
||||
elements,appender elements, categories and an optional root
|
||||
element. -->
|
||||
|
||||
<!ELEMENT log4j:configuration (renderer*, appender*,(category|logger)*,root?,
|
||||
categoryFactory?)>
|
||||
|
||||
<!-- The "threshold" attribute takes a level value such that all -->
|
||||
<!-- logging statements with a level equal or below this value are -->
|
||||
<!-- disabled. -->
|
||||
|
||||
<!-- Setting the "debug" enable the printing of internal log4j logging -->
|
||||
<!-- statements. -->
|
||||
|
||||
<!-- By default, debug attribute is "null", meaning that we not do touch -->
|
||||
<!-- internal log4j logging settings. The "null" value for the threshold -->
|
||||
<!-- attribute can be misleading. The threshold field of a repository -->
|
||||
<!-- cannot be set to null. The "null" value for the threshold attribute -->
|
||||
<!-- simply means don't touch the threshold field, the threshold field -->
|
||||
<!-- keeps its old value. -->
|
||||
|
||||
<!ATTLIST log4j:configuration
|
||||
xmlns:log4j CDATA #FIXED "http://jakarta.apache.org/log4j/"
|
||||
threshold (all|debug|info|warn|error|fatal|off|null) "null"
|
||||
debug (true|false|null) "null"
|
||||
>
|
||||
|
||||
<!-- renderer elements allow the user to customize the conversion of -->
|
||||
<!-- message objects to String. -->
|
||||
|
||||
<!ELEMENT renderer EMPTY>
|
||||
<!ATTLIST renderer
|
||||
renderedClass CDATA #REQUIRED
|
||||
renderingClass CDATA #REQUIRED
|
||||
>
|
||||
|
||||
<!-- Appenders must have a name and a class. -->
|
||||
<!-- Appenders may contain an error handler, a layout, optional parameters -->
|
||||
<!-- and filters. They may also reference (or include) other appenders. -->
|
||||
<!ELEMENT appender (errorHandler?, param*, layout?, filter*, appender-ref*)>
|
||||
<!ATTLIST appender
|
||||
name ID #REQUIRED
|
||||
class CDATA #REQUIRED
|
||||
>
|
||||
|
||||
<!ELEMENT layout (param*)>
|
||||
<!ATTLIST layout
|
||||
class CDATA #REQUIRED
|
||||
>
|
||||
|
||||
<!ELEMENT filter (param*)>
|
||||
<!ATTLIST filter
|
||||
class CDATA #REQUIRED
|
||||
>
|
||||
|
||||
<!-- ErrorHandlers can be of any class. They can admit any number of -->
|
||||
<!-- parameters. -->
|
||||
|
||||
<!ELEMENT errorHandler (param*, root-ref?, logger-ref*, appender-ref?)>
|
||||
<!ATTLIST errorHandler
|
||||
class CDATA #REQUIRED
|
||||
>
|
||||
|
||||
<!ELEMENT root-ref EMPTY>
|
||||
|
||||
<!ELEMENT logger-ref EMPTY>
|
||||
<!ATTLIST logger-ref
|
||||
ref IDREF #REQUIRED
|
||||
>
|
||||
|
||||
<!ELEMENT param EMPTY>
|
||||
<!ATTLIST param
|
||||
name CDATA #REQUIRED
|
||||
value CDATA #REQUIRED
|
||||
>
|
||||
|
||||
|
||||
<!-- The priority class is org.apache.log4j.Level by default -->
|
||||
<!ELEMENT priority (param*)>
|
||||
<!ATTLIST priority
|
||||
class CDATA #IMPLIED
|
||||
value CDATA #REQUIRED
|
||||
>
|
||||
|
||||
<!-- The level class is org.apache.log4j.Level by default -->
|
||||
<!ELEMENT level (param*)>
|
||||
<!ATTLIST level
|
||||
class CDATA #IMPLIED
|
||||
value CDATA #REQUIRED
|
||||
>
|
||||
|
||||
|
||||
<!-- If no level element is specified, then the configurator MUST not -->
|
||||
<!-- touch the level of the named category. -->
|
||||
<!ELEMENT category (param*,(priority|level)?,appender-ref*)>
|
||||
<!ATTLIST category
|
||||
class CDATA #IMPLIED
|
||||
name CDATA #REQUIRED
|
||||
additivity (true|false) "true"
|
||||
>
|
||||
|
||||
<!-- If no level element is specified, then the configurator MUST not -->
|
||||
<!-- touch the level of the named logger. -->
|
||||
<!ELEMENT logger (level?,appender-ref*)>
|
||||
<!ATTLIST logger
|
||||
name ID #REQUIRED
|
||||
additivity (true|false) "true"
|
||||
>
|
||||
|
||||
|
||||
<!ELEMENT categoryFactory (param*)>
|
||||
<!ATTLIST categoryFactory
|
||||
class CDATA #REQUIRED>
|
||||
|
||||
<!ELEMENT appender-ref EMPTY>
|
||||
<!ATTLIST appender-ref
|
||||
ref IDREF #REQUIRED
|
||||
>
|
||||
|
||||
<!-- If no priority element is specified, then the configurator MUST not -->
|
||||
<!-- touch the priority of root. -->
|
||||
<!-- The root category always exists and cannot be subclassed. -->
|
||||
<!ELEMENT root (param*, (priority|level)?, appender-ref*)>
|
||||
|
||||
|
||||
<!-- ==================================================================== -->
|
||||
<!-- A logging event -->
|
||||
<!-- ==================================================================== -->
|
||||
<!ELEMENT log4j:eventSet (log4j:event*)>
|
||||
<!ATTLIST log4j:eventSet
|
||||
xmlns:log4j CDATA #FIXED "http://jakarta.apache.org/log4j/"
|
||||
version (1.1|1.2) "1.2"
|
||||
includesLocationInfo (true|false) "true"
|
||||
>
|
||||
|
||||
|
||||
|
||||
<!ELEMENT log4j:event (log4j:message, log4j:NDC?, log4j:throwable?,
|
||||
log4j:locationInfo?) >
|
||||
|
||||
<!-- The timestamp format is application dependent. -->
|
||||
<!ATTLIST log4j:event
|
||||
logger CDATA #REQUIRED
|
||||
level CDATA #REQUIRED
|
||||
thread CDATA #REQUIRED
|
||||
timestamp CDATA #REQUIRED
|
||||
>
|
||||
|
||||
<!ELEMENT log4j:message (#PCDATA)>
|
||||
<!ELEMENT log4j:NDC (#PCDATA)>
|
||||
|
||||
<!ELEMENT log4j:throwable (#PCDATA)>
|
||||
|
||||
<!ELEMENT log4j:locationInfo EMPTY>
|
||||
<!ATTLIST log4j:locationInfo
|
||||
class CDATA #REQUIRED
|
||||
method CDATA #REQUIRED
|
||||
file CDATA #REQUIRED
|
||||
line CDATA #REQUIRED
|
||||
>
|
||||
@@ -0,0 +1,44 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
|
||||
|
||||
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
|
||||
|
||||
<!-- Appenders -->
|
||||
<appender name="console" class="org.apache.log4j.ConsoleAppender">
|
||||
<param name="Target" value="System.out" />
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<!-- Application logger -->
|
||||
<logger name="com.studerw">
|
||||
<level value="debug" />
|
||||
</logger>
|
||||
|
||||
<!-- 3rdparty Loggers -->
|
||||
<logger name="org.springframework">
|
||||
<level value="warn" />
|
||||
</logger>
|
||||
|
||||
<logger name="org.springframework.jdbc">
|
||||
<level value="warn" />
|
||||
</logger>
|
||||
|
||||
<logger name="org.springframework.transaction">
|
||||
<level value="warn" />
|
||||
</logger>
|
||||
|
||||
<logger name="org.springframework.orm">
|
||||
<level value="warn" />
|
||||
</logger>
|
||||
|
||||
|
||||
<!-- Root Logger -->
|
||||
<root>
|
||||
<priority value="info" />
|
||||
<!--<level value="info"></level>-->
|
||||
<appender-ref ref="console" />
|
||||
</root>
|
||||
|
||||
</log4j:configuration>
|
||||
@@ -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=
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
|
||||
|
||||
<context:component-scan base-package="com.studerw.spring" annotation-config="true" use-default-filters="false">
|
||||
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
|
||||
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
|
||||
</context:component-scan>
|
||||
|
||||
<context:property-placeholder location="classpath:spring/app.properties"/>
|
||||
|
||||
<import resource="db-config.xml" />
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,49 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
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-4.0.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
|
||||
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
|
||||
|
||||
<tx:annotation-driven proxy-target-class="true"/>
|
||||
|
||||
|
||||
<!-- BoneCP configuration -->
|
||||
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
|
||||
<property name="driverClass" value="${jdbc.driver}" />
|
||||
<property name="jdbcUrl" value="${jdbc.url}" />
|
||||
<property name="username" value="${jdbc.user}"/>
|
||||
<property name="password" value="${jdbc.pw}"/>
|
||||
<property name="idleConnectionTestPeriod" value="60"/>
|
||||
<property name="idleMaxAge" value="240"/>
|
||||
<property name="maxConnectionsPerPartition" value="30"/>
|
||||
<property name="minConnectionsPerPartition" value="10"/>
|
||||
<property name="partitionCount" value="3"/>
|
||||
<property name="acquireIncrement" value="5"/>
|
||||
<property name="statementsCacheSize" value="100"/>
|
||||
<property name="releaseHelperThreads" value="3"/>
|
||||
</bean>
|
||||
|
||||
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
<property name="packagesToScan" value="com.studerw.spring.model"/>
|
||||
<property name="hibernateProperties">
|
||||
<value>
|
||||
hibernate.format_sql=true
|
||||
hibernate.show_sql=true
|
||||
hibernate.hbm2ddl.auto=update
|
||||
</value>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<!-- A transaction manager for working with Hibernate SessionFactories -->
|
||||
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
|
||||
<property name="sessionFactory" ref="sessionFactory"/>
|
||||
</bean>
|
||||
|
||||
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,26 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:mvc="http://www.springframework.org/schema/mvc"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
|
||||
|
||||
<context:component-scan base-package="com.studerw.spring" annotation-config="true" use-default-filters="false">
|
||||
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
|
||||
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
|
||||
</context:component-scan>
|
||||
|
||||
<mvc:annotation-driven/>
|
||||
|
||||
<mvc:resources order="1" location="/resources/" mapping="/resources/**"/>
|
||||
|
||||
<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"/>
|
||||
|
||||
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
||||
<property name="prefix" value="/WEB-INF/pages/"/>
|
||||
<property name="suffix" value=".jsp"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
<h1>${message}</h1>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"
|
||||
metadata-complete="true">
|
||||
<description>
|
||||
Spring 4 and Hibernate 4 Example
|
||||
</description>
|
||||
|
||||
<display-name>Spring 4 and Hibernate 4</display-name>
|
||||
|
||||
<context-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>classpath:spring/applicationContext.xml</param-value>
|
||||
</context-param>
|
||||
|
||||
<listener>
|
||||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<servlet>
|
||||
<servlet-name>bootstrap-dispatcher</servlet-name>
|
||||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>bootstrap-dispatcher</servlet-name>
|
||||
<url-pattern>/</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
<welcome-file-list>
|
||||
<welcome-file>index.jsp</welcome-file>
|
||||
</welcome-file-list>
|
||||
|
||||
<session-config>
|
||||
<tracking-mode>COOKIE</tracking-mode>
|
||||
</session-config>
|
||||
</web-app>
|
||||
@@ -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. --%>
|
||||
<c:redirect url="/index.htm" />
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
|
||||
|
||||
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
|
||||
|
||||
<!-- Appenders -->
|
||||
<appender name="console" class="org.apache.log4j.ConsoleAppender">
|
||||
<param name="Target" value="System.out" />
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<!-- Application logger -->
|
||||
<logger name="com.studerw">
|
||||
<level value="debug" />
|
||||
</logger>
|
||||
|
||||
<logger name="org.hibernate">
|
||||
<level value="info"/>
|
||||
</logger>
|
||||
|
||||
<!-- 3rdparty Loggers -->
|
||||
<logger name="org.springframework">
|
||||
<level value="warn" />
|
||||
</logger>
|
||||
|
||||
<logger name="org.springframework.jdbc">
|
||||
<level value="warn" />
|
||||
</logger>
|
||||
|
||||
<logger name="org.springframework.transaction">
|
||||
<level value="warn" />
|
||||
</logger>
|
||||
|
||||
<logger name="org.springframework.orm">
|
||||
<level value="warn" />
|
||||
</logger>
|
||||
|
||||
|
||||
<!-- Root Logger -->
|
||||
<root>
|
||||
<priority value="info" />
|
||||
<!--<level value="info"></level>-->
|
||||
<appender-ref ref="console" />
|
||||
</root>
|
||||
|
||||
</log4j:configuration>
|
||||
@@ -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=
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
|
||||
|
||||
<context:component-scan base-package="com.studerw.spring" annotation-config="true" use-default-filters="false">
|
||||
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
|
||||
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
|
||||
</context:component-scan>
|
||||
|
||||
<context:property-placeholder location="classpath:spring/test.properties"/>
|
||||
|
||||
<import resource="db-config.xml" />
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user