学习总目录👈
- 数据库信息请参考: 上一篇
Spring Boot 整合 JPA
简易说明:上一篇使用了注解和配置文件来对数据库进行操作,这次使用spring boot JPA框架来实现同样(不表示相等)的功能,在JpaRepository的基础上也可以
简单解释下下面要用的注解
-
@Entity
告诉程序需要和数据库表格映射的实体类,默认映射的是当前类名小写后的表格,也可以使用属性name来指定表。 -
Id
告诉程序哪个属性是表格中的主键 -
@GeneratedValue
告诉程序你的主键的变化趋势,专业词语是生成策略。 -
@Column
指定属性对应表格中的那个字段(字段与属性名不一样时),可以默认不写。可以假装支持驼峰命名。 -
@Query
编写自定义的查询规则,课堂也提到了 ?1 ,?2等,1和2表示对应方法的第一第二个参数。 -
@Transactional
表示当前方法支持事务管理,也就是结果是唯一的,要么成功要么失败,而且回执行回滚操作,假如你有个女朋友,你毕业了要结婚,你同意结婚,但人家不同意,你只能回滚,不能说,你和她结婚了,而她和你没结婚,这种无脑操作。 -
@Modifying
告诉程序你这个方法是要修改数据库内容的,比如update、delecte都需要加上这个注解。 -
pom.xml 在上一章的基础上添加
<!-- Spring Data JPA启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
-
创建一个实体类:domain/Discuss.java
- @Entity 注解使用name属性指定需要映射到实体类的表名,name不写默认为类名首字母小写与表名对应
- @Id 表名属性对应的主键
- @Column 当属性与表中字段名不同时,能够使用name属性指定
package wx0725.top.domain;
import javax.persistence.Id;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
/**
* @author WEN
* @version 1.0
* @description: Wen Xuan
* @date 2021/3/20 下午 13:17
* @link http://wx0725.top
*/
@Entity(name = "t_comment")
public class Discuss {
@Id
@GeneratedValue(strategy = GenerationType.AUTO.IDENTITY)
private Integer id;
private String content;
private String author;
@Column(name = "a_id")
private Integer aId;
@Override
public String toString() {
return "Discuss{" +
"id=" + id +
", content='" + content + '\'' +
", author='" + author + '\'' +
", aId=" + aId +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Integer getaId() {
return aId;
}
public void setaId(Integer aId) {
this.aId = aId;
}
}
-
repository/DiscussRepository.java
注意:@EnableJpaRepositories(basePackages = {“wx0725.top.repository”})
需要指定,不然不扫描
package wx0725.top.repository;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.Transactional;
import wx0725.top.domain.Discuss;
import java.util.List;
/**
* @author WEN
* @version 1.0
* @description: Wen Xuan
* @date 2021/3/20 下午 13:51
* @link http://wx0725.top
*/
@EnableJpaRepositories(basePackages = {"wx0725.top.repository"})
public interface DiscussRepository extends JpaRepository<Discuss, Integer> {
public List<Discuss> findByAuthorNotNull();
@Query("SELECT c from t_comment c where c.aId = ?1")
public List<Discuss> getDiscussPaged(Integer aid, Pageable pageable);
@Query(value = "SELECT * from t_comment c where a_Id = ?1", nativeQuery = true)
public List<Discuss> getDiscussPaged2(Integer aid, Pageable pageable);
@Transactional
@Modifying
@Query("UPDATE t_comment c SET c.author = ?1 WHERE c.id = ?2")
public List<Discuss> updateDiscuss(String author, Integer id);
@Transactional
@Modifying
@Query("DELETE FROM t_comment c where c.id = ?1")
public int deleteDiscuss(Integer id);
}
- 测试类:
package wx0725.top;
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.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.junit4.SpringRunner;
import wx0725.top.domain.Discuss;
import wx0725.top.repository.DiscussRepository;
import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.startsWith;
import java.util.List;
import java.util.Optional;
/**
* @author WEN
* @version 1.0
* @description: Wen Xuan
* @date 2021/3/20 下午 15:19
* @link http://wx0725.top
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class JPATest {
@Autowired
private DiscussRepository discussRepository;
@Test
public void selectDiscussComment() {
Optional<Discuss> optional = discussRepository.findById(1);
if (optional.isPresent()) {
System.out.println(optional.get());
}
}
@Test
public void selectCommentByKeys() {
List<Discuss> list = discussRepository.findByAuthorNotNull();
System.out.println(list);
}
@Test
public void selectCommentPaged() {
Pageable pageable = PageRequest.of(0, 3);
List<Discuss> list = discussRepository.getDiscussPaged(1, pageable);
System.out.println(list);
}
@Test
public void selectCommentByExample() {
Discuss discuss = new Discuss();
discuss.setAuthor("文轩");
Example<Discuss> example = Example.of(discuss);
List<Discuss> list = discussRepository.findAll(example);
System.out.println(list);
}
@Test
public void selectCommentByExampleMatcher() {
Discuss discuss = new Discuss();
discuss.setAuthor("流星蝴蝶没有剑");
ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("author", startsWith());
Example<Discuss> example = Example.of(discuss, exampleMatcher);
List<Discuss> list = discussRepository.findAll(example);
System.out.println(list);
}
}