选择具有类似条件

时间:2017-12-22 07:18:43

标签: java spring jpa

美好的一天,

  • 我试图选择所有以1开头的保险号码,但我只是收到1行而不是全部以1开头。
  • 我想收到30天内到期的那些。

我如何更改2条件的代码? 我已经尝试了一些示例,我可以如何更改查询,但没有一个工作。

另外我添加了我的代码:

存储库:

@Query("From PERSON where exdate > current_date - to_char(30) and insnr like :insnr%") 
       Stream<PERSON> findAllWithSearchParams(@Param("insnr") String insnr); 

控制器:

@Autowired 
   private PersonService personService; 
   @RequestMapping(value="/person/list/**") 
   public List<Person> loadPersonList(   
                   @RequestParam(value = "insnr" ,required=false) String insnr) throws ParseException {         
       mysearch.setInsnr(insnr); 
       return personService.all(mysearch).collect(Collectors.toList()); 
   } 

服务:

@Service 
public class PersonService { 

    @Autowired 
    private PersonRepository personRepository;         
    public Stream<Person> all(Person mysearch){ 
        return personRepository 
               .findAll(Example.of(mysearch)) 
               .stream() 
               .map(Person::fromPerson); 
   } 
} 

班主任:

public class Person { 

    public Integer index; 
    public String firstname; 
    public String lastname; 
    @JsonFormat(pattern="dd.MM.yyyy") 
    public Date exdate; 
    public String insnr; 

    private Person(Integer index, String firstname, String lastname, Date exdate, String insnr){ 
        this.index=index; 
        this.firstname=firstname; 
        this.lastname=lastname; 
        this.exdate=exdate; 
        this.insnr=insnr; 
    } 

    public static Person fromPerson(Person person){ 
        return person == null ? null : new Person(person.getIndex(), person.getFirstname(), person.getLastname(), person.getExdate(), person.getInsnr()); 
    } 
} 

3 个答案:

答案 0 :(得分:1)

您可以使用

更改存储库
<?php
	include("connection1.php");

	// connecting to db
	$conn = mysqli_connect($hostname_localhost, $username_localhost, $password_localhost, $database_localhost);

	/* check connection */
	if (mysqli_connect_errno()) {
		print "Error: Connect failed: %s\n";
		exit();
	}
	
	mysqli_set_charset($conn, 'utf8');
	$response = array();
		
	/* Select queries return a resultset */
	$query = "SELECT image FROM subject WHERE version = 'new'";
	
	if ($result = mysqli_query($conn, $query)) {
		$response = array();

		while ($row = mysqli_fetch_array($result)) {
			$item = array();
			$item["image"] = $row["image"];
			array_push($response, $item);
		}

		/* close result set */
		mysqli_free_result($result);
	}
	
	echo json_encode($response);
	
	/* close connection */
	mysqli_close($conn);
?>

您可以使用所需日期调用该方法,并将“1”调用为List<Person> findByExdateAfterAndInsnrStartingWith(Date exdate, String insnr);

答案 1 :(得分:1)

我以这种方式使用了类似的条件:

@Query("select ....... where codition and date like  CONCAT('%','-',:m,'-',:d)")

所以在你的情况下应该是:

@Query("From PERSON where exdate > current_date - to_char(30) and insnr like CONCAT(:insnr,'%'))

答案 2 :(得分:0)

我认为如果您在资源库方法中使用关键字之后会更容易

List<Person> findByExdateAfterAndInsnrLike(Date date,String 
like);

这有助于https://stackoverflow.com/a/45692717/2494123Supported keywords inside method names Keyword Sample