preface

The date type was ignored in the earlier parameter passing experiment, which led to repeated tread pits. Because it is a study note, it is only a copy of key code for recording.

Preparation stage

Since the @jsonFormat annotation and the DateUtils utility class will be used, we first add dependencies to the packages in pom.xml:

<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-annotations</artifactId>
	<version>2.9.6</version>
</dependency>

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-lang3</artifactId>
	<version>3.8.1</version>
</dependency>
Copy the code

When we build a project, it’s best to build a project with Maven. When we use packages that are not locally available, we only need to add a dependency to the pom.xml file. Otherwise, we need to download them from the Internet and import them manually. Adding dependencies can be understood as importing related packages into a project and can save us a lot of development time.

The core part of the

1. The Entity layer

On the date attribute of the entity class, we need to add @jsonFormat annotation, use its pattern attribute to format the time, and use the timeZone attribute to solve the problem of time difference. “GMT+8” means we are in e8, otherwise we may receive the front end of the time difference of 8 hours.

@Column(name = "drop_date")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date dropDate;
Copy the code

2. The dao layer

I have to say, this automatic generation of SQL statements is really convenient, not only for date types (ok, any field types that exist in the database), but also for different databases (cross-platform support). As for the rules that generate SQL statements, there is another wave of documentation: official documentation

public interface MessageRepository extends JpaRepository<MessageAlarmRecord.Long> {
	// where dropDate <= startDate
    public List<Message> findByDropDateLessThanEqual(Date startDate); 
    // where dropDate >= startDate
    public List<Message> findByDropDateGreaterThanEqual(Date statDate);
    // where startDate <= dropDate and dropDate <= endDate
    public List<Message> findByDropDateBetween(Date startDate, Date endDate);  
    // You can use the And keyword to concatenate other query conditions
    public List<Message> findByDropDateGreaterThanEqualAndDropDateLessThanEqual(Date startDate, Date endDate);
}
Copy the code

3. The controller layer

Thought there was no problem here, in fact, the pit just started…

  1. DateUtils pasteDate function to convert the DateUtils to a Date-class object. The DateUtils pasteDate function converts the DateUtils to a date-class object.
  2. Since the entity class contains the date attribute, it is not possible to directly use the entity class to obtain the parameters passed by the front end when adding data
  3. Generally, when doing projects, it is necessary to specify how the front end passes parameters to the back end, such as JSON, form, URL, etc., because different ways of passing parameters will correspond to different annotations.


RequestBody + Map is used to receive the key-value pairs from the front end. The dateutils.parseDate () function takes a time string as its first argument and a time format as its second argument (see SimpleDateFormat for details).

    @RequestMapping(path = "/find3")
    public List<Message> findBetween(@RequestBody Map<String, Object> params) {
        Date startDate = null;
        Date endDate = null;
        try {
        	// The first argument is a string representing the time, and the second argument is the format of the time
            startDate = DateUtils.parseDate((String)params.get("startDate"), "yyyy-MM-dd HH:mm:ss");
            endDate = DateUtils.parseDate((String)params.get("endDate"), "yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return marRepository.findByDropDateBetween(startDate, endDate);
    }
Copy the code

Data passed during testing:

{
     "startDate":"The 2019-05-23 14:35:12"."endDate":"The 2019-08-12 11:02:00"
}
Copy the code


The resources

  1. www.sojson.com/blog/246.ht… (about @ JsonFormat)
  2. Blog.csdn.net/yaomingyang… (About DateUtils, make do)