SpringBoot-Maven 实现简答查找(增删改用法差不多)

  • 导入 Jar 到 pom.xml 文件


    4.0.0

    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.6.RELEASE
         
    
    com.bookshop
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
    

    
        
        
            mysql
            mysql-connector-java
        

        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



  • 在application.properties配置数据库文件
######数据库链接配置########
spring.datasource.url = jdbc:mysql://localhost:3306/bookshop?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#开启数据库映射
spring.jpa.generate-ddl=true
#自定义数据库名字
spring.jpa.hibernate.naming.implicit-strategy=com.bookshop.support.BookNamingStrategy
#把SQL打到控制台
spring.jpa.show-sql=true
#SQL打到控制台的时候 格式化
spring.jpa.properties.hibernate.format_sql=true
#指出是什么操作生成了该语句
#spring.jpa.properties.hibernate.use_sql_comments=true

  • java文件下创建domain映射数据库,这里举几个例子
package com.bookshop.domain;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

@Entity
public class Author extends DomainImpl {

    private String name;

    //设置字段长度
    @Column(columnDefinition = "INT(3)")
    private int age;

    //只要日期
    @Temporal(TemporalType.DATE)
    private Date birthday;

    //设置枚举,数据库字段类型
    @Enumerated(EnumType.STRING)
    private Sex sex;

    //插入式对象
    @Embedded
    private Address address;

    //集合
    @ElementCollection
    private List hobbies;

    @ElementCollection
    private List
addresses; @OneToMany(mappedBy = "author") @OrderBy("book.name ASC") private List books; @OneToOne private AuthorInfo info; public AuthorInfo getInfo() { return info; } public void setInfo(AuthorInfo info) { this.info = info; } public List getBooks() { return books; } public void setBooks(List books) { this.books = books; } public Address getAddress() { return address; } public List
getAddresses() { return addresses; } public void setAddresses(List
addresses) { this.addresses = addresses; } public void setAddress(Address address) { this.address = address; } public List getHobbies() { return hobbies; } public void setHobbies(List hobbies) { this.hobbies = hobbies; } public Sex getSex() { return sex; } public void setSex(Sex sex) { this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
package com.bookshop.domain;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

@Entity
public class Author extends DomainImpl {

    private String name;

    //设置字段长度
    @Column(columnDefinition = "INT(3)")
    private int age;

    //只要日期
    @Temporal(TemporalType.DATE)
    private Date birthday;

    //设置枚举,数据库字段类型
    @Enumerated(EnumType.STRING)
    private Sex sex;

    //插入式对象
    @Embedded
    private Address address;

    //集合
    @ElementCollection
    private List hobbies;

    @ElementCollection
    private List
addresses; @OneToMany(mappedBy = "author") @OrderBy("book.name ASC") private List books; @OneToOne private AuthorInfo info; public AuthorInfo getInfo() { return info; } public void setInfo(AuthorInfo info) { this.info = info; } public List getBooks() { return books; } public void setBooks(List books) { this.books = books; } public Address getAddress() { return address; } public List
getAddresses() { return addresses; } public void setAddresses(List
addresses) { this.addresses = addresses; } public void setAddress(Address address) { this.address = address; } public List getHobbies() { return hobbies; } public void setHobbies(List hobbies) { this.hobbies = hobbies; } public Sex getSex() { return sex; } public void setSex(Sex sex) { this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
package com.bookshop.domain;

import javax.persistence.*;
import java.util.Date;

@MappedSuperclass
public class DomainImpl {
    @Id
    @GeneratedValue
    private Long id;

    @Temporal(TemporalType.DATE)
    private Date createdTime = new Date();

    public Date getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

  • 接下来写repository层 相当于 Dao层
package com.bookshop.repository;

import com.bookshop.domain.Book;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.List;

public interface BookRepository extends JpaRepository, JpaSpecificationExecutor {
    Book findByName(String bookname);

    Book findById(long id);
}

  • Java 下定义入口文件 BookShopApplication
package com.bookshop;

import com.bookshop.repository.BookRepository;
import com.bookshop.support.BookShopRepositoryImpl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories(repositoryBaseClass = BookShopRepositoryImpl.class)
public class BookShopApplication {

    public static void main(String [] args) {
        SpringApplication.run(BookShopApplication.class, args);
    }

}

  • 测试映射数据库
package com.bookshop;

import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

//Test启动
@RunWith(SpringRunner.class)
//指定测试类
@SpringBootTest(classes = BookShopApplication.class)
//事务,对数据改变进行回滚
@Transactional
public class BaseTest {

}

  • 最后测试查找
package com.bookshop.repository;

import com.bookshop.BaseTest;
import com.bookshop.domain.Book;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class RepositoryTest extends BaseTest {

    @Autowired
    private BookRepository bookRepository;

    @Test
    public void test1(){
        Book book = bookRepository.findById(1L);
        book.setName("美女与野兽");
        bookRepository.save(book);
        System.out.println(book.getName());
    }
}

你可能感兴趣的:(SpringBoot)