起因:
老项目是SpringMVC,为了之后能使用更方便的SpringBoot。
所以在其中添加了SpringBoot项目,
但是老项目SpringMVC使用的Hibernate,SpringBoot希望使用JPA
解决方案:
一 POM
说明:
1 spring boot标签中,是需要添加的架包
由于我的发布方式是tomcat,所以需要打包时过滤springboot中的tomcat
2 build标签中,是打包方式。这里可以不看
<?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>2.7.2</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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- srping boot end--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 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--> </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下
三 启动类
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); } }
标签:Hibernate,SpringBoot,SpringMVC,spring,boot,kintech,springframework,import,org From: https://www.cnblogs.com/hanjun0612/p/16938223.html