spring data继承CrudRepository接口并自定义接口和实现

上一篇文章中写到,自定义的接口继承了CrudRepository、JpaSpecificationExecutor接口后有默认的实现类感觉很方便。

上一篇文章地址:http://blog.csdn.net/jieyanqulaopo123/article/details/79461107

今天想到如果既要有默认的实现功能、又要在接口中添加方法然后自己实现接口中添加的方法,怎么办???

于是有了下面这篇文章。

1、自定义一个接口

public interface Custom {
public String sayHello();

}

2、编写Custom接口的实现类

public class CustomRepositoryImpl implements Custom{
@Override
public String sayHello() {
// TODO Auto-generated method stub
return "sayHello";
}

}

3、让自定义的接口继承CrudRepository、JpaSpecificationExecutor、Custom接口

public interface CustomRepository extends

 CrudRepository<TestEntity, Long>,JpaSpecificationExecutor<TestEntity>,Custom{

}

4、编写测试类

        @Autowired

CustomRepository customRepository;

@RequestMapping("/testAdd")
public String testCustomAdd(){
TestEntity entity = new TestEntity("WangWu","123456");
customRepository.save(entity);
return entity.toString();
}

@RequestMapping("/testCustom")
public String testCustomCount(){
return customRepository.sayHello();
}

5、运行结果

/testAdd:

TestEntity [id=3, userName=WangWu, passWord=123456, email=null, mobile=null, nickName=null]

/testCustom

sayHello

6、需要注意的地方

自定义接口实现类的名称一定是你继承了CrudRepository、JpaSpecificationExecutor、和你自定义接口的接口的名称+Impl。如本文中的:CustomRepositoryImpl=CustomRepository+Impl。


Custom.java内容


CustomRepository.java内容


CustomRepositoryImpl.java内容


如果名称不对则会报:实体里找不到sayHello方法。

完。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个关于Spring Boot整合数据库的问题,我会为您解答。 整合Spring Data JPA的基本流程如下: 1.在pom.xml中添加Spring Data JPA依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> ``` 2.在application.properties文件中配置数据库连接信息: ``` spring.datasource.url=jdbc:mysql://localhost:3306/db_name spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 3.创建实体类,使用@Entity注解标注实体类,使用@Id注解标注主键。 4.定义Repository接口,使用@Repository注解标注Repository接口,并继承JpaRepository或者CrudRepository接口。 5.在Service层中使用@Autowired注解注入Repository接口,并调用相应的方法实现对数据库的操作。 整合Redis的基本流程如下: 1.在pom.xml中添加Redis依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2.在application.properties文件中配置Redis连接信息: ``` spring.redis.host=localhost spring.redis.port=6379 ``` 3.创建RedisTemplate实例,使用@Autowired注解注入RedisTemplate。 4.使用RedisTemplate的api实现对Redis数据库的操作。 关于设计一个数据库项目,可以按照以下步骤进行: 1.确定项目需求,确定所需要的实体类。 2.根据实体类定义数据库表结构,在数据库中创建相应的表。 3.编写实体类,使用注解标注实体类的属性,使其与数据库表字段对应。 4.编写Repository接口,用于操作数据库,使用注解标注Repository接口。 5.编写Service层,使用注解注入Repository接口,调用Repository接口中的方法实现对数据库的操作。 6.在Controller层中接收请求,调用Service层中的方法,返回相应的数据。 最后,您可以通过注解、定义实体类和自定义Repository接口来完成数据整合实验。如果您还有其他问题,请继续提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值