AD

spring mvc 整合 jpa 搭建基于领域驱动模型框架搭建

一、maven pom.xml

<project xmlns="**maven.apache**/POM/4.0.0" xmlns:xsi="****w3**/2001/XMLSchema-instance"
  xsi:schemaLocation="**maven.apache**/POM/4.0.0 **maven.apache**/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.fengshu</groupId>
  <artifactId>gotian</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>gotian</name>
  <url>**maven.apache**</url>

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java-version>1.6</java-version>
        <servlet-api.version>2.5</servlet-api.version>
        <jsp-api.version>2.2</jsp-api.version>
        <jstl.version>1.2</jstl.version>
        <org.springframework-version>3.2.5.RELEASE</org.springframework-version>
        <org.aspectj-version>1.7.4</org.aspectj-version>
        <org.slf4j-version>1.7.5</org.slf4j-version>
    </properties>

  <dependencies>

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <!-- 用于支持事务 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc-portlet</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${org.springframework-version}</version>
            </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <!-- hibernate begin -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.3.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.1.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.6.3.Final</version>
        </dependency>

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>3.1.0.GA</version>
        </dependency>

        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.22</version>
            </dependency>

        <!-- hibernate end -->

        <!-- AspectJ -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${org.aspectj-version}</version>
        </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.6.12</version>
            </dependency>
        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.mail</groupId>
                    <artifactId>mail</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.jms</groupId>
                    <artifactId>jms</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jdmk</groupId>
                    <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jmx</groupId>
                    <artifactId>jmxri</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- @Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <dependency>
            <groupId>org.apache**mons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache**mons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.1</version>
        </dependency>
        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>${servlet-api.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>${jsp-api.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
           <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>2.2</version>
        </dependency>
  </dependencies>
  <modules>
    <module>gotian-web</module>
    <module>gotian-core</module>
    <module>gotian-application</module>
    <module>gotian-applicationImpl</module>
    <module>gotian-conf</module>
  </modules>

    <build>
        <plugins>
            <!-- 配置加入jetty服务器,开发时我们一般使用jetty服务器 -->
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <configuration>
                    <!-- 设置扫描target/classes内部文件变化时间间隔 -->
                    <scanIntervalSeconds>2</scanIntervalSeconds>
                    <webApp>
                        <contextPath>/gotian-web</contextPath>
                    </webApp>
                </configuration>
            </plugin>
            <!-- compiler插件, 设定JDK版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>

            <!-- war打包插件, 设定war包名称不带版本号 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <warName>gotian</warName>
                </configuration>
            </plugin>
            <!-- eclipse插件, 设定wtp版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                    <wtpversion>2.0</wtpversion>

                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>install</id>
                        <phase>install</phase>
                        <goals>
                            <goal>sources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
                <!-- 用于支持跨模块加载配置文件 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.5</version>
                    <configuration>
                        <encoding>UTF-8</encoding>
                        <nonFilteredFileExtensions>
                            <nonFilteredFileExtension>dll</nonFilteredFileExtension>
                            <nonFilteredFileExtension>so</nonFilteredFileExtension>
                        </nonFilteredFileExtensions>
                    </configuration>
                </plugin>
        </plugins>
    </build>
</project>

二、web.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="****w3**/2001/XMLSchema-instance"
    xmlns="**java.sun**/xml/ns/javaee" xmlns:web="**java.sun**/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="**java.sun**/xml/ns/javaee **java.sun**/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>bsl-system-web</display-name>

    <!-- Character Filter -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:META-INF/spring/ApplicationContext.xml</param-value>
    </context-param>
        <!-- Spring Listener -->
     <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
    <!-- SpringMVC -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/WEB-INF/pages/error/500.html</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/WEB-INF/pages/error/500.html</location>
    </error-page>
    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/pages/error/404.jsp</location>
    </error-page>

    <welcome-file-list>
        <welcome-file>/pages/index.jsp</welcome-file>
    </welcome-file-list>

    <distributable />
</web-app>

三、Application.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="****springframework**/schema/beans"
    xmlns:context="****springframework**/schema/context" xmlns:tx="****springframework**/schema/tx"
    xmlns:aop="****springframework**/schema/aop" xmlns:util="****springframework**/schema/util"
    xmlns:xsi="****w3**/2001/XMLSchema-instance"
    xsi:schemaLocation="****springframework**/schema/beans ****springframework**/schema/beans/spring-beans-3.0.xsd
        ****springframework**/schema/context ****springframework**/schema/context/spring-context-3.0.xsd
        ****springframework**/schema/tx ****springframework**/schema/tx/spring-tx-3.0.xsd
        ****springframework**/schema/util ****springframework**/schema/util/spring-util-3.0.xsd
        ****springframework**/schema/aop ****springframework**/schema/aop/spring-aop-2.5.xsd">
    <import resource="root-context.xml" />
    <import resource="spring-context.xml" />
</beans>

四、spring-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="****springframework**/schema/beans"
    xmlns:xsi="****w3**/2001/XMLSchema-instance"
    xmlns:p="****springframework**/schema/p"
    xmlns:context="****springframework**/schema/context"
    xsi:schemaLocation="
        ****springframework**/schema/beans
        ****springframework**/schema/beans/spring-beans-3.0.xsd
        ****springframework**/schema/context
        ****springframework**/schema/context/spring-context-3.0.xsd">

    <!-- DispatcherPortlet Context: defines this olaf's request-processing infrastructure -->

    <!-- Autodetect annotated controllers -->
    <context:component-scan base-package="com.fengshu.gotian.controller" />
 <bean id="defaultAnnotationHandlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    <bean id="annotationMethodHandlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html;charset=utf-8</value>
                            <value>text/xml</value>
                            <value>text/plain</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

五、root-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="****springframework**/schema/beans"
    xmlns:xsi="****w3**/2001/XMLSchema-instance" xmlns:p="****springframework**/schema/p"
    xmlns:aop="****springframework**/schema/aop" xmlns:tx="****springframework**/schema/tx"
    xmlns:context="****springframework**/schema/context"
    xsi:schemaLocation="****springframework**/schema/beans
    ****springframework**/schema/beans/spring-beans-3.0.xsd
    ****springframework**/schema/tx
    ****springframework**/schema/tx/spring-tx.xsd
    ****springframework**/schema/aop
    ****springframework**/schema/aop/spring-aop.xsd
    ****springframework**/schema/context
    ****springframework**/schema/context/spring-context.xsd">

    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
    </bean>
    <bean
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor">
    </bean>

    <bean id="dataSource" class="org.apache**mons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver">
        </property>
        <property name="url" value="jdbc:mysql://localhost:3306/ddd">
        </property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    <bean id="emf"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="packagesToScan" value="com.fengshu.gotian.core" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter"></property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.max_fetch_depth">3</prop>
                <prop key="hibernate.jdbc.fetch_size">18</prop>
                <prop key="hibernate.jdbc.batch_size">10</prop>
            </props>
        </property>
    </bean>
    <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="database" value="MYSQL"></property>
        <property name="showSql" value="true"></property>
        <property name="generateDdl" value="true"></property>
        <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect">
        </property>
    </bean>

    <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect">
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="emf"></property>
        <property name="jpaDialect" ref="jpaDialect"></property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />
    <context:annotation-config />
    <context:component-scan base-package="com.fengshu.gotian.*" />
    <aop:aspectj-autoproxy />
</beans>

六、查询写法

package com.fengshu.gotian.applicationImpl;

import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import com.fengshu.gotian.application.UserApplication;
import com.fengshu.gotian.core.User;

@Named
@Transactional
public class UserApplicationImpl implements UserApplication {

    private Logger logger = Logger.getLogger(this.getClass());
    @PersistenceContext
    private EntityManager entityManager;

    public void save(User user) {
        logger**("applicationimpl");
        entityManager.persist(user);
    }

}

七、项目结构

spring mvc 整合 jpa 搭建基于领域驱动模型框架搭建

八、注意事项

实体bean相关注解必须写在get方法上,否则可能无法自动建表,更新表。

九、学习过程中参考的相关文章

spring mvc + jpa **blog.csdn**/wfung_kwok/article/details/7762142

Hibernate 参数设置一览表 ****blogjava**/i369/articles/194855.html

基于注解的SpringMVC整合JPA(经验证是可以的) **hi.baidu**/hety_love/item/6a5e61cb165d0a3c45941676

Spring MVC 示例-注解; RESTful 风格

关于支持RESTful的思考

Java回顾之Spring基础 - 李胜攀博客园

五、CXF WebService整合Spring - hoojo - 博客园

Spring整合CXF,发布RSETful 风格WebService

为什么要使用Spring?

基于注解的SpringMVC整合JPA

spring数据源配置及事务管理--entityManager

使用Spring框架写JPA程序 **blog.sina**.cn/s/blog_4fb490ff0100xbly.html

Jpa规范中persistence.xml 配置文件解析

JPA全注解的自动扫描问题

JPA 不在 persistence.xml 文件中配置每个Entity实体类的2种解决办法 这个教训整的要他妈的吸取

dddlib

jpa entityManager 配置

标签: jpa, spring, 事务, 注解, SpringMVC
分类: Spring
时间: 2014-06-16

相关文章

  1. spring mvc整合freemarker基于注解方式

    spring mvc整合freemarker基于注解方式,需要的朋友可以参考一下 基于网络改进为:最正常版本 <?xml version="1.0" encoding="UTF-8&q ...
  2. 我的"第一次",就这样没了:DDD(领域驱动设计)理论结合实践

    写在前面 插一句:本人超爱落网-<平凡的世界>这一期,分享给大家. 阅读目录: 关于DDD 前期分析 框架搭建 代码实现 开源-发布 后记 第一次听你,清风吹送,田野短笛:第一次看你,半弯新湖,鱼跃翠堤:第一 ...
  3. DDD领域驱动设计和实践

    引言 软件系统面向对象的设计思想可谓历史悠久,20世纪70年代的Smalltalk可以说是面向对象语言的经典,直到今天我们依然将这门语言视为面向对象语言的基础.随着编程语言和技术的发展,各种语言特性层出不穷,面向对象是大 ...
  4. 领域驱动设计和实践

    引言 软件系统面向对象的设计思想可谓历史悠久,20世纪70年代的Smalltalk可以说是面向对象语言的经典,直到今天我们依然将这门语言视为面向对象语言的基础.随着编程语言和技术的发展,各种语言特性层出不穷,面向对象是大 ...
  5. 一缕阳光:DDD(领域驱动设计)应对具体业务场景,如何聚焦 Domain Model(领域模型)?

    写在前面 阅读目录: 问题根源是什么? <领域驱动设计-软件核心复杂性应对之道>分层概念 Repository(仓储)职责所在? Domain Model(领域模型)重新设计 Domain Service(领 ...
  6. 缓存与饼干,spring mvc使用ehcache

    缓存与饼干,spring mvc使用ehcache 需要用到的jar包 ehcache-2.7.5.jar(主程序) ehcache-spring-annotations-1.2.0.jar(注解) guava-r09. ...
  7. JAVA WEB搭建Spring mvc+Spring+MyBatis整合框架

    JAVA WEB搭建Spring mvc+Spring+MyBatis整合框架 http://my.codeweblog.com/Thinkeryjgfn/blog 有时间在写吧!!http://mail.qq.com/ ...
  8. 使用 Spring 2.5 基于注解驱动的 Spring MVC

    继 Spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能.现在你无须让 Controller 继承任何接口,无需在 XML 配置文件中定义请 ...
  9. Spring MVC 框架搭建配置方法及详解

    现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过要想灵活运用Spri ...
  10. 基于上下文图的策略性领域驱动开发

    简介 当应用程序逐渐变得庞大和复杂后,很多面向对象建模的方法都达不到非常好的可伸缩性.上下文图是一种通用目的的技术,作为领域驱动开发大家族的一名成员,它帮助架构师和开发人员管理他们在软件开发项目中不得不面对的形形色色的复 ...
  11. Spring mvc 和 CXF 搭建SOAP环境

    最近用Spring mvc框架搭建web工程,后因为业务需要重新需要在原有web工程基础上,添加Webservice接口.这就涉及到一个问题: 在spring mvc 配置文件中如何添加一些WS框架.在添加之初发现一个问 ...
  12. (二)在一的基础上集成spring 4.0.3 并整合JPA以及spring的单元测试

    1)加上sping的依赖包,并在base模块进行引用. <!-- spring包依赖--> <dependency> <groupId>org.springframework</ ...
  13. spring 4 + jpa(hibernate 3/4) + spring mvc 多数据源配置

    转载于:http://www.loveweir.com/ 先从persistence.xml开始: <?xml version="1.0″ encoding="UTF-8″?> <p ...
  14. Spring MVC中基于自定义Editor的表单数据处理技巧分享

    Spring MVC中基于自定义Editor的表单数据处理技巧.需要的朋友可以过来参考下,希望对大家有所帮助 面向对象的编程方式极大地方便了程序员在管理数据上所花费的精力.在基于Spring MVC的Web开发过程当中, ...
  15. Spring MVC入门基础项目环境搭建

    本文章知道简单的能够搭建Sping MVC环境进行代码基础开发. 根据Spring的体系结构图画出Spring开发过程中涉及的文件和交流细节: Spring MVC流程说明: 用户请求http://127.0.0.1:8 ...
  16. Spring MVC JSP和freemaker整合

    关于springmvc如何整合freemaker 可以参考 http://my.codeweblog.com/bddiudiu/blog/228788 但是如果只是发布到前端的页面是需要静态化 即使用freemaker来 ...
  17. 基于spring mvc 开发电影网站sharemov.net

    基于spring mvc 开发电影网站 sharemov.net 页面支持html5 欢迎进来光临
  18. spring mvc+spring3.05+mybatis3.1.1环境搭建____maven管理

    =====================pom.xml=================================================== <project xmlns="http:/ ...
  19. freemarker+shiro+spring mvc+spring+mybatis整合

    shiro配置: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://ww ...