标签:实现 read iter 电子 数据类型 root 平台 扩展 系统
MyBatis框架的核心功能其实不难,无非就是动态代理和jdbc的操作,难的是写出来可扩展,高内聚,低耦合的规范的代码。
本文完成的Mybatis功能比较简单,代码还有许多需要改进的地方,大家可以结合Mybatis源码去动手完善。
1. Mybatis框架流程简介
我们对上图进行分析总结:
根据上文Mybatis流程,我简化了下,分为以下步骤:
我们经常在使用框架时看到Session,Session到底是什么呢?一个Session仅拥有一个对应的数据库连接。类似于一个前段请求Request,它可以直接调用exec(SQL)来执行SQL语句。
3. 实现自己的Mybatis
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.liugh</groupId> <artifactId>liugh-mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <java.version>1.8</java.version> </properties> <dependencies> <!-- 读取xml文件 --> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.29</version> </dependency> </dependencies> </project>
<?xml version="1.0" encoding="UTF-8"?> <database> <property name="driverClassName">com.mysql.jdbc.Driver</property> <property name="url">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8</property> <property name="username">root</property> <property name="password">123456</property> </database>
CREATE TABLE `user` ( `id` varchar(64) NOT NULL, `password` varchar(255) DEFAULT NULL, `username` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; INSERT INTO `test`.`user` (`id`, `password`, `username`) VALUES (‘1‘, ‘123456‘, ‘liugh‘);
package com.liugh.bean; public class User { private String id; private String username; private String password; //省略get set toString方法... } package com.liugh.mapper; import com.liugh.bean.User; public interface UserMapper { public User getUserById(String id); } <?xml version="1.0" encoding="UTF-8"?> <mapper nameSpace="com.liugh.mapper.UserMapper"> <select id="getUserById" resultType ="com.liugh.bean.User"> select * from user where id = ? </select> </mapper>
package com.liugh.sqlSession; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import com.liugh.config.Function; import com.liugh.config.MapperBean; /** * 读取与解析配置信息,并返回处理后的Environment */ public class MyConfiguration { private static ClassLoader loader = ClassLoader.getSystemClassLoader(); /** * 读取xml信息并处理 */ public Connection build(String resource){ try { InputStream stream = loader.getResourceAsStream(resource); SAXReader reader = new SAXReader(); Document document = reader.read(stream); Element root = document.getRootElement(); return evalDataSource(root); } catch (Exception e) { throw new RuntimeException("error occured while evaling xml " + resource); } } private Connection evalDataSource(Element node) throws ClassNotFoundException { if (!node.getName().equals("database")) { throw new RuntimeException("root should be <database>"); } String driverClassName = null; String url = null; String username = null; String password = null; //获取属性节点 for (Object item : node.elements("property")) { Element i = (Element) item; String value = getValue(i); String name = i.attributeValue("name"); if (name == null || value == null) { throw new RuntimeException("[database]: <property> should contain name and value"); } //赋值 switch (name) { case "url" : url = value; break; case "username" : username = value; break; case "password" : password = value; break; case "driverClassName" : driverClassName = value; break; default : throw new RuntimeException("[database]: <property> unknown name"); } } Class.forName(driverClassName); Connection connection = null; try { //建立数据库链接 connection = DriverManager.getConnection(url, username, password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return connection; } //获取property属性的值,如果有value值,则读取 没有设置value,则读取内容 private String getValue(Element node) { return node.hasContent() ? node.getText() : node.attributeValue("value"); } @SuppressWarnings("rawtypes") public MapperBean readMapper(String path){ MapperBean mapper = new MapperBean(); try{ InputStream stream = loader.getResourceAsStream(path); SAXReader reader = new SAXReader(); Document document = reader.read(stream); Element root = document.getRootElement(); mapper.setInterfaceName(root.attributeValue("nameSpace").trim()); //把mapper节点的nameSpace值存为接口名 List<Function> list = new ArrayList<Function>(); //用来存储方法的List for(Iterator rootIter = root.elementIterator();rootIter.hasNext();) {//遍历根节点下所有子节点 Function fun = new Function(); //用来存储一条方法的信息 Element e = (Element) rootIter.next(); String sqltype = e.getName().trim(); String funcName = e.attributeValue("id").trim(); String sql = e.getText().trim(); String resultType = e.attributeValue("resultType").trim(); fun.setSqltype(sqltype); fun.setFuncName(funcName); Object newInstance=null; try { newInstance = Class.forName(resultType).newInstance(); } catch (InstantiationException e1) { e1.printStackTrace(); } catch (IllegalAccessException e1) { e1.printStackTrace(); } catch (ClassNotFoundException e1) { e1.printStackTrace(); } fun.setResultType(newInstance); fun.setSql(sql); list.add(fun); } mapper.setList(list); } catch (DocumentException e) { e.printStackTrace(); } return mapper; } }
package com.liugh.config; import java.util.List; public class MapperBean { private String interfaceName; //接口名 private List<Function> list; //接口下所有方法 //省略 get set方法... }
package com.liugh.config; public class Function { private String sqltype; private String funcName; private String sql; private Object resultType; private String parameterType; //省略 get set方法 }
package com.liugh.sqlSession; import java.lang.reflect.Proxy; public class MySqlsession { private Excutor excutor= new MyExcutor(); private MyConfiguration myConfiguration = new MyConfiguration(); public <T> T selectOne(String statement,Object parameter){ return excutor.query(statement, parameter); } @SuppressWarnings("unchecked") public <T> T getMapper(Class<T> clas){ //动态代理调用 return (T)Proxy.newProxyInstance(clas.getClassLoader(),new Class[]{clas}, new MyMapperProxy(myConfiguration,this)); } }
package com.liugh.sqlSession; public interface Excutor { public <T> T query(String statement,Object parameter); }
package com.liugh.sqlSession; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.liugh.bean.User; public class MyExcutor implements Excutor{ private MyConfiguration xmlConfiguration = new MyConfiguration(); @Override public <T> T query(String sql, Object parameter) { Connection connection=getConnection(); ResultSet set =null; PreparedStatement pre =null; try { pre = connection.prepareStatement(sql); //设置参数 pre.setString(1, parameter.toString()); set = pre.executeQuery(); User u=new User(); //遍历结果集 while(set.next()){ u.setId(set.getString(1)); u.setUsername(set.getString(2)); u.setPassword(set.getString(3)); } return (T) u; } catch (SQLException e) { e.printStackTrace(); } finally{ try{ if(set!=null){ set.close(); }if(pre!=null){ pre.close(); }if(connection!=null){ connection.close(); } }catch(Exception e2){ e2.printStackTrace(); } } return null; } private Connection getConnection() { try { Connection connection =xmlConfiguration.build("config.xml"); return connection; } catch (Exception e) { e.printStackTrace(); } return null; } }
package com.liugh.sqlSession; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.util.List; import com.liugh.config.Function; import com.liugh.config.MapperBean; public class MyMapperProxy implements InvocationHandler{ private MySqlsession mySqlsession; private MyConfiguration myConfiguration; public MyMapperProxy(MyConfiguration myConfiguration,MySqlsession mySqlsession) { this.myConfiguration=myConfiguration; this.mySqlsession=mySqlsession; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { MapperBean readMapper = myConfiguration.readMapper("UserMapper.xml"); //是否是xml文件对应的接口 if(!method.getDeclaringClass().getName().equals(readMapper.getInterfaceName())){ return null; } List<Function> list = readMapper.getList(); if(null != list || 0 != list.size()){ for (Function function : list) { //id是否和接口方法名一样 if(method.getName().equals(function.getFuncName())){ return mySqlsession.selectOne(function.getSql(), String.valueOf(args[0])); } } } return null; } }
package com.liugh; import com.liugh.bean.User; import com.liugh.mapper.UserMapper; import com.liugh.sqlSession.MySqlsession; public class TestMybatis { public static void main(String[] args) { MySqlsession sqlsession=new MySqlsession(); UserMapper mapper = sqlsession.getMapper(UserMapper.class); User user = mapper.getUserById("1"); System.out.println(user); } }
标签:实现 read iter 电子 数据类型 root 平台 扩展 系统
原文地址:https://www.cnblogs.com/gdjk/p/10592182.html