<?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>
<!-- 自动生成相应的版本号 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bookshop</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 数据库连接 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 实现对数据的访问和操作 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 构建Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 在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<String> hobbies;
@ElementCollection
private List<Address> addresses;
@OneToMany(mappedBy = "author")
@OrderBy("book.name ASC")
private List<BookAuthor> books;
@OneToOne
private AuthorInfo info;
public AuthorInfo getInfo() {
return info;
}
public void setInfo(AuthorInfo info) {
this.info = info;
}
public List<BookAuthor> getBooks() {
return books;
}
public void setBooks(List<BookAuthor> books) {
this.books = books;
}
public Address getAddress() {
return address;
}
public List<Address> getAddresses() {
return addresses;
}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
public void setAddress(Address address) {
this.address = address;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> 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<String> hobbies;
@ElementCollection
private List<Address> addresses;
@OneToMany(mappedBy = "author")
@OrderBy("book.name ASC")
private List<BookAuthor> books;
@OneToOne
private AuthorInfo info;
public AuthorInfo getInfo() {
return info;
}
public void setInfo(AuthorInfo info) {
this.info = info;
}
public List<BookAuthor> getBooks() {
return books;
}
public void setBooks(List<BookAuthor> books) {
this.books = books;
}
public Address getAddress() {
return address;
}
public List<Address> getAddresses() {
return addresses;
}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
public void setAddress(Address address) {
this.address = address;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> 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;
}
}
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<Book,Long>, JpaSpecificationExecutor<Book> {
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());
}
}