如果想快速搭建后台跨域使用这些技术
- 反射
- mybatis-plus
- json
反射可以实现动态数据的传输
一般对数据库进行操作肯定离不开这些代码
如果我们用反射机制只需要这一个就行
而说到反射的好处,一般情况下我们做增删改查需要大量的接口才能完成,而用反射我们需要一个接口就能完成,如果觉得代码臃肿可以进行细分为四个接口分别是增删改查四个接口。
这是数据库的数据
数据库信息
查询Student数据
现在我要查询Studnet里第一条数据,我可以用json传给后台的数据如下
{
"class":"Student","type":"getOne","data":{
"Sno":060101}}
查询结果
查询Couser数据
再比如我要查询Couser的第一条数据,我可以传入如下的json数据
{
"class":"Course","type":"getOne","data":{
"Cno":"C01"}}
查询结果
查询所有数据
要是查询Student的list数据可以输入这个json
{
"class":"Student","type":"list"}
查询结果
加入条件查询所有数据
要是想分页并且查询的数男生可以输入这个json
{
"class":"Student","type":"list","data":{
"Sno":060101,"Sname":"钟文辉","Ssex":"男"},"condition":{
"page":"1,2"}}
查询结果
或者查询第二页
模糊查询
如果你想要模糊查询可以输入这个json
{
"class":"Student","type":"like","condition":{
"like":"Sname:文"}}
查询结果
模糊查询并加入查询条件
要输想在模糊查询中查询男生可以输入这个json
{
"class":"Student","type":"like","data":{
"Ssex":"男"},"condition":{
"like":"Sname:文"}}
查询结果
向Student表插入数据
如果想Student表里插入数据可以这样
[{
"key":"data","value":"{\"class\":\"Student\",\"type\":\"save\",\"data\":{\"Sno\":06010123,\"Sname\":\"樱木花道\",\"Ssex\":\"男\"}}","description":"","type":"default","enabled":true}]
向Course表插入数据
如果想Course表里插入数据可以这样
{
"class":"Course","type":"save","data":{
"Cno":"C123","Cname":"数学"}}
结果如下
修改数据
如果想修改Student中新加的数据
{
"class":"Student","type":"update","data":{
"Sno":"6010123","Sname":"三井寿"}}
删除数据
如果要删除Student表的数据
[{
"key":"data","value":"{\"class\":\"Student\",\"type\":\"remove\",\"data\":{\"removeId\":\"6010123\"}}","description":"","type":"default","enabled":true}]
结果
结论
这些所有的数据操作都是来自这个接口,无需写其他接口即可完成一系列的数据库操作。如果想快速完成一个网站可以使用反射+mybatis-plus+json来完成。
代码
直接复制代码就能使用了
ReflexController
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.context.ApplicationContext;
import java.io.Serializable;
import java.lang.reflect.Method;
public class ReflexController {
public static Object Controller(ApplicationContext applicationContext,String data) throws Exception {
JSONObject jsonObject = JSONObject.parseObject(data);
JSONObject returnData = JSONObject.parseObject(String.valueOf(jsonObject.get("data")));
JSONObject conditionData = JSONObject.parseObject(String.valueOf(jsonObject.get("condition")));
Class cls = ClassNameUtil.serviceClassMap.get(jsonObject.get("class"));
Object obj = applicationContext.getBean(cls);
// {"type":"getOne"}
if (jsonObject.get("type").equals("getOne")){
QueryWrapper queryWrapper = new QueryWrapper<>();
for (String s : returnData.keySet()) {
queryWrapper.eq(s,returnData.get(s));
}
Method getOne = cls.getMethod("getOne", Wrapper.class);
getOne.setAccessible(true);
return getOne.invoke(obj,queryWrapper);
}
else if (jsonObject.get("type").equals("list")){
// 获取全部集合
// 有判断条件 {"type":"list","data":{"Sname":"钟文辉","Ssex":"男"}}
QueryWrapper queryWrapper = new QueryWrapper<>();
if (jsonObject.containsKey("data")){
// QueryWrapper queryWrapper = new QueryWrapper<>();
for (String s : returnData.keySet()) {
queryWrapper.eq(s,returnData.get(s));
}
// 有条件 例如分页 limit
// {"type":"list","data":{,"Ssex":"男"},"condition":{"page":"1,3"}}
if (jsonObject.containsKey("condition")){
return Condition.isConditionData(conditionData,cls,obj,queryWrapper);
}
// 没有条件 {"type":"list","data":{"Sname":"钟文辉","Ssex":"男"}}
Method list = cls.getMethod("list", Wrapper.class);
list.setAccessible(true);
return list.invoke(obj,queryWrapper);
}
// 没有判断条件 但有条件 {"type":"list","condition":{"page":"1,3"}}
if (jsonObject.containsKey("condition")){
return Condition.isConditionData(conditionData,cls,obj,queryWrapper);
}
// {"type":"list"}
Method list = cls.getMethod("list",Wrapper.class);
list.setAccessible(true);
return list.invoke(obj,queryWrapper);
}
// {"type":"like"}
else if (jsonObject.get("type").equals("like")){
QueryWrapper queryWrapper = new QueryWrapper<>();
// {"type":"like","data":{"Sno":060101,"Sname":"钟文辉"}}
if (jsonObject.containsKey("data")){
for (String s : returnData.keySet()) {
queryWrapper.eq(s,returnData.get(s));
}
}
// {"type":"like","data":{"Ssex":"男"},"condition":{"like":"Sname:嘉"}}
if (jsonObject.containsKey("condition")){
return Condition.isConditionData(conditionData,cls,obj,queryWrapper);
}
}
else if (jsonObject.get("type").equals("count")){
// 获取数量
// {"class":"Student","type":"count","data":{"Ssex":"男"}}
QueryWrapper queryWrapper = new QueryWrapper();
if (jsonObject.containsKey("data")){
for (String s : returnData.keySet()) {
queryWrapper.eq(s,returnData.get(s));
}
}
Method count = cls.getMethod("count",Wrapper.class);
count.setAccessible(true);
return count.invoke(obj,queryWrapper);
}else if (jsonObject.get("type").equals("save")){
// 插入数据
Method getOne = cls.getMethod("save", Object.class);
getOne.setAccessible(true);
getOne.invoke(obj,returnData.toJavaObject(ClassNameUtil.entityClassMap.get(jsonObject.get("class"))));
}else if (jsonObject.get("type").equals("update")){
//更新数据
Method getOne = cls.getMethod("updateById", Object.class);
getOne.setAccessible(true);
getOne.invoke(obj,returnData.toJavaObject(ClassNameUtil.entityClassMap.get(jsonObject.get("class"))));
}else if (jsonObject.get("type").equals("remove")){
//删除数据
Method getOne = cls.getMethod("removeById", Serializable.class);
getOne.setAccessible(true);
getOne.invoke(obj,returnData.get("removeId"));
}
return null;
}
}
Condition
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Condition {
// 有data查询数据库的条件并且condition是分页 例如 select * from student where Sname = "廖嘉乐" limit 1,3
public static Object isConditionData(JSONObject conditionData,Class cls,Object obj, QueryWrapper queryWrapper)
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
if (conditionData.containsKey("page")){
String p = (String) conditionData.get("page");
Page page = new Page<>(Integer.parseInt(p.split(",")[0]),Integer.parseInt(p.split(",")[1]));
Method method = cls.getMethod("page", IPage.class,Wrapper.class);
method.setAccessible(true);
if (conditionData.containsKey("like")||conditionData.containsKey("llike")||conditionData.containsKey("rlike")){
method.invoke(obj,page,dimQuery(conditionData,queryWrapper));
}
return method.invoke(obj,page,queryWrapper);
}
//{"type":"like","data":{"Ssex":"男"},"condition":{"like":"Sname:嘉"}}
Method method = cls.getMethod("list", Wrapper.class);
method.setAccessible(true);
return method.invoke(obj,dimQuery(conditionData,queryWrapper));
}
public static QueryWrapper dimQuery(JSONObject conditionData, QueryWrapper queryWrapper){
if (conditionData.containsKey("like")){
// 例子 {like:"Sname:嘉"} %嘉%
String[] split = String.valueOf(conditionData.get("like")).split(":");
queryWrapper.like(split[0],split[1]);
return queryWrapper;
}else if (conditionData.containsKey("llike")){
// 例如 Sname like %嘉
String[] split = String.valueOf(conditionData.get("llike")).split(":");
queryWrapper.likeLeft(split[0],split[1]);
return queryWrapper;
}else if (conditionData.containsKey("rlike")) {
// 例如 Sname like 嘉%
String[] split = String.valueOf(conditionData.get("rlike")).split(":");
queryWrapper.likeRight(split[0], split[1]);
return queryWrapper;
}
return null;
}
}
ClassNameUtil
import com.example.adminproject.utils.ClassNameUtil;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class MapCommandRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// 存储反射提高性能
// 存储service和entity
List serviceClassByPackage = ClassNameUtil.getClassByPackage(ClassNameUtil.serviceClass);
List entityClassByPackage = ClassNameUtil.getClassByPackage(ClassNameUtil.entityClass);
for (int i = 0; i < serviceClassByPackage.size(); i++) {
String service = String.valueOf(serviceClassByPackage.get(i)).replaceAll("Service", "");
ClassNameUtil.serviceClassMap
.put(service, ClassNameUtil.serviceName(ClassNameUtil.serviceClass, service));
}
for (int i = 0; i < entityClassByPackage.size(); i++) {
String entity = String.valueOf(entityClassByPackage.get(i));
ClassNameUtil.entityClassMap
.put(entity, ClassNameUtil.entityName(ClassNameUtil.entityClass,entity));
}
}
}
MapCommandRunner
import com.example.adminproject.utils.ClassNameUtil;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class MapCommandRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// 存储反射提高性能
// 存储service和entity
List serviceClassByPackage = ClassNameUtil.getClassByPackage(ClassNameUtil.serviceClass);
List entityClassByPackage = ClassNameUtil.getClassByPackage(ClassNameUtil.entityClass);
for (int i = 0; i < serviceClassByPackage.size(); i++) {
String service = String.valueOf(serviceClassByPackage.get(i)).replaceAll("Service", "");
ClassNameUtil.serviceClassMap
.put(service, ClassNameUtil.serviceName(ClassNameUtil.serviceClass, service));
}
for (int i = 0; i < entityClassByPackage.size(); i++) {
String entity = String.valueOf(entityClassByPackage.get(i));
ClassNameUtil.entityClassMap
.put(entity, ClassNameUtil.entityName(ClassNameUtil.entityClass,entity));
}
}
}
controller
@Autowired
private ApplicationContext applicationContext;
@PostMapping("/getData")
public Object getData(String data) throws Exception {
return ReflexController.Controller(applicationContext,data);
}
转载:https://blog.csdn.net/weixin_46909573/article/details/129207467
查看评论