• SpringBoot + JPA(Hibernate)环境快速搭建


    1、导入相关Jar包

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    </dependencies>
    
    

    2、编写application.properties配置文件

    #通用数据源配置
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://118.31.170.247:3306/test?charset=utf8mb4&useSSL=false
    spring.datasource.username=root
    spring.datasource.password=
    # Hikari 数据源专用配置
    spring.datasource.hikari.maximum-pool-size=20
    spring.datasource.hikari.minimum-idle=5
    # JPA 相关配置
    spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
    spring.jpa.show-sql=true
    spring.jpa.hibernate.ddl-auto=update
    

    3、编写实体类

    DaySeason.java

    @Table(name = "day_season")
    @Entity
    public class DaySeason implements Serializable {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id ;
    
        @NotNull
        @Column(name = "uuid", nullable = false)
        private String uuid;
    
     
        @OneToMany(mappedBy = "daySeason")
        private List<DaySeasonMember> daySeasonMember ;
    }
    

    DaySeasonMember.java

    
    @Table(name = "day_season_member")
    @Entity
    public class DaySeasonMember implements Serializable {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id ;
    
        @NotNull
        @Column(name = "uuid", nullable = false)
        private String uuid;
    
        @ManyToOne(targetEntity = DaySeason.class)
        @JoinColumn(name="day_season_id",referencedColumnName ="id")
        private DaySeason daySeason;
    }
    
    
    

    4、编写接口

    DaySeasonDao.java

    @Repository
    public interface DaySeasonDao extends JpaRepository<DaySeason,Long> {
    }
    

    DaySeasonMemberDao.java

    @Repository
    public interface DaySeasonMemberDao extends JpaRepository<DaySeasonMember,Long> {
    }
    

    5、测试

    	@Autowired
        private DaySeasonMemberDao daySeasonMemberDao ;
    
        @Autowired
        private DaySeasonDao daySeasonDao ;
    
    	@Test
        public void test1() throws IOException, JRException {
            List<DaySeasonMember> all = daySeasonMemberDao.findAll();
            List<DaySeason> all1 = daySeasonDao.findAll();
            all.forEach((daySeasonMember)->{
                System.out.println(daySeasonMember);
            });
        }
    
  • 相关阅读:
    C#用递归求阶乘 n!
    视图与表的区别和联系(小结)
    ref 和 out 的异同
    什么是重载?(最文艺的理解)
    [windows c]关于winsock2.h的recv函数的记录
    [windows c]关于指针函数和参数可变函数同时应用情况的疑问
    [windows c]CreateProcess
    c++ boost asio库初学习
    c#中ObservableCollection<T>排序方法
    安装opensuse时遇到的一些问题
  • 原文地址:https://www.cnblogs.com/xiaolaodi1999/p/14242436.html
Copyright © 2020-2023  润新知