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=8080
server.servlet.context-path=/ # database configuration
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/blog?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123 # jpa configuration
# 更新或者创建数据库表结构
spring.jpa.hibernate.ddl-auto=update
# 控制台打印sql语句
spring.jpa.show-sql=true
spring.jpa.open-in-view=false # log configuration
logging.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;
}

3)编写一个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);
}

4)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
@Transactional
public 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();
} }
04-14 02:18