zl程序教程

您现在的位置是:首页 >  Javascript

当前栏目

SpringBoot+SpringDataJpa @Query之 JPQL使用书写模板(模糊查询and条件查询)

2023-02-18 16:35:48 时间

一些准备工作请看→ 依赖以及数据库数据

Dao

@Query中的查询语句表名字段名直接写实体类类名属性名即可

package com.lianxi.jpa.dao;

import com.lianxi.jpa.pojo.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import java.util.List;

/**
 * JpaRepository提供了基本的增删改查
 * JpaSpecificationExecutor用于做复杂的条件查询
 */
public interface UserDao extends JpaRepository<User,Long>, JpaSpecificationExecutor<User> {


    //JPQL查询数据
    //根据名字模糊查询
    @Query(value = "from User where name like %?1%")
    List<User> queryNames(String name);

    //两个条件
    //1.根据name模糊查询
    //2.并且符合年龄age条件
    @Query(value = "from User where name like %?1% and age = ?2")
    List<User> queryByNameAndAge(String name,Integer age);

}

实体类

package com.lianxi.jpa.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.Date;

/**
 * 实体类和表的映射关系
      @Entity   表示实体类
      @Table    表示和表的关系
 *类中属性和表中字段的映射关系
        @Id     指明主键
    @GeneratedValue     主键的生成策略
    @Column     属性和字段对应关系,一般是字段名和属性名相差比较大使用
 */

@Entity
@Table(name ="lx_user")
@Data
@NoArgsConstructor
public class User{
    // id
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    // 用户名
    @Column(name ="user_name")
    private String userName;
    // 密码
    private String password;
    // 姓名
    private String name;
    // 年龄
    private Integer age;
    // 性别,1男性,2女性
    private Integer sex;
    // 出生日期
    private Date birthday;
    // 创建时间
    private Date created;
    // 更新时间
    private Date updated;
    // 备注
    private String note;

    public User(String userName, String password, String name, Integer age, Integer sex, String note) {
        this.userName = userName;
        this.password = password;
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.note = note;
    }

    public User(Long id,String userName, String password, String name, Integer age, Integer sex, String note) {
        this.id=id;
        this.userName = userName;
        this.password = password;
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.note = note;
    }

}

Controller

package com.lianxi.jpa.controller;

import com.lianxi.jpa.pojo.User;
import com.lianxi.jpa.service.JpqlQueryUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/quser")
public class JpqlQueryUserController {

    @Autowired
    private JpqlQueryUserService quService;

    /**
     *  JPQL查询数据
     *   根据名字模糊查询
     * @param name
     * @return
     */
    @GetMapping("/findByName/{name}")
    public List<User> findByNames(@PathVariable("name") String name){

        List<User> qusersList = quService.findByNames(name);
        //返回结果
        return qusersList;
    }

    /**
     * 两个条件
     * 1.根据name模糊查询
     * 2.并且符合年龄age条件
     * @param name
     * @param age
     * @return
     */
    @GetMapping("/find/{name}/{age}")
    public List<User> findByNameAndage(@PathVariable("name") String name,@PathVariable("age") Integer age){

        List<User> userList = quService.findByNameAndage(name,age);

        //返回结果
        return userList;
    }

}

Service

package com.lianxi.jpa.service;

import com.lianxi.jpa.dao.UserDao;
import com.lianxi.jpa.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JpqlQueryUserService {

    @Autowired
    private UserDao userDao;

    /**
     *  JPQL查询数据
     *    根据名字模糊查询
     * @param name
     * @return
     */
    public List<User> findByNames(String name) {

        List<User> userList = userDao.queryNames(name);

        //返回查询结果
        return userList;
    }

    /**
     * 两个条件
     * 1.根据name模糊查询
     * 2.并且符合年龄age条件
     * @param name
     * @param age
     * @return
     */
    public List<User> findByNameAndage(String name, Integer age) {

        List<User> users = userDao.queryByNameAndAge(name, age);

        //返回查询结果
        return users;
    }
}

启动类

启动类中什么也没加,有启动类可忽略

package com.lianxi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class UserSpringDataJpaApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserSpringDataJpaApplication.class,args);
    }
}

只有模糊查询和条件查询,只为了快速上手