zoukankan      html  css  js  c++  java
  • maven 构建spring boot + mysql 的基础项目

    一、maven 依赖

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.4.1.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
    
        </dependencies>

    二、配置文件 application.properties

    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/sbtest
    spring.datasource.username=root
    spring.datasource.password=1234
    spring.jpa.database=mysql
    spring.jpa.show-sql=true
    spring.jpa.hibernate.ddl-auto=update
    spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy

    三、建立Entity和Repository

    1、Book 实体类

    package com.casic.entity;
    
    import javax.persistence.*;
    import java.util.List;
    
    @Entity
    public class Book {
        @Id
        @GeneratedValue
        private Long id;
        private String isbn;
        private String title;
        private String description;
    
        @ManyToOne
        private Author author;
        @ManyToOne
        private Publisher publisher;
    
        @ManyToMany
        private List<Reviewer> reviewers;
    
        protected Book() { }
    
        public Book(Author author, String isbn, Publisher publisher, String title) {
            this.author = author;
            this.isbn = isbn;
            this.publisher = publisher;
            this.title = title;
        }
    }

    2、Author 实体类

    package com.casic.entity;
    
    import javax.persistence.*;
    import java.util.List;
    
    @Entity
    public class Author {
        @Id
        @GeneratedValue
        private Long id;
        private String firstName;
        private String lastName;
        @OneToMany(mappedBy = "author")
        private List<Book> books;
    
        protected Author() {
    
        }
    
        public Author(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }
    }

    3、Publisher 实体类

    package com.casic.entity;
    
    import javax.persistence.*;
    import java.util.List;
    
    @Entity
    public class Publisher {
        @Id
        @GeneratedValue
        private Long id;
        private String name;
    
        @OneToMany(mappedBy = "publisher")
        private List<Book> books;
    
        protected Publisher() { }
    
        public Publisher(String name) {
            this.name = name;
        }
    
    }

    4、Reviewer

    package com.casic.entity;
    import javax.persistence.*;
    @Entity
    public class Reviewer {
        @Id
        @GeneratedValue
        private Long id;
        private String firstName;
        private String lastName;
    
        protected Reviewer() { }
    
        public Reviewer(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }
    }

    5、BookRepository 

    package com.casic.repository;
    
    
    import com.casic.entity.Book;
            import org.springframework.data.repository.CrudRepository;
            import org.springframework.stereotype.Repository;
    
    @Repository
    public interface BookRepository extends CrudRepository<Book, Long> {
        Book findBookByIsbn(String isbn);
    }

    6、MyStartupRunner 命令行启动类

    package com.casic.service;
    
    import com.casic.repository.BookRepository;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.stereotype.Component;
    
    /**
     * 服务启动执行
     * @author   oftenlin
     */
    @Component
    public class MyStartupRunner implements CommandLineRunner {
    
        @Autowired
        private BookRepository bookRepository;
        @Override
        public void run(String... args) throws Exception {
            System.out.print("book count:"+bookRepository.count());
        }
    
    }

    7、启动类 MyApp 

    package com.casic;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
    import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
    
    @SpringBootApplication
    public class MyApp{
    
        public static void main(String args[]) {
            SpringApplication.run(MyApp.class, args);
        }
    }
  • 相关阅读:
    查漏补缺:QT入门
    添砖加瓦:设计模式(工厂方法模式)
    Luogu 4784 [BalticOI 2016 Day2]城市
    Luogu 1606 [USACO07FEB]白银莲花池Lilypad Pond
    Luogu 3698 [CQOI2017]小Q的棋盘
    CF547D Mike and Fish
    Luogu 3066 [USACO12DEC]逃跑的BarnRunning Away From…
    Luogu 2403 [SDOI2010]所驼门王的宝藏
    NEERC17 J Journey from Petersburg to Moscow
    Luogu 3350 [ZJOI2016]旅行者
  • 原文地址:https://www.cnblogs.com/oftenlin/p/6553920.html
Copyright © 2011-2022 走看看