自定义mybatis工具类

Config类

public class Config {

    /**
     * 解析xml,读取配置文件的内容,补全SqlSession所必须的信息
     * @param session
     */
    public static void loadConfiguration(DefaultSqlSession session, InputStream config) {
        try {
            //1.根据配置流对象构建Document
            Document document = new SAXReader().read(config);
            //2.获取根节点
            Element root = document.getRootElement();
            //3.使用XPATH得到所有的property节点
            List<Element> propElements = root.selectNodes("//dataSource/property");
            //4.遍历elements取出每个节点的属性,给创建数据库的信息赋值
            for(Element propElement : propElements) {
                //取出name属性的值
                String name = propElement.attributeValue("name");
                //取出value属性的值
                String value = propElement.attributeValue("value");
                //判断name是driver, url,username,password
                if("driver".equals(name)) {
                    driver = value;
                }
                if("url".equals(name)) {
                    url = value;
                }
                if("username".equals(name)) {
                    username = value;
                }
                if("password".equals(name)) {
                    password = value;
                }
            }
            //5.判断是否使用数据源
            Node dataSourceNode = root.selectSingleNode("//environment/dataSource");
            String useDataSource = dataSourceNode.valueOf("@type");
            if("POOLED".equalsIgnoreCase(useDataSource)) {
                //使用数据源
                DataSource ds = createDataSource();
                //给SqlSession中的连接数据库信息赋值
                session.setDataSource(ds);
            }else if("UNPOOLED".equalsIgnoreCase(useDataSource)){
                //不使用
                Connection conn = createConnection();
                //给SqlSession中的连接数据库信息赋值
                session.setConnection(conn);
            }
            //6.得到所有的mapper节点
            List<Element> mapperElements = root.selectNodes("//mappers/mapper");
            //7.遍历mapper的节点集合
            for(Element mapperElement : mapperElements) {
                //取出mapper节点的属性
                Attribute resource = mapperElement.attribute("resource");
                if(resource != null) {
                    //使用的是xml的配置方式
                    //取出resource属性的值
                    String mapperPath = resource.getValue();//com/itheima/dao/IUserDao.xml
                    //map
                    Map<String,Mapper> mappers = loadXMLMapperConfig(mapperPath);
                    session.setMappers(mappers);
                }else {
                    //使用注解的方式
                    //取出class属性的值
                    String daoClassPath = mapperElement.attributeValue("class");
                    Map<String,Mapper> mappers = loadAnnotationMapperConfig(daoClassPath);
                    session.setMappers(mappers);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 解析映射配置文件(例如:IUserDao.xml),准备sqlSession所必须的映射信息
     * @param mapperPath    参数就是:com/itheima/dao/IUserDao.xml
     * @return
     * @throws Exception
     */
    public static Map<String, Mapper> loadXMLMapperConfig(String mapperPath) throws Exception {
        Map<String,Mapper> map = new HashMap<String,Mapper>();
        InputStream in = null;
        try {
            //1.根据传入的参数
            in = Resources.getResourceAsStream(mapperPath);
            //2.获取Document对象
            Document document = new SAXReader().read(in);
            //3.获取根节点
            Element root = document.getRootElement();
            //4.取出节点中namespace属性的值
            String namespace = root.attributeValue("namespace");
            //5.取出所有的select节点
            List<Element> selectElements = root.elements("select");
            //6.遍历select节点集合
            for(Element selectElement : selectElements) {
                //取出id属性的值
                String id = selectElement.attributeValue("id");
                //取出resultType属性的值
                String resultType = selectElement.attributeValue("resultType");
                //取出文本内容
                String sql = selectElement.getText();
                //创建Mapper对象
                Mapper mapper = new Mapper();
                mapper.setQueryString(sql);
                mapper.setResultType(resultType);
                //拼接key
                String key = namespace+"."+id;
                //把key和mapper存入map中
                map.put(key, mapper);
            }
            return map;
        }finally {
            in.close();
        }
    }

















//此处是注解的加载方式

   public static Map<String, Mapper> loadAnnotationMapperConfig(String daoClassPath) {
//        Map<String,Mapper> map = new HashMap<String,Mapper>();
//
//        //1.根据传入的参数,反射获取当前类的字节码
//        Class daoClass = null;
//        try {
//            daoClass = Class.forName(daoClassPath);
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }
//        String className = daoClass.getName();
//        //2.得到类中所有的方法
//        Method[] ms = daoClass.getMethods();
//        //3.遍历方法的数组
//        for(Method method : ms) {
//            //判断每个方法上是否有Select注解
//            boolean isAnnotated = method.isAnnotationPresent(Select.class);
//            if(isAnnotated) {//一旦进入if内部,则表示当前方法有Select注解
//                //创建Mapper对象
//                Mapper mapper = new Mapper();
//
//                //获取方法的名称
//                String methodName = method.getName();
//                //获取当前方法的返回值
//                Type type = method.getGenericReturnType();//List<User>
//                //看看type是不是参数化的类型
//                if(type instanceof ParameterizedType) {
//                    //强转:因为我要到ParameterizedType里面的方法了
//                    ParameterizedType ptype = (ParameterizedType)type;
//                    //获取参数化类型中的实际类型参数
//                    Type[] types = ptype.getActualTypeArguments();
//                    //取出数组中的第一个元素
//                    Class domainClass = (Class)types[0];
//                    //给mapper中的resultType属性赋值
//                    mapper.setResultType(domainClass.getName());
//                }
//                //得到当前方法上的注解
//                Select selectAnno = method.getAnnotation(Select.class);
//                //获取注解的属性
//                String sql = selectAnno.value();//只是mapper中的一个属性
//                //给mapper中的querystring属性赋值
//                mapper.setQueryString(sql);
//                //创建map中的key
//                String key = className+"."+methodName;
//                //给map填充内容
//                map.put(key, mapper);
//            }
//        }
//        return map;
        return  null;
    }



    //连接数据库的信息
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    /**
     * 获取连接
     * @return
     * @throws Exception
     */
    public static Connection createConnection()throws Exception {
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);
        return conn;
    }

    /**
     * 获取数据源
     * @return
     * @throws Exception
     */
    public static DataSource createDataSource() throws Exception{
        ComboPooledDataSource ds = new ComboPooledDataSource();
        ds.setDriverClass(driver);
        ds.setJdbcUrl(url);
        ds.setUser(username);
        ds.setPassword(password);
        return ds;
    }
}

Executor类

public class Executor {
    public static  <E> List<E> handle(ResultSet rs, String resultType) throws Exception {
        List list = new ArrayList();
        //1.获取参数中resultType所对应的字节码
        Class domainClass = Class.forName(resultType);
        //3.遍历结果集
        while (rs.next()) {
            //4.获取要封装的实体类对象
            Object domain = domainClass.newInstance();
            //取出rs中的源信息(结果集中有多少列,每列的名称等等)
            ResultSetMetaData rsmd = rs.getMetaData();
            //得到当前的列数
            int columnCount = rsmd.getColumnCount();
            //遍历列数
            for (int i = 1; i <= columnCount; i++) {
                //取出每列的名称
                String columnName = rsmd.getColumnName(i);//它其实就是我们实体类中的属性名称
                //得到当前列名(属性名)的属性描述器
                PropertyDescriptor pd = new PropertyDescriptor(columnName, domainClass);
                //得到它的写方法
                Method writeMethod = pd.getWriteMethod();
                //取出要赋的值
                Object value = rs.getObject(columnName);
                //方法执行
                writeMethod.invoke(domain, value);
            }
            //把封装好的domain加入到集合中
            list.add(domain);
        }
        return list;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容