springboot学习(八) 使用jpa访问数据库
1、添加maven依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2、添加数据库连接信息
spring.datasource.url=jdbc:mysql://localhost:3309/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.hbm2ddl.auto是hibernate的配置属性,其主要作用是:自动创建、更新、验证数据库表结构。
该参数的几种配置如下:
create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。create-drop:每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除。update:最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等应用第一次运行起来后才会。validate:每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。
3、创建实体类
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; @Entity
public class User { @Id
@GeneratedValue
private Long id; private String name; ....... }
4、创建数据访问接口
import cn.origal.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param; /**
* Created by Administrator on 2017/9/24.
*/
public interface UserRepository extends JpaRepository<User, Long> { User findUserById(long id); @Query("from User u where u.name = :name")
List<User> findUser(@Param("name") String name); }
5、进行单元测试
import cn.origal.Application;
import cn.origal.dao.UserRepository;
import cn.origal.domain.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; @RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringApplicationConfiguration(Application.class)
public class TestUser { @Autowired
private UserRepository userRepository; @Test
public void test() throws Exception { userRepository.save(new User("1"));
userRepository.save(new User("2"));
userRepository.save(new User("3")); System.out.println(userRepository.findUser("3"));
System.out.println(userRepository.findUserById(1)); } }
springboot学习(八) 使用jpa访问数据库的更多相关文章
- springboot学习-jdbc操作数据库--yml注意事项--controller接受参数以及参数校验--异常统一管理以及aop的使用---整合mybatis---swagger2构建api文档---jpa访问数据库及page进行分页---整合redis---定时任务
springboot学习-jdbc操作数据库--yml注意事项--controller接受参数以及参数校验-- 异常统一管理以及aop的使用---整合mybatis---swagger2构建api文档 ...
- JPA访问数据库的几种方式
JPA访问数据库的几种方式 本文为原创,转载请注明出处:https://www.cnblogs.com/supiaopiao/p/10901793.html 1. Repository 1.1. 通过 ...
- Springboot 系列(十)使用 Spring data jpa 访问数据库
前言 Springboot data jpa 和 Spring jdbc 同属于 Spring开源组织,在 Spring jdbc 之后又开发了持久层框架,很明显 Spring data jpa 相对 ...
- 十八、springboot中hibernate配置sessionFactory访问数据库
前提 在yml或properties文件中配置数据库与数据库连接池 Hibernate配置 几种方式: 方式一: @Configuration public class HibernateConfig ...
- SpringBoot学习(八)-->SpringBoot之过滤器、监听器
本文将直接使用@WebFilter和@WebListener的方式,完成一个Filter 和一个 Listener. 过滤器(Filter)和 监听器(Listener)的注册方法和 Servlet ...
- Spring Boot2 系列教程 (七) | 使用 Spring Data JPA 访问 Mysql
前言 如题,今天介绍 Spring Data JPA 的使用. 什么是 Spring Data JPA 在介绍 Spring Data JPA 之前,首先介绍 Hibernate . Hibernat ...
- SpringBoot学习笔记(10):使用MongoDB来访问数据
SpringBoot学习笔记(10):使用MongoDB来访问数据 快速开始 本指南将引导您完成使用Spring Data MongoDB构建应用程序的过程,该应用程序将数据存储在MongoDB(基于 ...
- SpringBoot学习笔记(9)----SpringBoot中使用关系型数据库以及事务处理
在实际的运用开发中,跟数据库之间的交互是必不可少的,SpringBoot也提供了两种跟数据库交互的方式. 1. 使用JdbcTemplate 在SpringBoot中提供了JdbcTemplate模板 ...
- SpringBoot学习(3)-SpringBoot添加支持CORS跨域访问
SpringBoot学习(3)-SpringBoot添加支持CORS跨域访问 https://blog.csdn.net/yft_android/article/details/80307672
随机推荐
- 使用es索引遇到的问题记录
1设置es索引的运行内存: 直接在启动文件里面改就好,启动命令是elasticsearch.bat,用notepad++编辑这个文件,里面添加这样的一行:SET ES_HEAP_SIZE=10g即可 ...
- 训练指南 UVA- 11865(有向最小生成树 + 朱刘算法 + 二分)
layout: post title: 训练指南 UVA- 11865(有向最小生成树 + 朱刘算法 + 二分) author: "luowentaoaa" catalog: tr ...
- 微信小程序开发教程(三)项目目录及文件构成
第二章我们已经创建了一个Hello WXapplet示例小程序.我们从文件目录结构来了解Hello WXapplet项目的构成. 目录结构显示,在小程序项目的根目录下面包含3个app开头的文件(app ...
- 【并查集】bzoj2054 疯狂的馒头
因为只有最后被染上的颜色会造成影响,所以倒着处理,用并查集维护已经染色的区间的右端点,即fa[i]为i所在的已染色区间的右端点,这样可以保证O(n)的复杂度. #include<cstdio&g ...
- 【点分治】bzoj2152 聪聪可可
模板题. #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> ...
- 在WPF中合并两个ObservableCollection
WPF中的ObservableCollection是一个非常常用的集合对象,我们可以通过将它绑定到ListBox之类的集合控件上时,当集合发生变更时,会同步更新到界面上.但是,有的时候我们需要合并两个 ...
- Getting terminal width in C?
转:http://stackoverflow.com/questions/1022957/getting-terminal-width-in-c 方法一: #include <sys/ioctl ...
- Shiro+SpringMVC 实现更安全的登录(加密匹配&登录失败超次数锁定帐号)
原文:http://blog.csdn.net/wlwlwlwl015/article/details/48518003 前言 初学shiro,shiro提供了一系列安全相关的解决方案,根据官方的介绍 ...
- Android ProgressBar手动控制开始和停止
这两天有个需求,点击按钮从SD卡解压压缩包,并读取压缩包内txt文档内容,然后在街面上显示出来.毕竟IO操作很耗时,如果文件较大会花费不少时间.所以,在处理数据的时候能给个进度就好了.我们通常的做法就 ...
- HashMap深度解析(一)
HashMap可以说是Java中最常用的集合类框架之一,是Java语言中非常典型的数据结构,我们总会在不经意间用到它,很大程度上方便了我们日常 开发.在很多Java的笔试题中也会被问到,最常见的,“H ...