当前位置: 首页 > news >正文

做网站建设个体经营小微企业坚决把快准严细实要求落实到位

做网站建设个体经营小微企业,坚决把快准严细实要求落实到位,漳州 网站建设公司,济南做设计公司网站★ 方法名关键字查询(全自动) (1)继承 CrudRepository 接口 的 DAO 组件可按特定规则来定义查询方法,只要这些查询方法的 方法名 遵守特定的规则,Spring Data 将会自动为这些方法生成 查询语句、提供 方法…

★ 方法名关键字查询(全自动)

(1)继承 CrudRepository 接口 的 DAO 组件可按特定规则来定义查询方法,只要这些查询方法的 方法名 遵守特定的规则,Spring Data 将会自动为这些方法生成 查询语句、提供 方法实现体。(2)方法名关键字的查询方法能以 find...By、read...By、query…By、count…By(查询记录的条数)、get…By开头,并在方法名中嵌入特定关键字即可,Spring Data就会自动生成相应的查询方法体。

▲ 关键字规则

 在方法名中将属性名、运算符都设计成关键字,比如如下:- findByName(String name),这表明根据name属性执行查询。- findByAgeGreaterThan(int age):表明查询age属性大于指定值的记录。

▲ 关键字方法中同样可定义Pagable、Sort参数,用于控制分页和排序。

 说明:如果要做分页或排序的查询,其实没必须要去继承PagingAndSortingRepository,继承CrudRepository也是可行的

▲ 需要说明的情况:

 一种情况需要说明,对比如下两个方法:- findByAddressAndZip:该方法要根据address和zip两个属性进行查询,它对应的JPQL片段为:... where x.address = ?1 and zip = ?2。- findByAddressZip:留意该方法名的Address和Zip之间既没有And,也没有Or,那就表明用的是“属性路径”方式,表明该方法要根据address属性的zip属性进行查询,它对应的JPQL片段为:... where x.address.zip = ?1。如果你的方法名中的关键字写错了,往往就会报QueryCreateException。

代码演示:

数据库数据

在这里插入图片描述

创建对应的两个实体类。
Student实体类
这节用到的一些类和配置文件
在这里插入图片描述

Clazz 实体类
在这里插入图片描述

//根据班级名称查询对象–ClazzDao

方法
在这里插入图片描述
测试结果
在这里插入图片描述

//根据班级名称模糊查询–ClazzDao

方法:
在这里插入图片描述

测试结果
在这里插入图片描述

查询年龄大于指定参数的学生–StudentDao

方法:
在这里插入图片描述

测试结果:
在这里插入图片描述

根据年龄和班级名称查询学生–StudentDao

在这里插入图片描述

方法:
在这里插入图片描述

测试结果:

在这里插入图片描述

根据地址后缀进行分页查询,查询 address 带有 “洞” 的学生并进行分页 – StudentDao

在这里插入图片描述

结果:
在这里插入图片描述
在这里插入图片描述

完整代码

Student

package cn.ljh.app.domain;import lombok.Getter;
import lombok.Setter;import javax.persistence.*;/*** strategy:策略 , GenerationType:生成类型  , Column:列*///修饰该类成为实体类
@Entity
@Getter
@Setter
//表示该类映射到数据库的student_inf表
@Table(name = "student_inf")
public class Student
{@Id //设置为主键@GeneratedValue(strategy = GenerationType.IDENTITY) // 主键自增策略@Column(name = "student_id") //该属性名映射到数据库表的列名private Integer id;private String name;private int age;private String address;private char gender;//关联关系,多个学生对应一个教室//targetEntity:指定关联实体的类型,不指定也没问题,底层会通过反射去识别@ManyToOne(targetEntity = Clazz.class)//JoinColumn: name = "clazz_code"  ,映射外键列列名 ,这个"clazz_code "是指映射的 Clazz 类中的成员变量//referencedColumnName = "clazz_code" ,这个"clazz_code" 是指表的列名//referencedColumnName: --> name = "clazz_code"这个外键列名引用到对方的数据表(clazz_inf)的列名叫"clazz_code"@JoinColumn(name = "clazz_code",referencedColumnName = "clazz_code")private Clazz clazz;@Overridepublic String toString(){return "Student{" +"id=" + id +", name='" + name + '\'' +", age=" + age +", address='" + address + '\'' +", gender=" + gender +'}';}
}

Clazz

package cn.ljh.app.domain;import lombok.Getter;
import lombok.Setter;import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;@Entity //修饰该类为实体类
@Table(name = "clazz_inf") //该实体类映射到数据库的 clazz_inf 表
@Getter
@Setter
public class Clazz
{@Id//主键id@Column(name = "clazz_code") //表示这个属性在数据库表中的列名叫这个clazz_code@GeneratedValue(strategy = GenerationType.IDENTITY) //主键id的自增策略private Integer clazz_code; //班级号private String name;  //班级名称//关联关系,一个教室对应多个学生//targetEntity:指定关联实体的类型,不指定也没问题,底层会通过反射去识别//mappedBy 属性(clazz) 关联实体(Student)中,哪个属性(student.clazz)引用到当前实体(Student实体)@OneToMany(targetEntity = Student.class,mappedBy = "clazz")private Set<Student> students = new HashSet<>();@Overridepublic String toString(){return "Clazz{" +"clazz_code=" + clazz_code +", name='" + name + '\'' +'}';}
}

ClazzDao

package cn.ljh.app.dao;import cn.ljh.app.domain.Clazz;
import org.springframework.data.repository.CrudRepository;import java.util.List;//CrudRepository 的第一个泛型参数是被操作的实体类型,第二个参数是实体的主键类型
//方法名关键字查询: Spring Data 会自动生成查询语句、生成查询方法体
public interface ClazzDao extends CrudRepository<Clazz, Integer>
{//根据班级名称查询对象Clazz findByName(String name);//根据班级名称模糊查询List<Clazz> findByNameLike(String namePattern);}

StudentDao

package cn.ljh.app.dao;import cn.ljh.app.domain.Student;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;import java.util.List;//CrudRepository 的第一个泛型参数是被操作的实体类型,第二个参数是实体的主键类型
public interface StudentDao extends CrudRepository<Student,Integer>
{//查询年龄大于指定参数的学生List<Student> findByAgeGreaterThan(int startAge);//根据年龄和班级名称查询学生//Age 和 ClazzName 用 And 连接起来,表示两个查询条件,//ClazzName这两个单词中间没有And连接起来,表示是一个路径写法,表示是Clazz类的name属性List<Student> findByAgeAndClazzName(int age , String clazzName);//根据地址后缀进行分页查询,查询 address 带有 "洞" 的学生并进行分页Page<Student> findByAddressEndingWith(String addrSuffix, Pageable pageable);}

ClazzDaoTest

package cn.ljh.app.dao;import cn.ljh.app.domain.Clazz;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;//SpringBootTest.WebEnvironment.NONE : 表示不需要web环境
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class ClazzDaoTest
{@Autowiredprivate ClazzDao clazzDao;//参数化测试@ParameterizedTest//参数只有一个的测试用 @ValueSource//根据班级名称查询对象@ValueSource(strings = {"超级A营","超级B班","aaa"})public void testFindByName(String name){Clazz clazz = clazzDao.findByName(name);System.err.println(clazz);}@ParameterizedTest//根据班级名称模糊查询@ValueSource(strings = {"超级%","%营"})public void testFindByNameLike(String namePattern){List<Clazz> clazzes = clazzDao.findByNameLike(namePattern);clazzes.forEach(System.err::println);}}

StudentDaoTest

package cn.ljh.app.dao;import cn.ljh.app.domain.Student;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;import java.util.List;//SpringBootTest.WebEnvironment.NONE : 表示不需要web环境
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class StudentDaoTest
{@Autowiredprivate StudentDao studentDao;/*** @ValueSource: 每次只能传一个参数* @CsvSource:每次可以传多个参数*///需求:查询年龄大于指定参数的记录//参数化测试@ParameterizedTest@ValueSource(ints = {20, 200})public void testFindByAgeGreaterThan(int startAge){List<Student> students = studentDao.findByAgeGreaterThan(startAge);students.forEach(System.err::println);}//根据年龄和班级名称查询学生//Age 和 ClazzName 用 And 连接起来,表示两个查询条件,//ClazzName这两个单词中间没有And连接起来,表示是一个路径写法,表示是Clazz类的name属性@ParameterizedTest//参数一个是int,一个是String,这个注解在传参的时候会自动进行类型转换@CsvSource(value = {"20,超级A营", "18,超级D班"})public void testFindByAgeAndClazzName(int age, String clazzName){List<Student> students = studentDao.findByAgeAndClazzName(age, clazzName);students.forEach(System.err::println);}//pageNo: 要查询哪一页的页数 , pageSize: 每页显示的条数@ParameterizedTest@CsvSource({"洞,2,3","洞,1,4","洞,3,2"})public void testFindByAddressEndingWith(String addrSuffix, int pageNo, int pageSize){//分页对象,此处的pageNo是从0开始的,0代表第一页,所以这里的 pageNo 要 -1Pageable pageable1 = PageRequest.of(pageNo - 1, pageSize);Page<Student> students = studentDao.findByAddressEndingWith(addrSuffix, pageable1);int number = students.getNumber() + 1;System.err.println("总页数:" + students.getTotalPages());System.err.println("总条数:" + students.getTotalElements());System.err.println("当前第:" + number + " 页");System.err.println("当前页有:" + students.getNumberOfElements() + " 条数据");students.forEach(System.err::println);}
}

application.properties

# 配置连接数据源,这些配置由 DataSourceProperties 类负责处理---
# SpringBoot 读取到这些配置信息后,会使用 AutoConfiguration 去容器中自动配置 DataSource Bean
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456# 针对 HikariCP 进行详细配置---
# 指定连接池最大只能有20个连接
#spring.datasource.hikari.maximum-pool-size=20# 配置 JPA 相关属性,由 JpaProperties 类负责处理---
# SpringBoot 读取到这些配置信息后,会使用 AutoConfiguration 去容器中自动配置 EntityManagerFactory Bean
# JPA 底层就是依赖这个 EntityManagerFactory# 自动建表,只能true或者false
# spring.jpa.generate-ddl=true
# 这个也是自动建表,不过比spring.jpa.generate-ddl更严谨,作用:如果已有数据表,无需创建,否则创建数据表
spring.jpa.hibernate.ddl-auto=update
# 指定操作的数据库
spring.jpa.database=mysql
# 是否在执行的时候显示sql语句
spring.jpa.show-sql=true

db.sql

drop database springboot;
create database springboot;
use springboot;
-- 创建clazz_inf表
create table clazz_inf
(clazz_code int primary key auto_increment,name varchar(255)
);
-- 创建student_inf表
create table student_inf
(student_id int primary key auto_increment,name varchar(255),age int,address varchar(255),gender char(2),clazz_code int,foreign key(clazz_code) references clazz_inf(clazz_code)
);
-- 向clazz_inf表插入数据
insert into clazz_inf
values
(null, '疯狂Java训练营'),
(null, '疯狂Java就业班'),
(null, '疯狂Java基础班'),
(null, '疯狂Java提高班');-- 向student_inf表插入数据
insert into student_inf
values
(null, '孙悟空', 500, '花果山水帘洞', '男', 1),
(null, '牛魔王', 800, '积雷山摩云洞', '男', 1),
(null, '猪八戒', 600, '福陵山云栈洞', '男', 2),
(null, '沙和尚', 580, '流沙河', '男', 3),
(null, '白鼠精', 23, '陷空山无底洞',  '女', 2),
(null, '蜘蛛精', 18, '盘丝岭盘丝洞', '女', 4),
(null, '玉面狐狸', 21, '积雷山摩云洞', '女', 3),
(null, '杏仙', 19, '荆棘岭木仙庵', '女', 4);

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.5</version></parent><groupId>cn.ljh</groupId><artifactId>spring_data_jpa_keyword</artifactId><version>1.0.0</version><name>spring_data_jpa_keyword</name><properties><java.version>11</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>

文章转载自:
http://dinncopcav.ssfq.cn
http://dinncotyphoid.ssfq.cn
http://dinncowolflike.ssfq.cn
http://dinncoalchemistic.ssfq.cn
http://dinncofail.ssfq.cn
http://dinncounabroken.ssfq.cn
http://dinncogom.ssfq.cn
http://dinncodetoxicate.ssfq.cn
http://dinncoglauconitic.ssfq.cn
http://dinncounimportance.ssfq.cn
http://dinncohypothyroidism.ssfq.cn
http://dinncojeroboam.ssfq.cn
http://dinncosprang.ssfq.cn
http://dinncomollify.ssfq.cn
http://dinncopsychotechnics.ssfq.cn
http://dinncobloodstained.ssfq.cn
http://dinncoelectroplexy.ssfq.cn
http://dinncodioptase.ssfq.cn
http://dinncoapagoge.ssfq.cn
http://dinncohousel.ssfq.cn
http://dinncoribbed.ssfq.cn
http://dinncoepideictic.ssfq.cn
http://dinnconeumatic.ssfq.cn
http://dinncozhejiang.ssfq.cn
http://dinncofissiparous.ssfq.cn
http://dinncokaiak.ssfq.cn
http://dinncoadipic.ssfq.cn
http://dinnconiccolite.ssfq.cn
http://dinncobeeswax.ssfq.cn
http://dinncotubulin.ssfq.cn
http://dinncocellarway.ssfq.cn
http://dinncocook.ssfq.cn
http://dinncosophonias.ssfq.cn
http://dinncoflair.ssfq.cn
http://dinncocavalierly.ssfq.cn
http://dinncorosalie.ssfq.cn
http://dinncobeebread.ssfq.cn
http://dinncopleiotropy.ssfq.cn
http://dinnconotchboard.ssfq.cn
http://dinncocontemptible.ssfq.cn
http://dinncomesenchyme.ssfq.cn
http://dinncocragsman.ssfq.cn
http://dinncoliquidator.ssfq.cn
http://dinnconates.ssfq.cn
http://dinncojigotai.ssfq.cn
http://dinncosubdural.ssfq.cn
http://dinncowagnerism.ssfq.cn
http://dinncoaldehyde.ssfq.cn
http://dinncoateliosis.ssfq.cn
http://dinncostrangelove.ssfq.cn
http://dinncoanticolonial.ssfq.cn
http://dinncoprolificacy.ssfq.cn
http://dinncoprefactor.ssfq.cn
http://dinncomiddlemost.ssfq.cn
http://dinncomacrograph.ssfq.cn
http://dinncocephalometry.ssfq.cn
http://dinncovane.ssfq.cn
http://dinncothenceforth.ssfq.cn
http://dinncoentoptic.ssfq.cn
http://dinncoshouting.ssfq.cn
http://dinncoremilitarization.ssfq.cn
http://dinncospeaker.ssfq.cn
http://dinncoinflationist.ssfq.cn
http://dinncolibidinous.ssfq.cn
http://dinncoretardate.ssfq.cn
http://dinncobrigandine.ssfq.cn
http://dinncovanessa.ssfq.cn
http://dinncohundredthly.ssfq.cn
http://dinncoalgid.ssfq.cn
http://dinncomisappropriate.ssfq.cn
http://dinncotoscana.ssfq.cn
http://dinncomegadose.ssfq.cn
http://dinncofusuma.ssfq.cn
http://dinncourc.ssfq.cn
http://dinncobev.ssfq.cn
http://dinncowandsworth.ssfq.cn
http://dinncocloverleaf.ssfq.cn
http://dinncolumberyard.ssfq.cn
http://dinncosurfable.ssfq.cn
http://dinncomyanmar.ssfq.cn
http://dinncokitchenware.ssfq.cn
http://dinncokudos.ssfq.cn
http://dinncoexempla.ssfq.cn
http://dinncoflagellated.ssfq.cn
http://dinncoanapestic.ssfq.cn
http://dinncohardie.ssfq.cn
http://dinncogoup.ssfq.cn
http://dinncoirrigative.ssfq.cn
http://dinncopurlin.ssfq.cn
http://dinncoepibiosis.ssfq.cn
http://dinncotranspositive.ssfq.cn
http://dinncoaerarium.ssfq.cn
http://dinncoinconclusively.ssfq.cn
http://dinncoirremovability.ssfq.cn
http://dinncohector.ssfq.cn
http://dinncoytterbium.ssfq.cn
http://dinnconecrophily.ssfq.cn
http://dinncoamativeness.ssfq.cn
http://dinncosucculently.ssfq.cn
http://dinncoresorptive.ssfq.cn
http://www.dinnco.com/news/115416.html

相关文章:

  • 适合个人做的网站有哪些东西百度指数功能
  • java语言建设网站好消息疫情要结束了
  • 网站建设的目标海淀搜索引擎优化seo
  • 网络游戏的利与弊重庆百度seo排名
  • 卖车网站新站网站推广公司
  • 济南网站自然优化成品在线视频免费入口
  • c web网站开发源码qq推广
  • 有创意的网站开发正规淘宝代运营去哪里找
  • CP网站建设搭建需要多少钱竞价排名广告
  • 做资讯网站需要什么资质广告公司广告牌制作
  • 站外做deal的网站百度权重域名
  • 手机免费自建网站百度竞价托管
  • 福建城乡建设网站谷歌seo详细教学
  • 响应式网页源码青岛seo软件
  • 太仓网站优化南京网站推广公司
  • 10g空间网站做视频网站教育培训排行榜前十名
  • 网站维护和推广方案免费网站建设
  • 怎么样做问卷网站企业官网建站
  • 网站是意识形态建设关键词工具软件
  • 企业查询信息宁波优化关键词首页排名
  • 做网站站长交加盟费如何建立和设计公司网站
  • 在vs中做网站免费的行情网站app软件
  • 医妃在上王爷别乱来seo关键词优化技术
  • 重庆智能网站建设费用上海网络推广外包
  • 兰州网站seo服务seo的优化原理
  • 网站后台m关键词分类
  • 南京制作网站公司网站徐州seo管理
  • 怎么把在微企点做响应式网站杭州网站制作排名
  • 疫情第二波最新消息qq群排名优化软件官网
  • javascript基础教程pdf下载seo和sem是什么意思啊