前面我们完成了自定义持久层框架的编写,接下来我们分析自定义持久层框架存在的问题,首先我们先来查看我们编写的测试类中的代码
package study.lagou.com.test;
import org.junit.Test;
import study.lagou.com.io.Resources;
import study.lagou.com.persistence.test.pojo.User;
import study.lagou.com.sqlSession.SqlSession;
import study.lagou.com.sqlSession.SqlSessionFactory;
import study.lagou.com.sqlSession.SqlSessionFactoryBuilder;
import java.io.InputStream;
import java.util.List;
/**
* @Description: 功能描述
* @Author houjh
* @Email: [email protected]
* @Date: 2021-1-28 21:19
*/
public class PersistenceTest {
@Test
public void testOne() throws Exception {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = build.openSession();
User user = new User();
user.setId(1);
user.setUsername("zhangsan");
User u = sqlSession.selectOne("user.selectOne", user);
System.out.println(u);
}
@Test
public void testList() throws Exception {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = build.openSession();
List userList = sqlSession.selectList("user.selectList", null);
for (User user : userList) {
System.out.println(user);
}
}
}
我们分析测试类中的代码如果是在实际的项目当中,此段测试代码会存在什么地方呢?
是不是会将此部分代码编写到我们的持久层中,之前我们使用端的项目当中没有实现持久层,现在我们先来实现一下持久层具体的代码
首先定义一个接口IUserDao
package study.lagou.com.persistence.test.dao;
import study.lagou.com.persistence.test.pojo.User;
import java.util.List;
public interface IUserDao {
/**
* 查询所有用户信息
* @return
*/
List findAll();
/**
* 根据条件查询单个用户信息
* @param user
* @return
*/
User findByCondition(User user);
}
接着实现具体的接口实现类UserDaoImpl
package study.lagou.com.persistence.test.dao.impl;
import org.dom4j.DocumentException;
import study.lagou.com.io.Resources;
import study.lagou.com.persistence.test.dao.IUserDao;
import study.lagou.com.persistence.test.pojo.User;
import study.lagou.com.sqlSession.SqlSession;
import study.lagou.com.sqlSession.SqlSessionFactory;
import study.lagou.com.sqlSession.SqlSessionFactoryBuilder;
import java.io.InputStream;
import java.util.List;
/**
* @Description: 功能描述
* @Author houjh
* @Email: [email protected]
* @Date: 2021-2-25 22:32
*/
public class UserDaoImpl implements IUserDao {
@Override
public List findAll() throws Exception {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = build.openSession();
List userList = sqlSession.selectList("user.selectList", null);
return userList;
}
@Override
public User findByCondition(User user) throws Exception {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = build.openSession();
return sqlSession.selectOne("user.selectOne", null);
}
}
我们分析UserDaoImpl这个实现类中的代码,该类中的代码主要存在2个问题
1、Dao层使用自定义持久层框架本身存在代码重,整个操作的过程模板重复(主要是加载配置文件、创建SqlSessionFactory、生产SqlSession这几步重复)
2、代码中的statementId还是存在硬编码问题
以上2个问题,我们有什么方式可以进行解决呢?
解决方案还是有的:我们可以基于JDK动态代理,使用代理模式来动态生成Dao层接口的代理实现类,不再进行持久层实现类的具体编写
具体实现步骤如下:
1、在自定义持久层框架本身项目SqlSession当中添加getMapper(Class> mapperInterfaceClass)方法,通过JDK动态代理通过接口类直接生成对应的代理实现类
SqlSession接口的调整
package study.lagou.com.sqlSession;
import java.util.List;
public interface SqlSession {
/**
* 查询列表信息
* @param statementId
* @param params
* @param
* @return
*/
List selectList(String statementId, Object... params) throws Exception;
/**
* 查询单个数据信息
* @param statementId
* @param params
* @param
* @return
*/
T selectOne(String statementId,Object... params) throws Exception;
/**
* 删除信息
* @param statementId
* @param params
*/
void delete(String statementId,Object... params);
/**
* 更新数据信息
* @param statementId
* @param params
*/
void update(String statementId,Object... params);
/**
* 通过接口
* @param mapperInterfaceClass
* @param
* @return
*/
T getMapper(Class> mapperInterfaceClass);
}
DefaultSqlSession的调整
package study.lagou.com.sqlSession;
import study.lagou.com.pojo.Configuration;
import study.lagou.com.pojo.MappedStatement;
import java.lang.reflect.*;
import java.util.List;
/**
* @Description: 功能描述
* @Author houjh
* @Email: [email protected]
* @Date: 2021-2-22 21:24
*/
public class DefaultSqlSession implements SqlSession {
private Configuration configuration;
public DefaultSqlSession(Configuration configuration) {
this.configuration = configuration;
}
@Override
public List selectList(String statementId, Object... params) throws Exception {
SimpleExecutor simpleExecutor = new SimpleExecutor();
MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
List
需要注意:此次的改造我们对Mapper.xml也有对应的调整,主要调整的地方就是namespace要和接口的全限定类型保持一致,具体SQL语句的id需要和接口中的方法名称保持一致(需要做此调整的主要原因是我们在使用JDK动态代理的时候只能拿到接口的全限定路径和方法名称,无法获取到Mapper.xml配置文件中的信息,但是需要执行底层的JDBC代码又必须通过statementId参数获取到对应的MappedStatement对象才能执行,为了解决此问题,我们约定通过让Mapper.xml文件中的 namespace.id = 接口全限定名.方法名 的方式来进行处理)
使用端UserMapper.xml的改造
接着编写测试类对改造优化后的框架进行测试
package study.lagou.com.test;
import org.junit.Test;
import study.lagou.com.io.Resources;
import study.lagou.com.persistence.test.dao.IUserDao;
import study.lagou.com.persistence.test.pojo.User;
import study.lagou.com.sqlSession.SqlSession;
import study.lagou.com.sqlSession.SqlSessionFactory;
import study.lagou.com.sqlSession.SqlSessionFactoryBuilder;
import java.io.InputStream;
import java.util.List;
/**
* @Description: 功能描述
* @Author houjh
* @Email: [email protected]
* @Date: 2021-1-28 21:19
*/
public class PersistenceMapperTest {
@Test
public void testOne() throws Exception {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = build.openSession();
IUserDao userDao = sqlSession.getMapper(IUserDao.class);
User user = new User();
user.setId(1);
user.setUsername("zhangsan");
User resultUser = userDao.findByCondition(user);
System.out.println(resultUser);
}
@Test
public void testList() throws Exception {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = build.openSession();
IUserDao userDao = sqlSession.getMapper(IUserDao.class);
List userList = userDao.findAll();
for (User user : userList) {
System.out.println(user);
}
}
}
至此,一个简单的MyBatis自定义持久层框架基本实现,下一节我们对增、删、改方法进行详细的实现
具体代码对应下载地址:https://gitee.com/happymima/mybatis.git