Spring DATA JPA快速入门

一、简单了解Spring DATA JPA

hibernate - 持久层框架。用ORM(对象关系映射)方式操作数据库。数据库中的表和实体类进行对应。

数据库表中的一条数据就和一个实体对象对应。

ORM - 当我们操作对象的时候,数据库里面的数据会随之发生变化。

​ 当我们查询数据库的时候,查询出来的就是对象。

hibernate和mybatis区别:hibernate是一个完全的ORM框架。mybatis是一个不完全的ORM框架。

​ hibernate可以不写sql。mybatis特点是自己写sql。

JPA 是对hiberante进行的抽象(提取出接口)。

Spring Data JPA 是Spring Data家族对JPA的封装。

底层的封装演变:Spring Data JPA – JPA — hibernate — jdbc – 驱动 — 数据库

二:快速入门:

1.导包

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

2.配置

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://139.9.37.235:3306/aa
    username: aa
    password: aaaa
  jpa:
    properties:
      hibernate:
        hbm2ddl:
          auto: update
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        format_sql: true
    show-sql: true  #展示sql语句spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.23.1:3306/mp?useUnicode=yes&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
    username: root
    password: root
  jpa:
    properties:
      hibernate:
        hbm2ddl:
          auto: update
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        format_sql: true
    show-sql: true

3.启动类 没有变化

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

4.业务代码

实体类

@Entity	//表示这是一个实体
@Table(name = "tb_label") //name = 表名
@Data  //自动生成set(),get()方法
public class Label {
    @Id	//此属性为ID
    @GeneratedValue(strategy = GenerationType.IDENTITY) //ID生成机制
    @Column(name = "id") // 与表字段一致 则可以不加
    private Long id;

    private String labelname;

    private String state;

    private Long count;

    private String recommend;

    private Long fans;
}

DAO接口

public interface LabelDao extends JpaRepository<Label, Long>, JpaSpecificationExecutor<Label> {

}
JpaRepository<Label, Long> -- 表示简单的CRUD操作。
			Label 表示要操作的实体类
			Long 表示实体类的主键类型
JpaSpecificationExecutor<Label> -- 只做查询,用于复杂的查询。
			Label 表示要操作的实体类

复杂查询:

@SpringBootTest  
   //text2.2版本以上不需要添加runwith注解
public class TestJpaApplicationTests {

    @Autowired
    private LabelDao labelDao;

    @Test
    @Transactional  //开启事务
    @Commit     //提交事务
    public void textInsert() { //如果数据库中没有tb_label数据表,会自动根据java实体类创建创对应的数据表

        Label label=new Label();

        label.setLabelname("xiaohua");
        label.setState("hhhh");
        
        labelDao.save(label);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值