spring jpa 学习笔记(一) 之集成
一、pom 配置
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.module.demo</groupId>
<artifactId>module-parent</artifactId>
<version>${module.version}</version>
<relativePath>../../module-parent/pom.xml</relativePath>
</parent>
<artifactId>module-jpa-dao</artifactId>
<name>module-jpa-dao</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</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> //引入JPA和数据库驱动
<dependency>
<groupId>com.module.demo</groupId>
<artifactId>module-dao-interface</artifactId>
</dependency>
</dependencies>
</project>
二、Jpa Tool 将数据库表生成 实体对象
由于数据库表格较多,且实体对象编写需要在了解并深入学习了 jpa 实体对象方面的知识后,才能正确配置,这样给集成工作带来了很大的难度,基于此,为了减少集成成本,使用了 eclipse 中的 JPA tool 工具直接将 数据库表格映射成实体类。
1、eclipse 建立 Database Connection
(1)点击 eclipse 中的 window --> show view ---> other ---> Data Management --> Data Source Explorer 打开 数据库连接管理视图


(2)建立数据库连接
这里使用的是 mysql。




Data Source Explorer 出现该视图表示 数据库连接已经建立完毕。
(3) 为项目添加 JPA Tool
选择项目,右键 选择 properties


(4) 通过(3)给项目添加了 JPA Tool,此时通过 JPA TOOL 生成实体类

经过以上步骤, 在 com.module.demo.dap.jpa.entity 下面就生成了对应的实体类,如:
package com.module.demo.dao.jpa.entity; import java.io.Serializable;
import javax.persistence.*; /**
* The persistent class for the student database table.
*
*/
@Entity
@NamedQuery(name="Student.findAll", query="SELECT s FROM Student s")
public class Student implements Serializable {
private static final long serialVersionUID = 1L; @Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id; private String name; //bi-directional many-to-one association to Timetable
@ManyToOne
@JoinColumn(name="timaeable")
private Timetable timetable; public Student() {
} public int getId() {
return this.id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public Timetable getTimetable() {
return this.timetable;
} public void setTimetable(Timetable timetable) {
this.timetable = timetable;
} }
三、JPA配置
(1) 配置 dao 层(即配置数据库访问层的仓库)
package com.module.demo.dao.jpa.repository;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.module.demo.dao.jpa.entity.Student;
public interface StudentRepository extends PagingAndSortingRepository<Student, Integer> { //一定要继承
}
(2)经过以上配置,需要配置 jpa 实体类扫描和 仓库扫描
配置扫描的方式有两种,一种使用 JPA的注解;另一种使用通用注解 a) JPA的注解
package com.module.demo.dao; import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @Configuration
@PropertySource("classpath:jpa.properties") //JPA 个性化配置
@ComponentScan(basePackageClasses=JpaConfiguration.class)
@EnableJpaRepositories //指明扫描的dao层,在这里没有配置具体的包,默认会在当前类所属包及其子包中进行扫描
@EntityScan //指明扫描的实体类,在这里没有配置具体的包,默认会在当前类所属包及其子包中进行扫描
public class JpaConfiguration {
}
b) 通用注解
- 在 application.yml 文件中添加注解 spring.main.allow-bean-definition-overriding=true;
spring:
application:
name: web
profiles:
active: "@package.env@"
# datasource:
# type: com.alibaba.druid.pool.DruidDataSource
main:
allow-bean-definition-overriding: true //允许bean定义覆盖 server:
port: 8090
servlet:
context-path: /web
- 在 配置类中添加 @EnableAutoConfiguration
@Configuration
@PropertySource("classpath:jpa.properties")
@ComponentScan(basePackageClasses=JpaConfiguration.class)
/*@EnableJpaRepositories
@EntityScan*/
@EnableAutoConfiguration //自动根据classpath 加载对应配置类
public class JpaConfiguration {
}
spring jpa 学习笔记(一) 之集成的更多相关文章
- Spring JPA学习笔记
目录 什么是JPA? 引入配置 新建一个Entity Bean类 JPA的增删改查 新建操作接口 新建测试类 总结 什么是JPA? 什么是JDBC知道吧?数据库有Mysql,SQL Server,Or ...
- Spring Boot学习笔记2——基本使用之最佳实践[z]
前言 在上一篇文章Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用已经对Spring Boot的基本体系与基本使用进行了学习,本文主要目的是更加进一步的来说明对于Spring B ...
- Spring框架学习笔记(1)
Spring 框架学习笔记(1) 一.简介 Rod Johnson(spring之父) Spring是分层的Java SE/EE应用 full-stack(服务端的全栈)轻量级(跟EJB比)开源框架, ...
- 【转】Spring.NET学习笔记——目录
目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...
- Spring MVC 学习笔记12 —— SpringMVC+Hibernate开发(1)依赖包搭建
Spring MVC 学习笔记12 -- SpringMVC+Hibernate开发(1)依赖包搭建 用Hibernate帮助建立SpringMVC与数据库之间的联系,通过配置DAO层,Service ...
- Spring.NET学习笔记——目录(原)
目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...
- Spring MVC 学习笔记一 HelloWorld
Spring MVC 学习笔记一 HelloWorld Spring MVC 的使用可以按照以下步骤进行(使用Eclipse): 加入JAR包 在web.xml中配置DispatcherServlet ...
- SpringBoot + Spring Security 学习笔记(五)实现短信验证码+登录功能
在 Spring Security 中基于表单的认证模式,默认就是密码帐号登录认证,那么对于短信验证码+登录的方式,Spring Security 没有现成的接口可以使用,所以需要自己的封装一个类似的 ...
- SpringBoot + Spring Security 学习笔记(三)实现图片验证码认证
整体实现逻辑 前端在登录页面时,自动从后台获取最新的验证码图片 服务器接收获取生成验证码请求,生成验证码和对应的图片,图片响应回前端,验证码保存一份到服务器的 session 中 前端用户登录时携带当 ...
随机推荐
- [MyBatis]再次向MySql一张表插入一千万条数据 批量插入 用时5m24s
本例代码下载:https://files.cnblogs.com/files/xiandedanteng/InsertMillionComparison20191012.rar 环境依然和原来一样. ...
- 机器学习之DBSCAN聚类算法
可以看该博客:https://www.cnblogs.com/aijianiula/p/4339960.html 1.知识点 """ 基本概念: 1.核心对象:某个点的密 ...
- [CDH] Acquire data: Flume and Kafka
Flume 基本概念 一.是什么 Ref: http://flume.apache.org/ 数据源获取:Flume.Google Refine.Needlebase.ScraperWiki.Bloo ...
- Spring-Kafka —— 实现批量消费和手动提交offset
spring-kafka的官方文档介绍,可以知道自1.1版本之后, @KafkaListener开始支持批量消费,只需要设置batchListener参数为true 把application.yml中 ...
- CentOS里查看内存的使用
转自 http://blog.csdn.net/blueman2012/article/details/6904597
- 给php安装openssl扩展
转自 http://blog.csdn.net/sinat_23678421/article/details/42217971
- bootstrap4 调整元素之间距离
影响元素之间的间距是可以通过style的margin或padding属性来实现,但这两个属性本意并不相同:margin影响的是本元素与相邻外界元素之间的距离,这里简称外边距:padding影响的元素本 ...
- 了解DrawCall
一.什么是DrawCall DrawCall的含义就是CPU调用图像编程接口,以命令GPU进行渲染的操作. CPU和GPU通过使用一个命令缓冲区实现并行工作.命令缓冲区包含一个命令队列,CPU向其中添 ...
- Shell中test比较
数字比较 比较 描述 -eq 检查是否相等 -ge 检查是否大于或等于 -gt 检查是否大于 -le 检查是否小于或者等于 -lt 检查是否小于 -ne 检查是否不等于 字符串比较 = 检查字符串1和 ...
- JavaScript 控制台打印window对象
示例代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...