1. 快速地创建一个项目,pom中选择如下

<?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.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <groupId>com.example</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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <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> <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> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

  

2. 新建一个实体,这边以Book对象为例,主要囊括了书店中书的一些信息

package com.example.demo.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.io.Serializable; @Entity
public class Book implements Serializable { public static final long seriaVersionUID = 1L; @Id
@GeneratedValue
private long id; @Column(nullable=false,unique=true)
private String name; @Column(nullable=false)
private String author; @Column(nullable=false)
private String ontime; @Column(nullable=false)
private int number; @Column(nullable=false)
private float price; @Column(nullable=false)
private String createdt;
@Column(nullable=false)
private String updatedt;
@Column(nullable=false)
private String creatuid;
@Column(nullable=false)
private String updateuid; public long getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAuthor() {
return author;
} public void setAuthor(String author) {
this.author = author;
} public String getOntime() {
return ontime;
} public void setOntime(String ontime) {
this.ontime = ontime;
} public int getNumber() {
return number;
} public void setNumber(int number) {
this.number = number;
} public float getPrice() {
return price;
} public void setPrice(float price) {
this.price = price;
} public String getCreatedt() {
return createdt;
} public void setCreatedt(String createdt) {
this.createdt = createdt;
} public String getUpdatedt() {
return updatedt;
} public void setUpdatedt(String updatedt) {
this.updatedt = updatedt;
} public String getCreatuid() {
return creatuid;
} public void setCreatuid(String creatuid) {
this.creatuid = creatuid;
} public String getUpdateuid() {
return updateuid;
} public void setUpdateuid(String updateuid) {
this.updateuid = updateuid;
} protected Book(){super();}; public Book(String name, String author, String ontime, int number, float price, String createdt, String updatedt, String creatuid, String updateuid) {
this.name = name;
this.author = author;
this.ontime = ontime;
this.number = number;
this.price = price;
this.createdt = createdt;
this.updatedt = updatedt;
this.creatuid = creatuid;
this.updateuid = updateuid;
} @Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
", ontime='" + ontime + '\'' +
", number=" + number +
", price=" + price +
", createdt='" + createdt + '\'' +
", updatedt='" + updatedt + '\'' +
", creatuid='" + creatuid + '\'' +
", updateuid='" + updateuid + '\'' +
'}';
}
}

  

3. 新建一个BookRepository接口,且继承JpaRepository

public interface BookRepository extends JpaRepository<Book, Long> {

}

  

4. 了解hibernate的ddl-auto四种模式和用途

# 服务端口
server.port=8083 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=validate

 

spring.jpa.hibernate.ddl-auto = 可以是4中Value
create
create-drop
validate
update

Hibernate我们公司的正式项目中基本不用,原因是性能上不是很好,不知道其他公司使用起来如何?

hibernate在多线程下,拼接sql,知道数据库有问题,却很难定位到是哪一段sql。后来渐渐弃用。用mybaits了~

5. 添加controller进行新增数据

package com.example.demo.controller;

import com.example.demo.entity.Book;
import com.example.demo.entity.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController
public class BookController { @Autowired
private BookRepository bookRepository; @RequestMapping("/save")
@ResponseBody
public void save() {
bookRepository.save(new Book("书名11", "作者", "上架时间", 100, 23, "创建时间", "更新时间", "创建uid", "更新uid"));
bookRepository.save(new Book("书名12", "作者", "20001111", 100, 23, "19990111", "19990111", "3", "4"));
bookRepository.save(new Book("西游记10", "吴承恩", "19991211", 100, 20, "19991111", "19991111", "2", "3"));
} }

  项目启动起来,在浏览器中输入 http://localhost:8083/save,查询数据库中,已经新增了三条记录;

可以顺带测试下ddl-auto的四种值,是否有如同描述一般的作用

6. 再添加查询方法,并在controller中进行查询

package com.example.demo.entity;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface BookRepository extends JpaRepository<Book, Long> {

    List<Book> findByAuthor(String author);
}

  再在BookRepository 中添加查找方法,根据作者查询出符合的Book 列表

package com.example.demo.controller;

import com.example.demo.entity.Book;
import com.example.demo.entity.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController
public class BookController { @Autowired
private BookRepository bookRepository; @RequestMapping("/save")
public void save() {
bookRepository.save(new Book("书名11", "作者", "上架时间", 100, 23, "创建时间", "更新时间", "创建uid", "更新uid"));
bookRepository.save(new Book("书名12", "作者", "20001111", 100, 23, "19990111", "19990111", "3", "4"));
bookRepository.save(new Book("西游记10", "吴承恩", "19991211", 100, 20, "19991111", "19991111", "2", "3"));
} @RequestMapping("/findByAuthor")
public void findByAuthor() {
List<Book> result = bookRepository.findByAuthor("作者");
System.out.println("一共有多少条呢----" + result.size());
for (Book book : result) {
System.out.println(book);
}
System.out.println("================================");
}
}

  保存,启动新项目,在浏览器中输入http://localhost:8083/findByAuthor,查看控制台是否打印出Book信息

Spring Boot的数据访问 之Spring Boot + jpa的demo的更多相关文章

  1. Spring Boot的数据访问:CrudRepository接口的使用

    示例 使用CrudRepository接口访问数据 创建一个新的Maven项目,命名为crudrepositorytest.按照Maven项目的规范,在src/main/下新建一个名为resource ...

  2. (8)Spring Boot 与数据访问

    文章目录 简介 整合基本的JDBC与数据源 整合 druid 数据源 整合 mybatis 简介 对于数据访问层,无论是 SQL 还是 NOSQL ,Spring Boot 默认都采用整合 Sprin ...

  3. Spring Boot实现数据访问计数器

    1.数据访问计数器   在Spring Boot项目中,有时需要数据访问计数器.大致有下列三种情形: 1)纯计数:如登录的密码错误计数,超过门限N次,则表示计数器满,此时可进行下一步处理,如锁定该账户 ...

  4. Spring Boot框架 - 数据访问 - 整合Mybatis

    一.新建Spring Boot项目 注意:创建的时候勾选Mybatis依赖,pom文件如下 <dependency> <groupId>org.mybatis.spring.b ...

  5. Spring Boot框架 - 数据访问 - JDBC&自动配置

    一.新建Spring Boot 工程 特殊勾选数据库相关两个依赖 Mysql Driver — 数据库驱动 Spring Data JDBC 二.配置文件application.properties ...

  6. Spring 梳理-数据访问-DB

    针对接口编程 DAO是指数据访问对象(data access object),它提供了数据读取和写入到数据库中的一种方式.Spring认为,它应该以接口的方式发布功能,而应用程序的其他部分需要通过接口 ...

  7. Spring Boot 揭秘与实战(二) 数据存储篇 - 数据访问与多数据源配置

    文章目录 1. 环境依赖 2. 数据源 3. 单元测试 4. 源代码 在某些场景下,我们可能会在一个应用中需要依赖和访问多个数据源,例如针对于 MySQL 的分库场景.因此,我们需要配置多个数据源. ...

  8. Spring Boot 数据访问集成 MyBatis 与事物配置

    对于软件系统而言,持久化数据到数据库是至关重要的一部分.在 Java 领域,有很多的实现了数据持久化层的工具和框架(ORM).ORM 框架的本质是简化编程中操作数据库的繁琐性,比如可以根据对象生成 S ...

  9. Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅

    在上一篇Spring中使用JdbcTemplate访问数据库中介绍了一种基本的数据访问方式,结合构建RESTful API和使用Thymeleaf模板引擎渲染Web视图的内容就已经可以完成App服务端 ...

随机推荐

  1. StructureStreaming与kafka集成读取数据必要的jar包

    <dependency> <!--structurStreaming读取kafka1.0以下必须的jar--> <groupId>org.apache.spark& ...

  2. django项目----函数和方法的区别

    一.函数和方法的区别 1.函数要手动传self,方法不用传 2.如果是一个函数,用类名去调用,如果是一个方法,用对象去调用 举例说明: class Foo(object): def __init__( ...

  3. Tomcat配置Manager管理员

    修改文件: D:\MyDev\Tomcat\apache-tomcat-7.0.68\conf\tomcat-users.xml  配置内容: <role rolename="mana ...

  4. linux操作文件和文件夹

    rm filerm -rf folder如将/test1目录下的file1复制到/test3目录,并将文件名改为file2,可输入以下命令:cp /test1/file1 /test3/file2 如 ...

  5. Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable) E: Unable to lock the administration di

    alexander@alexander-virtual-machine:~$ sudo apt-get install -y httpdE: Could not get lock /var/lib/d ...

  6. daemon进程fork一次和fork两次的区别?

    守护进程也称为精灵进程(Daemon),是运行在后台的一种特殊的进程.它独立于控制终端并且周期性的执行某种任务负等待处理某些发生的事件.因为他们没有控制终端,所以说他们是在后台运行的. 守护进程的特点 ...

  7. oracle parallel_index hint在非分区表的生效

    之前没特别注意,在有些场景下希望使用并行索引扫描的时候,发现parallel_index hint并没有生效,于是抽空看了下文档:The PARALLEL_INDEX hint instructs t ...

  8. openstack tap complete

    $ openstack complete > /etc/bash_completion.d/osc.bash_completion re-login and bash

  9. ldap集成nginx

    nginx版本:1.10.2 nginx安装: wget http://nginx.org/download/nginx-1.10.2.tar.gz tar zxvf nginx-1.10.2.tar ...

  10. Base64编码为什么会使数据量变大?

    当把byte[]通过Convert.ToBase64String转换成Base64编码字符串时数据量明显变大,为何呢?这里就得先探究一下什么是Base64编码. Base64编码的思想是是采用64个基 ...