zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    [转贴] IPSEC From 知乎
    intel 的架构图
    IPV6 简单验证
    exsi6.0远程修改密码
    Oracle单个datafile大小的限制
    用Linux完成Oracle自动物理备份
    vSphere Client 更改 ESX/ESXi 主机的端口
    netstat Recv-Q和Send-Q
    Linux下安装(卸载)KDE和GNOME
    完美解决xhost +报错: unable to open display ""
  • 原文地址:https://www.cnblogs.com/linding/p/13706280.html
Copyright © 2011-2022 走看看