现在的位置: 首页 > 编程语言 > 正文

SpringBoot整合SpringDataJPA过程解析

2020年02月13日 编程语言 ⁄ 共 3277字 ⁄ 字号 评论关闭

Spring Boot整合Spring Data JPA

1)加入依赖

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

2)增加配置(application.properties)

server.port=8080server.servlet.context-path=/# database configurationspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/blog?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8spring.datasource.username=rootspring.datasource.password=123# jpa configuration# 更新或者创建数据库表结构spring.jpa.hibernate.ddl-auto=update# 控制台打印sql语句 spring.jpa.show-sql=truespring.jpa.open-in-view=false# log configurationlogging.level.root=info

3)编写一个实体类(bean)和数据表进行映射,并且配置好映射关系

import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;import javax.validation.constraints.NotBlank;/** * 使用JPA注解配置映射关系 * Created by zxf on 2019年9月30日 */@Entity // 告诉JPA这是一个实体类(和数据库映射的类)@Table(name = "t_type") // @Table来指定和哪个数据表对应,如果省略默认表名就是类名首字母小写public class Type { @Id // 表明这是一个主键 @GeneratedValue(strategy = GenerationType.IDENTITY) // 自增主键 private Long id; @Column(name = "last_name", length = 50) // 这是和数据表对应的一个列,省略默认列名就是属性名 private String name;}

4)编写一个Dao接口来操作实体类对应的数据表

import org.springframework.data.jpa.repository.JpaRepository;/** * Created by zxf on 2019年10月1日 */// 第一个泛型表示操作的类是Type,第二个泛型Long表示Type的主键id为Long类型public interface TypeRepository extends JpaRepository<Type, Long> { // 定义自己的方法 Type findTypeByName(String name);}

5)service层调用测试

import java.util.List;import java.util.Optional;import javax.transaction.Transactional;import org.springframework.beans.BeanUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.domain.Page;import org.springframework.data.domain.Pageable;import org.springframework.stereotype.Service;import com.fei.NotFoundException;import com.fei.po.Type;import com.fei.repository.TypeRepository;import com.fei.service.TypeService;/** * Created by zxf on 2019年10月1日 */@Service@Transactionalpublic class TypeServiceImpl implements TypeService { @Autowired private TypeRepository typeRepository; /** * 保存一个分类 * * @param type * @return */ @Override public Type saveType(Type type) { return typeRepository.save(type); } /** * 根据id获得一个分类对象 * * @param id * @return */ @Override public Type getType(Long id) { return typeRepository.findById(id).get(); } /** * 根据分页参数查询一个分类列表 * * @param pageable * @return */ @Override public Page<Type> listType(Pageable pageable) { return typeRepository.findAll(pageable); } /** * 更新分类 * * @param id * @param type * @return */ @Override public Type updateType(Long id, Type type) { Type t = typeRepository.findById(id).get(); if (t == null) { throw new NotFoundException("类型不存在"); } BeanUtils.copyProperties(type, t); return typeRepository.save(t); } /** * 根据id删除一个分类 * * @param id */ @Override public void deleteType(Long id) { typeRepository.deleteById(id); } /** * 根据名字查询一个分类对象 * * @param name * @return */ @Override public Type getTypeByName(String name) { return typeRepository.findTypeByName(name); } /** * 不带参数的查询所有分类 * * @return */ @Override public List<Type> listType() { return typeRepository.findAll(); }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

本文标题: Spring Boot整合Spring Data JPA过程解析

以上就上有关SpringBoot整合SpringDataJPA过程解析的全部内容,学步园全面介绍编程技术、操作系统、数据库、web前端技术等内容。

抱歉!评论已关闭.