├── README.md ├── .settings ├── org.eclipse.m2e.core.prefs ├── org.eclipse.core.resources.prefs └── org.eclipse.jdt.core.prefs ├── target └── classes │ ├── com │ └── liugh │ │ ├── bean │ │ └── User.class │ │ ├── TestMybatis.class │ │ ├── config │ │ ├── Function.class │ │ └── MapperBean.class │ │ ├── mapper │ │ └── UserMapper.class │ │ └── sqlSession │ │ ├── Excutor.class │ │ ├── MyExcutor.class │ │ ├── MySqlsession.class │ │ ├── MyMapperProxy.class │ │ └── MyConfiguration.class │ ├── META-INF │ ├── MANIFEST.MF │ └── maven │ │ └── com.liugh │ │ └── liugh-mybatis │ │ ├── pom.properties │ │ └── pom.xml │ ├── UserMapper.xml │ └── config.xml ├── src └── main │ ├── java │ └── com │ │ └── liugh │ │ ├── sqlSession │ │ ├── Excutor.java │ │ ├── MySqlsession.java │ │ ├── MyMapperProxy.java │ │ ├── MyExcutor.java │ │ └── MyConfiguration.java │ │ ├── mapper │ │ └── UserMapper.java │ │ ├── TestMybatis.java │ │ ├── config │ │ ├── MapperBean.java │ │ └── Function.java │ │ └── bean │ │ └── User.java │ └── resources │ ├── UserMapper.xml │ └── config.xml ├── .project ├── pom.xml └── .classpath /README.md: -------------------------------------------------------------------------------- 1 | ### [博客地址](https://my.oschina.net/liughDevelop/blog/1631006) -------------------------------------------------------------------------------- /.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /target/classes/com/liugh/bean/User.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qq53182347/liugh-mybatis/HEAD/target/classes/com/liugh/bean/User.class -------------------------------------------------------------------------------- /target/classes/com/liugh/TestMybatis.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qq53182347/liugh-mybatis/HEAD/target/classes/com/liugh/TestMybatis.class -------------------------------------------------------------------------------- /target/classes/com/liugh/config/Function.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qq53182347/liugh-mybatis/HEAD/target/classes/com/liugh/config/Function.class -------------------------------------------------------------------------------- /target/classes/com/liugh/config/MapperBean.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qq53182347/liugh-mybatis/HEAD/target/classes/com/liugh/config/MapperBean.class -------------------------------------------------------------------------------- /target/classes/com/liugh/mapper/UserMapper.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qq53182347/liugh-mybatis/HEAD/target/classes/com/liugh/mapper/UserMapper.class -------------------------------------------------------------------------------- /target/classes/com/liugh/sqlSession/Excutor.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qq53182347/liugh-mybatis/HEAD/target/classes/com/liugh/sqlSession/Excutor.class -------------------------------------------------------------------------------- /target/classes/com/liugh/sqlSession/MyExcutor.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qq53182347/liugh-mybatis/HEAD/target/classes/com/liugh/sqlSession/MyExcutor.class -------------------------------------------------------------------------------- /target/classes/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Built-By: Administrator 3 | Build-Jdk: 1.8.0_101 4 | Created-By: Maven Integration for Eclipse 5 | 6 | -------------------------------------------------------------------------------- /target/classes/com/liugh/sqlSession/MySqlsession.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qq53182347/liugh-mybatis/HEAD/target/classes/com/liugh/sqlSession/MySqlsession.class -------------------------------------------------------------------------------- /target/classes/com/liugh/sqlSession/MyMapperProxy.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qq53182347/liugh-mybatis/HEAD/target/classes/com/liugh/sqlSession/MyMapperProxy.class -------------------------------------------------------------------------------- /target/classes/com/liugh/sqlSession/MyConfiguration.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qq53182347/liugh-mybatis/HEAD/target/classes/com/liugh/sqlSession/MyConfiguration.class -------------------------------------------------------------------------------- /src/main/java/com/liugh/sqlSession/Excutor.java: -------------------------------------------------------------------------------- 1 | package com.liugh.sqlSession; 2 | 3 | public interface Excutor { 4 | public T query(String statement,Object parameter); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/liugh/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.liugh.mapper; 2 | 3 | import com.liugh.bean.User; 4 | 5 | public interface UserMapper { 6 | public User getUserById(String id); 7 | } 8 | -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/main/resources=UTF-8 4 | encoding//src/test/java=UTF-8 5 | encoding//src/test/resources=UTF-8 6 | encoding/=UTF-8 7 | -------------------------------------------------------------------------------- /src/main/resources/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /target/classes/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 3 | org.eclipse.jdt.core.compiler.compliance=1.8 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.8 6 | -------------------------------------------------------------------------------- /target/classes/META-INF/maven/com.liugh/liugh-mybatis/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven Integration for Eclipse 2 | #Wed Mar 07 09:19:11 CST 2018 3 | version=0.0.1-SNAPSHOT 4 | groupId=com.liugh 5 | m2e.projectName=liugh-mybatis 6 | m2e.projectLocation=D\:\\javaSoft\\eclipse-neon\\eclipse\\workspace\\liugh-mybatis 7 | artifactId=liugh-mybatis 8 | -------------------------------------------------------------------------------- /target/classes/config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | com.mysql.jdbc.Driver 4 | jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 5 | root 6 | TW123 7 | 8 | -------------------------------------------------------------------------------- /src/main/resources/config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | com.mysql.jdbc.Driver 4 | jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 5 | root 6 | TW123 7 | 8 | -------------------------------------------------------------------------------- /src/main/java/com/liugh/TestMybatis.java: -------------------------------------------------------------------------------- 1 | package com.liugh; 2 | 3 | import com.liugh.bean.User; 4 | import com.liugh.mapper.UserMapper; 5 | import com.liugh.sqlSession.MySqlsession; 6 | 7 | public class TestMybatis { 8 | 9 | public static void main(String[] args) { 10 | MySqlsession sqlsession=new MySqlsession(); 11 | UserMapper mapper = sqlsession.getMapper(UserMapper.class); 12 | User user = mapper.getUserById("2"); 13 | System.out.println(user); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liugh/config/MapperBean.java: -------------------------------------------------------------------------------- 1 | package com.liugh.config; 2 | 3 | import java.util.List; 4 | 5 | public class MapperBean { 6 | 7 | private String interfaceName; //接口名 8 | 9 | private List list; //接口下所有方法 10 | 11 | public String getInterfaceName() { 12 | return interfaceName; 13 | } 14 | public void setInterfaceName(String interfaceName) { 15 | this.interfaceName = interfaceName; 16 | } 17 | public List getList() { 18 | return list; 19 | } 20 | public void setList(List list) { 21 | this.list = list; 22 | } 23 | 24 | 25 | } 26 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | liugh-mybatis 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/main/java/com/liugh/sqlSession/MySqlsession.java: -------------------------------------------------------------------------------- 1 | package com.liugh.sqlSession; 2 | 3 | import java.lang.reflect.Proxy; 4 | 5 | public class MySqlsession { 6 | 7 | private Excutor excutor= new MyExcutor(); 8 | 9 | private MyConfiguration myConfiguration = new MyConfiguration(); 10 | 11 | public T selectOne(String statement,Object parameter){ 12 | return excutor.query(statement, parameter); 13 | } 14 | 15 | @SuppressWarnings("unchecked") 16 | public T getMapper(Class clas){ 17 | //动态代理调用 18 | return (T)Proxy.newProxyInstance(clas.getClassLoader(),new Class[]{clas}, 19 | new MyMapperProxy(myConfiguration,this)); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/liugh/bean/User.java: -------------------------------------------------------------------------------- 1 | package com.liugh.bean; 2 | 3 | public class User { 4 | 5 | private String id; 6 | private String username; 7 | private String password; 8 | 9 | @Override 10 | public String toString() { 11 | return "User [id=" + id + ", username=" + username + ", password=" + password + "]"; 12 | } 13 | public String getId() { 14 | return id; 15 | } 16 | public void setId(String id) { 17 | this.id = id; 18 | } 19 | public String getUsername() { 20 | return username; 21 | } 22 | public void setUsername(String username) { 23 | this.username = username; 24 | } 25 | public String getPassword() { 26 | return password; 27 | } 28 | public void setPassword(String password) { 29 | this.password = password; 30 | } 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | com.liugh 4 | liugh-mybatis 5 | 0.0.1-SNAPSHOT 6 | jar 7 | 8 | 9 | UTF-8 10 | 1.8 11 | 1.8 12 | 1.8 13 | 14 | 15 | 16 | 17 | 18 | dom4j 19 | dom4j 20 | 1.6.1 21 | 22 | 23 | 24 | 25 | mysql 26 | mysql-connector-java 27 | 5.1.29 28 | 29 | 30 | -------------------------------------------------------------------------------- /src/main/java/com/liugh/config/Function.java: -------------------------------------------------------------------------------- 1 | package com.liugh.config; 2 | 3 | public class Function { 4 | private String sqltype; //sql的类型,计划在xml读取有四种情况 5 | private String funcName; // 方法名 6 | private String sql; //执行的sql语句 7 | private Object resultType; // 返回类型 8 | private String parameterType; //参数类型 9 | public String getSqltype() { 10 | return sqltype; 11 | } 12 | public void setSqltype(String sqltype) { 13 | this.sqltype = sqltype; 14 | } 15 | public String getFuncName() { 16 | return funcName; 17 | } 18 | public void setFuncName(String funcName) { 19 | this.funcName = funcName; 20 | } 21 | public String getSql() { 22 | return sql; 23 | } 24 | public void setSql(String sql) { 25 | this.sql = sql; 26 | } 27 | public Object getResultType() { 28 | return resultType; 29 | } 30 | public void setResultType(Object resultType) { 31 | this.resultType = resultType; 32 | } 33 | public String getParameterType() { 34 | return parameterType; 35 | } 36 | public void setParameterType(String parameterType) { 37 | this.parameterType = parameterType; 38 | } 39 | 40 | 41 | } 42 | -------------------------------------------------------------------------------- /target/classes/META-INF/maven/com.liugh/liugh-mybatis/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | com.liugh 4 | liugh-mybatis 5 | 0.0.1-SNAPSHOT 6 | jar 7 | 8 | 9 | UTF-8 10 | 1.8 11 | 1.8 12 | 1.8 13 | 14 | 15 | 16 | 17 | 18 | dom4j 19 | dom4j 20 | 1.6.1 21 | 22 | 23 | 24 | 25 | mysql 26 | mysql-connector-java 27 | 5.1.29 28 | 29 | 30 | -------------------------------------------------------------------------------- /src/main/java/com/liugh/sqlSession/MyMapperProxy.java: -------------------------------------------------------------------------------- 1 | package com.liugh.sqlSession; 2 | 3 | import java.lang.reflect.InvocationHandler; 4 | import java.lang.reflect.Method; 5 | import java.util.List; 6 | import com.liugh.config.Function; 7 | import com.liugh.config.MapperBean; 8 | 9 | public class MyMapperProxy implements InvocationHandler{ 10 | 11 | private MySqlsession mySqlsession; 12 | 13 | private MyConfiguration myConfiguration; 14 | 15 | public MyMapperProxy(MyConfiguration myConfiguration,MySqlsession mySqlsession) { 16 | this.myConfiguration=myConfiguration; 17 | this.mySqlsession=mySqlsession; 18 | } 19 | 20 | @Override 21 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 22 | MapperBean readMapper = myConfiguration.readMapper("UserMapper.xml"); 23 | //是否是xml文件对应的接口 24 | if(!method.getDeclaringClass().getName().equals(readMapper.getInterfaceName())){ 25 | return null; 26 | } 27 | List list = readMapper.getList(); 28 | if(null != list || 0 != list.size()){ 29 | for (Function function : list) { 30 | //id是否和接口方法名一样 31 | if(method.getName().equals(function.getFuncName())){ 32 | return mySqlsession.selectOne(function.getSql(), String.valueOf(args[0])); 33 | } 34 | } 35 | } 36 | return null; 37 | } 38 | 39 | 40 | } 41 | -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /src/main/java/com/liugh/sqlSession/MyExcutor.java: -------------------------------------------------------------------------------- 1 | package com.liugh.sqlSession; 2 | 3 | import java.sql.Connection; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | import com.liugh.bean.User; 8 | 9 | 10 | public class MyExcutor implements Excutor{ 11 | 12 | private MyConfiguration xmlConfiguration = new MyConfiguration(); 13 | 14 | @Override 15 | public T query(String sql, Object parameter) { 16 | Connection connection=getConnection(); 17 | ResultSet set =null; 18 | PreparedStatement pre =null; 19 | try { 20 | pre = connection.prepareStatement(sql); 21 | //设置参数 22 | pre.setString(1, parameter.toString()); 23 | set = pre.executeQuery(); 24 | User u=new User(); 25 | //遍历结果集 26 | while(set.next()){ 27 | u.setId(set.getString(1)); 28 | u.setUsername(set.getString(2)); 29 | u.setPassword(set.getString(3)); 30 | } 31 | return (T) u; 32 | } catch (SQLException e) { 33 | e.printStackTrace(); 34 | } finally{ 35 | try{ 36 | if(set!=null){ 37 | set.close(); 38 | }if(pre!=null){ 39 | pre.close(); 40 | }if(connection!=null){ 41 | connection.close(); 42 | } 43 | }catch(Exception e2){ 44 | e2.printStackTrace(); 45 | } 46 | } 47 | return null; 48 | } 49 | 50 | private Connection getConnection() { 51 | try { 52 | Connection connection =xmlConfiguration.build("config.xml"); 53 | return connection; 54 | } catch (Exception e) { 55 | e.printStackTrace(); 56 | } 57 | return null; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/liugh/sqlSession/MyConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.liugh.sqlSession; 2 | 3 | import java.io.InputStream; 4 | import java.sql.Connection; 5 | import java.sql.DriverManager; 6 | import java.sql.SQLException; 7 | import java.util.ArrayList; 8 | import java.util.Iterator; 9 | import java.util.List; 10 | import org.dom4j.Document; 11 | import org.dom4j.DocumentException; 12 | import org.dom4j.Element; 13 | import org.dom4j.io.SAXReader; 14 | import com.liugh.config.Function; 15 | import com.liugh.config.MapperBean; 16 | 17 | 18 | /** 19 | * 读取与解析配置信息,并返回处理后的Environment 20 | */ 21 | public class MyConfiguration { 22 | private static ClassLoader loader = ClassLoader.getSystemClassLoader(); 23 | 24 | /** 25 | * 读取xml信息并处理 26 | */ 27 | public Connection build(String resource){ 28 | try { 29 | InputStream stream = loader.getResourceAsStream(resource); 30 | SAXReader reader = new SAXReader(); 31 | Document document = reader.read(stream); 32 | Element root = document.getRootElement(); 33 | return evalDataSource(root); 34 | } catch (Exception e) { 35 | throw new RuntimeException("error occured while evaling xml " + resource); 36 | } 37 | } 38 | 39 | private Connection evalDataSource(Element node) throws ClassNotFoundException { 40 | if (!node.getName().equals("database")) { 41 | throw new RuntimeException("root should be "); 42 | } 43 | String driverClassName = null; 44 | String url = null; 45 | String username = null; 46 | String password = null; 47 | //获取属性节点 48 | for (Object item : node.elements("property")) { 49 | Element i = (Element) item; 50 | String value = getValue(i); 51 | String name = i.attributeValue("name"); 52 | if (name == null || value == null) { 53 | throw new RuntimeException("[database]: should contain name and value"); 54 | } 55 | //赋值 56 | switch (name) { 57 | case "url" : url = value; break; 58 | case "username" : username = value; break; 59 | case "password" : password = value; break; 60 | case "driverClassName" : driverClassName = value; break; 61 | default : throw new RuntimeException("[database]: unknown name"); 62 | } 63 | } 64 | 65 | Class.forName(driverClassName); 66 | Connection connection = null; 67 | try { 68 | //建立数据库链接 69 | connection = DriverManager.getConnection(url, username, password); 70 | } catch (SQLException e) { 71 | // TODO Auto-generated catch block 72 | e.printStackTrace(); 73 | } 74 | return connection; 75 | } 76 | 77 | //获取property属性的值,如果有value值,则读取 没有设置value,则读取内容 78 | private String getValue(Element node) { 79 | return node.hasContent() ? node.getText() : node.attributeValue("value"); 80 | } 81 | 82 | 83 | 84 | @SuppressWarnings("rawtypes") 85 | public MapperBean readMapper(String path){ 86 | MapperBean mapper = new MapperBean(); 87 | try{ 88 | InputStream stream = loader.getResourceAsStream(path); 89 | SAXReader reader = new SAXReader(); 90 | Document document = reader.read(stream); 91 | Element root = document.getRootElement(); 92 | mapper.setInterfaceName(root.attributeValue("nameSpace").trim()); //把mapper节点的nameSpace值存为接口名 93 | List list = new ArrayList(); //用来存储方法的List 94 | for(Iterator rootIter = root.elementIterator();rootIter.hasNext();) {//遍历根节点下所有子节点 95 | Function fun = new Function(); //用来存储一条方法的信息 96 | Element e = (Element) rootIter.next(); 97 | String sqltype = e.getName().trim(); 98 | String funcName = e.attributeValue("id").trim(); 99 | String sql = e.getText().trim(); 100 | String resultType = e.attributeValue("resultType").trim(); 101 | fun.setSqltype(sqltype); 102 | fun.setFuncName(funcName); 103 | Object newInstance=null; 104 | try { 105 | newInstance = Class.forName(resultType).newInstance(); 106 | } catch (InstantiationException e1) { 107 | e1.printStackTrace(); 108 | } catch (IllegalAccessException e1) { 109 | e1.printStackTrace(); 110 | } catch (ClassNotFoundException e1) { 111 | e1.printStackTrace(); 112 | } 113 | fun.setResultType(newInstance); 114 | fun.setSql(sql); 115 | list.add(fun); 116 | } 117 | mapper.setList(list); 118 | 119 | } catch (DocumentException e) { 120 | e.printStackTrace(); 121 | } 122 | return mapper; 123 | } 124 | } 125 | --------------------------------------------------------------------------------