手写mybatis源码(一)

1、添加mysql驱动包

手写mybatis源码(一)_第1张图片

2、实体类

public class User {
    private Integer id;
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

3、mapper

public interface UserMapper {
    public User getUserById(String id);
}

4、xml

import java.util.HashMap;
import java.util.Map;

public class UserMapperXml {
    //类xml的命名空间
    public static final String namespate="UserMapper";

    public static final Map map=new HashMap();

    static{
        //模拟xml中的id与sql语句
        map.put("getUserById", "select * from t_user where id =?");
    }

}

5、核心类之Excutor

public interface Excutor {
    public  T query(String statement,Object parameter);
}

6、实现类SimpleExcutor

import java.sql.*;

public class SimpleExcutor implements Excutor {

    public  T query(String sql, Object parameter) {
        Connection connection=null;
        PreparedStatement pre=null;
        ResultSet result = null;
        connection=getConnection();
        try {
            pre = connection.prepareStatement(sql);
            pre.setString(1,parameter.toString());
            result = pre.executeQuery();
            User u=new User();
            while(result.next()){
                u.setId(result.getInt(1));
                u.setName(result.getString(2));
            }
            return (T) u;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                if(result != null) result.close();
                if(pre != null) pre.close();
                if(connection != null) connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private Connection getConnection() {
        String driver="com.mysql.jdbc.Driver";
        String url="jdbc:mysql://192.168.184.128:3306/test?useUnicode=true&characterEncoding=UTF-8";
        String username="root";
        String password="root";
        try {
            Class.forName(driver);
            Connection connection = DriverManager.getConnection(url, username, password);
            return connection;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}

7、核心类之MyMapperHandler(代理类)

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class MyMapperHandler implements InvocationHandler {
    private MySqlsession xiaoSqlsession=null;

    public MyMapperHandler(MySqlsession xiaoSqlsession) {
        this.xiaoSqlsession=xiaoSqlsession;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // TODO Auto-generated method stub
        if(method.getDeclaringClass().getName().equals(UserMapperXml.namespate)){
            String sql = UserMapperXml.map.get(method.getName());
            return xiaoSqlsession.selectOne(sql, String.valueOf(args[0]));
        }
        return null;
    }

}

8、核心类之MySqlsession

import java.lang.reflect.Proxy;

public class MySqlsession {
    private Excutor excutor=new SimpleExcutor();


    public  T selectOne(String statement,Object parameter){
        return excutor.query(statement, parameter);
    }

    public  T getMapper(Class clas){
        return (T) Proxy.newProxyInstance(clas.getClassLoader(),new Class[]{clas}, new MyMapperHandler(this));
    }
}

9、测试类之TestManuMybaties

public class TestManuMybaties {
    public static void main(String[] args) {
        MySqlsession sqlsession=new MySqlsession();
        UserMapper mapper = sqlsession.getMapper(UserMapper.class);
        User userById = mapper.getUserById("1");
        System.out.println(userById);
    }
}

 

你可能感兴趣的:(源码分析)