spring data jpa 使用SQL语句查询
package com.ytkj.entity; import javax.persistence.*;
import java.io.Serializable; /**
* @Entity
* 作用:指定当前类是实体类。
* @Table
* 作用:指定实体类和表之间的对应关系。
* 属性:
* name:指定数据库表的名称
* @Id
* 作用:指定当前字段是主键。
* @GeneratedValue
* 作用:指定主键的生成方式。。
* 属性:
* strategy :指定主键生成策略。
* @Column
* 作用:指定实体类属性和数据库表之间的对应关系
* 属性:
* name:指定数据库表的列名称。
* unique:是否唯一
* nullable:是否可以为空
* inserttable:是否可以插入
* updateable:是否可以更新
* columnDefinition: 定义建表时创建此列的DDL
* secondaryTable: 从表名。如果此列不建在主表上(默认建在主表),该属性定义该列所在从表的名字搭建开发环境[重点]
*
* 客户实体类
* 配置映射关系
* 实体类和表映射
* 实体类属性和表字段映射
*/
@Entity
@Table(name = "cst_customer")
public class Customer implements Serializable {
/**
* 声明主键配置
*/
@Id
/**
* 配置主键的生成策略
*/
@GeneratedValue(strategy = GenerationType.IDENTITY)
/**
* 指定实体类属性和数据库表之间的对应关系
*/
@Column(name ="cust_id")
private Long custId;//客户主键
@Column(name = "cust_name")
private String custName;//客户名称
@Column(name ="cust_source" )
private String custSource;//客户来源
@Column(name = "cust_industry")
private String custIndustry;//客户行业
@Column(name ="cust_level")
private String custLevel;//客户级别
@Column(name ="cust_address")
private String custAddress;//客户地址
@Column(name = "cust_phone")
private String custPhone;//客户电话 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 getCustSource() {
return custSource;
} public void setCustSource(String custSource) {
this.custSource = custSource;
} public String getCustIndustry() {
return custIndustry;
} public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry;
} public String getCustLevel() {
return custLevel;
} public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
} public String getCustAddress() {
return custAddress;
} public void setCustAddress(String custAddress) {
this.custAddress = custAddress;
} public String getCustPhone() {
return custPhone;
} public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
} @Override
public String toString() {
return "Customer{" +
"custId=" + custId +
", custName='" + custName + '\'' +
", custSource='" + custSource + '\'' +
", custIndustry='" + custIndustry + '\'' +
", custLevel='" + custLevel + '\'' +
", custAddress='" + custAddress + '\'' +
", custPhone='" + custPhone + '\'' +
'}';
}
}
package com.ytkj.dao; import com.ytkj.entity.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query; import java.util.List; /**
* JpaRepository<实体类类型,主键类型>:用来完成基本CRUD操作
* JpaSpecificationExecutor<实体类类型>:用于复杂查询(分页等查询操作)
*/
public interface CustomerDao2 extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> {
/**
* 使用sql查询
* nativeQuery = true:使用sql查询
* nativeQuery = false:使用jpql查询,默认就是false
*/
@Query(value = "select * from cst_customer",nativeQuery = true)
List<Customer> findAll(); /**
* 使用sql条件查询
* 占位符
* 方法参数顺序尽量和占位符位置一样
* nativeQuery = true:使用sql查询
* nativeQuery = false:使用jpql查询,默认就是false
*/
@Query(value = "select * from cst_customer where cust_name=? ",nativeQuery = true)
Customer findByName(String name); }
import com.ytkj.dao.CustomerDao2;
import com.ytkj.entity.Customer;
import jdk.nashorn.internal.parser.Lexer;
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.List; @RunWith(SpringJUnit4ClassRunner.class)//声明spring提供的单元测试环境
@ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置信息
public class SpringdatajpaSqlTest { @Autowired
CustomerDao2 customerDao2;
@Test
public void findAll(){
List<Customer> list = customerDao2.findAll();
for (int i=0;i<list.size();i++){
Customer customer = list.get(i);
System.out.println(customer);
}
} @Test
public void findByName(){
Customer customer = customerDao2.findByName("中国人");
System.out.println(customer);
}
}
spring data jpa 使用SQL语句查询的更多相关文章
- Spring Data JPA中的动态查询 时间日期
功能:Spring Data JPA中的动态查询 实现日期查询 页面对应的dto类private String modifiedDate; //实体类 @LastModifiedDate protec ...
- spring data jpa 利用@Query进行查询
参照https://blog.csdn.net/yingxiake/article/details/51016234#reply https://blog.csdn.net/choushi300/ar ...
- Spring Data Jpa的四种查询方式
一.调用接口的方式 1.基本介绍 通过调用接口里的方法查询,需要我们自定义的接口继承Spring Data Jpa规定的接口 public interface UserDao extends JpaR ...
- Spring Data Jpa (四)注解式查询方法
详细讲解声明式的查询方法 1 @Query详解 使用命名查询为实体声明查询是一种有效的方法,对于少量查询很有效.一般只需要关心@Query里面的value和nativeQuery的值.使用声明式JPQ ...
- Spring Data JPA 自定义对象接收查询结果集
Spring Data JPA 简介 Spring Data JPA 是 Spring 基于 ORM 框架.JPA 规范的基础上封装的一套JPA应用框架,可使开发者用极简的代码即可实现对数据库的访问和 ...
- Spring Data JPA 的 Specifications动态查询
主要的结构: 有时我们在查询某个实体的时候,给定的条件是不固定的,这时就需要动态构建相应的查询语句,在Spring Data JPA中可以通过JpaSpecificationExecutor接口查询. ...
- Spring MVC和Spring Data JPA之按条件查询和分页(kkpaper分页组件)
推荐视频:尚硅谷Spring Data JPA视频教程,一学就会,百度一下就有, 后台代码:在DAO层继承Spring Data JPA的PagingAndSortingRepository接口实现的 ...
- Spring data jpa 实现简单动态查询的通用Specification方法
本篇前提: SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法 这篇文章中的第二种方法 实现Specification 这块的方法 只适用于一个对象针对某一个固定字 ...
- spring data jpa实现多条件查询(分页和不分页)
目前的spring data jpa已经帮我们干了CRUD的大部分活了,但如果有些活它干不了(CrudRepository接口中没定义),那么只能由我们自己干了.这里要说的就是在它的框架里,如何实现自 ...
随机推荐
- 牛客假日团队赛2 C 修围栏 ( 哈夫曼树,贪心)
链接:https://ac.nowcoder.com/acm/contest/924/C 来源:牛客网 修围栏 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...
- 强化学习(Reinfment Learning) 简介
本文内容来自以下两个链接: https://morvanzhou.github.io/tutorials/machine-learning/reinforcement-learning/ https: ...
- jvm 堆
1.堆内存线程共享,在虚拟机启动时创建. 2.几乎所有的对象实例都在堆上分配:栈中存放基本数据类型和堆中对象的引用. GC回收 程序计数器.虚拟机栈.本地方法栈随线程而生,随线程而灭.栈中的栈帧随着方 ...
- php 强制类型转换
123 123.01 array("123",123) true false null (string) "123" "123.01" ...
- pyserial库-串口通讯模块
pySerial 封装了串口通讯模块,支持Linux.Windows.BSD(可能支持所有支持POSIX的操作系统),支持Jython(Java)和IconPython(.NET and Mono). ...
- java调用存储过程的方式
1.问号是入参和出参,出参要指定类型 CallableStatement pstmt = conn.prepareCall("{call dbo.UP_CodeUp_***(?,?,?,?, ...
- mysql——批量插入数据
要测试一下新功能,需要测试环境下的数据库有大量的数据,一个个插入显然不现实,需要了解一下存储过程 https://www.cnblogs.com/endtel/p/5407455.html Navic ...
- linux运维、架构之路-Kubernetes集群部署TLS双向认证
一.kubernetes的认证授权 Kubernetes集群的所有操作基本上都是通过kube-apiserver这个组件进行的,它提供HTTP RESTful形式的API供集群内外客户端调 ...
- Ubuntu 16.04下使用docker部署ceph集群
ceph集群docker部署 通过docker可以快速部署小规模Ceph集群的流程,可用于开发测试. 以下的安装流程是通过linux shell来执行的:假设你只有一台机器,装了linux(如Ubun ...
- Android Bluetooth 文件接收路径修改方法
修改文件: packages/apps/Bluetooth/src/com/android/bluetooth/opp/BluetoothOppReceiveFileInfo.java 相关代码片段: ...