亲宝软件园·资讯

展开

SpringMVC Hibernate与JPA使用教程

正怒月神 人气:0

起因:

老项目是SpringMVC,为了之后能使用更方便的SpringBoot。

所以在其中添加了SpringBoot项目,

但是老项目SpringMVC使用的Hibernate,SpringBoot希望使用JPA

解决方案如下:

一、POM

说明:

1 spring boot标签中,是需要添加的架包

由于我的发布方式是tomcat,所以需要打包时过滤springboot中的tomcat

2 build标签中,是打包方式。这里可以不看

3 这里有一个jpa版本的问题。

由于之前使用的jpa版本很低。这导致和springboot2.7.2版本的jpa版本不匹配。

其中,jpa2.7.2版本的findOne方法没有了,改为了findById。

这导致调用基础dao的方法会报错:NoSuchMethod。

所以下面也提供了JPA的架包导入方式。如果JPA版本是匹配的,那直接使用下面注释的

spring-boot-starter-data-jpa

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.14.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.kintech</groupId>
    <packaging>war</packaging>
    <artifactId>kintech.webInvoice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>kintech.webInvoice</name>
    <description>kintech.webInvoice</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring-boot.version>2.1.1.RELEASE</spring-boot.version>
        <fastjson.version>2.0.10</fastjson.version>
        <spring-cloud.version>2021.0.3</spring-cloud.version>
    </properties>
    <dependencies>
        <!--        srping boot begin-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <!--        srping boot end-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        -->
        <!--  JPA  -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.12.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.4.12.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>5.4.12.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
            <version>1.13.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.11.13.RELEASE</version>
        </dependency>
        <!--  JPA  -->
        <!--        kintech begin-->
        <dependency>
            <groupId>com.kintech</groupId>
            <artifactId>kintech.web</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.kintech</groupId>
            <artifactId>kintech.common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <exclusions>
                <exclusion>
                    <groupId>redis.clients</groupId>
                    <artifactId>jedis</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-data-redis</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.session</groupId>
                    <artifactId>spring-session</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.commons</groupId>
                    <artifactId>commons-pool2</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.kintech</groupId>
            <artifactId>kintech.service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!--        kintech end-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.9</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <finalName>${project.name}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/assembly.xml</descriptor>
                        <!--对应在src/main/resource包下创建buildzip.xml配置文件-->
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <skip>true</skip>
                    <!-- <skipTests>true</skipTests> -->
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

二、引入springMVC中的applicationContext.xml

1 先看一下springmvc中的xml

将其拷贝到springboot中的resources下

2 application.yml (springboot)

添加如下:

spring:
  main:
    allow-bean-definition-overriding: true

三、启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
//过滤hibernate jpa的自动加载
@SpringBootApplication(exclude = {HibernateJpaAutoConfiguration.class})
//扫描包
@ComponentScan(basePackages={"com.kintech"},lazyInit=true)
//扫描jpa的dao
@EnableJpaRepositories(basePackages = {"com.kintech.dao.jpa"})
//扫描jpa 用到的entity
@EntityScan("com.kintech.model.domain")
//导入springmvc的applicationContext.xml
@ImportResource({"classpath:spring/applicationContext.xml"})
public class WebInvoiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(WebInvoiceApplication.class, args);
    }
}

加载全部内容

相关教程
猜你喜欢
用户评论