SpringMVC Hibernate與JPA使用教程
起因:
老項(xiàng)目是SpringMVC,為了之后能使用更方便的SpringBoot。
所以在其中添加了SpringBoot項(xiàng)目,
但是老項(xiàng)目SpringMVC使用的Hibernate,SpringBoot希望使用JPA
解決方案如下:
一、POM
說(shuō)明:
1 spring boot標(biāo)簽中,是需要添加的架包
由于我的發(fā)布方式是tomcat,所以需要打包時(shí)過(guò)濾springboot中的tomcat
2 build標(biāo)簽中,是打包方式。這里可以不看
3 這里有一個(gè)jpa版本的問(wèn)題。
由于之前使用的jpa版本很低。這導(dǎo)致和springboot2.7.2版本的jpa版本不匹配。
其中,jpa2.7.2版本的findOne方法沒(méi)有了,改為了findById。
這導(dǎo)致調(diào)用基礎(chǔ)dao的方法會(huì)報(bào)錯(cuò):NoSuchMethod。
所以下面也提供了JPA的架包導(dǎo)入方式。如果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>
<!--對(duì)應(yīng)在src/main/resource包下創(chuàng)建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
三、啟動(dòng)類(lèi)
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;
//過(guò)濾hibernate jpa的自動(dòng)加載
@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")
//導(dǎo)入springmvc的applicationContext.xml
@ImportResource({"classpath:spring/applicationContext.xml"})
public class WebInvoiceApplication {
public static void main(String[] args) {
SpringApplication.run(WebInvoiceApplication.class, args);
}
}到此這篇關(guān)于SpringMVC Hibernate與JPA使用教程的文章就介紹到這了,更多相關(guān)SpringMVC Hibernate與JPA內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決RedisTemplate的key默認(rèn)序列化器的問(wèn)題
這篇文章主要介紹了解決RedisTemplate的key默認(rèn)序列化器的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
java數(shù)據(jù)結(jié)構(gòu)與算法之桶排序?qū)崿F(xiàn)方法詳解
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之桶排序?qū)崿F(xiàn)方法,結(jié)合具體實(shí)例形式詳細(xì)分析了桶排序的概念、原理、實(shí)現(xiàn)方法與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
JetBrains?發(fā)布下一代?IDE無(wú)比輕量幾秒就能啟動(dòng)干活
雖然?JetBrains?公司說(shuō)?Fleet?的定位和目標(biāo)并不是代替其他?IDE,但個(gè)人覺(jué)得,?如果?Fleet?火起來(lái)了,其他?IDE?就會(huì)黯然失色,特別是多語(yǔ)言開(kāi)發(fā)者,誰(shuí)愿意裝多個(gè)?IDE?呢?到時(shí)候,可能?JetBrains?以后的所有?IDE?要一統(tǒng)江湖了2021-12-12
基于Apache組件分析對(duì)象池原理的實(shí)現(xiàn)案例分析
本文從對(duì)象池的一個(gè)簡(jiǎn)單案例切入,主要分析common-pool2組件關(guān)于:池、工廠、配置、對(duì)象管理幾個(gè)角色的源碼邏輯,并且參考其在Redis中的實(shí)踐,對(duì)Apache組件分析對(duì)象池原理相關(guān)知識(shí)感興趣的朋友一起看看吧2022-04-04
SpringBoot?@Transactional事務(wù)不生效排查方式
這篇文章主要介紹了SpringBoot?@Transactional事務(wù)不生效排查方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
SpringSecurity請(qǐng)求授權(quán)規(guī)則配置與注解詳解
這篇文章主要介紹了SpringSecurity請(qǐng)求授權(quán)規(guī)則配置與注解詳解,我們常使用@Secured與@PreAuthorize兩個(gè)注解在進(jìn)入方法前進(jìn)行角色、權(quán)限的控制,進(jìn)入方法前數(shù)據(jù)的過(guò)濾@PreFilter注解偶爾會(huì)看到,需要的朋友可以參考下2023-12-12
hadoop的hdfs文件操作實(shí)現(xiàn)上傳文件到hdfs
這篇文章主要介紹了使用hadoop的API對(duì)HDFS上的文件訪問(wèn),其中包括上傳文件到HDFS上、從HDFS上下載文件和刪除HDFS上的文件,需要的朋友可以參考下2014-03-03
Spring @RestController注解組合實(shí)現(xiàn)方法解析
這篇文章主要介紹了Spring @RestController注解組合實(shí)現(xiàn)方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06

