如果想快速搭建后台跨域使用这些技术
- 反射
- 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来完成。
代码
我现在只完成了一部分的数据库操作,后面会进行完善,到时候搭建后台只需要复制到项目就可以了,附上一部分核心的代码
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.example.adminproject.entity.Student;
import com.example.adminproject.service.CourseService;
import com.example.adminproject.service.ScService;
import com.example.adminproject.service.StudentService;
import com.example.adminproject.utils.ClassNameUtil;
import com.example.adminproject.utils.Condition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RestController;
import sun.reflect.misc.ReflectUtil;import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;/*** <p>* 前端控制器* </p>** @author Talisman* @since 2023-02-22*/
@RestController
@Component
public class StudentController {@Autowiredprivate StudentService service;@Autowiredprivate CourseService courseService;@Autowiredprivate ScService scService;@Autowiredprivate ApplicationContext applicationContext;public static HashMap<String,Class>serviceClassMap = new HashMap<>();public static HashMap<String,Class>entityClassMap = new HashMap<>();@PostMapping("/getData")public Object getData(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 = 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);getOne.invoke(obj,queryWrapper);return cls.getMethod("getOne",Wrapper.class).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":"男"}}return service.list(queryWrapper);}// 没有判断条件 但有条件 {"type":"list","condition":{"page":"1,3"}}if (jsonObject.containsKey("condition")){return Condition.isConditionData(conditionData,cls,obj,queryWrapper);}// {"type":"list"}return service.list(null);}// {"type":"like"}else if (jsonObject.get("type").equals("like")){QueryWrapper<Student> 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")){ // 获取数量return service.count();}else if (jsonObject.get("type").equals("save")){ // 插入数据Method getOne = cls.getMethod("save", Object.class);getOne.setAccessible(true);getOne.invoke(obj,returnData.toJavaObject(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(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;}
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;import java.io.File;import java.io.File;
import java.net.URL;
import java.util.Enumeration;
import java.util.List;/*** @author lingkang* @date 2021/12/3*/
public class ClassUtils {// getClassName("com.example.adminproject.entity")public static List getClassByPackage(String packageName) {try {Enumeration<URL> resources = ClassUtils.class.getClassLoader().getResources(packageName.replaceAll("\\.", "/"));while (resources.hasMoreElements()) {URL url = resources.nextElement();String[] file = new File(url.getFile()).list();ArrayList<String> list = new ArrayList<>();for (int i = 0; i < file.length; i++) {if (file[i].indexOf(".class")!=-1){list.add(file[i].replaceAll("\\.class", ""));}}return list;}} catch (Exception e) {e.printStackTrace();}return null;}}