Spring整合Hibernate实现Spring Data JPA (介绍和使用)
Spring Data JPA是Spring基于Hibernate开发的一个JPA框架。如果用过Hibernate或者MyBatis的话,就会知道对象关系映射(ORM)框架有多么方便。
但是Spring Data JPA框架功能更进一步,为我们做了 一个数据持久层框架几乎能做的任何事情。下面来逐步介绍它的强大功能。
直接上代码:
pom.xml

<!-- hibernate start -->
<!-- spring data jpa -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.0.Final</version>
</dependency>
<!-- hibernate end -->

项目结构:

the_data_jpa 包 里面的 各类 代码
User

package com.oukele.the_data_jpa.entity; import org.springframework.stereotype.Component; import javax.persistence.*; @Entity
@Table(name = "user")
public class User { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Id")
private int id; @Column(name = "userName")
private String name;
private String password; public int 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 getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
}

UserDao

package com.oukele.the_data_jpa.dao; import com.oukele.the_data_jpa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; public interface UserDao extends JpaRepository<User, Integer> { User findByNameAndPassword(String name,String password); }

UserService

package com.oukele.the_data_jpa.service; import com.oukele.the_data_jpa.dao.UserDao;
import com.oukele.the_data_jpa.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class UserService {
@Autowired
private UserDao userDao; public User findNameAndPassword(String name,String password){
return userDao.findByNameAndPassword(name, password);
} public List<User> list(){
List<User> list = userDao.findAll();
return list;
}
}

SpringConfig

package com.oukele.the_data_jpa; import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager; import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.util.Properties; @Configuration//声明当前配置类
@ComponentScan(basePackages = "com.oukele.the_data_jpa")// 扫描当前包 使用 spring 注解
@PropertySource("classpath:jdbc.properties")//加载 资源文件
@EnableJpaRepositories(basePackages = "com.oukele.the_data_jpa.dao")//扫描 使用 jpa 注解的接口
public class SpringConfig { //配置数据源
@Bean
DataSource dataSource(Environment env) throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(env.getProperty("jdbc.driver"));
dataSource.setJdbcUrl(env.getProperty("jdbc.url"));
dataSource.setUser(env.getProperty("jdbc.user"));
dataSource.setPassword(env.getProperty("jdbc.password"));
return dataSource;
} // @Bean
// public JdbcTemplate jdbcTemplate() {
// JdbcTemplate jdbcTemplate = new JdbcTemplate();
// jdbcTemplate.setDataSource(dataSource1);
// return jdbcTemplate;
// } @Bean
public PlatformTransactionManager transactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
} //SqlSessionFactory
@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource){ LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
bean.setDataSource(dataSource);
//加载 实体类
bean.setPackagesToScan("com.oukele.the_data_jpa.entity");
//设置 适配器
bean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); Properties properties = new Properties();
//更新表结构
properties.setProperty("hibernate.hbm2ddl.auto", "update");
//设置方言
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
bean.setJpaProperties(properties); return bean;
} }

Main ( 测试 类)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package com.oukele.the_data_jpa;import com.oukele.the_data_jpa.entity.User;import com.oukele.the_data_jpa.service.UserService;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import java.util.List;public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); UserService service = context.getBean(UserService.class); User user = service.findNameAndPassword("oukele","oukele"); System.out.println(user.getName()); List<User> list = service.list(); for (User user1 : list) { System.out.println(user1.getName()+" - "+user1.getPassword()); } }} |
测试结果:
1 oukele
2 oukele - oukele
jdbc.properties文件:
jdbc.driver=org.mariadb.jdbc.Driver
jdbc.url=jdbc:mariadb://localhost:3306/test
jdbc.user=oukele
jdbc.password=oukele
Spring整合Hibernate实现Spring Data JPA (介绍和使用)的更多相关文章
- Spring整合Hibernate实现Spring Data JPA (简单使用)
直接上代码: pom.xml <!-- hibernate start --> <!-- spring data jpa --> <dependency> < ...
- 【SSH框架】系列之 Spring 整合 Hibernate 框架
1.SSH 三大框架整合原理 Spring 与 Struts2 的整合就是将 Action 对象交给 Spring 容器来负责创建. Spring 与 Hibernate 的整合就是将 Session ...
- springboot:spring data jpa介绍
转载自:https://www.cnblogs.com/ityouknow/p/5891443.html 在上篇文章springboot(二):web综合开发中简单介绍了一下spring data j ...
- spring boot(五)Spring data jpa介绍
在上篇文章springboot(二):web综合开发中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法以及注意事项 使用spr ...
- Spring Data JPA介绍与简单案例
一.Spring Data JPA介绍 可以理解为JPA规范的再次封装抽象,底层还是使用了Hibernate的JPA技术实现,引用JPQL(Java Persistence Query Languag ...
- Spring 整合 Hibernate
Spring 整合 Hibernate •Spring 支持大多数流行的 ORM 框架, 包括 Hibernate JDO, TopLink, Ibatis 和 JPA. •Spring 对这些 OR ...
- Spring整合Hibernate笔记
1. Spring 指定datasource a) 参考文档,找dbcp.BasicDataSource i. c3p0 ii. dbcp <bean id="dataSource&q ...
- Spring(四):Spring整合Hibernate,之后整合Struts2
背景: 上一篇文章<Spring(三):Spring整合Hibernate>已经介绍使用spring-framework-4.3.8.RELEASE与hibernate-release-5 ...
- Spring整合Hibernate的方法
一.基本支持 Spring 支持大多数流行的 ORM 框架, 包括 Hibernate JDO, TopLink, Ibatis 和 JPA. Spring 对这些 ORM 框架的支持是一致的, 因此 ...
随机推荐
- go net库
1 使用Listen函数创建一个server ln, err := net.Listen("tcp", ":8080") if err != nil { // ...
- Spring Boot 2.2.0 正式发布,支持 JDK 13!
Java技术栈 www.javastack.cn 优秀的Java技术公众号 推荐阅读: Spring Boot 2.2.0 正式发布了,可从 repo.spring.io 或是 Maven Centr ...
- OpenGL字体绘制
/* glfont.hpp sdragonx 2019-08-15 00:03:33 opengl字体类,提供初学者参考学习 opengl初始化之后,创建字体 font.init(L"微软雅 ...
- [Codeforces 1246B] Power Products (STL+分解质因数)
[Codeforces 1246B] Power Products (STL+分解质因数) 题面 给出一个长度为\(n\)的序列\(a_i\)和常数k,求有多少个数对\((i,j)\)满足\(a_i ...
- [LeetCode] 164. 最大间距
题目链接 : https://leetcode-cn.com/problems/maximum-gap/ 题目描述: 给定一个无序的数组,找出数组在排序之后,相邻元素之间最大的差值. 如果数组元素个数 ...
- vue 防抖和节流
函数防抖(debounce):当持续触发事件时,一定时间段内没有再触发事件,事件处理函数才会执行一次,如果设定的时间到来之前,又一次触发了事件,就重新开始延时. 函数节流(throttle):当持续触 ...
- console 对象
JavaScript 原生中默认是没有 Console 对象,这是宿主对象(也就是游览器)提供的内置对象. 用于访问调试控制台,在不同的浏览器里效果可能不同.Console 对象方法:
- Axiso解决跨域访问
问题: 在项目中需要需要讲本地项目去请求一个URL接口获取数据 例如: 本地请求地址:http://127.0.0.1:19323/site/info.json 请求Url地址:http://www. ...
- IDEA tomcat热部署方法
项目开发过程中,我们一般希望在修改完代码之后不重启项目即可提现出修改的结果,那么热部署项目就显得十分必要了.在idea中将项目热部署至tomcat中的方法如下: 首先打开tomcat配置界面,在ser ...
- Qt下载(多种下载通道+所有版本)
http://c.biancheng.net/view/3851.html Qt 体积很大,有 1GB~3GB,官方下载通道非常慢,相信很多读者会崩溃,所以建议大家使用国内的镜像网站(较快),或者使用 ...