Springboot整合Spring Data Jpa
程序员文章站
2023-01-29 18:14:18
Springboot整合Spring Data Jpa创建Springboot工程并添加junit依赖 org.springframework.boot spring-boot-starter-data-jpa ...
- 创建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>
- 配置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
- 编写实体类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";
}
}
- 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);
}
- 启动类
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);
}
}
- 编写测试类
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));
}
}
测试结果:
本文地址:https://blog.csdn.net/weixin_47772040/article/details/110230344
推荐阅读
-
使用Spring Data JPA进行数据分页与排序
-
spring-data-jpa实现增删改查以及分页操作方法
-
详解Spring Data JPA使用@Query注解(Using @Query)
-
Spring Data JPA使用Sort进行排序(Using Sort)
-
详解Spring Data JPA系列之投影(Projection)的用法
-
详解spring boot jpa整合QueryDSL来简化复杂操作
-
spring data jpa使用详解(推荐)
-
javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful
-
Spring-Data-JPA整合MySQL和配置的方法
-
Spring Data JPA例子代码[基于Spring Boot、Mysql]