mybatis3

public class TestMyBais {
    public static void main(String[] args) {
        MySqlSession mySqlSession = new MySqlSession();
        TestMapper mapper = mySqlSession.getMapper(TestMapper.class);
        Test test = mapper.selectById(1);
        System.out.println(test);
    }
}

import java.lang.reflect.Proxy;
import java.sql.SQLException;

public class MySqlSession {

    private Executor executor = new SimpleExecutor();

    public <T> T getMapper(Class<T> mapperInterface){
        return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(),new Class[]{mapperInterface},new MapperProxy(this,mapperInterface));
    }

    public <T> T selectOne(String sql, String paramter) throws SQLException {
        return executor.query(sql,paramter);
    }
}

import java.sql.SQLException;

public interface Executor {

    <T> T query(String sql, String paramter) throws SQLException;
}

import com.wenxin.it.mybatis2.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SimpleExecutor implements Executor{
    @Override
    public <T> T query(String sql, String paramter) throws SQLException {
        Connection connecion = com.wenxin.it.mybatis2.SimpleExecutor.getConnecion();
        PreparedStatement preparedStatement = connecion.prepareStatement(String.format(sql, paramter));
        ResultSet resultSet = preparedStatement.executeQuery();
        Test test = new Test();
        while (resultSet.next()) {
            test.setId(resultSet.getInt(1));
            test.setName(resultSet.getString(2));
        }
        return (T) test;
    }
}

import com.wenxin.it.mybatis2.Test;

public interface TestMapper {

    Test selectById(Integer id);
}


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

public class TestMapperXml {

    public static final String nameSpace = "com.wenxin.it.mybatis3.TestMapper";
    public static final Map<String,String> methodMapperSql = new HashMap<>();
    static {
        methodMapperSql.put("selectById","select * from test where id = %s");
    }
}

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

public class MapperProxy<T> implements InvocationHandler {

    private MySqlSession sqlSession;
    private Class<T> mapperInterfact;

    public MapperProxy(MySqlSession sqlSession, Class<T> mapperInterfact) {
        this.sqlSession = sqlSession;
        this.mapperInterfact = mapperInterfact;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (method.getDeclaringClass().getName().equals(TestMapperXml.nameSpace)) {
            String sql = TestMapperXml.methodMapperSql.get(method.getName());
            System.out.printf("the sql is "+ sql,args[0]);
            System.out.println();
            return sqlSession.selectOne(sql, args[0].toString());
        }
        return null;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值