Spring和SpringDataJpa整合详解

Spring和SpringDataJpa整合详解Spring和SpringDataJpa整合详解一、概述SpringBoot操作数据库有多种方式,如JDBC直接操作:太古老了,没人愿意这样玩Mybatis插件:比较时髦,比较适合sql复杂,或者对性能要求高的应用,因为sql都是自己写的。Spring-data-jpa:使用hibernate作为实现,基本上不需要写sql,因为sql都是统一的,总是会产生多余的查询,性能上…

大家好,又见面了,我是你们的朋友全栈君。

Spring和SpringDataJpa整合详解

一、概述

SpringBoot操作数据库有多种方式,如

  • JDBC直接操作:太古老了,没人愿意这样玩

  • Mybatis插件:比较时髦,比较适合sql复杂,或者对性能要求高的应用,因为sql都是自己写的。

  • Spring-data-jpa: 使用hibernate作为实现,基本上不需要写sql,因为sql都是统一的,总是会产生多余的查询,性能上相对而言会低,但不绝对,影响性能的因素是多种的,这里说的性能是 从最终的查询的sql来对比的,毕竟生成的sql没有经过深思熟虑写出来的性能好。

  • JdbcTemplate:spring在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中。Spring-data-jpa引入的时候,JdbcTemplate必然会被引入的。

当然还有其他中间件,主流使用的就是Mybatis和Spring-data-jpa。

首发地址:

如果大家正在寻找一个java的学习环境,或者在开发中遇到困难,可以加入我们的java学习圈,点击即可加入,共同学习,节约学习时间,减少很多在学习中遇到的难题。

二、环境配置

本文假设你已经引入Spring必备的一切了,已经是个Spring项目了,如果不会搭建,可以打开这篇文章看一看《Spring和Spring Mvc 5整合详解》

2.1 maven依赖

使用Spring-data-jpa需要引入spring-data-jpa,因为是非Springboot项目,我们不能通过starter引入,需要引入spring-data-jpa、javax.transaction-api、hibernate-core。

<?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>cn.pomit</groupId>
		<artifactId>SpringWork</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>SpringDataJpa</artifactId>
	<packaging>jar</packaging>
	<name>SpringDataJpa</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-dbcp2</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-jpa</artifactId>
			<version>2.0.10.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>javax.transaction</groupId>
			<artifactId>javax.transaction-api</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>5.2.17.Final</version>
			<scope>compile</scope>
			<exclusions>
				<exclusion>
					<artifactId>jboss-transaction-api_1.2_spec</artifactId>
					<groupId>org.jboss.spec.javax.transaction</groupId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>
	<build>
		<finalName>SpringDataJpa</finalName>
	</build>
</project>

父模块可以在https://www.pomit.cn/spring/SpringWork/pom.xml获取。

2.2 Spring配置

需要配置数据源、jdbcTemplate、entityManagerFactory、transactionManager和jpa:repositories。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

	<context:annotation-config />
	<context:component-scan base-package="cn.pomit.springwork.springdatajpa">
	</context:component-scan>

	<bean id="annotationPropertyConfigurerJpaLock" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="order" value="1" />
		<property name="ignoreUnresolvablePlaceholders" value="true" />
		<property name="locations">
			<list>
				<value>classpath:db.properties</value>
			</list>
		</property>
	</bean>


	<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${db.dirverClass}"></property>
		<property name="url" value="${db.url}" />
		<property name="username" value="${db.username}" />
		<property name="password" value="${db.password}" />

		<property name="initialSize" value="1" />
		<property name="minIdle" value="1" />
		<property name="maxTotal" value="20" />

		<property name="validationQuery" value="SELECT 1" />
		<property name="testWhileIdle" value="true" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
	</bean>

	<!-- jdbcTemplate -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="packagesToScan" value="cn.pomit.springwork.springdatajpa.domain"></property>
		<property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
				<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect">
			</property>
			</bean>
		</property>
	</bean>
	<jpa:repositories base-package="cn.pomit.springwork.springdatajpa.dao" />
	
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>
	<!-- 使用annotation定义事务 -->
	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
</beans>

这里面,需要注意的是:

  • entityManagerFactory,是实体和数据库选择信息。

  • jpa:repositories,指明Spring-data-jpa的repositories地址。就是我们的数据库交互层。

  • transactionManager,事务处理器。

  • tx:annotation-driven:开启事务注解。

db.properties中存放数据库的地址端口等连接信息。

db.properties:

db.url=jdbc:mysql://127.0.0.1:3306/boot?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
db.username=cff
db.password=123456
db.dirverClass=com.mysql.cj.jdbc.Driver

三、数据访问Dao层

我们直接使用Spring-data-jpa, 一切都会变的特别简单。Spring-data-jpa支持快速查询,也支持@Query自定义查询,只需要新建接口继承JpaRepository或者CrudRepository等接口即可。

UserInfoDao :

package cn.pomit.springwork.springdatajpa.dao;

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

import cn.pomit.springwork.springdatajpa.domain.UserInfo;

@Repository
public interface UserInfoDao extends JpaRepository<UserInfo, String> { 
   
	UserInfo findByUserName(String userName);
}

注意加上@Repository注解。实体要加上@Entity和@Table注解。

四、测试业务逻辑

我们定义一个service和web接口来做测试。

UserInfoService :

package cn.pomit.springwork.springdatajpa.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.pomit.springwork.springdatajpa.dao.UserInfoDao;
import cn.pomit.springwork.springdatajpa.domain.UserInfo;

@Service
public class UserInfoService { 
   
	@Autowired
	UserInfoDao userInfoDao;
	public UserInfo getUserInfoByUserName(String userName){ 
   
		return userInfoDao.findByUserName(userName);
	}
}

SpringDataJpaRest:

package cn.pomit.springwork.springdatajpa.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.pomit.springwork.springdatajpa.domain.UserInfo;
import cn.pomit.springwork.springdatajpa.service.UserInfoService;

@RestController
@RequestMapping("/springdatajpa")
public class SpringDataJpaRest { 
   

	@Autowired
	UserInfoService userInfoService;
	
	@RequestMapping(value = "/test/{name}", method = { 
    RequestMethod.GET })
	public UserInfo testMybatis(@PathVariable("name") String name) { 
   
		return userInfoService.getUserInfoByUserName(name);
	}
}

五、过程中用到的实体

UserInfo:


详细完整的实体,可以访问品茗IT-博客《Spring和SpringDataJpa整合详解》进行查看

全部代码可以在Spring组件化构建https://www.pomit.cn/java/spring/spring.html中的SpringDataJpa组件中查看,并下载。

快速构建项目

Spring组件化构建

SpringBoot组件化构建

SpringCloud服务化构建

喜欢这篇文章么,喜欢就加入我们一起讨论Spring技术吧!
品茗IT交流群

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/133267.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ubuntu下为anaconda安装opencv[通俗易懂]

    ubuntu下为anaconda安装opencv[通俗易懂]终端执行:pipinstallopencv-python

    2022年10月21日
    0
  • LASSO回归与L1正则化 西瓜书「建议收藏」

    1.结构风险与经验风险在支持向量机部分,我们接触到松弛变量,正则化因子以及最优化函数,在朴素贝叶斯分类,决策树我们也遇到类似的函数优化问题。其实这就是结构风险和经验风险两种模型选择策略,经验风险负责最小化误差,使得模型尽可能的拟合数据,而结构风险则负责规则化参数,使得参数的形式尽量简洁,从而达到防止过拟合的作用.所以针对常见模型,我们都有下式:                           …

    2022年4月11日
    75
  • Ruby On Rails 教程学习

    Ruby On Rails 教程学习

    2021年8月25日
    80
  • C语言冒泡法_冒泡编程c语言

    C语言冒泡法_冒泡编程c语言在考试前依然有很多同学不清楚冒泡法怎么用所以这期我专门整理了一下冒泡法的用法,供大家参考哦!

    2022年8月31日
    1
  • 解析this指针

    解析this指针要想明白为什么有this指针,我们先来看一看数据成员与成员函数之间的关系  每个对象中的数据成员都占有存储空间,如果一个类定义了n个对象的话,那么就有n个大小相等的空间来存放这n个对象的数据成员,但是不同的对象都调用同一个代码段。  那么问题来了!!!  当不同对象的成员函数引用数据成员时,怎么能够保证引用的是所指定的对象的数据成员呢?例如:设计一个长方体类

    2022年5月17日
    35
  • 手动ghost备份系统步骤_手动ghost备份图解

    手动ghost备份系统步骤_手动ghost备份图解备份前我们需要ghost,在此我提供下,在压缩文件下找到ghost百度网盘:http://pan.baidu.com/s/1mh77iWS 密码:ivxq进入ghost界面以后,按回车键,进入下一个操作界面。如下图所示:使用键盘上的方向键依次选择“Local”(本机)“Partition”(分区)“ToImage”(到镜像)然后

    2022年9月6日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号