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);
        }
    }
  • 相关阅读:
    css3动画之1--animation小例子
    炎炎夏日,走入美妙的前端设计案例
    模拟腾讯、携程、百度音乐 移动端图片切换第一版
    仿QQ空间长图效果简易版--母亲节感恩
    001-搭建框架
    javascript事件绑定1-模拟jquery可爱的东西
    图片尺寸
    mvc3结合spring.net-依赖注入
    *创建索引初步
    Lucene的分词_中文分词器介绍
  • 原文地址:https://www.cnblogs.com/linding/p/13706280.html
Copyright © 2011-2022 走看看