boolean insert(Object o);
Object byId(Integer id);
boolean del(Integer id);
boolean editor(Integer interfaces, Object o;
通常我们的创建流程是这样子的.
伪代码如下
boolean b = mapper.save(s)
if (b){
redis.save(s)
}
此处以byId为例. where 条件不易放入 redis 作为 key ,如果作为 key 数据量会过多
通常操作
伪代码
Object o =null
o = redis.read(id)
if (o ==null) {
o = mapper.read(id)
}
通常操作
伪代码
redis.del()
mapper.update(o)
redis.save(o)
redis.del()
mapper.del(o)
上述的操作大部分内容相似度很高, 在日常编写的时候我们可能会经常写这样子的代码.代码量过多, 以及代码复用率不是那么好.因此对其做出改良.
抽象数据库操作和缓存操作. 其操作内容就是 CRUD 几个基本操作. 再次强调:单表操作
在抽象 crud 基本操作前需要在提出一个问题.
IdInterface
它用来标识这是一个 id 接口public interface IdInterface {
}
id()
用来返回具体的 id 值public interface IntIdInterface extends IdInterface {
int id();
}
public interface StrIdInterface extends IdInterface {
String id();
}
public interface DbOperation<T, I extends IdInterface> {
boolean insert(T t);
T byId(I interfaces);
boolean del(I interfaces);
boolean editor(I interfaces, T t);
}
代码解释, 此处使用泛型 T
来标识接受的类型, I 来标识接收的接口, 这里存在2中情况. 第一种 I = IntIdInterface , 第二种 I = StrIdInterface
继续定义 redis 的操作
public interface RedisOperation<T, I extends IdInterface> {
void insert(T t);
void update(I i, T t);
void del(I i);
T byId(I i);
}
public abstract class RedisHashKeyOperation<T> {
Gson gson = new Gson();
@Autowired
private StringRedisTemplate redisTemplate;
protected void update(String key, String id, T t) {
T redisObj = this.byId(key, id, t.getClass());
if (!Objects.isNull(redisObj)) {
// 如果是redis中的类型和当前传入的类型相同
if (redisObj.getClass().equals(t.getClass())) {
this.insert(key, id, t);
}
}
}
protected void insert(String key, String id, T t) {
redisTemplate.opsForHash().put(key, id, gson.toJson(t));
}
protected T byId(String key, String id, Class<?> clazz) {
String o = (String) redisTemplate.opsForHash().get(key, id);
return (T) gson.fromJson(o, clazz);
}
protected void delete(String key, String id) {
this.redisTemplate.opsForHash().delete(key, id);
}
}
一般的对 redis hash 操作基本就是这样子的. 不会有过多的修改.
创建两个表来使用以下. 实体对象 mapper 不在此进行展开代码
CREATE TABLE `project_int` (
`id` int(32) NOT NULL AUTO_INCREMENT,
`name` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
CREATE TABLE `project_str` (
`id` varchar(50) COLLATE utf8mb4_bin NOT NULL,
`name` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
@Service("ProjectIntDbOperationImpl")
public class ProjectIntDbOperationImpl implements DbOperation<ProjectInt, IntIdInterface> {
@Autowired
private ProjectIntMapper projectIntMapper;
@Override
public boolean insert(ProjectInt projectInt) {
return projectIntMapper.insert(projectInt) > 0;
}
@Override
public ProjectInt byId(IntIdInterface interfaces) {
return projectIntMapper.selectByPrimaryKey(interfaces.id());
}
@Override
public boolean del(IntIdInterface interfaces) {
return projectIntMapper.deleteByPrimaryKey(interfaces.id()) > 0;
}
@Override
public boolean editor(IntIdInterface interfaces, ProjectInt projectInt) {
// 更新存在策略
ProjectInt projectInt1 = this.byId(interfaces);
projectInt1.setName(projectInt.getName());
return this.projectIntMapper.updateByPrimaryKey(projectInt1) > 0;
}
}
@Service("ProjectIntRedisOperationImpl")
public class ProjectIntRedisOperationImpl
extends RedisHashKeyOperation<ProjectInt>
implements
RedisOperation<ProjectInt, IntIdInterface> {
public static final String CACHE_PROJECT_INT = "cache:project_int";
public void insert(ProjectInt projectInt) {
super.insert(CACHE_PROJECT_INT, String.valueOf(projectInt.getId()), projectInt);
}
public void update(IntIdInterface strIdInterface, ProjectInt projectInt) {
super.update(CACHE_PROJECT_INT, String.valueOf(strIdInterface.id()), projectInt);
}
public void del(IntIdInterface strIdInterface) {
super.delete(CACHE_PROJECT_INT, String.valueOf(strIdInterface.id()));
}
public ProjectInt byId(IntIdInterface intIdInterface) {
return super.byId(CACHE_PROJECT_INT, String.valueOf(intIdInterface.id()), ProjectInt.class);
}
}
@Service("ProjectStrDbOperationImpl")
public class ProjectStrDbOperationImpl implements DbOperation<ProjectStr, StrIdInterface> {
@Autowired
private ProjectStrMapper projectStrMapper;
@Override
public boolean insert(ProjectStr projectInt) {
return projectStrMapper.insert(projectInt) > 0;
}
@Override
public ProjectStr byId(StrIdInterface interfaces) {
return projectStrMapper.selectByPrimaryKey(interfaces.id());
}
@Override
public boolean del(StrIdInterface interfaces) {
return projectStrMapper.deleteByPrimaryKey(interfaces.id()) > 0;
}
@Override
public boolean editor(StrIdInterface interfaces, ProjectStr projectInt) {
// 更新存在策略
ProjectStr projectInt1 = this.byId(interfaces);
projectInt1.setName(projectInt.getName());
return this.projectStrMapper.updateByPrimaryKey(projectInt1) > 0;
}
}
@Service("ProjectStrRedisOperationImpl")
public class ProjectStrRedisOperationImpl
extends RedisHashKeyOperation<ProjectStr>
implements
RedisOperation<ProjectStr, StrIdInterface> {
public static final String CACHE_PROJECT_INT = "cache:project_int";
public void insert(ProjectStr projectStr) {
super.insert(CACHE_PROJECT_INT, String.valueOf(projectStr.getId()), projectStr);
}
public void update(StrIdInterface strIdInterface, ProjectStr projectStr) {
super.update(CACHE_PROJECT_INT, String.valueOf(strIdInterface.id()), projectStr);
}
public void del(StrIdInterface strIdInterface) {
super.delete(CACHE_PROJECT_INT, String.valueOf(strIdInterface.id()));
}
public ProjectStr byId(StrIdInterface intIdInterface) {
return super.byId(CACHE_PROJECT_INT, String.valueOf(intIdInterface.id()), ProjectStr.class);
}
}
public interface ByIdOperationFacade<T, I extends IdInterface> {
boolean insert(T t);
T byId(I i);
boolean del(I i);
boolean editor(I i, T t);
DbOperation getDbOperation();
}
public class OperationCollections {
private DbOperation dbOperation;
private RedisOperation redisOperation;
}
public interface ByIdOperationFactory {
OperationCollections factory(Class<?> clazz);
}
DbOperation
和 RedisOperation
添加一个函数用来获取类型@Service("ByIdOperationFactoryImpl")
public class ByIdOperationFactoryImpl implements ByIdOperationFactory {
static Map<Class, DbOperation> dbOperationMap = new HashMap<>();
static Map<Class, RedisOperation> redisOperationHashMap = new HashMap<>();
@Autowired
private ApplicationContext context;
@PostConstruct
public void init() {
Map<String, DbOperation> beansOfType = context.getBeansOfType(DbOperation.class);
beansOfType.forEach(
(k, v) -> {
Class type = v.type();
dbOperationMap.put(type, v);
}
);
Map<String, RedisOperation> beansOfType1 = context.getBeansOfType(RedisOperation.class);
beansOfType1.forEach((k, v) -> {
Class type = v.type();
redisOperationHashMap.put(type, v);
});
}
@Override
public OperationCollections factory(Class<?> clazz) {
OperationCollections operationCollections = new OperationCollections();
DbOperation dbOperation = dbOperationMap.get(clazz);
RedisOperation redisOperation = redisOperationHashMap.get(clazz);
operationCollections.setDbOperation(dbOperation);
operationCollections.setRedisOperation(redisOperation);
return operationCollections;
}
}
public abstract class CommonByIdOperation<T, I extends IdInterface>
implements ByIdOperationFacade<T, I> {
@Autowired
@Qualifier("ByIdOperationFactoryImpl")
ByIdOperationFactory byIdOperationFactory;
@Override
public boolean insert(T t) {
throw new RuntimeException("没有实现");
}
@Override
public DbOperation getDbOperation() {
throw new RuntimeException("没有实现");
}
public boolean editor(I i, T t) {
boolean editor = false;
OperationCollections operationCollections = this.operationCollection();
RedisOperation redisOperation = operationCollections.getRedisOperation();
if (redisOperation != null) {
redisOperation.del(i);
}
DbOperation dbOperation = operationCollections.getDbOperation();
if (dbOperation != null) {
editor = dbOperation.editor(i, t);
}
if (redisOperation != null) {
redisOperation.insert(t);
}
return editor;
}
public boolean del(I i) {
boolean del = false;
OperationCollections operationCollections = this.operationCollection();
RedisOperation redisOperation = operationCollections.getRedisOperation();
if (redisOperation != null) {
redisOperation.del(i);
}
DbOperation dbOperation = operationCollections.getDbOperation();
if (dbOperation != null) {
del = dbOperation.del(i);
}
return del;
}
public T byId(I i) {
T result = null;
RedisOperation redisOperation = this.operationCollection().getRedisOperation();
if (redisOperation != null) {
result = (T) redisOperation.byId(i);
}
DbOperation dbOperation = this.operationCollection().getDbOperation();
if (dbOperation != null) {
if (result == null) {
System.out.println("从数据库获取");
result = (T) dbOperation.byId(i);
}
}
return result;
}
public OperationCollections operationCollection() {
return byIdOperationFactory.factory(clazz());
}
protected Class<?> clazz() {
throw new IllegalArgumentException("类型异常");
}
}
public class BeanCopy {
public static void main(String[] args) {
StrId strId = new StrId();
strId.setId("str_id");
CpId cpId = new CpId();
BeanUtils.copyProperties(strId, cpId);
System.out.println(cpId.getId());
}
@Data
static class CpId {
private Object id;
}
@Data
static class StrId {
private String id;
}
}
@Service("ProjectIntDbOperationImpl")
public class ProjectIntDbOperationImpl implements DbOperation<ProjectInt, IntIdInterface> {
@Autowired
private ProjectIntMapper projectIntMapper;
@Override
public boolean insert(ProjectInt projectInt) {
return projectIntMapper.insert(projectInt) > 0;
}
@Override
public ProjectInt byId(IntIdInterface interfaces) {
return projectIntMapper.selectByPrimaryKey(interfaces.id());
}
@Override
public boolean del(IntIdInterface interfaces) {
return projectIntMapper.deleteByPrimaryKey(interfaces.id()) > 0;
}
@Override
public boolean editor(IntIdInterface interfaces, ProjectInt projectInt) {
// 更新存在策略
ProjectInt projectInt1 = this.byId(interfaces);
projectInt1.setName(projectInt.getName());
return this.projectIntMapper.updateByPrimaryKey(projectInt1) > 0;
}
@Override
public Class<?> type() {
return ProjectInt.class;
}
}
@Service("ProjectStrFacade")
public class ProjectStrFacade extends CommonByIdOperation<ProjectStr, StrIdInterface> implements
ByIdOperationFacade<ProjectStr, StrIdInterface> {
@Override
public boolean insert(ProjectStr projectInt) {
DbOperation dbOperation = this.getDbOperation();
boolean insert = false;
if (dbOperation != null) {
insert = dbOperation.insert(projectInt);
}
RedisOperation redisOperation = this.operationCollection().getRedisOperation();
if (redisOperation != null) {
redisOperation.insert(projectInt);
}
return insert;
}
@Override
public ProjectStr byId(StrIdInterface strIdInterface) {
return super.byId(strIdInterface);
}
@Override
public boolean del(StrIdInterface strIdInterface) {
return super.del(strIdInterface);
}
public boolean editor(StrIdInterface strIdInterface, ProjectStr projectInt) {
return super.editor(strIdInterface, projectInt);
}
@Override
public DbOperation getDbOperation() {
return this.operationCollection().getDbOperation();
}
@Override
protected Class<?> clazz() {
return ProjectStr.class;
}
}
@SpringBootTest
class DemoApplicationTests {
Gson gson = new Gson();
@Autowired
private ProjectStrMapper projectStrMapper;
@Autowired
@Qualifier("projectIntFacade")
private ByIdOperationFacade<ProjectInt, IntIdInterface> byIdOperationFacade;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void testInsert() {
ProjectInt projectInt = new ProjectInt();
projectInt.setName("JJJ");
this.byIdOperationFacade.insert(projectInt);
}
@Test
void testUpdate() {
ProjectInt projectInt = this.byIdOperationFacade.byId(new IntIdInterface() {
@Override
public int id() {
return 1;
}
});
projectInt.setName("update");
this.byIdOperationFacade.editor(new IntIdInterface() {
@Override
public int id() {
return projectInt.getId();
}
}, projectInt);
}
@Test
void testDel() {
this.byIdOperationFacade.del(new IntIdInterface() {
@Override
public int id() {
return 1;
}
});
}
@Test
void testById() {
ProjectInt projectInt = this.byIdOperationFacade.byId(new IntIdInterface() {
@Override
public int id() {
return 1;
}
});
System.out.println();
}
}
@Autowired
@Qualifier("projectIntFacade")
private ByIdOperationFacade<ProjectInt, IntIdInterface> byIdOperationFacade;
boolean insert(Object o);
Object byId(Integer id);
boolean del(Integer id);
boolean editor(Integer interfaces, Object o;
通常我们的创建流程是这样子的.
伪代码如下
boolean b = mapper.save(s)
if (b){
redis.save(s)
}
此处以byId为例. where 条件不易放入 redis 作为 key ,如果作为 key 数据量会过多
通常操作
伪代码
Object o =null
o = redis.read(id)
if (o ==null) {
o = mapper.read(id)
}
通常操作
伪代码
redis.del()
mapper.update(o)
redis.save(o)
redis.del()
mapper.del(o)
上述的操作大部分内容相似度很高, 在日常编写的时候我们可能会经常写这样子的代码.代码量过多, 以及代码复用率不是那么好.因此对其做出改良.
抽象数据库操作和缓存操作. 其操作内容就是 CRUD 几个基本操作. 再次强调:单表操作
在抽象 crud 基本操作前需要在提出一个问题.
IdInterface
它用来标识这是一个 id 接口public interface IdInterface {
}
id()
用来返回具体的 id 值public interface IntIdInterface extends IdInterface {
int id();
}
public interface StrIdInterface extends IdInterface {
String id();
}
public interface DbOperation<T, I extends IdInterface> {
boolean insert(T t);
T byId(I interfaces);
boolean del(I interfaces);
boolean editor(I interfaces, T t);
}
代码解释, 此处使用泛型 T
来标识接受的类型, I 来标识接收的接口, 这里存在2中情况. 第一种 I = IntIdInterface , 第二种 I = StrIdInterface
继续定义 redis 的操作
public interface RedisOperation<T, I extends IdInterface> {
void insert(T t);
void update(I i, T t);
void del(I i);
T byId(I i);
}
public abstract class RedisHashKeyOperation<T> {
Gson gson = new Gson();
@Autowired
private StringRedisTemplate redisTemplate;
protected void update(String key, String id, T t) {
T redisObj = this.byId(key, id, t.getClass());
if (!Objects.isNull(redisObj)) {
// 如果是redis中的类型和当前传入的类型相同
if (redisObj.getClass().equals(t.getClass())) {
this.insert(key, id, t);
}
}
}
protected void insert(String key, String id, T t) {
redisTemplate.opsForHash().put(key, id, gson.toJson(t));
}
protected T byId(String key, String id, Class<?> clazz) {
String o = (String) redisTemplate.opsForHash().get(key, id);
return (T) gson.fromJson(o, clazz);
}
protected void delete(String key, String id) {
this.redisTemplate.opsForHash().delete(key, id);
}
}
一般的对 redis hash 操作基本就是这样子的. 不会有过多的修改.
创建两个表来使用以下. 实体对象 mapper 不在此进行展开代码
CREATE TABLE `project_int` (
`id` int(32) NOT NULL AUTO_INCREMENT,
`name` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
CREATE TABLE `project_str` (
`id` varchar(50) COLLATE utf8mb4_bin NOT NULL,
`name` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
@Service("ProjectIntDbOperationImpl")
public class ProjectIntDbOperationImpl implements DbOperation<ProjectInt, IntIdInterface> {
@Autowired
private ProjectIntMapper projectIntMapper;
@Override
public boolean insert(ProjectInt projectInt) {
return projectIntMapper.insert(projectInt) > 0;
}
@Override
public ProjectInt byId(IntIdInterface interfaces) {
return projectIntMapper.selectByPrimaryKey(interfaces.id());
}
@Override
public boolean del(IntIdInterface interfaces) {
return projectIntMapper.deleteByPrimaryKey(interfaces.id()) > 0;
}
@Override
public boolean editor(IntIdInterface interfaces, ProjectInt projectInt) {
// 更新存在策略
ProjectInt projectInt1 = this.byId(interfaces);
projectInt1.setName(projectInt.getName());
return this.projectIntMapper.updateByPrimaryKey(projectInt1) > 0;
}
}
@Service("ProjectIntRedisOperationImpl")
public class ProjectIntRedisOperationImpl
extends RedisHashKeyOperation<ProjectInt>
implements
RedisOperation<ProjectInt, IntIdInterface> {
public static final String CACHE_PROJECT_INT = "cache:project_int";
public void insert(ProjectInt projectInt) {
super.insert(CACHE_PROJECT_INT, String.valueOf(projectInt.getId()), projectInt);
}
public void update(IntIdInterface strIdInterface, ProjectInt projectInt) {
super.update(CACHE_PROJECT_INT, String.valueOf(strIdInterface.id()), projectInt);
}
public void del(IntIdInterface strIdInterface) {
super.delete(CACHE_PROJECT_INT, String.valueOf(strIdInterface.id()));
}
public ProjectInt byId(IntIdInterface intIdInterface) {
return super.byId(CACHE_PROJECT_INT, String.valueOf(intIdInterface.id()), ProjectInt.class);
}
}
@Service("ProjectStrDbOperationImpl")
public class ProjectStrDbOperationImpl implements DbOperation<ProjectStr, StrIdInterface> {
@Autowired
private ProjectStrMapper projectStrMapper;
@Override
public boolean insert(ProjectStr projectInt) {
return projectStrMapper.insert(projectInt) > 0;
}
@Override
public ProjectStr byId(StrIdInterface interfaces) {
return projectStrMapper.selectByPrimaryKey(interfaces.id());
}
@Override
public boolean del(StrIdInterface interfaces) {
return projectStrMapper.deleteByPrimaryKey(interfaces.id()) > 0;
}
@Override
public boolean editor(StrIdInterface interfaces, ProjectStr projectInt) {
// 更新存在策略
ProjectStr projectInt1 = this.byId(interfaces);
projectInt1.setName(projectInt.getName());
return this.projectStrMapper.updateByPrimaryKey(projectInt1) > 0;
}
}
@Service("ProjectStrRedisOperationImpl")
public class ProjectStrRedisOperationImpl
extends RedisHashKeyOperation<ProjectStr>
implements
RedisOperation<ProjectStr, StrIdInterface> {
public static final String CACHE_PROJECT_INT = "cache:project_int";
public void insert(ProjectStr projectStr) {
super.insert(CACHE_PROJECT_INT, String.valueOf(projectStr.getId()), projectStr);
}
public void update(StrIdInterface strIdInterface, ProjectStr projectStr) {
super.update(CACHE_PROJECT_INT, String.valueOf(strIdInterface.id()), projectStr);
}
public void del(StrIdInterface strIdInterface) {
super.delete(CACHE_PROJECT_INT, String.valueOf(strIdInterface.id()));
}
public ProjectStr byId(StrIdInterface intIdInterface) {
return super.byId(CACHE_PROJECT_INT, String.valueOf(intIdInterface.id()), ProjectStr.class);
}
}
public interface ByIdOperationFacade<T, I extends IdInterface> {
boolean insert(T t);
T byId(I i);
boolean del(I i);
boolean editor(I i, T t);
DbOperation getDbOperation();
}
public class OperationCollections {
private DbOperation dbOperation;
private RedisOperation redisOperation;
}
public interface ByIdOperationFactory {
OperationCollections factory(Class<?> clazz);
}
DbOperation
和 RedisOperation
添加一个函数用来获取类型@Service("ByIdOperationFactoryImpl")
public class ByIdOperationFactoryImpl implements ByIdOperationFactory {
static Map<Class, DbOperation> dbOperationMap = new HashMap<>();
static Map<Class, RedisOperation> redisOperationHashMap = new HashMap<>();
@Autowired
private ApplicationContext context;
@PostConstruct
public void init() {
Map<String, DbOperation> beansOfType = context.getBeansOfType(DbOperation.class);
beansOfType.forEach(
(k, v) -> {
Class type = v.type();
dbOperationMap.put(type, v);
}
);
Map<String, RedisOperation> beansOfType1 = context.getBeansOfType(RedisOperation.class);
beansOfType1.forEach((k, v) -> {
Class type = v.type();
redisOperationHashMap.put(type, v);
});
}
@Override
public OperationCollections factory(Class<?> clazz) {
OperationCollections operationCollections = new OperationCollections();
DbOperation dbOperation = dbOperationMap.get(clazz);
RedisOperation redisOperation = redisOperationHashMap.get(clazz);
operationCollections.setDbOperation(dbOperation);
operationCollections.setRedisOperation(redisOperation);
return operationCollections;
}
}
public abstract class CommonByIdOperation<T, I extends IdInterface>
implements ByIdOperationFacade<T, I> {
@Autowired
@Qualifier("ByIdOperationFactoryImpl")
ByIdOperationFactory byIdOperationFactory;
@Override
public boolean insert(T t) {
throw new RuntimeException("没有实现");
}
@Override
public DbOperation getDbOperation() {
throw new RuntimeException("没有实现");
}
public boolean editor(I i, T t) {
boolean editor = false;
OperationCollections operationCollections = this.operationCollection();
RedisOperation redisOperation = operationCollections.getRedisOperation();
if (redisOperation != null) {
redisOperation.del(i);
}
DbOperation dbOperation = operationCollections.getDbOperation();
if (dbOperation != null) {
editor = dbOperation.editor(i, t);
}
if (redisOperation != null) {
redisOperation.insert(t);
}
return editor;
}
public boolean del(I i) {
boolean del = false;
OperationCollections operationCollections = this.operationCollection();
RedisOperation redisOperation = operationCollections.getRedisOperation();
if (redisOperation != null) {
redisOperation.del(i);
}
DbOperation dbOperation = operationCollections.getDbOperation();
if (dbOperation != null) {
del = dbOperation.del(i);
}
return del;
}
public T byId(I i) {
T result = null;
RedisOperation redisOperation = this.operationCollection().getRedisOperation();
if (redisOperation != null) {
result = (T) redisOperation.byId(i);
}
DbOperation dbOperation = this.operationCollection().getDbOperation();
if (dbOperation != null) {
if (result == null) {
System.out.println("从数据库获取");
result = (T) dbOperation.byId(i);
}
}
return result;
}
public OperationCollections operationCollection() {
return byIdOperationFactory.factory(clazz());
}
protected Class<?> clazz() {
throw new IllegalArgumentException("类型异常");
}
}
public class BeanCopy {
public static void main(String[] args) {
StrId strId = new StrId();
strId.setId("str_id");
CpId cpId = new CpId();
BeanUtils.copyProperties(strId, cpId);
System.out.println(cpId.getId());
}
@Data
static class CpId {
private Object id;
}
@Data
static class StrId {
private String id;
}
}
@Service("ProjectIntDbOperationImpl")
public class ProjectIntDbOperationImpl implements DbOperation<ProjectInt, IntIdInterface> {
@Autowired
private ProjectIntMapper projectIntMapper;
@Override
public boolean insert(ProjectInt projectInt) {
return projectIntMapper.insert(projectInt) > 0;
}
@Override
public ProjectInt byId(IntIdInterface interfaces) {
return projectIntMapper.selectByPrimaryKey(interfaces.id());
}
@Override
public boolean del(IntIdInterface interfaces) {
return projectIntMapper.deleteByPrimaryKey(interfaces.id()) > 0;
}
@Override
public boolean editor(IntIdInterface interfaces, ProjectInt projectInt) {
// 更新存在策略
ProjectInt projectInt1 = this.byId(interfaces);
projectInt1.setName(projectInt.getName());
return this.projectIntMapper.updateByPrimaryKey(projectInt1) > 0;
}
@Override
public Class<?> type() {
return ProjectInt.class;
}
}
@Service("ProjectStrFacade")
public class ProjectStrFacade extends CommonByIdOperation<ProjectStr, StrIdInterface> implements
ByIdOperationFacade<ProjectStr, StrIdInterface> {
@Override
public boolean insert(ProjectStr projectInt) {
DbOperation dbOperation = this.getDbOperation();
boolean insert = false;
if (dbOperation != null) {
insert = dbOperation.insert(projectInt);
}
RedisOperation redisOperation = this.operationCollection().getRedisOperation();
if (redisOperation != null) {
redisOperation.insert(projectInt);
}
return insert;
}
@Override
public ProjectStr byId(StrIdInterface strIdInterface) {
return super.byId(strIdInterface);
}
@Override
public boolean del(StrIdInterface strIdInterface) {
return super.del(strIdInterface);
}
public boolean editor(StrIdInterface strIdInterface, ProjectStr projectInt) {
return super.editor(strIdInterface, projectInt);
}
@Override
public DbOperation getDbOperation() {
return this.operationCollection().getDbOperation();
}
@Override
protected Class<?> clazz() {
return ProjectStr.class;
}
}
@SpringBootTest
class DemoApplicationTests {
Gson gson = new Gson();
@Autowired
private ProjectStrMapper projectStrMapper;
@Autowired
@Qualifier("projectIntFacade")
private ByIdOperationFacade<ProjectInt, IntIdInterface> byIdOperationFacade;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void testInsert() {
ProjectInt projectInt = new ProjectInt();
projectInt.setName("JJJ");
this.byIdOperationFacade.insert(projectInt);
}
@Test
void testUpdate() {
ProjectInt projectInt = this.byIdOperationFacade.byId(new IntIdInterface() {
@Override
public int id() {
return 1;
}
});
projectInt.setName("update");
this.byIdOperationFacade.editor(new IntIdInterface() {
@Override
public int id() {
return projectInt.getId();
}
}, projectInt);
}
@Test
void testDel() {
this.byIdOperationFacade.del(new IntIdInterface() {
@Override
public int id() {
return 1;
}
});
}
@Test
void testById() {
ProjectInt projectInt = this.byIdOperationFacade.byId(new IntIdInterface() {
@Override
public int id() {
return 1;
}
});
System.out.println();
}
}
@Autowired
@Qualifier("projectIntFacade")
private ByIdOperationFacade<ProjectInt, IntIdInterface> byIdOperationFacade;