• springboot之jpa(简述)


    1、maven引入jar包(jpa和mysql)

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
    

     2、配置文件

    spring.datasource.url=jdbc:mysql://localhost:3306/test
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
    server.port=8080
    

     3、创建实体类(示例)

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    import java.io.Serializable;
    
    @Entity
    @Table(name="student")
    public class Student implements Serializable {
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue
        private Integer sid;
        private String sname;
        private Integer sage;
        private String ssex;
    
        public Integer getSid() {
            return sid;
        }
    
        public void setSid(Integer sid) {
            this.sid = sid;
        }
    
        public String getSname() {
            return sname;
        }
    
        public void setSname(String sname) {
            this.sname = sname;
        }
    
        public Integer getSage() {
            return sage;
        }
    
        public void setSage(Integer sage) {
            this.sage = sage;
        }
    
        public String getSsex() {
            return ssex;
        }
    
        public void setSsex(String ssex) {
            this.ssex = ssex;
        }
    }
    

     4、创建对应实体的接口

    import com.demo.entity.Student;
    import org.springframework.data.jpa.repository.JpaRepository;
    
    //1、这个类中自带一些简单的增删改查方法,可以直接调用
    public interface StudentRepository extends JpaRepository<Student,Integer> {

    /*2、也支持执行sql语句(例如)*/
    @Query("from Student where sage = ?1")
    List<Student> getStudentsBySage(@Param("sage") Integer sage);

    /*3、支持根据方法名的查询(例如)*/
    Student findBySid(Integer sid);

    }
    KeywordSampleJPQL snippet

    And

    findByLastnameAndFirstname

    … where x.lastname = ?1 and x.firstname = ?2

    Or

    findByLastnameOrFirstname

    … where x.lastname = ?1 or x.firstname = ?2

    Is,Equals

    findByFirstname,findByFirstnameIs,findByFirstnameEquals

    … where x.firstname = ?1

    Between

    findByStartDateBetween

    … where x.startDate between ?1 and ?2

    LessThan

    findByAgeLessThan

    … where x.age < ?1

    LessThanEqual

    findByAgeLessThanEqual

    … where x.age <= ?1

    GreaterThan

    findByAgeGreaterThan

    … where x.age > ?1

    GreaterThanEqual

    findByAgeGreaterThanEqual

    … where x.age >= ?1

    After

    findByStartDateAfter

    … where x.startDate > ?1

    Before

    findByStartDateBefore

    … where x.startDate < ?1

    IsNull

    findByAgeIsNull

    … where x.age is null

    IsNotNull,NotNull

    findByAge(Is)NotNull

    … where x.age not null

    Like

    findByFirstnameLike

    … where x.firstname like ?1

    NotLike

    findByFirstnameNotLike

    … where x.firstname not like ?1

    StartingWith

    findByFirstnameStartingWith

    … where x.firstname like ?1 (parameter bound with appended %)

    EndingWith

    findByFirstnameEndingWith

    … where x.firstname like ?1 (parameter bound with prepended %)

    Containing

    findByFirstnameContaining

    … where x.firstname like ?1 (parameter bound wrapped in %)

    OrderBy

    findByAgeOrderByLastnameDesc

    … where x.age = ?1 order by x.lastname desc

    Not

    findByLastnameNot

    … where x.lastname <> ?1

    In

    findByAgeIn(Collection<Age> ages)

    … where x.age in ?1

    NotIn

    findByAgeNotIn(Collection<Age> age)

    … where x.age not in ?1

    True

    findByActiveTrue()

    … where x.active = true

    False

    findByActiveFalse()

    … where x.active = false

    IgnoreCase

    findByFirstnameIgnoreCase

    … where UPPER(x.firstame) = UPPER(?1)

    详细介绍:

    https://blog.csdn.net/ityouknow/article/details/52688664

  • 相关阅读:
    线程基础之进程,线程,任务
    Jobs深入学习
    Quartz的API简介及Jobs和Trigger介绍
    Quartz入门及简单实现
    maven仓库配置阿里云镜像
    Activiti图表bpmn对应的xml文件
    Activiti流程设计工具
    Activiti的25张表
    subprocess.Popen指令包含中文导致乱码问题解决
    Qt5.9使用QWebEngineView加载网页速度非常慢,问题解决
  • 原文地址:https://www.cnblogs.com/lu51211314/p/9728015.html
Copyright © 2020-2023  润新知