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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
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 name;
@Column(name="country_code")
private String countryCode;
@Column
private String district;
@Column
private int population;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
[email protected],來簡化冗余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<City, Long>, CityRepositoryCustom{
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
這裏的CityRepository繼承了2個父類,包括用戶自定義的接口類,讓用戶自定義的接口可以暴漏出來。
這裏的CityRepsoitoryCustom定義了用戶的自定義方法:
public interface CityRepositoryCustom {
public void testAA();
}
- 1
- 2
- 3
- 1
- 2
- 3
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<Object[]> 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]);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
這裏的實現類就是讀取了幾條記錄,然後打印出來。其實現了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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
測試
測試用例:
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();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
測試的結果圖示:
總結
約定大於配置,Custom後綴實現與擴展,非常的簡單實用。
http://www.woaipu.com/shops/zuzhuan/61406
http://nanning.xjwy.cn/f/bencandy.php?fid=43&id=117777
http://nanning.xjwy.cn/f/bencandy.php?fid=43&id=117890
http://nanning.xjwy.cn/f/bencandy.php?fid=43&id=117994
http://nanning.xjwy.cn/f/bencandy.php?fid=43&id=118376
Spring Boot下如何自定義Repository中的DAO方法