• springboot整合jpa


    一、项目结构

    二、pom.xml

    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.example</groupId>
        <artifactId>A01springboot</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <!-- 父级依赖 -->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.3.4.RELEASE</version>
        </parent>
    
        <!-- springmvc和spring的jar包 -->
        <dependencies>
            <!--web相关-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--jpa-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.12</version>
                <scope>provided</scope>
            </dependency>
            <!-- MySql -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <!--测试-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <!-- maven项目指定编译器版本 -->
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.9</source>
                        <target>1.9</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>

    三、MyApplication

    package com.wuxi;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class MyApplication {
        //入口
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class, args);
        }
    }

    四、application.yml

    server:
      port: 8088 #端口号
      servlet:
        context-path: /wuxi #虚拟目录
    
    spring:
      datasource:
        url: jdbc:mysql://172.21.25.10:3306/sbssm?characterEncoding=utf8&useSSL=false
        username: root
        password: 123456
        driver-class-name: com.mysql.cj.jdbc.Driver
      jpa:
        database: MySQL
        show-sql: true
        generate-ddl: true
        hibernate:
          ddl-auto: update
          naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy

    五、bean

    package com.wuxi.bean;
    
    import lombok.Data;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    @Data
    @Entity
    public class Student {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer id;
        private String name;
        private Integer age;
    }

    六、repository

    package com.wuxi.repository;
    
    import com.wuxi.bean.Student;
    import org.springframework.data.jpa.repository.JpaRepository;
    
    import java.util.List;
    
    public interface StudentRepository extends JpaRepository<Student, Integer> {
        List<Student> findAll();
    }

    七、测试

    package com.wuxi.test;
    
    import com.wuxi.MyApplication;
    import com.wuxi.bean.Student;
    import com.wuxi.repository.StudentRepository;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import java.util.List;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = MyApplication.class)
    public class MyJpaTest {
        @Autowired
        private StudentRepository studentRepository;
    
        @Test
        public void test01() {
            List<Student> students = studentRepository.findAll();
            System.out.println(students);
        }
    }
  • 相关阅读:
    1.C和C++区别,以及const分析(底层const/顶层const)
    4.移植驱动到3.4内核-移植总结
    3.移植驱动到3.4内核-移植DM9000C驱动
    2.移植3.4内核-支持烧写yaffs2,裁剪内核并制作补丁
    Android Support v4、v7、v13、v14、v17的区别和应用场景
    Android利用canvas画各种图形
    ActionBar 自定义布局定义
    android动画坐标定义
    GitHub上最著名的Android播放器开源项目大全
    CardView 简介和使用
  • 原文地址:https://www.cnblogs.com/linding/p/13706280.html
Copyright © 2020-2023  润新知