IDEA+gradle+spring+springMVC+hibernate+JPA+SpringData的web项目

原创 2016年09月29日 21:59:50

1.build.gradle

group 'com.lyj'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'war'
sourceCompatibility = 1.8

repositories {

    mavenLocal()
    maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
    maven { url "http://repo.maven.apache.org/maven2/"}
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    // servlet-api
    compile group: 'javax.servlet', name: 'servlet-api', version: '2.5'



    //spring相关
    compile group: 'org.springframework', name: 'spring-webmvc', version: '4.3.3.RELEASE'
    compile group: 'org.springframework', name: 'spring-orm', version: '4.3.3.RELEASE'
    compile group: 'org.springframework', name: 'spring-aspects', version: '4.3.3.RELEASE'

    //hibernate jpa相关
    compile group: 'org.jboss.spec.javax.transaction', name: 'jboss-transaction-api_1.2_spec', version: '1.0.1.Final'
    compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.2.2.Final'
    //c3p0连接池
    compile group: 'org.hibernate', name: 'hibernate-c3p0', version: '5.2.2.Final'
    //ehcahe二级缓存
    compile group: 'org.hibernate', name: 'hibernate-ehcache', version: '5.2.2.Final'
    //mysql
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.39'
    //springData
    compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '1.10.3.RELEASE'



}

2.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">

    <!--配置启动IOC容器的Listener-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 配置字符编码过滤器  必须配置在所有过滤器的最前面 -->
    <filter>
        <filter-name>CharacterEncodingFilter</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!-- 配置看可以把POST请求转为PUT,DELETE请求的Filter -->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 配置 OpenEntityManagerInViewFilter. 可以解决懒加载异常的问题 -->
    <filter>
        <filter-name>OpenEntityManagerInViewFilter</filter-name>
        <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>OpenEntityManagerInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>



    <!--配置SpringMVC的DispatcherServlet-->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>


    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>




</web-app>

3.springDispatcherServlet-servlet.xml配置springMVC解析器

<?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"
       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.lyj" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>
    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <mvc:default-servlet-handler/>
    <mvc:annotation-driven></mvc:annotation-driven>

   <!-- &lt;!&ndash; 配置 MultipartResolver 文件上传&ndash;&gt;
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"></property>
        <property name="maxUploadSize" value="1024000000"></property>
    </bean>-->

</beans>

4.applicationContext.xml 配置spring容器

<?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" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <!-- 配置自动扫描的包 -->
    <context:component-scan base-package="com.lyj">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation"
                                expression="org.springframework.web.bind.annotation.ControllerAdvice" />
    </context:component-scan>

    <!-- 配置数据源 -->
    <context:property-placeholder location="classpath:db.properties" />
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>

        <!-- 配置其他属性 -->
        <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    </bean>
    <!-- 配置JPA部分 -->
    <!-- 配置JPA的EntityManagerFactory -->
    <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
        </property>
        <property name="packagesToScan" value="com.lyj"></property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>

                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory
                </prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
            </props>
        </property>
        <!--使用二級緩存-->
        <property name="sharedCacheMode" value="ENABLE_SELECTIVE"></property>
    </bean>


    <!-- 配置事务 -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"></property>
    </bean>
    <!-- 配置支持基于注解的事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- 配置SpringData部分 -->
    <jpa:repositories base-package="com.lyj"
                      entity-manager-factory-ref="entityManagerFactory"></jpa:repositories>



</beans>

5.db.properties

jdbc.user=root
jdbc.password=
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///sssptest?useUnicode=true&characterEncoding=UTF-8

jdbc.initPoolSize=5
jdbc.maxPoolSize=20

6.测试

(1).Entity实体类

package com.lyj.entity;

import javax.persistence.*;

/**
 * Created by LYJ on 2016/9/27.
 */

@Table
@Entity
public class User {
    @GeneratedValue
    @Id
    private Integer id;
    @Column(name="USER_NAME")
    private String userName;

    @Column(name="USER_PASSWORD")
    private String password;

    @Column(name="USER_THIRD_ID")
    private String thirdId;




    public String getThirdId() {
        return thirdId;
    }

    public void setThirdId(String thirdId) {
        this.thirdId = thirdId;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

(2).repository JPA接口

package com.lyj.repository;

import com.lyj.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * Created by LYJ on 2016/9/27.
 */
public interface UserRepository extends JpaRepository<User,Integer> {
    public User findByUserName(String userName);
}

(3).service 为了方便次数省略接口世界写类,实际开发中需要面向接口编程

package com.lyj.service;

import com.lyj.entity.User;
import com.lyj.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by LYJ on 2016/9/27.
 */
@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;
    public User findByUserName(String userName){
        return userRepository.findByUserName(userName);
    }
}

(4).handler控制器

package com.lyj.handler;

import com.lyj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by LYJ on 2016/9/29.
 */
public class UserHandler {
    @Autowired
    private UserService userService;
    //链接映射匹配
    @RequestMapping("userLogin")
    public String userLogin(@RequestParam("username") String userName,
                            @RequestParam("password") String password){
        System.out.println(userName + "      --------- userName    --- UserHander.userLogin");
        System.out.println(password + "      --------- password    --- UserHander.userLogin");
        return "success";//跳转页面到success.jsp
    }
}

7.备注(上述:省略了index.jsp和success.jsp) 附上项目结构图

image

版权声明:本文为博主原创文章,未经博主允许不得转载。

java 搭建基于springboot的ssh(spring + springmvc + hibernate)的gradle项目(基础篇)

之前本人有写过一篇 java 搭建基于springboot的ssm(spring + springmvc + mybatis)的maven项目,从需求量上来看还是很多同学对springboot感兴...
  • liboyang71
  • liboyang71
  • 2017年07月25日 23:26
  • 3386

Gradle学习笔记 Gradle介绍和安装

Gradle介绍Gradle是一个现代的项目构建工具。如果你使用过Maven的话,就会明白项目构建工具的优点。但是Maven有一些缺点。由于Maven使用的是xml语言描述项目,虽然描述非常清晰,但是...
  • u011054333
  • u011054333
  • 2017年01月03日 21:33
  • 413

SpringBoot+SpringData实现SpringMVC简单项目

最近一段时间在学习《Spring in action》这本书。 为什么要使用Spring?因为可以减少Java开发的复杂性。 为什么要使用Spring Boot?为了简化Spring应用的搭建和开发过...
  • mrtif
  • mrtif
  • 2016年08月25日 16:25
  • 415

J2EE项目系列(三)--Spring Data JPA+Spring+SpringMVC+Maven快速开发(1)项目架构

好久没给大家弄Java后端项目系列了,因为接了Java后端外包以及学习一些后端优化的东西。而现在开始慢慢在这个系列的(三)逐渐把后端外包的一些有价值的东西分享给大家,并且自己也总结一遍嘛。本博客是这个...
  • Jack__Frost
  • Jack__Frost
  • 2017年03月21日 17:27
  • 3000

java web 项目框架 springmvc + Spring data jpa

  • 2017年03月08日 14:51
  • 687KB
  • 下载

eclipse 使用maven 搭建spring + springMVC + spring Data + jpa 框架

第一项目结构展示: 项目中所引用到的jar文件 4.0.0 springdataSample springdataSample 0.0.1-SNAPSHOT war ...
  • zhouzhiwengang
  • zhouzhiwengang
  • 2017年03月07日 02:29
  • 795

spring + springMVC + spring Data jpa + maven 项目框架搭建

首先看一下项目结构: 所用到的jar(pom.xml):
  • u012843873
  • u012843873
  • 2017年07月04日 16:51
  • 356

25.Maven 的单模块 / 多模块之 Spring MVC + Spring + Spring Data JPA 项目(基于 IntelliJ IDEA) - IntelliJ IDEA 使用教程

Maven 的单模块 / 多模块之 Spring MVC + Spring + Spring Data JPA 项目(基于 IntelliJ IDEA) 初衷 本章展示 IntelliJ IDEA...
  • x380481791
  • x380481791
  • 2016年05月04日 23:17
  • 1206

spring + springMVC + spring Data + jpa + maven +(Restful风格) 项目框架搭建(即sssp框架)

本文主要总结本人工作中曾用的web项目框架,由于全权交于我一人,故从头搭起,各种查资料,趟坑。。。 一度想照搬http://www.cnblogs.com/007sx/p/5658194.html上的...
  • weixin_36607803
  • weixin_36607803
  • 2016年12月07日 18:11
  • 456

手把手教你从最基本的Java工程搭建SpringMVC+SpringDataJPA+Hibernate(含源码下载)

转载请注明出处:http://blog.csdn.net/anxpp/article/details/51415366,谢谢!     本文会介绍从一个最基本的java工程,到Web工程,到集成...
  • snakeMoving
  • snakeMoving
  • 2017年10月19日 21:08
  • 157
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:IDEA+gradle+spring+springMVC+hibernate+JPA+SpringData的web项目
举报原因:
原因补充:

(最多只允许输入30个字)