SpringBoot+JPA@Autowired注⼊泛型成员封装统⼀接⼝这是⽬录
SpringBoot
输⼊java -version得到我的java version "16.0.1"。
cd到l⽂件所在位置即项⽬根⽬录并执⾏指令./mvnw dependency:list来查看引⽤版本:
org.springframework.data:spring-data-jpa:2.5.2
javax.validation:validation-api:2.0.1.Final
com.alibaba:fastjson:1.2.78
org.projectlombok:lombok:1.18.20
org.springframework:spring-web:5.3.8
org.springframework.boot:spring-boot:2.5.2
实体类
Media.java
⼀个很普通的实体类,包名省略。
package;
import JSONField;
import Serializable;
import*;
import*;
@SuppressWarnings({"LombokEqualsAndHashCodeInspection"})
@EqualsAndHashCode
@ToString
@Getter
@Setter
@Entity
@Table(name ="media")
@AllArgsConstructor
@NoArgsConstructor
public class Media implements Serializable
{
@Id
@JSONField(ordinal =1)
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name ="id", insertable =false, updatable =false)
private Integer id;
@JSONField(ordinal =2)
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name ="book_id", updatable =false)
private Integer bookId;
@JSONField(ordinal =3)
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name ="url", length =128)
private String url;
}
持久层
IMediaRepository.java
⼀个啥也没有的接⼝。
package;
import JpaRepository;
import JpaSpecificationExecutor; import Media;
public interface IMediaRepository extends
JpaRepository<Media, Integer>,
JpaSpecificationExecutor<Media>
{
}
服务
写⼀些接⼝。
IService.java
package;
import Nullable;
public interface IService<T, K>
{
@Nullable
T get(K primaryKey);
@Nullable
T addOrUpdate(T obj);
boolean delete(K primaryKey);
}
IQueryService.java
package;
import*;
public interface IQueryService<T>
{
List<T>find(int index,int size);
long count();
List<T>find(int index,int size, Map<String, String> colAndPatterns);
long count(Map<String, String> colAndPatterns);
}
springboot是啥
BaseService.java
抽象服务类。
可以这样写
import Autowired;
import PageRequest;
import Specification;
import*;
import Common;
import*;
import*;
import*;
public abstract class BaseService<T, K, R extends JpaRepository<T, K>& JpaSpecificationExecutor<T>> implements IService<T, K>, IQueryService<T>
{
@Autowired
protected R repo;// 让@Autowired⾃⼰合适的类注⼊
...
@SuppressWarnings({"UnnecessarySemicolon","OverlyBroadCatchBlock"})
@Override
public final T addOrUpdate(@NotNull final T obj)
{
T ret =null;
if(canAddOrUpdate(obj))
{
try
{
ret = repo.saveAndFlush(obj);
}
catch(Exception e)
{
e.printStackTrace();
}
}
return ret;
}
protected abstract boolean canAddOrUpdate(final T obj);
}
还可以这样写
感觉这个⽐上⾯那种要好⼀点,减少⼀个泛型参数。
public abstract class BaseService<T, K>implements IService<T, K>, IQueryService<T> {
@Autowired
protected JpaRepository<T, K> repo;
@Autowired
protected JpaSpecificationExecutor<T> queryRepo;// 少⼀个泛型参数,多注⼊⼀次...
@SuppressWarnings({"UnnecessarySemicolon","OverlyBroadCatchBlock"})
@Override
public final T addOrUpdate(@NotNull final T obj)
{
T ret =null;
if(canAddOrUpdate(obj))
{
try
{
ret = repo.saveAndFlush(obj);
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
;
}
}
return ret;
}
protected abstract boolean canAddOrUpdate(final T obj);
}
MediaService.java
服务类指定了类参数,然后实现之前的BaseService的抽象⽅法。package;
import Service;
import Media;
import IMediaRepository;
@Service(value ="mediaService")
public class MediaService extends BaseService<Media, Integer, IMediaRepository>
{
}
或
```java
package;
import Service;
import Media;
@Service(value ="mediaService")
public class MediaService extends BaseService<Media, Integer> {
...
}
控制器
IController.java
接⼝
package;
import RequestMapping; import RequestMethod;
public interface IController<T, K>
{
@RequestMapping(method = RequestMethod.POST)
String add(T body);
@RequestMapping(method = RequestMethod.PUT)
String update(T body);
@RequestMapping(method = RequestMethod.DELETE)
String delete(K id);
@RequestMapping(value ="/list", method = RequestMethod.GET) String gets(Integer index, Integer size);
@RequestMapping(method = RequestMethod.GET)
String get(K id);
}
BaseController.java
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论