zoukankan      html  css  js  c++  java
  • Atitit mybatis prblm n solu v1 u55 目录 1.1. 加载任意文职cfg 1 1.2. 根据dburl获取factory 1 1.3. Load any mapper

    Atitit mybatis prblm n solu v1 u55

     

    目录

    1.1. 加载任意文职cfg 1

    1.2. 根据dburl获取factory 1

    1.3. Load any mapper file 2

    2. Debug 3

    2.1. Other 4

     

     

      1. 加载任意文职cfg

    public static SqlSessionFactory getSqlSessionFactory() throws Exception {

    String mybatisCfg_result = getCfgTxtV2();

     

    // System.out.println(mybatisCfg_result);

    InputStream is2 = new ByteArrayInputStream(mybatisCfg_result.getBytes());

    // ����sqlSession ����

    return new SqlSessionFactoryBuilder().build(is2);

    }

     

      1. 根据dburl获取factory

     

    public static SqlSessionFactory getSqlSessionFactoryByDburl(String dburl) {

    String mybatisCfg_result = getCfgTxtByDburl(dburl.trim());

     

    // System.out.println(mybatisCfg_result);

    InputStream is2 = new ByteArrayInputStream(mybatisCfg_result.getBytes());

    // ����sqlSession ����

    return new SqlSessionFactoryBuilder().build(is2);

    }

     

    private static String getCfgTxtByDburl(@NotNull @NonNull String mysqlConnUrl) {

    String mybatisCfg_result = null;

    try {

    mybatisCfg_result = mybatisCfgTxt();

    } catch (IOException e) {

    ExUtilV2t33.throwExV2(e);

    }

     

    // String getCfgFile = SpringUtil.getCfgFile();

    String propFilePath = "/cfg";

     

    ConnectionUrlParser connStringParser = ConnectionUrlParser.parseConnectionString(mysqlConnUrl);

    System.out.println(connStringParser);

     

    Object url = mysqlConnUrl;

    Object usr = UrlUtil.parse4Q(connStringParser.getQuery()).get("user");

    Object pwd = UrlUtil.parse4Q(connStringParser.getQuery()).get("password");

    if (pwd == null)

    pwd = "";

    url = cn.hutool.core.util.XmlUtil.escape(url.toString() + "&allowMultiQueries=true");

     

    mybatisCfg_result = mybatisCfg_result.replaceAll("\$\{mysql.url}", url.toString());

    mybatisCfg_result = mybatisCfg_result.replaceAll("\$\{mysql.username}", usr.toString());

     

    mybatisCfg_result = mybatisCfg_result.replaceAll("\$\{mysql.password}", pwd.toString());

    return mybatisCfg_result;

    // return null;

    }

     

      1. Load any mapper file

    Or change cfg file  gvet factory new

     

    // Configuration.addLoadedResource

    SqlSessionFactory sqlSessionFactory = MybatisUtil.getSqlSessionFactory(li);

    // cfg on effect in ini factory

    // Configuration c= sqlSessionFactory.getConfiguration();

    //  for (String mpXmlPathRes : li) {

    //  c.addLoadedResource(mpXmlPathRes);

    // }

     

    public static SqlSession getConn(String mpXmlPathRes) {

    try {

    // Configuration.addLoadedResource

    SqlSessionFactory sqlSessionFactory = MybatisUtil.getSqlSessionFactory();

    Configuration c = sqlSessionFactory.getConfiguration();

     

    c.addLoadedResource(mpXmlPathRes);

    SqlSession session = sqlSessionFactory.openSession(true);

     

    return session;

    } catch (Exception e) {

    throw new RuntimeException(e);

    }

     

    }

     

    1. Debug

     

    SqlSession conn = MybatisUtil.getConn(li);

    Collection<MappedStatement> mappedStatements = conn.getConfiguration().getMappedStatements();

    for (MappedStatement mappedStatement : mappedStatements) {

    System.out.println("--------------------------");

     

    System.out.println("StatementType:    " + mappedStatement.getStatementType());

    System.out.println("id:    " + mappedStatement.getId());

    System.out.println("res:    " + mappedStatement.getResource());

    // System.out.println("getBoundSql:

    // "+mappedStatement.getBoundSql(null).getSql());

    }

    System.out.println(mappedStatements);

      1. Other

     

    protected static void checkSqlValid(String sql, MappedStatement s) {

    if (sql == "")

    return;

    try {

    SQLStatementParser parser = new MySqlStatementParser(sql);

     

    // 使用Parser解析生成AST,这里SQLStatement就是AST

    SQLStatement statement = parser.parseStatement();

    System.out.println("---check ok");

    System.out.println(statement);

    System.out.println(s.getResource());

    System.out.println(sql);

    System.out.println("---check ok end");

    } catch (Exception e) {

    System.out.println("---check err");

    System.out.println(s.getResource());

    System.out.println(sql);

    System.out.println("---check err end");

    throw e;

    }

     

    ;

     

    }

    private static String getSqlBound(SqlSession conn, String sttID, Map m) {

    Configuration Configuration1 = conn.getConfiguration();

     

    MappedStatement MappedStaement1 = Configuration1.getMappedStatement(sttID);

    String sqlBound = MappedStaement1.getSqlSource().getBoundSql(m).getSql();

    return sqlBound;

    }

  • 相关阅读:
    数据库操作,内外联查询,分组查询,嵌套查询,交叉查询,多表查询,语句小结
    重复控件Repeater和数据列表控件DataList
    网格视图控件GridView (2)
    用好VS2005之扩展membership服务(1)
    5.4 网格视图控件GridView (1)
    数据源控件
    ASP.NET程序中常用的三十三种代码
    在DataSet和DataReader之间选择
    自定义ASP.net 2.0 Membership的步骤,和entry 'AspNetSqlMembershipProvider' has already been added错误的解决
    INNER JOIN
  • 原文地址:https://www.cnblogs.com/attilax/p/15196926.html
Copyright © 2011-2022 走看看