闲来无事情,发现mybatis逆向生成器很方便,但是也很臃肿,特别是service层开发,增删改查一样的逻辑,所以由此抽出来做一个BaseService,可以通用删除,添加,修改,mapper类型那个全类名硬编码,有感兴趣的同学自己修改下,我也是小白,分享一下哈哈,多多回复,评论啊。
BaseService层代码:
package cn.dbw.springboot.springbootwebjsp.service;
import cn.dbw.springboot.springbootwebjsp.controller.BaseController;
import cn.dbw.springboot.springbootwebjsp.utils.SpringContextUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.context.ApplicationContext;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;
/**
* mybatis 通用service
* @author by:dbw
* @param <T>
*/
public abstract class BaseService<T> {
private Class mapperClass=null;
private Class<T> argumentClazz=null;
/**
* 添加
* @param t
* @return
*/
public T add(T t){
Object bean = getApplicationContext().getBean(getMapperType());
try {
Method insert = getMapperType().getMethod("insertSelective", getTypeArguement());
insert.setAccessible(true);
insert.invoke(bean,t);
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
/**
* 删除
* @param id
*/
public void delete(Long id){
Object bean = getApplicationContext().getBean(getMapperType());
try {
Method insert = getMapperType().getMethod("deleteByPrimaryKey", Long.class);
insert.setAccessible(true);
insert.invoke(bean,id);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 更新
* @param t
* @return
*/
public T update(T t){
Object bean = getApplicationContext().getBean(getMapperType());
try {
Method insert = getMapperType().getMethod("updateByPrimaryKeySelective", getTypeArguement());
insert.setAccessible(true);
insert.invoke(bean,t);
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
public T newinstance(Map map){
JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(map));
return jsonObject.toJavaObject(getTypeArguement());
}
/**
* 返回子类泛型参数类型
* @return
*/
public Class<T> getTypeArguement(){
if(null==argumentClazz){
this.argumentClazz = (Class<T>)((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
return argumentClazz;
}
/**
* 获取子类Mapper Class
* @return
*/
public Class getMapperType() {
if(null==mapperClass){
Type type = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
String mapperClassName= type.toString().split(" ")[1].replace("po","mapper")+"Mapper";
try {
this.mapperClass=Class.forName(mapperClassName);
return mapperClass;
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}else{
return mapperClass;
}
return null;
}
/**
* 获取spring容器
* @return
*/
public ApplicationContext getApplicationContext(){
return SpringContextUtils.getApplicationContext();
}
}
spring容器获取工具代码:
package cn.dbw.springboot.springbootwebjsp.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class SpringContextUtils implements ApplicationContextAware {
private static ApplicationContext APPLICATION_CONTEXT=null;
private static final ReadWriteLock readWriteLock=new ReentrantReadWriteLock();
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtils.APPLICATION_CONTEXT=applicationContext;
}
public static ApplicationContext getApplicationContext(){
Lock lock = readWriteLock.readLock();
lock.lock();
try {
if(null!=APPLICATION_CONTEXT){
return APPLICATION_CONTEXT;
}else {
return null;
}
}finally {
lock.unlock();
}
}
}