封装CRUD操作
首先我们需要创建一个SqlSession接口类,在其中定义会话的内容接口,同样,今天所提及的类都存放在“sqlSession”包下,SqlSession接口类的代码如下所示:
package com.lwl.sqlSession;
import java.util.List;
public interface SqlSession {
/**
* 查询所有
* @param statementId
* @param params
* @param <E>
* @return
*/
public <E> List<E> selectList(String statementId,Object... params) throws Exception;
/**
*
* 根据条件查询
* @param <T>
* @param statementId
* @param
* @return
*/
public <T> T selectOne(String statementId, Object... params) throws Exception;
/**
*
* 修改用户
* @param <T>
* @param statementId
* @param
* @return
*/
Integer update(String statementId, Object... params) throws Exception;
//为dao层生成代理实现类
public <T> T getMapper(Class<?> mapperClass);
}
创建DefaultSession 定义对数据库的操作 CRUD
package com.lwl.sqlSession;
import com.lwl.pojo.Configuration;
import com.lwl.pojo.MappedStatement;
import java.lang.reflect.*;
import java.util.List;
import java.util.Map;
public class DefaultSqlSession implements SqlSession {
private Configuration configuration;
// 处理器对象
private Executor simpleExecutor = new SimpleExecutor();
public DefaultSqlSession(Configuration configuration) {
this.configuration = configuration;
}
@Override
public <E> List<E> selectList(String statementId, Object... params) throws Exception {
//将要完成对simpleExecutor类的创建 query()的调用
SimpleExecutor simpleExecutor = new SimpleExecutor();
Map<String, MappedStatement> mappedStatementMap = configuration.getMappedStatementMap();
MappedStatement mappedStatement = mappedStatementMap.get(statementId);
List<Object> list = simpleExecutor.query(configuration, mappedStatement, params);
return (List<E>) list;
}
@Override
public <T> T selectOne(String statementId, Object... params) throws Exception {
List<Object> objects = selectList(statementId, params);
if (objects.size() == 1){
return (T)objects.get(0);
}else if (objects.size() > 1){
throw new RuntimeException("查询结果返回多个");
}else {
return null;
}
}
/**
* 修改用户
*
* @param
* @param params
* @return
*/
@Override
public Integer update(String statementId, Object... params) throws Exception {
SimpleExecutor simpleExecutor = new SimpleExecutor();
Map<String, MappedStatement> mappedStatementMap = configuration.getMappedStatementMap();
MappedStatement mappedStatement = mappedStatementMap.get(statementId);
return simpleExecutor.update(configuration,mappedStatement,params);
}
@Override
public <T> T getMapper(Class<?> mapperClass) {
//使用jdk动态代理 来为dao生成代理对象 并返回
//代理对象调用接口的任意方法,都会去执行invoke方法
Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
/**
*
* @param proxy 当前代理对象的引用
* @param method 当前被调用的方法
* @param args 传递的参数
* @return
* @throws Throwable
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//底层还是要去执行JDBC方法
//根据不同情况来调用selectList 或者selectOne
//准备参数 statementId sql语句的唯一标示 namespace.id = 接口的权限定名.方法名
String methodName = method.getName();
String className = method.getDeclaringClass().getName();
String statementId = className+"."+methodName;
if (methodName.startsWith("find")){
//获取调用方法的返回值类型
Type genericReturnType = method.getGenericReturnType();
//判断参数的返回值类型
if (genericReturnType instanceof ParameterizedType){
List<Object> list = selectList(statementId, args);
return list;
}
return selectOne(statementId,args);
}else {
return update(statementId,args);
}
}
});
return (T) proxyInstance;
}
}