
思路:
- 使用端:项目-引用自定义持久层框架
- 定义sqlMapperConfig.xml(数据库的配置信息,存放mapper.xml的路径);
- 定义mapper.xml(存放sql语句);
- 自定义持久层框架本身;
- 加载配置文件:
- 根据配置文件的路径,加载配置文件成输入流,存在内存中;
- 创建Resource类---存在方法:inputSteam getInputSteam(String path);
- 根据配置文件的路径,加载配置文件成输入流,存在内存中;
- 创建两个JavaBean容器
- 存放解析配置文件的内容:
- Configuartion:核心配置类-存放sqlMapperConfig.xml解析出来的内容;
- MappedStatement:映射配置类,存放mapper.xml解析出来的内容;
- 存放解析配置文件的内容:
- 解析配置文件(dom4j):
- 创建类SqlSessionFactorBuilder方法:存在bulid(inputSteam)
- 使用dom4j解析配置文件,将解析出来的信息保存到容器中;
- 创建SqlsessionFactory对象;生产SqlSession会话对象;
- 创建类SqlSessionFactorBuilder方法:存在bulid(inputSteam)
- 创建SqlsessionFactory接口及它的实现类DefaultSqlsessionFactory:
- 存在openSession-生产SqlSession;
- 创建SqlSession接口及它的实现类DefaultSqlSession:
- 定义对数据库的CRUD *** 作;
- 创建Executor接口及实现类SimpleExecutor实现类:
- query(Configuration,MappedStatement,Object...)
- 加载配置文件:
代码实现:
持久层框架
框架结构图
- 创建Configuration类(用于保存sqlMapConfig.xml解析后的信息)
public class Configuration {
private DataSource dataSource;
private Map mappedStatementMap = new HashMap<>();
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public Map getMappedStatementMap() {
return mappedStatementMap;
}
public void setMappedStatementMap(Map mappedStatementMap) {
this.mappedStatementMap = mappedStatementMap;
}
}
- 创建MapperStament类,用于保存Mapper.xml解析后的信息
public class MappedStatement {
private String id;
private String resultType;
private String paramterType;
private String sql;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getResultType() {
return resultType;
}
public void setResultType(String resultType) {
this.resultType = resultType;
}
public String getParamterType() {
return paramterType;
}
public void setParamterType(String paramterType) {
this.paramterType = paramterType;
}
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
}
- 创建BoundSql类,用于存储解析后的SQL语句
public class BoundSql {
private String sql;
private List parameterMappings = new ArrayList<>();
public BoundSql(String sql, List parameterMappings) {
this.sql = sql;
this.parameterMappings = parameterMappings;
}
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
public List getParameterMappings() {
return parameterMappings;
}
public void setParameterMappings(List parameterMappings) {
this.parameterMappings = parameterMappings;
}
}
- 创建Resource类
public class Resources {
public static InputStream getInputStream(String path){
InputStream resourceAsStream = Resources.class.getClassLoader().getResourceAsStream(path);
return resourceAsStream;
}
}
- 创建SqlSession接口
- 方法selectList():根据条件查询,返回多条记录,statemenid为mapper.xml文件中的namespace+.+标签id,object..为条件参数
- 方法selectOne():根据条件查询,返回单条记录
public interface SqlSession {
List selectList(String statemenid,Object... params) throws IllegalAccessException, IntrospectionException, InstantiationException, NoSuchFieldException, SQLException, InvocationTargetException, ClassNotFoundException;
T selectOne(String statemenid,Object... params) throws IllegalAccessException, ClassNotFoundException, IntrospectionException, InstantiationException, SQLException, InvocationTargetException, NoSuchFieldException;
}
- 创建sqlSessionFactory接口
- 方法openSession():生产SqlSession对象
public interface SqlSessionFactory {
SqlSession openSession();
}
- 创建Executrer接口
- 方法query():执行查询语句的方法
public interface Executer {
List query(Configuration configuration, MappedStatement mappedStatement, Object... params) throws SQLException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException, IntrospectionException, InvocationTargetException;
}
- 创建XMLMapperBuilder类,用于解析Mapper.xml文件
public class XMLMapperBuilder {
private Configuration configuration;
public XMLMapperBuilder(Configuration configuration){
this.configuration=configuration;
}
public void parse(InputStream inputStream) throws documentException {
document document = new SAXReader().read(inputStream);
//获取根节点
Element rootElement = document.getRootElement();
String namespace = rootElement.attributevalue("namespace");
//查找标签
List listSelect = rootElement.selectNodes("//select");
for (Element element : listSelect){
String id = element.attributevalue("id");
String resultType = element.attributevalue("resultType");
String paramterType = element.attributevalue("paramterType");
String sqlText = element.getTextTrim();
String key = namespace+"."+id;
MappedStatement mappedStatement = new MappedStatement();
mappedStatement.setId(id);
mappedStatement.setResultType(resultType);
mappedStatement.setParamterType(paramterType);
mappedStatement.setSql(sqlText);
configuration.getMappedStatementMap().put(key,mappedStatement);
}
}
}
- 创建XMLConfigBuilder(用于解析配置文件sqlMapConfig.xml)
public class XMLConfigBuilder {
private Configuration configuration;
public XMLConfigBuilder(){
this.configuration = new Configuration();
}
public Configuration parseConfig(InputStream inputStream) throws documentException, PropertyVetoException {
document document = new SAXReader().read(inputStream);
//获取根对象
Element rootElement = document.getRootElement();
//Xpath表达式查询 标签
List listProperty = rootElement.selectNodes("//property");
Properties properties = new Properties();
for (Element element : listProperty){
String name = element.attributevalue("name");
String value = element.attributevalue("value");
properties.setProperty(name,value);
}
//建立连接池
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
//写入链接参数
comboPooledDataSource.setDriverClass(properties.getProperty("driverClass"));
comboPooledDataSource.setJdbcUrl(properties.getProperty("jdbcUrl"));
comboPooledDataSource.setUser(properties.getProperty("username"));
comboPooledDataSource.setPassword(properties.getProperty("password"));
this.configuration.setDataSource(comboPooledDataSource);
//解析mapper.xml
List listMapper = rootElement.selectNodes("mapper");
for(Element element : listMapper){
String mapperPath = element.attributevalue("resource");
InputStream inputStreamMapper = Resources.getInputStream(mapperPath);
XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(this.configuration);
xmlMapperBuilder.parse(inputStreamMapper);
}
return this.configuration;
}
}
- 实现SqlSessionFactor的实现类DefaultSqlSessionFactor
public class DefaultSqlSesionFactory implements SqlSessionFactory {
private Configuration configuration;
public DefaultSqlSesionFactory(Configuration configuration){
this.configuration=configuration;
}
@Override
public SqlSession openSession() {
return new DefaultSqlSesion(configuration);
}
}
- 创建GenericTokenParser,解析SQL语句
public class GenericTokenParser {
private final String openToken; //开始标记
private final String closeToken; //结束标记
private final TokenHandler handler; //标记处理器
public GenericTokenParser(String openToken, String closeToken, TokenHandler handler) {
this.openToken = openToken;
this.closeToken = closeToken;
this.handler = handler;
}
public String parse(String text) {
// 验证参数问题,如果是null,就返回空字符串。
if (text == null || text.isEmpty()) {
return "";
}
// 下面继续验证是否包含开始标签,如果不包含,默认不是占位符,直接原样返回即可,否则继续执行。
int start = text.indexOf(openToken, 0);
if (start == -1) {
return text;
}
// 把text转成字符数组src,并且定义默认偏移量offset=0、存储最终需要返回字符串的变量builder,
// text变量中占位符对应的变量名expression。判断start是否大于-1(即text中是否存在openToken),如果存在就执行下面代码
char[] src = text.toCharArray();
int offset = 0;
final StringBuilder builder = new StringBuilder();
StringBuilder expression = null;
while (start > -1) {
// 判断如果开始标记前如果有转义字符,就不作为openToken进行处理,否则继续处理
if (start > 0 && src[start - 1] == '\') {
builder.append(src, offset, start - offset - 1).append(openToken);
offset = start + openToken.length();
} else {
//重置expression变量,避免空指针或者老数据干扰。
if (expression == null) {
expression = new StringBuilder();
} else {
expression.setLength(0);
}
builder.append(src, offset, start - offset);
offset = start + openToken.length();
int end = text.indexOf(closeToken, offset);
while (end > -1) {存在结束标记时
if (end > offset && src[end - 1] == '\') {//如果结束标记前面有转义字符时
// this close token is escaped. remove the backslash and continue.
expression.append(src, offset, end - offset - 1).append(closeToken);
offset = end + closeToken.length();
end = text.indexOf(closeToken, offset);
} else {//不存在转义字符,即需要作为参数进行处理
expression.append(src, offset, end - offset);
offset = end + closeToken.length();
break;
}
}
if (end == -1) {
// close token was not found.
builder.append(src, start, src.length - start);
offset = src.length;
} else {
//首先根据参数的key(即expression)进行参数处理,返回?作为占位符
builder.append(handler.handleToken(expression.toString()));
offset = end + closeToken.length();
}
}
start = text.indexOf(openToken, offset);
}
if (offset < src.length) {
builder.append(src, offset, src.length - offset);
}
return builder.toString();
}
}
- 创建ParameterMapping类
public class ParameterMapping {
private String content;
public ParameterMapping(String content) {
this.content = content;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
- 创建TokenHandler接口
public interface TokenHandler {
String handleToken(String content);
}
- 创建TokenHandler接口实现类
public class ParameterMappingTokenHandler implements TokenHandler {
private List parameterMappings = new ArrayList();
// context是参数名称 #{id} #{username}
public String handleToken(String content) {
parameterMappings.add(buildParameterMapping(content));
return "?";
}
private ParameterMapping buildParameterMapping(String content) {
ParameterMapping parameterMapping = new ParameterMapping(content);
return parameterMapping;
}
public List getParameterMappings() {
return parameterMappings;
}
public void setParameterMappings(List parameterMappings) {
this.parameterMappings = parameterMappings;
}
}
- 创建SqlSessionFactoryBulidr类(用于生产SqlSessionFactory)
public class SqlSessionFactoryBuildr {
public SqlSessionFactory builder(InputStream inputStream) throws documentException, PropertyVetoException {
XMLConfigBuilder xmlConfigBuilder = new XMLConfigBuilder();
Configuration configuration = xmlConfigBuilder.parseConfig(inputStream);
SqlSessionFactory sqlSessionFactory = new DefaultSqlSesionFactory(configuration);
return sqlSessionFactory;
}
}
- 创建Executrer的实现类SimpleExecutrer
public class SimpleExecutor implements Executer{
@Override
public List query(Configuration configuration, MappedStatement mappedStatement, Object... params) throws SQLException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException, IntrospectionException, InvocationTargetException {
//1、注册驱动,获取链接
Connection connection = configuration.getDataSource().getConnection();
//2、获取sql语句-》转换sql语句
String sql = mappedStatement.getSql();
BoundSql boundSql = getBoundSql(sql);
//3、获取预处理对象
PreparedStatement preparedStatement = connection.prepareStatement(boundSql.getSql());
//4、设置参数
String paramterType = mappedStatement.getParamterType();
Class> paramertypeClass = getClassType(paramterType);
List parameterMappings = boundSql.getParameterMappings();
for (int i=0; i resultClassType = getClassType(resultType);
ArrayList
- 创建SqlSession的实现类DefaultSqlSession
public class DefaultSqlSesion implements SqlSession {
private Configuration configuration;
public DefaultSqlSesion(Configuration configuration){
this.configuration = configuration;
}
@Override
public List selectList(String statemenid, Object... params) throws IllegalAccessException, IntrospectionException, InstantiationException, NoSuchFieldException, SQLException, InvocationTargetException, ClassNotFoundException {
SimpleExecutor simpleExecutor = new SimpleExecutor();
MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statemenid);
List
使用端
引用自定义持久层框架,并调用相关类
public class IpersistenceTest {
@Test
public void test() throws PropertyVetoException, documentException, IllegalAccessException, IntrospectionException, InstantiationException, NoSuchFieldException, SQLException, InvocationTargetException, ClassNotFoundException {
InputStream inputStream = Resources.getInputStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuildr().builder(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
User user = new User();
user.setId(1);
user.setUsername("lucy");
Object o = sqlSession.selectOne("user.selectOne", user);
System.out.print(user);
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)