SpringBoot运用Spring-Data-Jpa实现CRUD实现
分类:
IT文章
•
2024-12-30 21:55:49
下面来演示下SpringBoot下,实用Spring-Data-Jpa来实现CRUD操作,视图层采用Freemarker
这里我们先把application.properties修改成application.yml 主流格式
内容也改成yml规范格式:
1 server:
2 port: 8888
3 context-path: /
4
5 helloWorld: spring Bootu4F60u597D
6
7 msyql:
8 jdbcName: com.mysql.jdbc.Driver
9 dbUrl: jdbc:mysql://localhost:3306/db_diary
10 userName: root
11 password: 123456
12
13 spring:
14 datasource:
15 driver-class-name: com.mysql.jdbc.Driver
16 url: jdbc:mysql://localhost:3306/db_book
17 username: root
18 password: passwd
19 jpa:
20 hibernate.ddl-auto: update
21 show-sql: true
View Code
yml格式有个注意点 冒号后面一定要加个空格
还有我们把context-path改成/方便开发应用
先写一个BookDao接口
1 package com.hik.dao;
2
3 import org.springframework.data.jpa.repository.JpaRepository;
4
5 import com.hik.entity.Book;
6
7 /**
8 * 图书Dao接口
9 * @author jed
10 *
11 */
12 public interface BookDao extends JpaRepository<Book, Integer>{
13
14 }
View Code
要求实现JpaRepository,JpaRepository是继承PagingAndSortingRepository,PagingAndSortingRepository是继承CrudRepository。CrudRepository实现了实体增删改查操作
1 /*
2 * Copyright 2008-2011 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.springframework.data.repository;
17
18 import java.io.Serializable;
19
20 /**
21 * Interface for generic CRUD operations on a repository for a specific type.
22 *
23 * @author Oliver Gierke
24 * @author Eberhard Wolff
25 */
26 @NoRepositoryBean
27 public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
28
29 /**
30 * Saves a given entity. Use the returned instance for further operations as the save operation might have changed the
31 * entity instance completely.
32 *
33 * @param entity
34 * @return the saved entity
35 */
36 <S extends T> S save(S entity);
37
38 /**
39 * Saves all given entities.
40 *
41 * @param entities
42 * @return the saved entities
43 * @throws IllegalArgumentException in case the given entity is {@literal null}.
44 */
45 <S extends T> Iterable<S> save(Iterable<S> entities);
46
47 /**
48 * Retrieves an entity by its id.
49 *
50 * @param id must not be {@literal null}.
51 * @return the entity with the given id or {@literal null} if none found
52 * @throws IllegalArgumentException if {@code id} is {@literal null}
53 */
54 T findOne(ID id);
55
56 /**
57 * Returns whether an entity with the given id exists.
58 *
59 * @param id must not be {@literal null}.
60 * @return true if an entity with the given id exists, {@literal false} otherwise
61 * @throws IllegalArgumentException if {@code id} is {@literal null}
62 */
63 boolean exists(ID id);
64
65 /**
66 * Returns all instances of the type.
67 *
68 * @return all entities
69 */
70 Iterable<T> findAll();
71
72 /**
73 * Returns all instances of the type with the given IDs.
74 *
75 * @param ids
76 * @return
77 */
78 Iterable<T> findAll(Iterable<ID> ids);
79
80 /**
81 * Returns the number of entities available.
82 *
83 * @return the number of entities
84 */
85 long count();
86
87 /**
88 * Deletes the entity with the given id.
89 *
90 * @param id must not be {@literal null}.
91 * @throws IllegalArgumentException in case the given {@code id} is {@literal null}
92 */
93 void delete(ID id);
94
95 /**
96 * Deletes a given entity.
97 *
98 * @param entity
99 * @throws IllegalArgumentException in case the given entity is {@literal null}.
100 */
101 void delete(T entity);
102
103 /**
104 * Deletes the given entities.
105 *
106 * @param entities
107 * @throws IllegalArgumentException in case the given {@link Iterable} is {@literal null}.
108 */
109 void delete(Iterable<? extends T> entities);
110
111 /**
112 * Deletes all entities managed by the repository.
113 */
114 void deleteAll();
115 }