首页 > 其他分享 >SpringMVC 项目中 创建SpringBoot,使用Hibernate和JPA

SpringMVC 项目中 创建SpringBoot,使用Hibernate和JPA

时间:2022-11-30 14:13:50浏览次数:39  
标签:Hibernate SpringBoot SpringMVC spring boot kintech springframework import org

起因:

老项目是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

相关文章

  • (4)SpringMVC核心配置文件详解
    在上一篇文章中,我们从DispatcherServlet谈起,最终为读者详细分析了SpringMVC的初始化主线的全部过程。整个初始化主线的研究,其实始终围绕着DispatcherServlet、WebApplicatio......
  • SpringBoot
    《静态资源导入》根据源码:静态资源放在下面的目录都可以:   ......
  • SpringMVC创建JSP页面的详细过程+配置模板+实现页面跳转+配置Tomcat。JSP和HTML配置模
    1、创建Maven项目2、填写项目基本信息、完成创建3、导入POM依赖打包方式设置为War包<packaging>war</packaging>依赖、可自行添加需要jar包依赖<dependencies>......
  • SpringBoot2 配置
    一.Properties与YamlSpringBoot支持properties与yaml两种配置文件application.properties/application.ymlyaml简单使用1.yaml简介yaml是以数据为中心的,比json,xml更适合做配......
  • 求超大文件上传方案( SpringBoot )
    ​ 这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数下面直接贴代码吧,一些难懂的我大部分都加上......
  • springboot2 生产部署注意事项【持续更新】
    注意事项1.去除不需要的jar开发工具jar:springs-boot-devtools2.监控一定要做好权限制或者去除 控制jar:spring-boot-starter-actuator监控druid的监控......
  • SpringBoot2 使用Spring Session集群
    有几种办法:1.扩展指定server利用Servlet容器提供的插件功能,自定义HttpSession的创建和管理策略,并通过配置的方式替换掉默认的策略。缺点:耦合Tomcat/Jetty等Servlet容器,不能......
  • SpringBoot2 web
    验证框架SpringBoot支持JSR-303,Bean等验证框架JSR-303JSR-303是Java的标准验证框架,已有实现Hibernatevalidator.JSR-303验证类型空检查@Null验证对象是否为null@Not......
  • SpringBoot JWT
    JWT是一种鉴权机制,实现前后端分离登陆和权限的一种解决方式,用户在登陆之后后端生成token传到前端,以后每次的请求都携带着token到后端验证,如果过期或者失效就要求重新登陆......
  • idea创建SpringBoot项目时,Type 选择 maven project 和maven pom 有什么区别?
    https://blog.csdn.net/bin_0203/article/details/121841615 在操作到Type选项时有四种项目类型可供选择:MavenProject(GenerateaMavenbasedprojectarchive)M......