<?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.5.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.alan</groupId> <artifactId>SpringBootJpa</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringBootJpa</name> <description>SpringBootJpa</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
package com.alan.SpringBootJpa; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootJpaApplication { public static void main(String[] args) { SpringApplication.run(SpringBootJpaApplication.class, args); } }
### 此版本为mysql 5.X 的 sql连接驱动器使用方法### # spring.datasource.driverClassName=com.mysql.jdbc.Driver ### 此版本为mysql 6.X 以上 的 sql连接驱动器使用方法### spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true spring.datasource.username=root spring.datasource.password=123456 #配置数据库连接池信息 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #配置Spring Jpa 信息 spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true
package com.alan.SpringBootJpa.pojo; import javax.persistence.*; import javax.print.attribute.standard.MediaSize; /* * * * @author: Alan_liu * @date 2021/5/26 21:21 */ @Entity @Table(name ="t_users") public class Users { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Integer id; @Column(name = "name") private String name; @Column(name="age") private Integer age; @Column(name = "address") private String address; public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Users{" + "id=" + id + ", name=‘" + name + ‘\‘‘ + ", age=" + age + ", address=‘" + address + ‘\‘‘ + ‘}‘; } }
package com.alan.SpringBootJpa.dao; /* * 参数一 T:当前需要映射的是实体 * 参数二 Id:当前映射的实体中的OID的类型 * * @author: Alan_liu * @date 2021/5/26 21:26 */ import com.alan.SpringBootJpa.pojo.Users; import org.springframework.data.jpa.repository.JpaRepository; public interface UsersRepository extends JpaRepository<Users,Integer> { }
/* * Copyright 2008-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.data.jpa.repository; import java.util.List; import javax.persistence.EntityManager; import org.springframework.data.domain.Example; import org.springframework.data.domain.Sort; import org.springframework.data.repository.NoRepositoryBean; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.query.QueryByExampleExecutor; /** * JPA specific extension of {@link org.springframework.data.repository.Repository}. * * @author Oliver Gierke * @author Christoph Strobl * @author Mark Paluch * @author Sander Krabbenborg * @author Jesse Wouters */ @NoRepositoryBean public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> { /* * (non-Javadoc) * @see org.springframework.data.repository.CrudRepository#findAll() */ @Override List<T> findAll(); /* * (non-Javadoc) * @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Sort) */ @Override List<T> findAll(Sort sort); /* * (non-Javadoc) * @see org.springframework.data.repository.CrudRepository#findAll(java.lang.Iterable) */ @Override List<T> findAllById(Iterable<ID> ids); /* * (non-Javadoc) * @see org.springframework.data.repository.CrudRepository#save(java.lang.Iterable) */ @Override <S extends T> List<S> saveAll(Iterable<S> entities); /** * Flushes all pending changes to the database. */ void flush(); /** * Saves an entity and flushes changes instantly. * * @param entity entity to be saved. Must not be {@literal null}. * @return the saved entity */ <S extends T> S saveAndFlush(S entity); /** * Saves all entities and flushes changes instantly. * * @param entities entities to be deleted. Must not be {@literal null}. * @return the saved entities * @since 2.5 */ <S extends T> List<S> saveAllAndFlush(Iterable<S> entities); /** * Deletes the given entities in a batch which means it will create a single query. This kind of operation leaves JPAs * first level cache and the database out of sync. Consider flushing the {@link EntityManager} before calling this * method. * * @param entities entities to be deleted. Must not be {@literal null}. * @deprecated Use {@link #deleteAllInBatch(Iterable)} instead. */ @Deprecated default void deleteInBatch(Iterable<T> entities){deleteAllInBatch(entities);} /** * Deletes the given entities in a batch which means it will create a single query. This kind of operation leaves JPAs * first level cache and the database out of sync. Consider flushing the {@link EntityManager} before calling this * method. * * @param entities entities to be deleted. Must not be {@literal null}. * @since 2.5 */ void deleteAllInBatch(Iterable<T> entities); /** * Deletes the entities identified by the given ids using a single query. This kind of operation leaves JPAs first * level cache and the database out of sync. Consider flushing the {@link EntityManager} before calling this method. * * @param ids the ids of the entities to be deleted. Must not be {@literal null}. * @since 2.5 */ void deleteAllByIdInBatch(Iterable<ID> ids); /** * Deletes all entities in a batch call. */ void deleteAllInBatch(); /** * Returns a reference to the entity with the given identifier. Depending on how the JPA persistence provider is * implemented this is very likely to always return an instance and throw an * {@link javax.persistence.EntityNotFoundException} on first access. Some of them will reject invalid identifiers * immediately. * * @param id must not be {@literal null}. * @return a reference to the entity with the given identifier. * @see EntityManager#getReference(Class, Object) for details on when an exception is thrown. * @deprecated use {@link JpaRepository#getById(ID)} instead. */ @Deprecated T getOne(ID id); /** * Returns a reference to the entity with the given identifier. Depending on how the JPA persistence provider is * implemented this is very likely to always return an instance and throw an * {@link javax.persistence.EntityNotFoundException} on first access. Some of them will reject invalid identifiers * immediately. * * @param id must not be {@literal null}. * @return a reference to the entity with the given identifier. * @see EntityManager#getReference(Class, Object) for details on when an exception is thrown. * @since 2.5 */ T getById(ID id); /* * (non-Javadoc) * @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example) */ @Override <S extends T> List<S> findAll(Example<S> example); /* * (non-Javadoc) * @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example, org.springframework.data.domain.Sort) */ @Override <S extends T> List<S> findAll(Example<S> example, Sort sort); }
/* * Copyright 2008-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.data.repository; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; /** * Extension of {@link CrudRepository} to provide additional methods to retrieve entities using the pagination and * sorting abstraction. * * @author Oliver Gierke * @see Sort * @see Pageable * @see Page */ @NoRepositoryBean public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> { /** * Returns all entities sorted by the given options. * * @param sort * @return all entities sorted by the given options */ Iterable<T> findAll(Sort sort); /** * Returns a {@link Page} of entities meeting the paging restriction provided in the {@code Pageable} object. * * @param pageable * @return a page of entities */ Page<T> findAll(Pageable pageable); }
/* * Copyright 2008-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.data.repository; import java.util.Optional; /** * Interface for generic CRUD operations on a repository for a specific type. * * @author Oliver Gierke * @author Eberhard Wolff * @author Jens Schauder */ @NoRepositoryBean public interface CrudRepository<T, ID> extends Repository<T, ID> { /** * Saves a given entity. Use the returned instance for further operations as the save operation might have changed the * entity instance completely. * * @param entity must not be {@literal null}. * @return the saved entity; will never be {@literal null}. * @throws IllegalArgumentException in case the given {@literal entity} is {@literal null}. */ <S extends T> S save(S entity); /** * Saves all given entities. * * @param entities must not be {@literal null} nor must it contain {@literal null}. * @return the saved entities; will never be {@literal null}. The returned {@literal Iterable} will have the same size * as the {@literal Iterable} passed as an argument. * @throws IllegalArgumentException in case the given {@link Iterable entities} or one of its entities is * {@literal null}. */ <S extends T> Iterable<S> saveAll(Iterable<S> entities); /** * Retrieves an entity by its id. * * @param id must not be {@literal null}. * @return the entity with the given id or {@literal Optional#empty()} if none found. * @throws IllegalArgumentException if {@literal id} is {@literal null}. */ Optional<T> findById(ID id); /** * Returns whether an entity with the given id exists. * * @param id must not be {@literal null}. * @return {@literal true} if an entity with the given id exists, {@literal false} otherwise. * @throws IllegalArgumentException if {@literal id} is {@literal null}. */ boolean existsById(ID id); /** * Returns all instances of the type. * * @return all entities */ Iterable<T> findAll(); /** * Returns all instances of the type {@code T} with the given IDs. * <p> * If some or all ids are not found, no entities are returned for these IDs. * <p> * Note that the order of elements in the result is not guaranteed. * * @param ids must not be {@literal null} nor contain any {@literal null} values. * @return guaranteed to be not {@literal null}. The size can be equal or less than the number of given * {@literal ids}. * @throws IllegalArgumentException in case the given {@link Iterable ids} or one of its items is {@literal null}. */ Iterable<T> findAllById(Iterable<ID> ids); /** * Returns the number of entities available. * * @return the number of entities. */ long count(); /** * Deletes the entity with the given id. * * @param id must not be {@literal null}. * @throws IllegalArgumentException in case the given {@literal id} is {@literal null} */ void deleteById(ID id); /** * Deletes a given entity. * * @param entity must not be {@literal null}. * @throws IllegalArgumentException in case the given entity is {@literal null}. */ void delete(T entity); /** * Deletes all instances of the type {@code T} with the given IDs. * * @param ids must not be {@literal null}. Must not contain {@literal null} elements. * @throws IllegalArgumentException in case the given {@literal ids} or one of its elements is {@literal null}. * @since 2.5 */ void deleteAllById(Iterable<? extends ID> ids); /** * Deletes the given entities. * * @param entities must not be {@literal null}. Must not contain {@literal null} elements. * @throws IllegalArgumentException in case the given {@literal entities} or one of its entities is {@literal null}. */ void deleteAll(Iterable<? extends T> entities); /** * Deletes all entities managed by the repository. */ void deleteAll(); }
/* * Copyright 2011-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.data.repository; import org.springframework.stereotype.Indexed; /** * Central repository marker interface. Captures the domain type to manage as well as the domain type‘s id type. General * purpose is to hold type information as well as being able to discover interfaces that extend this one during * classpath scanning for easy Spring bean creation. * <p> * Domain repositories extending this interface can selectively expose CRUD methods by simply declaring methods of the * same signature as those declared in {@link CrudRepository}. * * @see CrudRepository * @param <T> the domain type the repository manages * @param <ID> the type of the id of the entity the repository manages * @author Oliver Gierke */ @Indexed public interface Repository<T, ID> { }
/* * Copyright 2016-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.data.repository.query; import java.util.Optional; import org.springframework.data.domain.Example; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; /** * Interface to allow execution of Query by Example {@link Example} instances. * * @param <T> * @author Mark Paluch * @author Christoph Strobl * @since 1.12 */ public interface QueryByExampleExecutor<T> { /** * Returns a single entity matching the given {@link Example} or {@literal null} if none was found. * * @param example must not be {@literal null}. * @return a single entity matching the given {@link Example} or {@link Optional#empty()} if none was found. * @throws org.springframework.dao.IncorrectResultSizeDataAccessException if the Example yields more than one result. */ <S extends T> Optional<S> findOne(Example<S> example); /** * Returns all entities matching the given {@link Example}. In case no match could be found an empty {@link Iterable} * is returned. * * @param example must not be {@literal null}. * @return all entities matching the given {@link Example}. */ <S extends T> Iterable<S> findAll(Example<S> example); /** * Returns all entities matching the given {@link Example} applying the given {@link Sort}. In case no match could be * found an empty {@link Iterable} is returned. * * @param example must not be {@literal null}. * @param sort the {@link Sort} specification to sort the results by, must not be {@literal null}. * @return all entities matching the given {@link Example}. * @since 1.10 */ <S extends T> Iterable<S> findAll(Example<S> example, Sort sort); /** * Returns a {@link Page} of entities matching the given {@link Example}. In case no match could be found, an empty * {@link Page} is returned. * * @param example must not be {@literal null}. * @param pageable can be {@literal null}. * @return a {@link Page} of entities matching the given {@link Example}. */ <S extends T> Page<S> findAll(Example<S> example, Pageable pageable); /** * Returns the number of instances matching the given {@link Example}. * * @param example the {@link Example} to count instances for. Must not be {@literal null}. * @return the number of instances matching the {@link Example}. */ <S extends T> long count(Example<S> example); /** * Checks whether the data store contains elements that match the given {@link Example}. * * @param example the {@link Example} to use for the existence check. Must not be {@literal null}. * @return {@literal true} if the data store contains elements that match the given {@link Example}. */ <S extends T> boolean exists(Example<S> example); }
关键字 | 方法命名 | sql where字句 |
And | findByNameAndPwd | where name= ? and pwd =? |
Or | findByNameOrSex | where name= ? or sex=? |
Is,Equals | findById,findByIdEquals | where id= ? |
Between | findByIdBetween | where id between ? and ? |
LessThan | findByIdLessThan | where id < ? |
LessThanEquals | findByIdLessThanEquals | where id <= ? |
GreaterThan | findByIdGreaterThan | where id > ? |
GreaterThanEqual | findByAgeGreaterThanEqual | where age >= ? |
After | findByIdAfter | where id > ? |
Before | findByIdBefore | where id < ? |
IsNull | findByNameIsNull | where name is null |
isNotNull,NotNull | findByNameNotNull | where name is not null |
Like | findByNameLike | where name like ? |
NotLike | findByNameNotLike | where name not like ? |
StartingWith |
findByNameStartingWith | where name like ‘?%‘ |
EndingWith | findByNameEndingWith | where name like ‘%?‘ |
Containing | findByNameContaining | where name like ‘%?%‘ |
OrderBy | findByIdOrderByXDesc | where id=? order by x desc |
Not | findByNameNot | where name <> ? |
In | findByIdIn(Collection<?> c) | where id in (?) |
NotIn | findByIdNotIn(Collection<?> c) | where id not in (?) |
True |
findByAaaTue |
where aaa = true |
False | findByAaaFalse | where aaa = false |
IgnoreCase | findByNameIgnoreCase | where UPPER(name)=UPPER(?) |
top | findTop100 | top 10/where ROWNUM <=10 |
package com.alan.SpringBootJpa.dao; /* * Repository 接口的方法名称命名查询 * * @author: Alan_liu * @date 2021/5/28 21:22 */ import com.alan.SpringBootJpa.pojo.Users; import org.springframework.data.repository.Repository; import org.springframework.data.repository.query.Param; import javax.jws.soap.SOAPBinding; import javax.persistence.criteria.CriteriaBuilder; import java.util.Collection; import java.util.List; public interface UsersRepositoryByName extends Repository<Users,Integer> { /*** * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 根据 id 全等于的条件进行查询 * * * * 关键字 方法命名 sql where字句 * * Is,Equals findByIdIs where id= ? * * @param id * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/28 22:12 */ public List<Users> findByIdIs(Integer id); /*** * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 根据 id 全等于的条件进行查询 * * * * 关键字 方法命名 sql where字句 * * Is,Equals findByIdEquals where id= ? * @param id * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/28 22:14 */ public List<Users> findByIdEquals(Integer id); /*** * * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 根据 id 全等于的条件进行查询 * * * * 关键字 方法命名 sql where字句 * * Between findByIdBetween where id between ? and ? * @param startId 开始值 * @param endId 结束值 * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/28 22:20 */ public List<Users> findByIdBetween(Integer startId,Integer endId); /*** * * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 根据 id 全等于的条件进行查询 * * * * 关键字 方法命名 sql where字句 * * LessThan findByIdLessThan where id <= ? * @param id * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/28 22:22 */ public List<Users> findByIdLessThan(Integer id); /*** * * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 根据 id 全等于的条件进行查询 * * * * 关键字 方法命名 sql where字句 * * LessThanEquals findByIdLessThanEquals where id <= ? * @param id * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/28 22:46 */ public List<Users> findByIdLessThanEquals(@Param("id") Integer id); /*** * * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 根据 id 全等于的条件进行查询 * * * * 关键字 方法命名 sql where字句 * * GreaterThan findByIdGreaterThan where id > ? * @param id * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/28 22:49 */ public List<Users> findByIdGreaterThan( Integer id); /*** * * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 关键字 方法命名 sql where字句 * * GreaterThanEqual findByAgeGreaterThanEqual where age >= ? * @param age * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/28 22:51 */ public List<Users> findByAgeGreaterThanEqual(Integer age); /*** * * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 关键字 方法命名 sql where字句 * * After findByIdAfter where id > ? * @param id * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/28 22:52 */ public List<Users> findByIdAfter(Integer id); /*** * * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 关键字 方法命名 sql where字句 * * Before findByIdBefore where id < ? * @param id * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/28 22:58 */ public List<Users> findByIdBefore(Integer id); /*** * * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 关键字 方法命名 sql where字句 * * IsNull findByNameIsNull where name is null * @param id * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/28 22:59 */ public List<Users> findByNameIsNull(@Param("name") String name); /** * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 关键字 方法命名 sql where字句 * * isNotNull,NotNull findByNameNotNull where name is not null * @param name * @return */ public List<Users> findByNameNotNull( String name); /*** * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 关键字 方法命名 sql where字句 * * Like findByNameLike where name like ? * @param name * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/28 21:45 */ public List<Users> findByNameLike(String name ); /** * * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 关键字 方法命名 sql where字句 * * NotLike findByNameNotLike where name not like ? * @param name * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/29 14:35 */ public List<Users> findByNameNotLike(String name ); /*** * * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 关键字 方法命名 sql where字句 * * StartingWith findByNameStartingWith where name like ‘?%‘ * @param name * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/29 14:36 */ public List<Users> findByNameStartingWith(String name ); /*** * * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 关键字 方法命名 sql where字句 * * EndingWith findByNameEndingWith where name like ‘%?‘ * @param name * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/29 14:37 */ public List<Users> findByNameEndingWith(String name ); /*** * * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 关键字 方法命名 sql where字句 * * Containing findByNameContaining where name like ‘%?%‘ * @param name * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/29 14:38 */ public List<Users> findByNameContaining(String name ); /** * * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 关键字 方法命名 sql where字句 * * OrderBy findByIdOrderByXDesc where id=? order by x desc * @param id * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/29 14:40 */ public List<Users> findByIdOrderByXDesc(Integer id ); /*** * * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 关键字 方法命名 sql where字句 * * Not findByNameNot where name <> ? * @param name * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/29 14:41 */ public List<Users> findByNameNot(String name ); /** * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 关键字 方法命名 sql where字句 * * In findByIdIn(Collection<?> c) where id in (?) * @param c * @return */ public List<Users> findByIdIn(Collection<?> c) ; /*** * * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 关键字 方法命名 sql where字句 * * NotIn findByIdNotIn(Collection<?> c) where id not in (?) * @param c * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/29 17:36 */ public List<Users> findByIdNotIn(Collection<?> c) ; /*** * * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 关键字 方法命名 sql where字句 * * True findByAaaTue where aaa = true * @param c * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/29 17:37 */ public List<Users> findByAaaTue(boolean c) ; /*** * * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 关键字 方法命名 sql where字句 * * False findByAaaFalse where aaa = false * @param c * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/29 17:37 */ public List<Users> findByAaaFalse(boolean c) ; /*** * * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 关键字 方法命名 sql where字句 * * IgnoreCase findByNameIgnoreCase where UPPER(name)=UPPER(?) * @param c * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/29 17:38 */ public List<Users> findByNameIgnoreCase(String c) ; /*** * * * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * * 关键字 方法命名 sql where字句 * * top findTop100 top 10/where ROWNUM <=10 * @param c * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/29 17:38 */ public List<Users> findTop100(String c) ; /*** * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * 根据名字查询 sql的查询条件值为全等于 * * 关键字 方法命名 sql where字句 * * findByName where name= ? * @param name * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/28 21:25 */ public List<Users> findByNameEquals(String name ); /** * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * 根据 name 和年龄 全等于的条件进行查询 * * 关键字 方法命名 sql where字句 * And findByNameAndAgeEquals where name= ? and age =? * @param name * @param age * @return */ public List<Users> findByNameAndAgeEquals(String name ,Integer age); /** * 说明:方法的名称必须要遵循 驼峰式命名;其命名格式:findBy(关键字)+属性名称(首字母要大写)+查询条件(字母大写) * 根据 用户名或者是年龄进行 全等于的条件值查询 * * * 关键字 方法命名 sql where字句 * * Or findByNameOrAgeEquals where name= ? or age=? * * @param name * @param age * @return {@link List< Users>} * @throws * @user Alan_liu * @date 2021/5/28 21:40 */ public List<Users> findByNameOrAgeEquals(String name , Integer age); }
/* * Copyright 2011-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.data.repository; import org.springframework.stereotype.Indexed; /** * Central repository marker interface. Captures the domain type to manage as well as the domain type‘s id type. General * purpose is to hold type information as well as being able to discover interfaces that extend this one during * classpath scanning for easy Spring bean creation. * <p> * Domain repositories extending this interface can selectively expose CRUD methods by simply declaring methods of the * same signature as those declared in {@link CrudRepository}. * * @see CrudRepository * @param <T> the domain type the repository manages * @param <ID> the type of the id of the entity the repository manages * @author Oliver Gierke */ @Indexed public interface Repository<T, ID> { }
测试类:UserRepositoryTest
package com.alan.SpringBootJpa; import com.alan.SpringBootJpa.dao.UsersRepository; import com.alan.SpringBootJpa.dao.UsersRepositoryByName; import com.alan.SpringBootJpa.pojo.Users; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; /* * 测试类 * * @author: Alan_liu * @date 2021/5/26 21:30 */ @SpringBootTest(classes =SpringBootJpaApplication.class ) public class UserRepositoryTest { @Autowired private UsersRepository usersRepository; @Autowired private UsersRepositoryByName usersRepositoryByName; @Test public void testSave(){ Users users=new Users(); users.setAddress("广东省广州市天河区华南师范大学"); users.setAge(20); users.setName("李四"); this.usersRepository.save(users); } @Test public void testFindByNameEquals(){ List<Users> list=this.usersRepositoryByName.findByNameEquals("李四"); System.out.println(list.toString()); } @Test public void testFindByNameAndAgeEquals(){ List<Users> list=this.usersRepositoryByName.findByNameAndAgeEquals("李四",20); System.out.println(list.toString()); } @Test public void testFindByNameOrAgeEquals(){ List<Users> list=this.usersRepositoryByName.findByNameOrAgeEquals("李四",21); System.out.println(list.toString()); } @Test public void testFindByNameLike(){ List<Users> list=this.usersRepositoryByName.findByNameLike("李%"); System.out.println(list.toString()); } }
原文:https://www.cnblogs.com/ios9/p/14825874.html