Spring Boot下如何自定义Repository中的DAO方法

网友投稿 255 2023-05-11


Spring Boot下如何自定义Repository中的DAO方法

环境配置介绍

jdk 1.8, spring Boot 1.5.3.RELEASE, mysql, Spring Data, JPA

问题描述

Spring Data提供了一套简单易用的DAO层抽象与封装,覆盖的CURD的基本功能,但是在诸多的情况下,需要用户自定义DAO的实现方法,来实现更为复杂和精细的数据库访问操作,该如何来解决这个问题?

目标描述

这里我们以自定义testAA的方法为例,来介绍如何实现自定义的DAO方法扩展。

数据库表的定义

我们这里定义了一个非常简单的mycity表,来作为示例的实体类BaseEntity:

数据库表定义:

import java.util.Date;

import javax.persistence.Column;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.MappedSuperclass;

import javax.persistence.Temporal;

import javax.persistence.TemporalType;

import javax.persistence.Version;

@MappedSuperclass

public abstract class BaseEntity implements java.io.Serializable {

private static final long serialVersionUID = -2420979951576787924L;

@Id

@GeneratedValue(strategy=GenerationType.IDENTITY)

@Column(name = "ID")

private Long id;

@Version

private Long version;

@Temporal(TemporalType.TIMESTAMP)

@Column(name = "CREATE_TIME",columnDefinition="timestamp default CURRENT_TIMESTAMP")

private Date createTime;

@Temporal(TemporalType.TIMESTAMP)

@Column(name = "UPDATE_TIME",columnDefinition="timestamp default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")

private Date updateTime;

}

MyCity的定义如下:

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Table;

import lombok.Data;

@Entity

@Table(name="mycity")

@Data

public class City extends BaseEntity {

private static final long serialVersionUID = -7510771121759944670L;

@Column(name="Name")

private String nhttp://ame;

@Column(name="country_code")

private String countryCode;

@Column

private String district;

@Column

private int population;

}

这里的@Data使用了lombok提供的强大标注,来简化冗余Getter/Setter方法的使用。

定义Repository

标准的CityRepository.Java,这里完全使用缺省提供的方法:

import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.stereotype.Repository;

import com.rose.money.City;

@Repository

public interface CityRepository extends JpaRepository, CityRepositoryCustom{

}

这里的CityRepository继承了2个父类,包括用户自定义的接口类,让用户自定义的接口可以暴漏出来。

这里的CityRepsoitoryCustom定义了用户的自定义方法:

public interface CityRepositoryCustom {

public void testAA();

}

Notice: 这里的Custom后缀是约定的,不能随意修改。

自定义方法的实现类:

import java.util.List;

import javax.persistence.EntityManager;

import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;

public class CityRepositoryImpl implements CityRepositoryCustom {

@Autowired

@PersistenceContext

private EntityManager entityManager;

@Override

public void testAA() {

List cities = entityManager.createNativeQuery("select id, name, district from mycity").getResultList();

for (Object[] objs : cities) {

System.out.print("location 1:" + objs[0]);

System.out.print("location 2:" + objs[1]);

System.out.print("location 3:" + objs[2]);

}

}

}

这里的实现类就是读取了几条记录,然后打印出来。其实现了Custom的接口类。

配置信息

application.properties:

spring.application.name=custom jpa

spring.jpa.database=MYSQL

spring.datasource.username=root

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.password=123456

spring.datasource.url=jdbc:mysql://localhost:3306/world?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=true

spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy

spring.jpa.show-sql=true

测试

测试用例:

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.test.context.junit4.SpringRunner;

import com.rose.money.repository.CityRepository;

@RunWith(SpringRunner.class)

@SpringBootTest

public class CustomjpaApplicationTests {

@Autowired

private CityRepository cityRepo;

@Test

public void contextLoads() {

City city = cityRepo.findOne(1l);

System.out.println("city=>" + city);

cityRepo.testAA();

}

}

测试的结果图示:

总结

约定大于配置,Custom后缀实现与扩展,非常的简单实用。

以上所述是给大家介绍的Spring Boot下如何自定义Repository中的DAO方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:接口实现多个接口(怎么实现多个接口)
下一篇:详解基于webpack搭建react运行环境
相关文章

 发表评论

暂时没有评论,来抢沙发吧~