SpringData JPA 搭建 xml的 配置方式

 

1.导入版本管理依赖 到父项目里

<dependencyManagement><dependencies><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-bom</artifactId><version>2021.1.10</version><scope>import</scope><type>pom</type></dependency></dependencies>
</dependencyManagement>

2.导入spring-data-jpa 依赖 在子模块

  <dependencies><!--    Junit    --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!--   hibernate --><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-entitymanager</artifactId><version>5.4.32.Final</version></dependency><!--        mysql  --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><!--        jpa  --><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-jpa</artifactId></dependency><!--     连接池   --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.8</version></dependency><!--     spring -  test    --><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.3.10</version><scope>test</scope></dependency></dependencies>

3.创建实体类

package com.kuang.pojo;import javax.persistence.*;@Entity//作为 hibernate实体类
@Table(name = "tb_customer")//映射的表名
public class Customer {/*** @Id: 声明主键的配置* @GeneratedValue: 配置主键的生成策略*        strategy :*            1. GenerationType.IDENTITY :自增 mysql*               底层数据库必须支持自动增长 (底层数据库支持的自动增长方式,对id自增)*            2. GenerationType.SEQUENCE : 序列 ,oracle*               底层书库必须支持序列*            3. GenerationType.TABLE : jpa 提供的一种机制, 通过一张数据库表的形式帮助我们完成主键的配置*            4. GenerationType.AUTO : 由程序自动的帮助我们选择主键生成策略*   @Column(name = "cust_id") 配置属性和字段的映射关系*       name: 数据库表中字段的名称*/@Id@GeneratedValue(strategy = GenerationType.IDENTITY)@Column(name = "cust_id")private Long custId;//客户的主键@Column(name = "cust_name")private String custName;//客户的名称@Column(name = "cust_address")private String custAddress;public Long getCustId() {return custId;}public void setCustId(Long custId) {this.custId = custId;}public String getCustName() {return custName;}public void setCustName(String custName) {this.custName = custName;}public String getCustAddress() {return custAddress;}public void setCustAddress(String custAddress) {this.custAddress = custAddress;}@Overridepublic String toString() {return "Customer{" +"custId=" + custId +", custName='" + custName + '\'' +", custAddress='" + custAddress + '\'' +'}';}
}

4.创建spring配置文件

<?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:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/data/jpahttps://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 用于整合 jpa  相当于 @EnableJpaRepositories       --><jpa:repositories base-package="com.kuang.repositories"entity-manager-factory-ref="entityManagerFactory"transaction-manager-ref="transactionManager"/><!-- 配置 bean  EntityManagerFactory    --><bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"><property name="jpaVendorAdapter"><!--         Hibernate 实现   --><bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"><!--            是否自动的表的生成  true 相当于之前的 update  false 相当于 none  --><property name="generateDdl" value="true"/><!--             是否显示sql   --><property name="showSql" value="true"/></bean></property><!--        扫描实体类的包  来决定哪些实体类做 ORM映射  --><property name="packagesToScan" value="com.kuang.pojo"></property>
<!--    数据源   druid --><property name="dataSource" ref="dataSource"/></bean><!--    数据源--><bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/springdata_jpa?useUnicode=true&amp;useSSL=false&amp;characterEncoding=UTF-8"/><property name="username" value="root"/><property name="password" value="2001"/></bean><!--    声明式事务  --><bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager"><property name="entityManagerFactory" ref="entityManagerFactory"/></bean>
<!-- 启动注解方式的声明式事务--><tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>

5.创建Repository接口

package com.kuang.repositories;import com.kuang.pojo.Customer;
import org.springframework.data.repository.CrudRepository;public interface CustomerRepository extends CrudRepository<Customer,Long> {}

6.测试通过主键查询

package com.kuang.test;import com.kuang.pojo.Customer;
import com.kuang.repositories.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.util.Optional;@ContextConfiguration("/spring.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringDataJpaTest {@Autowiredprivate CustomerRepository customerRepository;@Testpublic void select() {Optional<Customer> byId = customerRepository.findById(1L);Customer customer = byId.get();System.out.println(customer);}
}

package com.kuang.test;import com.kuang.pojo.Customer;
import com.kuang.repositories.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.util.Optional;@ContextConfiguration("/spring.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringDataJpaTest {@Autowiredprivate CustomerRepository customerRepository;@Testpublic void select() {Optional<Customer> byId = customerRepository.findById(1L);Customer customer = byId.get();System.out.println(customer);}@Testpublic void insert() {Customer customer = new Customer();customer.setCustAddress("南环路");customer.setCustName("豪哥");Customer save = customerRepository.save(customer);save.setCustAddress("刘备");System.out.println(customer);}@Testpublic void update() {Customer customer = new Customer();customer.setCustId(1L);customer.setCustAddress("栖霞路");customer.setCustName("张飞");Customer save = customerRepository.save(customer);}@Testpublic void remove() {Customer customer = new Customer();customer.setCustId(1L);customerRepository.delete(customer);}
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/214490.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【力扣100】238.除自身以外数组的乘积

添加链接描述 class Solution:def productExceptSelf(self, nums: List[int]) -> List[int]:# 构造第i个数的左右数组n len(nums)left,right,res [1]*n,[1]*n,[1]*nfor i in range(1,n):left[i] nums[i-1]*left[i-1]for i in range(n-2,-1,-1):right[i] nums[i1]*right…

STM32Cube高效开发教程<基础篇>(十二)----ADC

声明:本人水平有限,博客可能存在部分错误的地方,请广大读者谅解并向本人反馈错误。    本专栏博客参考《STM32Cube高效开发教程(基础篇)》,有意向的读者可以购买正版书籍辅助学习,本书籍由王维波老师、鄢志丹老师、王钊老师倾力打造,书籍内容干货满满。 一、功能概述 …

【C++11】lambda表达式及包装器

一.lambda表达式 1.可调用对象 可调用对象即可以像函数一样被调用的对象&#xff0c;有以下三种&#xff1a; 函数(指针)仿函数对象lambda表达式 tips&#xff1a;调用函数时&#xff0c;既可以用函数名&#xff0c;也可以用函数地址&#xff0c;因为函数名和函数地址是一回事…

Python从入门到精通五:Python数据容器

数据容器入门 为什么学习数据容器 思考一个问题&#xff1a;如果我想要在程序中&#xff0c;记录5名学生的信息&#xff0c;如姓名。 如何做呢&#xff1f; 学习数据容器&#xff0c;就是为了批量存储或批量使用多份数据 Python中的数据容器&#xff1a; 一种可以容纳多份…

Kalman滤波、扩展Kalman滤波、无迹Kalman滤波和异步滤波的原理及其Matlab代码

目录 引言Kalman滤波代码及其结果展示 扩展Kalman滤波代码及其结果展示 无迹Kalman滤波无迹变换无迹Kalman滤波代码及其结果展示 异步无迹Kalman滤波原理代码及其结果展示 引言 本文给出了Kalman Filter&#xff08;卡尔曼滤波&#xff09;、Extended Kalman Filter&#xff0…

leetcode 98. 验证二叉搜索树

leetcode 98. 验证二叉搜索树 题目 给你一个二叉树的根节点 root &#xff0c;判断其是否是一个有效的二叉搜索树。 有效 二叉搜索树定义如下&#xff1a; 节点的左子树只包含 小于 当前节点的数。 节点的右子树只包含 大于 当前节点的数。 所有左子树和右子树自身必须也是…

vue3 引入 markdown编辑器

参考文档 安装依赖 pnpm install mavon-editor // "mavon-editor": "3.0.1",markdown 编辑器 <mavon-editor></mavon-editor>新增文本 <mavon-editor ref"editorRef" v-model"articleModel.text" codeStyle"…

Adams与Abaqus冲突问题

随着工程仿真软件的广泛应用&#xff0c;Adams和Abaqus已成为众多工程师的首选工具。然而&#xff0c;在使用过程中&#xff0c;一些用户可能会遇到这两个软件之间的冲突问题&#xff0c;导致无法正常进行仿真分析。为了帮助大家解决这一难题&#xff0c;我们推出了一篇关于Ada…

Softmax回归

一、Softmax回归关键思想 1、回归问题和分类问题的区别 Softmax回归虽然叫“回归”&#xff0c;但是它本质是一个分类问题。回归是估计一个连续值&#xff0c;而分类是预测一个离散类别。 2、Softmax回归模型 Softmax回归跟线性回归一样将输入特征与权重做线性叠加。与线性回归…

Linux安装Nginx并部署Vue项目

今天部署了一个Vue项目到阿里云的云服务器上&#xff0c;现记录该过程。 1. 修改Vue项目配置 我们去项目中发送axios请求的文件里更改一下后端的接口路由&#xff1a; 2. 执行命令打包 npm run build ### 或者 yarn build 打包成功之后&#xff0c;我们会看到一个dist包&a…

[MySQL]SQL优化之索引的使用规则

&#x1f308;键盘敲烂&#xff0c;年薪30万&#x1f308; 目录 一、索引失效 &#x1f4d5;最左前缀法则 &#x1f4d5;范围查询> &#x1f4d5;索引列运算&#xff0c;索引失效 &#x1f4d5;前模糊匹配 &#x1f4d5;or连接的条件 &#x1f4d5;字符串类型不加 …

110. 平衡二叉树(Java)

给定一个二叉树&#xff0c;判断它是否是高度平衡的二叉树。 本题中&#xff0c;一棵高度平衡二叉树定义为&#xff1a; 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。 示例 1&#xff1a; 输入&#xff1a;root [3,9,20,null,null,15,7] 输出&#xff1a;t…

如何通过SPI控制Peregrine的数控衰减器

概要 Peregrine的数控衰减器PE4312是6位射频数字步进衰减器(DSA,Digital Step Attenuator)工作频率覆盖1MHz~4GHz,插入损耗2dB左右,衰减步进0.5dB,最大衰减量为31.5dB,高达59dBm的IIP3提供了良好的动态性能,切换时间0.5微秒,供电电源2.3V~5.5V,逻辑控制兼容1.8V,20…

​如何使用https://www.krea.ai/来实现文生图,图生图,

网址&#xff1a;https://www.krea.ai/apps/image/realtime Krea.ai 是一个强大的人工智能艺术生成器&#xff0c;可用于创建各种创意内容。它可以用来生成文本描述的图像、将图像转换为其他图像&#xff0c;甚至写博客文章。 文本描述生成图像 要使用 Krea.ai 生成文本描述…

设计模式——建造者模式(Java示例)

引言 生成器是一种创建型设计模式&#xff0c; 使你能够分步骤创建复杂对象。 与其他创建型模式不同&#xff0c; 生成器不要求产品拥有通用接口。 这使得用相同的创建过程生成不同的产品成为可能。 复杂度&#xff1a; 中等 流行度&#xff1a; 流行 使用示例&#xff1a…

【conda】利用Conda创建虚拟环境,Pytorch各版本安装教程(Ubuntu)

TOC conda 系列&#xff1a; 1. conda指令教程 2. 利用Conda创建虚拟环境&#xff0c;安装Pytorch各版本教程(Ubuntu) 1. 利用Conda创建虚拟环境 nolonolo:~/sun/SplaTAM$ conda create -n splatam python3.10查看结果&#xff1a; (splatam) nolonolo:~/sun/SplaTAM$ cond…

Java 中的 Deque 接口及其用途

文章目录 Deque 介绍Deque 使用双端队列普通队列栈 总结 在 Java 中&#xff0c;Deque 接口是一个双端队列&#xff08;double-ended queue&#xff09;的数据结构&#xff0c;它支持在两端插入和移除元素。Deque 是 “Double Ended Queue” 的缩写&#xff0c;而且它可以同时充…

Linux系统编程(一):基本概念

参考引用 Unix和Linux操作系统有什么区别&#xff1f;一文带你彻底搞懂posix Linux系统编程&#xff08;文章链接汇总&#xff09; 1. Unix 和 Linux 1.1 Unix Unix 操作系统诞生于 1969 年&#xff0c;贝尔实验室发布了一个用 C 语言编写的名为「Unix」的操作系统&#xff0…

【基于LSTM的电商评论情感分析:Flask与Sklearn的完美结合】

基于LSTM的电商评论情感分析&#xff1a;Flask与Sklearn的完美结合 引言数据集与爬取数据处理与可视化情感分析模型构建Flask应用搭建词云展示创新点结论 引言 在当今数字化时代&#xff0c;电商平台上涌现出大量的用户评论数据。了解和分析这些评论对于企业改进产品、服务以及…

❀expect命令运用于bash❀

目录 ❀expect命令运用于bash❀ expect使用原理 expet使用场景 常用的expect命令选项 Expect脚本的结尾 常用的expect命令选参数 Expect执行方式 单一分支语法 多分支模式语法第一种 多分支模式语法第二种 在shell 中嵌套expect Shell Here Document&#xff08;内…