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 中 前端用户登录时携带当 ...
随机推荐
- C#_switch语句,for循环,do while循环,while循环
1:switch语句,代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Tex ...
- fastcgi代理
一.fastcgi代理 1.示意图 2.fastcgi 代理配置语法 a.设置PHP服务代理地址 Syntax: fastcgi_pass address; Default: — Context: l ...
- 如何用MATLAB GUI创建图形用户界面
MATLAB是众多理工科学生及工程师经常使用的一款数学软件,除了可以实现数据处理,矩阵运算.函数绘制等功能外,MATLAB还可以实现图形用户界面的设计. 下面介绍如何让小白也能用GUI创建最基本的用户 ...
- 自定义vue-cli生成项目模板配置(1)
最近在读<变量>,目前得到的认知之一:慢变量才是决定事物长期发展的因素. 打算自定义vue-cli的脚手架或者根据自己的需要设置项目模板的相关参数,很大程度与慢变量这个概念相关. 当然,我 ...
- ajax修改表单的值后dom没更新的解决办法
添加一个扩展方法,通过$("#id").html($("#id").formhtml())更改. 扩展方法: (function ($) { var oldHT ...
- nginx 反向代理实现负载均衡*理论
Nginx负载均衡集群介绍 负载均衡集群提供了一种廉价,有效,透明的方法,来扩展网络设备和服务器的负载,带宽和吞吐量,同时加强了网络数据处理能力,提高了网络的灵活性和可用性. 搭建负载均衡服务的需求: ...
- LNMP分离
1.Nginx 找php 在nginx 配置文件的server里 添加 location ~.*\.(php|php5)?$ { root /var/www/html/wwwcom; fastc ...
- JavaScript中字符串,数组的基本操作
JavaScript的字符串就是用”或”“括起来的字符表示. js中操作字符串: 1.获得字符串的长度 var s = 'Hello, world!'; s.length; // 132.获取指定字符 ...
- 增强for循环的简单总结
整体来说:增强型for循环使用起来比较方便,代码也比较简单,如果只是操作集合中元素的而不使用索引的话,建议用此方法.对于普通for循环,如果需要使用索引进行其它操作的话,建议用这个. 详细来说:1,区 ...
- phpstudy批量getshell工具
phpstudy批量getshell工具,最新phpstudygetshell,配合精确采集实现每天轻松上万! 详情可加QQ:1743685523