SpringBoot整合SpringDataJPA过程解析
Spring Boot整合Spring Data JPA

专注于为中小企业提供成都网站设计、网站建设、外贸网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业镇巴免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
1)加入依赖
org.springframework.boot spring-boot-starter-data-jpa MySQL mysql-connector-java runtime
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;
}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 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
@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 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 listType() {
return typeRepository.findAll();
}
} 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。
文章名称:SpringBoot整合SpringDataJPA过程解析
转载来于:http://www.jxjierui.cn/article/jodppc.html


咨询
建站咨询
