欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Springboot整合Spring Data Jpa

程序员文章站 2023-01-29 18:14:18
Springboot整合Spring Data Jpa创建Springboot工程并添加junit依赖 org.springframework.boot spring-boot-starter-data-jpa ...
  1. 创建Springboot工程并添加junit依赖
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
  1. 配置application.properties
#database
spring.datasource.url = jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username = northwind
spring.datasource.password = 123456
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
#Jpa
spring.jpa.database = MYSQL
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.DefaultNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
  1. 编写实体类News.java
package com.cxh.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class News {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
     private int id;
     private String title;
     private String content;

    public News() {
    }

    public News(String title, String content) {
        this.title = title;
        this.content = content;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        News news = (News) o;

        return id == news.id;
    }

    @Override
    public int hashCode() {
        return id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "News{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                "}\n";
    }
}
  1. NewsDao接口
package com.cxh.dao;

import com.cxh.model.News;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface NewsDao extends JpaRepository<News,Integer> {
    //约定大于配置
    List<News> findByIdGreaterThanOrderByContentDesc(Integer id);
    //使用@Query注解查询
    @Query(value = "select * from news where content = ?",nativeQuery = true)
    News queryByContent(String content);
}
  1. 启动类
package com.cxh;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories
public class SpringHibernateJpaApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringHibernateJpaApplication.class, args);
    }
}
  1. 编写测试类
package com.cxh.daoTest;

import com.cxh.dao.NewsDao;
import com.cxh.model.News;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class NewsDaoTest {
    @Autowired
    private NewsDao newsDao;

    //批量增加数据
    @Test
    public void addTest(){
        for (int i = 0; i < 100; i++) {
            newsDao.save(new News("新闻"+(i+1),"内容"+(i+1)));
        }
    }

    @Test
    public void queryTest(){
        List<News> list=newsDao.findAll();
        System.out.println(list);
    }

    @Test
    public void queryByIdTest(){
        List<News> list=newsDao.findByIdGreaterThanOrderByContentDesc(20);
        System.out.println(list);
    }

    @Test
    public void  queryByContentTest(){
        News news = newsDao.queryByContent("内容50");
        System.out.println(news);
    }

    @Test
    public void deleteTest(){
        newsDao.deleteById(10);
        System.out.println(newsDao.findAll());
    }

    @Test
    public void updateTest(){
        News news=newsDao.queryByContent("内容1");
        news.setContent("主人来电话了!");
        newsDao.save(news);
        System.out.println(newsDao.findById(1));
    }
}

测试结果:
Springboot整合Spring Data Jpa

本文地址:https://blog.csdn.net/weixin_47772040/article/details/110230344